2 * Image Library -- Basic image manipulation
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "lib/mempool.h"
14 #include "images/images.h"
17 #define MAX_IMAGE_BYTES (1 << 30)
20 image_thread_init(struct image_thread *it)
22 DBG("image_thread_init()");
23 bzero(it, sizeof(*it));
24 it->pool = mp_new(1024);
28 image_thread_cleanup(struct image_thread *it)
30 DBG("image_thread_cleanup()");
35 image_thread_err_format(struct image_thread *it, uns code, char *msg, ...)
40 it->err_msg = mp_vprintf(it->pool, msg, args);
45 flags_to_pixel_size(uns flags)
48 switch (flags & IMAGE_COLOR_SPACE)
50 case COLOR_SPACE_GRAYSCALE:
59 if (flags & IMAGE_ALPHA)
65 image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool)
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)))
71 image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
75 uns pixel_size, row_size, align;
76 pixel_size = flags_to_pixel_size(flags);
82 flags |= IMAGE_PIXELS_ALIGNED;
85 if (flags & IMAGE_PIXELS_ALIGNED)
91 if (flags & IMAGE_SSE_ALIGNED)
92 align = IMAGE_SSE_ALIGN_SIZE;
93 else if (flags & IMAGE_PIXELS_ALIGNED)
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))
103 image_thread_err(it, IMAGE_ERR_INVALID_DIMENSIONS, "Image does not fit in memory");
107 img = mp_alloc(pool, bytes_64);
110 img = xmalloc(bytes_64);
111 flags |= IMAGE_NEED_DESTROY;
113 bzero(img, sizeof(struct image));
114 byte *p = (byte *)img + sizeof(struct image);
115 img->pixels = ALIGN_PTR(p, IMAGE_SSE_ALIGN_SIZE);
117 img->pixel_size = pixel_size;
120 img->row_size = row_size;
121 img->image_size = image_size_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);
128 image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool)
130 DBG("image_clone(src=%p flags=0x%x pool=%p)", src, src->flags, pool);
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)))
138 if (src->pixel_size != img->pixel_size) /* conversion between aligned and unaligned RGB */
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"
148 else if (src->row_size != img->row_size)
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--; )
161 memcpy(img->pixels, src->pixels, img->image_size);
167 image_destroy(struct image *img)
169 DBG("image_destroy(img=%p)", img);
170 if (img->flags & IMAGE_NEED_DESTROY)
175 image_clear(struct image_thread *it UNUSED, struct image *img)
177 DBG("image_clear(img=%p)", img);
179 bzero(img->pixels, img->image_size);
183 image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags)
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)))
188 image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
191 img->pixels = pixels;
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);
202 image_init_subimage(struct image_thread *it UNUSED, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows)
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;
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;
218 color_space_to_name(enum color_space cs)
220 return image_channels_format_to_name(cs);
224 image_channels_format_to_name(uns format)
228 case COLOR_SPACE_GRAYSCALE:
230 case COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA:
232 case COLOR_SPACE_RGB:
234 case COLOR_SPACE_RGB | IMAGE_ALPHA:
242 image_name_to_channels_format(byte *name)
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;