]> mj.ucw.cz Git - libucw.git/blob - images/image.c
unfinished revision of image comparisions
[libucw.git] / images / image.c
1 /*
2  *      Image Library -- Basic image manipulation
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "lib/mempool.h"
14 #include "images/images.h"
15 #include <string.h>
16
17 #define MAX_IMAGE_BYTES (1 << 30)
18
19 void
20 image_thread_init(struct image_thread *it)
21 {
22   DBG("image_thread_init()");
23   bzero(it, sizeof(*it));
24   it->pool = mp_new(1024);
25 }
26
27 void
28 image_thread_cleanup(struct image_thread *it)
29 {
30   DBG("image_thread_cleanup()");
31   mp_delete(it->pool);
32 }
33
34 void
35 image_thread_err_format(struct image_thread *it, uns code, char *msg, ...)
36 {
37   va_list args;
38   va_start(args, msg);
39   it->err_code = code;
40   it->err_msg = mp_vprintf(it->pool, msg, args);
41   va_end(args);
42 }
43
44 static inline uns
45 flags_to_pixel_size(uns flags)
46 {
47   uns pixel_size;
48   switch (flags & IMAGE_COLOR_SPACE)
49     {
50       case COLOR_SPACE_GRAYSCALE:
51         pixel_size = 1;
52         break;
53       case COLOR_SPACE_RGB:
54         pixel_size = 3;
55         break;
56       default:
57         ASSERT(0);
58     }
59   if (flags & IMAGE_ALPHA)
60     pixel_size++;
61   return pixel_size;
62 }
63
64 struct image *
65 image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool)
66 {
67   DBG("image_new(cols=%u rows=%u flags=0x%x pool=%p)", cols, rows, flags, pool);
68   flags &= IMAGE_NEW_FLAGS;
69   if (unlikely(!image_dimensions_valid(cols, rows)))
70     {
71       image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
72       return NULL;
73     }
74   struct image *img;
75   uns pixel_size, row_size, align;
76   pixel_size = flags_to_pixel_size(flags);
77   switch (pixel_size)
78     {
79       case 1:
80       case 2:
81       case 4:
82         flags |= IMAGE_PIXELS_ALIGNED;
83         break;
84       case 3:
85         if (flags & IMAGE_PIXELS_ALIGNED)
86           pixel_size = 4;
87         break;
88       default:
89         ASSERT(0);
90     }
91   if (flags & IMAGE_SSE_ALIGNED)
92     align = IMAGE_SSE_ALIGN_SIZE;
93   else if (flags & IMAGE_PIXELS_ALIGNED)
94     align = pixel_size;
95   else
96     align = 1;
97   row_size = cols * pixel_size;
98   row_size = ALIGN(row_size, align);
99   u64 image_size_64 = (u64)row_size * rows;
100   u64 bytes_64 = image_size_64 + (sizeof(struct image) + IMAGE_SSE_ALIGN_SIZE - 1 + sizeof(uns));
101   if (unlikely(bytes_64 > MAX_IMAGE_BYTES))
102     {
103       image_thread_err(it, IMAGE_ERR_INVALID_DIMENSIONS, "Image does not fit in memory");
104       return NULL;
105     }
106   if (pool)
107     img = mp_alloc(pool, bytes_64);
108   else
109     {
110       img = xmalloc(bytes_64);
111       flags |= IMAGE_NEED_DESTROY;
112     }
113   bzero(img, sizeof(struct image));
114   byte *p = (byte *)img + sizeof(struct image);
115   img->pixels = ALIGN_PTR(p, IMAGE_SSE_ALIGN_SIZE);
116   img->flags = flags;
117   img->pixel_size = pixel_size;
118   img->cols = cols;
119   img->rows = rows;
120   img->row_size = row_size;
121   img->image_size = bytes_64;
122   DBG("img=%p flags=0x%x pixel_size=%u row_size=%u image_size=%u pixels=%p",
123     img, img->flags, img->pixel_size, img->row_size, img->image_size, img->pixels);
124   return img;
125 }
126
127 struct image *
128 image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool)
129 {
130   DBG("image_clone(src=%p flags=0x%x pool=%p)", src, src->flags, pool);
131   struct image *img;
132   flags &= IMAGE_NEW_FLAGS & ~IMAGE_CHANNELS_FORMAT;
133   flags |= src->flags & IMAGE_CHANNELS_FORMAT;
134   if (!(img = image_new(it, src->cols, src->rows, flags, pool)))
135     return NULL;
136   if (img->image_size)
137     {
138       if (src->pixel_size != img->pixel_size) /* conversion between aligned and unaligned RGB */
139         {
140 #         define IMAGE_WALK_PREFIX(x) walk_##x
141 #         define IMAGE_WALK_INLINE
142 #         define IMAGE_WALK_IMAGE img
143 #         define IMAGE_WALK_SEC_IMAGE src
144 #         define IMAGE_WALK_DOUBLE
145 #         define IMAGE_WALK_DO_STEP do{ walk_pos[0] = walk_sec_pos[0]; walk_pos[1] = walk_sec_pos[1]; walk_pos[2] = walk_sec_pos[2]; }while(0)
146 #         include "images/image-walk.h"
147         }
148       else if (src->row_size != img->row_size)
149         {
150           byte *s = src->pixels;
151           byte *d = img->pixels;
152           uns bytes = src->cols * img->pixel_size;
153           for (uns row = src->rows; row--; )
154             {
155               memcpy(d, s, bytes);
156               d += img->row_size;
157               s += src->row_size;
158             }
159         }
160       else
161         memcpy(img->pixels, src->pixels, img->image_size);
162     }
163   return img;
164 }
165
166 void
167 image_destroy(struct image *img)
168 {
169   DBG("image_destroy(img=%p)", img);
170   if (img->flags & IMAGE_NEED_DESTROY)
171     xfree(img);
172 }
173
174 void
175 image_clear(struct image_thread *it UNUSED, struct image *img)
176 {
177   DBG("image_clear(img=%p)", img);
178   if (img->image_size)
179     bzero(img->pixels, img->image_size);
180 }
181
182 struct image *
183 image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags)
184 {
185   DBG("image_init_matrix(img=%p pixels=%p cols=%u rows=%u row_size=%u flags=0x%x)", img, pixels, cols, rows, row_size, flags);
186   if (unlikely(!image_dimensions_valid(cols, rows)))
187     {
188       image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
189       return NULL;
190     }
191   img->pixels = pixels;
192   img->cols = cols;
193   img->rows = rows;
194   img->pixel_size = flags_to_pixel_size(flags);
195   img->row_size = row_size;
196   img->image_size = rows * row_size;
197   img->flags = flags & (IMAGE_NEW_FLAGS | IMAGE_GAPS_PROTECTED);
198   return img;
199 }
200
201 struct image *
202 image_init_subimage(struct image_thread *it UNUSED, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows)
203 {
204   DBG("image_init_subimage(img=%p src=%p left=%u top=%u cols=%u rows=%u)", img, src, left, top, cols, rows);
205   ASSERT(left + cols <= src->cols && top + rows <= src->rows);
206   img->pixels = src->pixels + left * src->pixel_size + top * src->row_size;
207   img->cols = cols;
208   img->rows = rows;
209   img->pixel_size = src->pixel_size;
210   img->row_size = src->row_size;
211   img->image_size = src->row_size * rows;
212   img->flags = src->flags & IMAGE_NEW_FLAGS;
213   img->flags |= IMAGE_GAPS_PROTECTED;
214   return img;
215 }
216
217 byte *
218 color_space_to_name(enum color_space cs)
219 {
220   return image_channels_format_to_name(cs);
221 }
222
223 byte *
224 image_channels_format_to_name(uns format)
225 {
226   switch (format)
227     {
228       case COLOR_SPACE_GRAYSCALE:
229         return "Gray";
230       case COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA:
231         return "GrayAlpha";
232       case COLOR_SPACE_RGB:
233         return "RGB";
234       case COLOR_SPACE_RGB | IMAGE_ALPHA:
235         return "RGBAlpha";
236       default:
237         return NULL;
238     }
239 }
240
241 uns
242 image_name_to_channels_format(byte *name)
243 {
244   if (!strcasecmp(name, "gray"))
245     return COLOR_SPACE_GRAYSCALE;
246   if (!strcasecmp(name, "grayscale"))
247     return COLOR_SPACE_GRAYSCALE;
248   if (!strcasecmp(name, "grayalpha"))
249     return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
250   if (!strcasecmp(name, "grayscalealpha"))
251     return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
252   if (!strcasecmp(name, "rgb"))
253     return COLOR_SPACE_RGB;
254   if (!strcasecmp(name, "rgbalpha"))
255     return COLOR_SPACE_RGB + IMAGE_ALPHA;
256   if (!strcasecmp(name, "rgba"))
257     return COLOR_SPACE_RGB + IMAGE_ALPHA;
258   return 0;
259 }