- /* Allocate the image... FIXME: use libjpeg feature to speed up downscale */
- volatile int need_scale = io->cols != i->cinfo.image_width || io->rows != i->cinfo.image_height;
- struct image * volatile img = need_scale ?
- image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags & IMAGE_PIXEL_FORMAT, NULL) :
- image_new(io->thread, i->cinfo.image_width, i->cinfo.image_height, io->flags, io->pool);
- if (!img)
+ /* Prepare the image */
+ struct image_io_read_data_internals rdi;
+ 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)))