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"
16 #include "images/io-main.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_dup(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_dup(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 switch (i->cinfo.jpeg_color_space)
223 io->flags = COLOR_SPACE_GRAYSCALE;
224 io->number_of_colors = 1 << 8;
227 io->flags = COLOR_SPACE_RGB;
228 io->number_of_colors = 1 << 24;
231 io->cols = i->cinfo.image_width;
232 io->rows = i->cinfo.image_height;
234 io->read_cancel = libjpeg_read_cancel;
239 libjpeg_read_data(struct image_io *io)
241 DBG("libjpeg_read_data()");
243 struct libjpeg_read_internals *i = io->read_data;
245 /* Select color space */
246 switch (io->flags & IMAGE_COLOR_SPACE)
248 case COLOR_SPACE_GRAYSCALE:
249 i->cinfo.out_color_space = JCS_GRAYSCALE;
251 case COLOR_SPACE_RGB:
252 i->cinfo.out_color_space = JCS_RGB;
255 jpeg_destroy_decompress(&i->cinfo);
256 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported color space.");
260 /* Prepare the image ... FIXME: use libjpeg feature to speed up downscale */
261 struct image_io_read_data_internals rdi;
262 if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.image_width, i->cinfo.image_height)))
264 jpeg_destroy_decompress(&i->cinfo);
269 if (setjmp(i->err.setjmp_buf))
271 DBG("Libjpeg failed to read the image, longjump saved us");
272 jpeg_destroy_decompress(&i->cinfo);
273 image_io_read_data_break(&rdi, io);
277 /* Decompress the image */
278 struct image *img = rdi.image;
279 jpeg_start_decompress(&i->cinfo);
280 switch (img->pixel_size)
282 /* grayscale or RGB */
286 byte *pixels = img->pixels;
287 for (uns r = img->rows; r--; )
289 jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&pixels, 1);
290 pixels += img->row_size;
294 /* garscale with alpha */
297 byte buf[img->cols], *src;
298 # define IMAGE_WALK_PREFIX(x) walk_##x
299 # define IMAGE_WALK_INLINE
300 # define IMAGE_WALK_IMAGE img
301 # define IMAGE_WALK_UNROLL 4
302 # define IMAGE_WALK_COL_STEP 2
303 # define IMAGE_WALK_DO_ROW_START do{ src = buf; jpeg_read_scanlines(&i->cinfo, (JSAMPLE **)&src, 1); }while(0)
304 # define IMAGE_WALK_DO_STEP do{ walk_pos[0] = *src++; walk_pos[1] = 255; }while(0)
305 # include "images/image-walk.h"
308 /* RGBA or aligned RGB */
311 byte buf[img->cols * 3], *src;
312 # define IMAGE_WALK_PREFIX(x) walk_##x
313 # define IMAGE_WALK_INLINE
314 # define IMAGE_WALK_IMAGE img
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 *)walk_pos = *(u32 *)src; walk_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 /* Finish the image */
332 return image_io_read_data_finish(&rdi, io);
336 libjpeg_write(struct image_io *io)
338 DBG("libjpeg_write()");
339 struct libjpeg_write_internals i;
340 i.fastbuf = io->fastbuf;
342 /* Create libjpeg write structure */
343 DBG("Creating libjpeg write structure");
344 i.cinfo.err = jpeg_std_error(&i.err.pub);
345 i.err.pub.error_exit = libjpeg_write_error_exit;
346 i.err.pub.emit_message = libjpeg_emit_message;
348 if (setjmp(i.err.setjmp_buf))
350 DBG("Libjpeg failed to write the image, longjump saved us");
351 jpeg_destroy_compress(&i.cinfo);
354 jpeg_create_compress(&i.cinfo);
356 /* Initialize destination manager */
357 i.cinfo.dest = &i.dest;
358 i.dest.init_destination = libjpeg_init_destination;
359 i.dest.term_destination = libjpeg_term_destination;
360 i.dest.empty_output_buffer = libjpeg_empty_output_buffer;
362 /* Set output parameters */
363 struct image *img = io->image;
364 i.cinfo.image_width = img->cols;
365 i.cinfo.image_height = img->rows;
366 switch (img->flags & IMAGE_COLOR_SPACE)
368 case COLOR_SPACE_GRAYSCALE:
369 i.cinfo.input_components = 1;
370 i.cinfo.in_color_space = JCS_GRAYSCALE;
372 case COLOR_SPACE_RGB:
373 i.cinfo.input_components = 3;
374 i.cinfo.in_color_space = JCS_RGB;
377 jpeg_destroy_compress(&i.cinfo);
378 image_thread_err(io->thread, IMAGE_ERR_INVALID_PIXEL_FORMAT, "Unsupported pixel format.");
381 jpeg_set_defaults(&i.cinfo);
382 if (io->jpeg_quality)
383 jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
385 /* Compress the image */
386 jpeg_start_compress(&i.cinfo, TRUE);
387 switch (img->pixel_size)
389 /* grayscale or RGB */
393 byte *pixels = img->pixels;
394 for (uns r = img->rows; r--; )
396 jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&pixels, 1);
397 pixels += img->row_size;
401 /* grayscale with alpha (ignore alpha) */
404 byte buf[img->cols], *dest = buf;
405 # define IMAGE_WALK_PREFIX(x) walk_##x
406 # define IMAGE_WALK_INLINE
407 # define IMAGE_WALK_IMAGE img
408 # define IMAGE_WALK_UNROLL 4
409 # define IMAGE_WALK_COL_STEP 2
410 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
411 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; }while(0)
412 # include "images/image-walk.h"
415 /* RGBA (ignore alpha) or aligned RGB */
418 byte buf[img->cols * 3], *dest = buf;
419 # define IMAGE_WALK_PREFIX(x) walk_##x
420 # define IMAGE_WALK_INLINE
421 # define IMAGE_WALK_IMAGE img
422 # define IMAGE_WALK_UNROLL 4
423 # define IMAGE_WALK_COL_STEP 4
424 # define IMAGE_WALK_DO_ROW_END do{ dest = buf; jpeg_write_scanlines(&i.cinfo, (JSAMPLE **)&dest, 1); }while(0)
425 # define IMAGE_WALK_DO_STEP do{ *dest++ = walk_pos[0]; *dest++ = walk_pos[1]; *dest++ = walk_pos[2]; }while(0)
426 # include "images/image-walk.h"
432 ASSERT(i.cinfo.next_scanline == i.cinfo.image_height);
433 jpeg_finish_compress(&i.cinfo);
434 jpeg_destroy_compress(&i.cinfo);