2 * Image Library -- libjpeg
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 "lib/fastbuf.h"
15 #include "images/images.h"
18 #include <sys/types.h>
23 struct jpeg_error_mgr pub;
28 struct libjpeg_read_internals {
29 struct jpeg_decompress_struct cinfo;
30 struct jpeg_source_mgr src;
31 struct libjpeg_err err;
32 struct fastbuf *fastbuf;
36 struct libjpeg_write_internals {
37 struct jpeg_compress_struct cinfo;
38 struct jpeg_destination_mgr dest;
39 struct libjpeg_err err;
40 struct fastbuf *fastbuf;
45 libjpeg_read_error_exit(j_common_ptr cinfo)
47 DBG("libjpeg_error_exit()");
48 struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
49 byte buf[JMSG_LENGTH_MAX];
50 e->pub.format_message(cinfo, buf);
51 image_thread_err(e->io->thread, IMAGE_ERR_READ_FAILED, buf);
52 longjmp(e->setjmp_buf, 1);
56 libjpeg_write_error_exit(j_common_ptr cinfo)
58 DBG("libjpeg_error_exit()");
59 struct libjpeg_err *e = (struct libjpeg_err *)cinfo->err;
60 byte buf[JMSG_LENGTH_MAX];
61 e->pub.format_message(cinfo, buf);
62 image_thread_err(e->io->thread, IMAGE_ERR_WRITE_FAILED, buf);
63 longjmp(e->setjmp_buf, 1);
67 libjpeg_emit_message(j_common_ptr cinfo UNUSED, int msg_level UNUSED)
70 byte buf[JMSG_LENGTH_MAX];
71 cinfo->err->format_message(cinfo, buf);
72 DBG("libjpeg_emit_message(): [%d] %s", msg_level, buf);
74 if (unlikely(msg_level == -1))
75 longjmp(((struct libjpeg_err *)(cinfo)->err)->setjmp_buf, 1);
79 libjpeg_fastbuf_read_prepare(struct libjpeg_read_internals *i)
82 uns len = bdirect_read_prepare(i->fastbuf, &start);
83 i->fastbuf_pos = start + len;
84 i->src.next_input_byte = start;
85 i->src.bytes_in_buffer = len;
90 libjpeg_fastbuf_read_commit(struct libjpeg_read_internals *i)
92 bdirect_read_commit(i->fastbuf, i->fastbuf_pos);
96 libjpeg_init_source(j_decompress_ptr cinfo)
98 DBG("libjpeg_init_source()");
99 libjpeg_fastbuf_read_prepare((struct libjpeg_read_internals *)cinfo);
103 libjpeg_term_source(j_decompress_ptr cinfo UNUSED)
105 DBG("libjpeg_term_source()");
106 //libjpeg_fastbuf_read_commit((struct libjpeg_read_internals *)cinfo);
110 libjpeg_fill_input_buffer(j_decompress_ptr cinfo)
112 DBG("libjpeg_fill_input_buffer()");
113 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
114 libjpeg_fastbuf_read_commit(i);
115 return !!libjpeg_fastbuf_read_prepare(i);
119 libjpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
121 DBG("libjpeg_skip_input_data(num_bytes=%d)", (int)num_bytes);
124 struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
125 if ((unsigned long)num_bytes <= i->src.bytes_in_buffer)
127 i->src.next_input_byte += num_bytes;
128 i->src.bytes_in_buffer -= num_bytes;
132 num_bytes -= i->src.bytes_in_buffer;
133 libjpeg_fastbuf_read_commit(i);
134 bskip(i->fastbuf, num_bytes);
135 libjpeg_fastbuf_read_prepare(i);
141 libjpeg_fastbuf_write_prepare(struct libjpeg_write_internals *i)
144 uns len = bdirect_write_prepare(i->fastbuf, &start);
145 i->fastbuf_pos = start + len;
146 i->dest.next_output_byte = start;
147 i->dest.free_in_buffer = len;
150 image_thread_err(i->err.io->thread, IMAGE_ERR_WRITE_FAILED, "Unexpected end of stream");
151 longjmp(i->err.setjmp_buf, 1);
156 libjpeg_init_destination(j_compress_ptr cinfo)
158 DBG("libjpeg_init_destination()");
159 libjpeg_fastbuf_write_prepare((struct libjpeg_write_internals *)cinfo);
163 libjpeg_term_destination(j_compress_ptr cinfo)
165 DBG("libjpeg_term_destination()");
166 struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
167 bdirect_write_commit(i->fastbuf, (byte *)i->dest.next_output_byte);
171 libjpeg_empty_output_buffer(j_compress_ptr cinfo)
173 DBG("libjpeg_empty_output_buffer()");
174 struct libjpeg_write_internals *i = (struct libjpeg_write_internals *)cinfo;
175 bdirect_write_commit(i->fastbuf, i->fastbuf_pos);
176 libjpeg_fastbuf_write_prepare(i);
181 libjpeg_read_cancel(struct image_io *io)
183 DBG("libjpeg_read_cancel()");
184 struct libjpeg_read_internals *i = io->read_data;
185 jpeg_destroy_decompress(&i->cinfo);
189 libjpeg_read_header(struct image_io *io)
191 DBG("libjpeg_read_header()");
192 struct libjpeg_read_internals *i = io->read_data = mp_alloc(io->internal_pool, sizeof(*i));
193 i->fastbuf = io->fastbuf;
195 /* Create libjpeg read structure */
196 DBG("Creating libjpeg read structure");
197 i->cinfo.err = jpeg_std_error(&i->err.pub);
198 i->err.pub.error_exit = libjpeg_read_error_exit;
199 i->err.pub.emit_message = libjpeg_emit_message;
201 if (setjmp(i->err.setjmp_buf))
203 DBG("Libjpeg failed to read the image, longjump saved us");
204 jpeg_destroy_decompress(&i->cinfo);
207 jpeg_create_decompress(&i->cinfo);
209 /* Initialize source manager */
210 i->cinfo.src = &i->src;
211 i->src.init_source = libjpeg_init_source;
212 i->src.fill_input_buffer = libjpeg_fill_input_buffer;
213 i->src.skip_input_data = libjpeg_skip_input_data;
214 i->src.resync_to_restart = jpeg_resync_to_restart;
215 i->src.term_source = libjpeg_term_source;
217 /* Read JPEG header and setup decompression options */
218 DBG("Reading image header");
219 jpeg_read_header(&i->cinfo, TRUE);
220 if (!(io->flags & IMAGE_COLOR_SPACE))
221 switch (i->cinfo.jpeg_color_space)
224 io->flags |= COLOR_SPACE_GRAYSCALE;
227 io->flags |= COLOR_SPACE_RGB;
231 io->cols = i->cinfo.image_width;
233 io->rows = i->cinfo.image_height;
235 io->read_cancel = libjpeg_read_cancel;
240 libjpeg_read_data(struct image_io *io)
242 DBG("libjpeg_read_data()");
244 struct libjpeg_read_internals *i = io->read_data;
246 /* Select color space */
247 switch (io->flags & IMAGE_COLOR_SPACE)
249 case COLOR_SPACE_GRAYSCALE:
250 i->cinfo.out_color_space = JCS_GRAYSCALE;
252 case COLOR_SPACE_RGB:
253 i->cinfo.out_color_space = JCS_RGB;
256 jpeg_destroy_decompress(&i->cinfo);
257 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
261 /* Allocate the image... FIXME: use libjpeg feature to speed up downscale */
262 volatile int need_scale = io->cols != i->cinfo.image_width || io->rows != i->cinfo.image_height;
263 struct image * volatile img = need_scale ?
264 image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags & IMAGE_PIXEL_FORMAT, NULL) :
265 image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags, io->pool);
268 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
273 if (setjmp(i->err.setjmp_buf))
275 DBG("Libjpeg failed to read the image, longjump saved us");
276 jpeg_destroy_decompress(&i->cinfo);
277 if (need_scale || !io->pool)
278 image_destroy(io->thread, img);
282 /* Decompress the image */
283 jpeg_start_decompress(&i->cinfo);
284 switch (img->pixel_size)
286 /* grayscale or RGB */
290 byte *pixels = img->pixels;
291 for (uns r = img->rows; r--; )
293 jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
294 pixels += img->row_size;
298 /* garscale with alpha */
301 byte buf[img->cols], *src;
302 # define IMAGE_WALK_INLINE
303 # define IMAGE_WALK_UNROLL 4
304 # define IMAGE_WALK_COL_STEP 2
305 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
306 # define IMAGE_WALK_DO_STEP do{ pos[0] = *src++; pos[1] = 255; }while(0)
307 # include "images/image-walk.h"
310 /* RGBA or aligned RGB */
313 byte buf[img->cols * 3], *src;
314 # define IMAGE_WALK_INLINE
315 # define IMAGE_WALK_UNROLL 4
316 # define IMAGE_WALK_COL_STEP 4
317 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
318 # define IMAGE_WALK_DO_STEP do{ *(u32 *)pos = *(u32 *)src; pos[3] = 255; src += 3; }while(0)
319 # include "images/image-walk.h"
325 ASSERT(i->cinfo.output_scanline == i->cinfo.output_height);
327 /* Destroy libjpeg object */
328 jpeg_finish_decompress(&i->cinfo);
329 jpeg_destroy_decompress(&i->cinfo);
331 /* Scale result if necessary */
334 DBG("Scaling image");
335 struct image *dest = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
338 image_destroy(io->thread, img);
341 if (!(image_scale(io->thread, dest, img)))
343 image_destroy(io->thread, img);
345 image_destroy(io->thread, dest);
348 image_destroy(io->thread, img);
353 io->image_destroy = !io->pool;
360 libjpeg_write(struct image_io *io)
362 DBG("libjpeg_write()");
363 struct libjpeg_write_internals i;
364 i.fastbuf = io->fastbuf;
366 /* Create libjpeg write structure */
367 DBG("Creating libjpeg write structure");
368 i.cinfo.err = jpeg_std_error(&i.err.pub);
369 i.err.pub.error_exit = libjpeg_write_error_exit;
370 i.err.pub.emit_message = libjpeg_emit_message;
372 if (setjmp(i.err.setjmp_buf))
374 DBG("Libjpeg failed to write the image, longjump saved us");
375 jpeg_destroy_compress(&i.cinfo);
378 jpeg_create_compress(&i.cinfo);
380 /* Initialize destination manager */
381 i.cinfo.dest = &i.dest;
382 i.dest.init_destination = libjpeg_init_destination;
383 i.dest.term_destination = libjpeg_term_destination;
384 i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
386 /* Set output parameters */
387 struct image *img = io->image;
388 i.cinfo.image_width = img->cols;
389 i.cinfo.image_height = img->rows;
390 switch (io->flags & IMAGE_COLOR_SPACE)
392 case COLOR_SPACE_GRAYSCALE:
393 i.cinfo.input_components = 1;
394 i.cinfo.in_color_space = JCS_GRAYSCALE;
396 case COLOR_SPACE_RGB:
397 i.cinfo.input_components = 3;
398 i.cinfo.in_color_space = JCS_RGB;
401 jpeg_destroy_compress(&i.cinfo);
402 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
405 jpeg_set_defaults(&i.cinfo);
407 /* Compress the image */
408 jpeg_start_compress(&i.cinfo, TRUE);
409 switch (img->pixel_size)
411 /* grayscale or RGB */
415 byte *pixels = img->pixels;
416 for (uns r = img->rows; r--; )
418 jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
419 pixels += img->row_size;
423 /* grayscale with alpha (ignore alpha) */
426 byte buf[img->cols], *dest = buf;
427 # define IMAGE_WALK_INLINE
428 # define IMAGE_WALK_UNROLL 4
429 # define IMAGE_WALK_COL_STEP 2
430 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
431 # define IMAGE_WALK_DO_STEP do{ *dest++ = pos[0]; }while(0)
432 # include "images/image-walk.h"
435 /* RGBA (ignore alpha) or aligned RGB */
438 byte buf[img->cols * 3 + 1], *dest = buf;
439 # define IMAGE_WALK_INLINE
440 # define IMAGE_WALK_UNROLL 4
441 # define IMAGE_WALK_COL_STEP 4
442 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
443 # define IMAGE_WALK_DO_STEP do{ *(u32 *)dest = *(u32 *)pos; dest += 3; }while(0)
444 # include "images/image-walk.h"
450 ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
451 jpeg_finish_compress(&i.cinfo);
452 jpeg_destroy_compress(&i.cinfo);