+ double dpi_x = 72.0 / w;
+ double dpi_y = 72.0 / h;
+ double dpi = dpi_x>dpi_y?dpi_x:dpi_y;
+ gfximage_t*rescaled_image = 0;
+ if(i->config_maxdpi && dpi > i->config_maxdpi) {
+ int newwidth = img->width*i->config_maxdpi/dpi;
+ int newheight = img->height*i->config_maxdpi/dpi;
+ rescaled_image = gfximage_rescale(img, newwidth, newheight);
+ msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)",
+ img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi);
+ img = rescaled_image;
+ }
+
+ char tempfile[128];
+ mktempname(tempfile);
+
+ gfximage_save_jpeg(img, tempfile, 96);
+
+ int imgid=-1;
+ if(has_alpha) {
+ char tempfile2[128];
+ mktempname(tempfile2);
+ int t;
+ int size = img->width*img->height;
+ unsigned char*alpha = malloc(size);
+ for(t=0;t<size;t++) {
+ alpha[t] = img->data[t].a;
+ }
+ jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2);
+ free(alpha);
+ int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask");
+ unlink(tempfile2);
+ char masked[80];
+ if(maskid<0) {
+ msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid);
+ return;
+ }
+ sprintf(masked, "masked %d", maskid);
+ imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked);
+ } else {
+ imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, "");
+ }
+
+ if(imgid<0) {
+ msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile);
+ return;
+ }
+ unlink(tempfile);
+