#include <stdio.h>
#include <sys/types.h>
#include <jpeglib.h>
+#include <jerror.h>
#include <setjmp.h>
struct libjpeg_err {
return TRUE;
}
+#ifdef CONFIG_IMAGES_EXIF
+
+static inline uns
+libjpeg_read_byte(struct libjpeg_read_internals *i)
+{
+ DBG("libjpeg_read_byte()");
+ if (!i->src.bytes_in_buffer)
+ if (!libjpeg_fill_input_buffer(&i->cinfo))
+ ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
+ i->src.bytes_in_buffer--;
+ return *i->src.next_input_byte++;
+}
+
+static inline void
+libjpeg_read_buf(struct libjpeg_read_internals *i, byte *buf, uns len)
+{
+ DBG("libjpeg_read_buf(len=%u)", len);
+ while (len)
+ {
+ if (!i->src.bytes_in_buffer)
+ if (!libjpeg_fill_input_buffer(&i->cinfo))
+ ERREXIT(&i->cinfo, JERR_CANT_SUSPEND);
+ uns buf_size = i->src.bytes_in_buffer;
+ uns read_size = MIN(buf_size, len);
+ memcpy(buf, i->src.next_input_byte, read_size);
+ i->src.bytes_in_buffer -= read_size;
+ i->src.next_input_byte += read_size;
+ len -= read_size;
+ }
+}
+
+static byte libjpeg_exif_header[6] = { 'E', 'x', 'i', 'f', 0, 0 };
+
+static boolean
+libjpeg_app1_preprocessor(j_decompress_ptr cinfo)
+{
+ struct libjpeg_read_internals *i = (struct libjpeg_read_internals *)cinfo;
+ struct image_io *io = i->err.io;
+ uns len = (libjpeg_read_byte(i) << 8) + libjpeg_read_byte(i);
+ DBG("Found APP1 marker, len=%u", len);
+ if (len < 2)
+ return TRUE;
+ len -= 2;
+ if (len < 7 /*|| io->exif_size*/)
+ {
+ libjpeg_skip_input_data(cinfo, len);
+ return TRUE;
+ }
+ byte header[6];
+ libjpeg_read_buf(i, header, 6);
+ if (memcmp(header, libjpeg_exif_header, 6))
+ {
+ libjpeg_skip_input_data(cinfo, len - 6);
+ return TRUE;
+ }
+ io->exif_size = len;
+ io->exif_data = mp_alloc(io->internal_pool, len);
+ memcpy(io->exif_data, header, 6);
+ libjpeg_read_buf(i, io->exif_data + 6, len - 6);
+ DBG("Parsed EXIF of length %u", len);
+ return TRUE;
+}
+
+#endif
+
static void
libjpeg_read_cancel(struct image_io *io)
{
i->src.resync_to_restart = jpeg_resync_to_restart;
i->src.term_source = libjpeg_term_source;
+#ifdef CONFIG_IMAGES_EXIF
+ if (io->flags & IMAGE_IO_WANT_EXIF)
+ jpeg_set_marker_processor(&i->cinfo, JPEG_APP0 + 1, libjpeg_app1_preprocessor);
+#endif
+
/* Read JPEG header and setup decompression options */
DBG("Reading image header");
jpeg_read_header(&i->cinfo, TRUE);
return 0;
}
- /* Prepare the image ... FIXME: use libjpeg feature to speed up downscale */
+ /* Prepare the image */
struct image_io_read_data_internals rdi;
- if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.image_width, i->cinfo.image_height)))
+ if (io->cols <= (i->cinfo.image_width >> 3) && io->rows <= (i->cinfo.image_height >> 3))
+ {
+ DBG("Scaling to 1/8");
+ i->cinfo.scale_num = 1;
+ i->cinfo.scale_denom = 8;
+ }
+ else if (io->cols <= (i->cinfo.image_width >> 2) && io->rows <= (i->cinfo.image_height >> 2))
+ {
+ DBG("Scaling to 1/4");
+ i->cinfo.scale_num = 1;
+ i->cinfo.scale_denom = 4;
+ }
+ else if (io->cols <= (i->cinfo.image_width >> 1) && io->rows <= (i->cinfo.image_height >> 1))
+ {
+ DBG("Scaling to 1/2");
+ i->cinfo.scale_num = 1;
+ i->cinfo.scale_denom = 2;
+ }
+ jpeg_calc_output_dimensions(&i->cinfo);
+ DBG("Output dimensions %ux%u", (uns)i->cinfo.output_width, (uns)i->cinfo.output_height);
+ if (unlikely(!image_io_read_data_prepare(&rdi, io, i->cinfo.output_width, i->cinfo.output_height, io->flags)))
{
jpeg_destroy_decompress(&i->cinfo);
return 0;
}
}
break;
- /* garscale with alpha */
+ /* grayscale with alpha */
case 2:
{
byte buf[img->cols], *src;
jpeg_set_defaults(&i.cinfo);
if (io->jpeg_quality)
jpeg_set_quality(&i.cinfo, MIN(io->jpeg_quality, 100), 1);
+#ifdef CONFIG_IMAGES_EXIF
+ if (io->exif_size)
+ {
+ /* According to the Exif specification, the Exif APP1 marker has to follow immediately after the SOI,
+ * just as the JFIF specification requires the same for the JFIF APP0 marker!
+ * Therefore a JPEG file cannot legally be both Exif and JFIF. */
+ i.cinfo.write_JFIF_header = FALSE;
+ i.cinfo.write_Adobe_marker = FALSE;
+ }
+#endif
/* Compress the image */
jpeg_start_compress(&i.cinfo, TRUE);
+#ifdef CONFIG_IMAGES_EXIF
+ if (io->exif_size)
+ {
+ DBG("Writing EXIF");
+ jpeg_write_marker(&i.cinfo, JPEG_APP0 + 1, io->exif_data, io->exif_size);
+ }
+#endif
switch (img->pixel_size)
{
/* grayscale or RGB */