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 image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool)
47 DBG("image_new(cols=%u rows=%u flags=0x%x pool=%p)", cols, rows, flags, pool);
48 flags &= IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED;
49 if (unlikely(!image_dimensions_valid(cols, rows)))
51 image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
55 uns pixel_size, row_size, align;
56 switch (flags & IMAGE_COLOR_SPACE)
58 case COLOR_SPACE_GRAYSCALE:
67 if (flags & IMAGE_ALPHA)
74 flags |= IMAGE_PIXELS_ALIGNED;
77 if (flags & IMAGE_PIXELS_ALIGNED)
83 if (flags & IMAGE_SSE_ALIGNED)
84 align = IMAGE_SSE_ALIGN_SIZE;
85 else if (flags & IMAGE_PIXELS_ALIGNED)
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))
95 image_thread_err(it, IMAGE_ERR_INVALID_DIMENSIONS, "Image does not fit in memory");
99 img = mp_alloc(pool, bytes_64);
102 img = xmalloc(bytes_64);
103 flags |= IMAGE_NEED_DESTROY;
105 bzero(img, sizeof(struct image));
106 byte *p = (byte *)img + sizeof(struct image);
107 img->pixels = ALIGN_PTR(p, IMAGE_SSE_ALIGN_SIZE);
109 img->pixel_size = pixel_size;
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);
120 image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool)
122 DBG("image_clone(src=%p flags=0x%x pool=%p)", src, src->flags, pool);
124 flags &= ~IMAGE_CHANNELS_FORMAT;
125 flags |= src->flags & IMAGE_CHANNELS_FORMAT;
126 if (!(img = image_new(it, src->cols, src->rows, flags, pool)))
130 if (src->pixel_size != img->pixel_size) /* conversion between aligned and unaligned RGB */
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"
140 else if (src->row_size != img->row_size)
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--; )
153 memcpy(img->pixels, src->pixels, img->image_size);
159 image_destroy(struct image *img)
161 DBG("image_destroy(img=%p)", img);
162 if (img->flags & IMAGE_NEED_DESTROY)
167 image_clear(struct image_thread *it UNUSED, struct image *img)
169 DBG("image_clear(img=%p)", img);
171 bzero(img->pixels, img->image_size);
175 image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags)
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)))
180 image_thread_err_format(it, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid image dimensions (%ux%u)", cols, rows);
183 img->pixels = pixels;
186 img->row_size = row_size;
187 img->image_size = rows * row_size;
188 img->flags = flags & (IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED);
193 image_init_subimage(struct image_thread *it UNUSED, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows)
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;
200 img->row_size = src->row_size;
201 img->image_size = src->row_size * rows;
202 img->flags = src->flags & ~IMAGE_NEED_DESTROY;
207 color_space_to_name(enum color_space cs)
209 return image_channels_format_to_name(cs);
213 image_channels_format_to_name(uns format)
217 case COLOR_SPACE_GRAYSCALE:
219 case COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA:
221 case COLOR_SPACE_RGB:
223 case COLOR_SPACE_RGB | IMAGE_ALPHA:
231 image_name_to_channels_format(byte *name)
233 if (!strcasecmp(name, "gray"))
234 return COLOR_SPACE_GRAYSCALE;
235 if (!strcasecmp(name, "grayscale"))
236 return COLOR_SPACE_GRAYSCALE;
237 if (!strcasecmp(name, "grayalpha"))
238 return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
239 if (!strcasecmp(name, "grayscalealpha"))
240 return COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
241 if (!strcasecmp(name, "rgb"))
242 return COLOR_SPACE_RGB;
243 if (!strcasecmp(name, "rgbalpha"))
244 return COLOR_SPACE_RGB + IMAGE_ALPHA;
245 if (!strcasecmp(name, "rgba"))
246 return COLOR_SPACE_RGB + IMAGE_ALPHA;