]> mj.ucw.cz Git - libucw.git/blob - images/image.c
cleaning image flags manipulation
[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 struct image *
45 image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool)
46 {
47   DBG("image_new(cols=%u rows=%u flags=0x%x pool=%p)", cols, rows, flags, pool);
48   flags &= IMAGE_NEW_FLAGS;
49   if (unlikely(!image_dimensions_valid(cols, rows)))
50     {
51       image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
52       return NULL;
53     }
54   struct image *img;
55   uns pixel_size, row_size, align;
56   switch (flags & IMAGE_COLOR_SPACE)
57     {
58       case COLOR_SPACE_GRAYSCALE:
59         pixel_size = 1;
60         break;
61       case COLOR_SPACE_RGB:
62         pixel_size = 3;
63         break;
64       default:
65         ASSERT(0);
66     }
67   if (flags & IMAGE_ALPHA)
68     pixel_size++;
69   switch (pixel_size)
70     {
71       case 1:
72       case 2:
73       case 4:
74         flags |= IMAGE_PIXELS_ALIGNED;
75         break;
76       case 3:
77         if (flags & IMAGE_PIXELS_ALIGNED)
78           pixel_size = 4;
79         break;
80       default:
81         ASSERT(0);
82     }
83   if (flags & IMAGE_SSE_ALIGNED)
84     align = IMAGE_SSE_ALIGN_SIZE;
85   else if (flags & IMAGE_PIXELS_ALIGNED)
86     align = pixel_size;
87   else
88     align = 1;
89   row_size = cols * pixel_size;
90   row_size = ALIGN(row_size, align);
91   u64 image_size_64 = (u64)row_size * rows;
92   u64 bytes_64 = image_size_64 + (sizeof(struct image) + IMAGE_SSE_ALIGN_SIZE - 1 + sizeof(uns));
93   if (unlikely(bytes_64 > MAX_IMAGE_BYTES))
94     {
95       image_thread_err(it, IMAGE_ERR_INVALID_DIMENSIONS, "Image does not fit in memory");
96       return NULL;
97     }
98   if (pool)
99     img = mp_alloc(pool, bytes_64);
100   else
101     {
102       img = xmalloc(bytes_64);
103       flags |= IMAGE_NEED_DESTROY;
104     }
105   bzero(img, sizeof(struct image));
106   byte *p = (byte *)img + sizeof(struct image);
107   img->pixels = ALIGN_PTR(p, IMAGE_SSE_ALIGN_SIZE);
108   img->flags = flags;
109   img->pixel_size = pixel_size;
110   img->cols = cols;
111   img->rows = rows;
112   img->row_size = row_size;
113   img->image_size = bytes_64;
114   DBG("img=%p flags=0x%x pixel_size=%u row_size=%u image_size=%u pixels=%p",
115     img, img->flags, img->pixel_size, img->row_size, img->image_size, img->pixels);
116   return img;
117 }
118
119 struct image *
120 image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool)
121 {
122   DBG("image_clone(src=%p flags=0x%x pool=%p)", src, src->flags, pool);
123   struct image *img;
124   flags &= IMAGE_NEW_FLAGS & ~IMAGE_CHANNELS_FORMAT;
125   flags |= src->flags & IMAGE_CHANNELS_FORMAT;
126   if (!(img = image_new(it, src->cols, src->rows, flags, pool)))
127     return NULL;
128   if (img->image_size)
129     {
130       if (src->pixel_size != img->pixel_size) /* conversion between aligned and unaligned RGB */
131         {
132 #         define IMAGE_WALK_PREFIX(x) walk_##x
133 #         define IMAGE_WALK_INLINE
134 #         define IMAGE_WALK_IMAGE img
135 #         define IMAGE_WALK_SEC_IMAGE src
136 #         define IMAGE_WALK_DOUBLE
137 #         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)
138 #         include "images/image-walk.h"
139         }
140       else if (src->row_size != img->row_size)
141         {
142           byte *s = src->pixels;
143           byte *d = img->pixels;
144           uns bytes = src->cols * img->pixel_size;
145           for (uns row = src->rows; row--; )
146             {
147               memcpy(d, s, bytes);
148               d += img->row_size;
149               s += src->row_size;
150             }
151         }
152       else
153         memcpy(img->pixels, src->pixels, img->image_size);
154     }
155   return img;
156 }
157
158 void
159 image_destroy(struct image *img)
160 {
161   DBG("image_destroy(img=%p)", img);
162   if (img->flags & IMAGE_NEED_DESTROY)
163     xfree(img);
164 }
165
166 void
167 image_clear(struct image_thread *it UNUSED, struct image *img)
168 {
169   DBG("image_clear(img=%p)", img);
170   if (img->image_size)
171     bzero(img->pixels, img->image_size);
172 }
173
174 int
175 image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags)
176 {
177   DBG("image_init_matrix(img=%p cols=%u rows=%u row_size=%u flags=0x%x)", img, cols, rows, row_size, uns flags);
178   if (unlikely(!image_dimensions_valid(cols, rows)))
179     {
180       image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
181       return 0;
182     }
183   img->pixels = pixels;
184   img->cols = cols;
185   img->rows = rows;
186   img->row_size = row_size;
187   img->image_size = rows * row_size;
188   img->flags = flags & (IMAGE_NEW_FLAGS | IMAGE_GAPS_PROTECTED);
189   return 1;
190 }
191
192 int
193 image_init_subimage(struct image_thread *it UNUSED, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows)
194 {
195   DBG("image_init_subimage(img=%p src=%p left=%u top=%u cols=%u rows=%u)");
196   ASSERT(left + cols <= src->cols && top + rows <= src->rows);
197   img->pixels = src->pixels + left * src->pixel_size + top * src->row_size;
198   img->cols = cols;
199   img->rows = rows;
200   img->row_size = src->row_size;
201   img->image_size = src->row_size * rows;
202   img->flags = src->flags & IMAGE_NEW_FLAGS;
203   img->flags |= IMAGE_GAPS_PROTECTED;
204   return 1;
205 }
206
207 byte *
208 color_space_to_name(enum color_space cs)
209 {
210   return image_channels_format_to_name(cs);
211 }
212
213 byte *
214 image_channels_format_to_name(uns format)
215 {
216   switch (format)
217     {
218       case COLOR_SPACE_GRAYSCALE:
219         return "Gray";
220       case COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA:
221         return "GrayAlpha";
222       case COLOR_SPACE_RGB:
223         return "RGB";
224       case COLOR_SPACE_RGB | IMAGE_ALPHA:
225         return "RGBAlpha";
226       default:
227         return NULL;
228     }
229 }
230
231 uns
232 image_name_to_channels_format(byte *name)
233 {
234   if (!strcasecmp(name, "gray"))
235     return COLOR_SPACE_GRAYSCALE;
236   if (!strcasecmp(name, "grayscale"))
237     return COLOR_SPACE_GRAYSCALE;
238   if (!strcasecmp(name, "grayalpha"))
239     return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
240   if (!strcasecmp(name, "grayscalealpha"))
241     return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
242   if (!strcasecmp(name, "rgb"))
243     return COLOR_SPACE_RGB;
244   if (!strcasecmp(name, "rgbalpha"))
245     return COLOR_SPACE_RGB + IMAGE_ALPHA;
246   if (!strcasecmp(name, "rgba"))
247     return COLOR_SPACE_RGB + IMAGE_ALPHA;
248   return 0;
249 }