Huge change, and it's almost working, too :)
[libopano.git] / src / utils_image.c
1 /*
2  *  Copyright (C) 2007  Florian octo Forster <octo at verplant.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU General Public License as published by the Free
6  *  Software Foundation; either version 2 of the License, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc., 51
16  *  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  **/
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <wand/magick-wand.h>
24
25 #include "utils_image.h"
26
27 ui_image_t *ui_create (uint32_t width, uint32_t height)
28 {
29   ui_image_t *img = NULL;
30
31   img = (ui_image_t *) malloc (sizeof (ui_image_t));
32   if (img == NULL)
33     return (NULL);
34   memset (img, '\0', sizeof (ui_image_t));
35
36   img->width = width;
37   img->height = height;
38   img->type = UIT_RGB;
39
40   img->data = (uint8_t **) malloc (3 * sizeof (uint8_t *));
41   if (img->data == NULL)
42   {
43     free (img);
44     return (NULL);
45   }
46
47   /* FIXME: s/4/3 when bug is fixed! */
48   fprintf (stderr, "Theres still a bug in %s, line %i!\n",
49       __FILE__, __LINE__);
50   img->data[0] = (uint8_t *) malloc (4 * width * height * sizeof (uint8_t));
51   if (img->data[0] == NULL)
52   {
53     free (img->data);
54     free (img);
55     return (NULL);
56   }
57   memset (img->data[0], '\0', 3 * width * height * sizeof (uint8_t));
58   /* FIXME: Remove +16 */
59   img->data[1] = img->data[0] + (width * height);
60   img->data[2] = img->data[1] + (width * height);
61
62   return (img);
63 } /* ui_image_t *ui_create */
64
65 ui_image_t *ui_create_file (const char *file)
66 {
67   MagickWand *mwand; 
68   MagickBooleanType status;
69
70   ui_image_t *img = NULL;
71
72   uint32_t width;
73   uint32_t height;
74   int i;
75
76   mwand = NewMagickWand ();
77   if (mwand == NULL)
78     return (NULL);
79
80   status = MagickReadImage (mwand, file);
81   if (status == MagickFalse)
82   {
83     DestroyMagickWand (mwand);
84     return (NULL);
85   }
86
87   width = (uint32_t) MagickGetImageWidth (mwand);
88   height = (uint32_t) MagickGetImageHeight (mwand);
89
90   img = ui_create (width, height);
91   if (img == NULL)
92   {
93     DestroyMagickWand (mwand);
94     return (NULL);
95   }
96
97   for (i = 0; i < 3; i++)
98   {
99     char *map;
100
101     switch (i)
102     {
103       case 0: map = "R"; break;
104       case 1: map = "G"; break;
105       case 2: map = "B"; break;
106       default: map = "*invalid*";
107     };
108
109     status = MagickGetImagePixels (mwand,
110         0, 0,
111         img->width, img->height,
112         map, CharPixel, (void *) img->data[i]);
113     if (status == MagickFalse)
114       break;
115   } /* for (i = 0, 1, 2) */
116   if (status == MagickFalse)
117   {
118     ui_destroy (img);
119     img = NULL;
120   }
121
122   DestroyMagickWand (mwand);
123   
124   return (img);
125 } /* ui_image_t *ui_create_file */
126
127 void ui_destroy (ui_image_t *img)
128 {
129   if (img != NULL)
130   {
131     if (img->data != NULL)
132     {
133       if (img->data[0] != NULL)
134         free (img->data[0]);
135       free (img->data);
136     }
137     free (img);
138   }
139 } /* void ui_destroy */
140
141 /*
142  * vim: set tabstop=8 softtabstop=2 shiftwidth=2 :
143  */