fz_compressed_buffer * pdf_load_compressed_stream(fz_context *ctx, pdf_document *doc, int num, int gen) { fz_compressed_buffer *bc = fz_malloc_struct(ctx, fz_compressed_buffer); fz_try(ctx) { bc->buffer = pdf_load_image_stream(ctx, doc, num, gen, num, gen, &bc->params, NULL); } fz_catch(ctx) { fz_free(ctx, bc); fz_rethrow(ctx); } return bc; }
/* * Load uncompressed contents of a stream into buf. */ fz_buffer * pdf_load_stream(pdf_document *xref, int num, int gen) { return pdf_load_image_stream(xref, num, gen, NULL); }
fz_buffer * pdf_load_renumbered_stream(fz_context *ctx, pdf_document *doc, int num, int gen, int orig_num, int orig_gen, int *truncated) { return pdf_load_image_stream(ctx, doc, num, gen, orig_num, orig_gen, NULL, truncated); }
/* * Load uncompressed contents of a stream into buf. */ fz_buffer * pdf_load_stream(fz_context *ctx, pdf_document *doc, int num, int gen) { return pdf_load_image_stream(ctx, doc, num, gen, num, gen, NULL, NULL); }
static pdf_image * pdf_load_image_imp(pdf_document *xref, pdf_obj *rdb, pdf_obj *dict, fz_stream *cstm, int forcemask) { fz_stream *stm = NULL; pdf_image *image = NULL; pdf_obj *obj, *res; int w, h, bpc, n; int imagemask; int interpolate; int indexed; fz_image *mask = NULL; /* explicit mask/softmask image */ int usecolorkey; int i; fz_context *ctx = xref->ctx; fz_var(stm); fz_var(mask); image = fz_malloc_struct(ctx, pdf_image); /* SumatraPDF: fix memory leak */ FZ_INIT_STORABLE(&image->base, 1, pdf_free_image); fz_try(ctx) { /* special case for JPEG2000 images */ if (pdf_is_jpx_image(ctx, dict)) { pdf_load_jpx(xref, dict, image); /* RJW: "cannot load jpx image" */ if (forcemask) { fz_pixmap *mask_pixmap; if (image->n != 2) fz_throw(ctx, "softmask must be grayscale"); mask_pixmap = fz_alpha_from_gray(ctx, image->tile, 1); fz_drop_pixmap(ctx, image->tile); image->tile = mask_pixmap; } break; /* Out of fz_try */ } w = pdf_to_int(pdf_dict_getsa(dict, "Width", "W")); h = pdf_to_int(pdf_dict_getsa(dict, "Height", "H")); bpc = pdf_to_int(pdf_dict_getsa(dict, "BitsPerComponent", "BPC")); imagemask = pdf_to_bool(pdf_dict_getsa(dict, "ImageMask", "IM")); interpolate = pdf_to_bool(pdf_dict_getsa(dict, "Interpolate", "I")); indexed = 0; usecolorkey = 0; mask = NULL; if (imagemask) bpc = 1; if (w == 0) fz_throw(ctx, "image width is zero"); if (h == 0) fz_throw(ctx, "image height is zero"); if (bpc == 0) fz_throw(ctx, "image depth is zero"); if (bpc > 16) fz_throw(ctx, "image depth is too large: %d", bpc); if (w > (1 << 16)) fz_throw(ctx, "image is too wide"); if (h > (1 << 16)) fz_throw(ctx, "image is too high"); obj = pdf_dict_getsa(dict, "ColorSpace", "CS"); if (obj && !imagemask && !forcemask) { /* colorspace resource lookup is only done for inline images */ if (pdf_is_name(obj)) { res = pdf_dict_get(pdf_dict_gets(rdb, "ColorSpace"), obj); if (res) obj = res; } image->base.colorspace = pdf_load_colorspace(xref, obj); /* RJW: "cannot load image colorspace" */ if (!strcmp(image->base.colorspace->name, "Indexed")) indexed = 1; n = image->base.colorspace->n; } else { n = 1; } obj = pdf_dict_getsa(dict, "Decode", "D"); if (obj) { for (i = 0; i < n * 2; i++) image->decode[i] = pdf_to_real(pdf_array_get(obj, i)); } else { float maxval = indexed ? (1 << bpc) - 1 : 1; for (i = 0; i < n * 2; i++) image->decode[i] = i & 1 ? maxval : 0; } obj = pdf_dict_getsa(dict, "SMask", "Mask"); if (pdf_is_dict(obj)) { /* Not allowed for inline images */ if (!cstm) { mask = (fz_image *)pdf_load_image_imp(xref, rdb, obj, NULL, 1); /* RJW: "cannot load image mask/softmask" */ } } else if (pdf_is_array(obj)) { usecolorkey = 1; for (i = 0; i < n * 2; i++) { if (!pdf_is_int(pdf_array_get(obj, i))) { fz_warn(ctx, "invalid value in color key mask"); usecolorkey = 0; } image->colorkey[i] = pdf_to_int(pdf_array_get(obj, i)); } } /* Now, do we load a ref, or do we load the actual thing? */ image->params.type = PDF_IMAGE_RAW; FZ_INIT_STORABLE(&image->base, 1, pdf_free_image); image->base.get_pixmap = pdf_image_get_pixmap; image->base.w = w; image->base.h = h; image->n = n; image->bpc = bpc; image->interpolate = interpolate; image->imagemask = imagemask; image->usecolorkey = usecolorkey; image->base.mask = mask; image->params.colorspace = image->base.colorspace; /* Uses the same ref as for the base one */ if (!indexed && !cstm) { /* Just load the compressed image data now and we can * decode it on demand. */ image->buffer = pdf_load_image_stream(xref, pdf_to_num(dict), pdf_to_gen(dict), &image->params); break; /* Out of fz_try */ } /* We need to decompress the image now */ if (cstm) { int stride = (w * image->n * image->bpc + 7) / 8; stm = pdf_open_inline_stream(xref, dict, stride * h, cstm, NULL); } else { stm = pdf_open_stream(xref, pdf_to_num(dict), pdf_to_gen(dict)); /* RJW: "cannot open image data stream (%d 0 R)", pdf_to_num(dict) */ } image->tile = decomp_image_from_stream(ctx, stm, image, cstm != NULL, indexed, 1, 1); } fz_catch(ctx) { fz_drop_image(ctx, &image->base); fz_rethrow(ctx); } return image; }
fz_buffer * pdf_load_stream_truncated(fz_context *ctx, pdf_document *doc, int num, int *truncated) { return pdf_load_image_stream(ctx, doc, num, NULL, truncated); }
fz_buffer * pdf_load_renumbered_stream(pdf_document *xref, int num, int gen, int orig_num, int orig_gen) { return pdf_load_image_stream(xref, num, gen, orig_num, orig_gen, NULL); }