/* Image specific methods */ static void res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table) { int len, k; pdf_obj *obj; pdf_obj *type; pdf_obj *res = NULL; fz_image *image = NULL; unsigned char digest[16]; fz_var(obj); fz_var(image); fz_var(res); fz_try(ctx) { table->hash = fz_new_hash_table(ctx, 4096, 16, -1); len = pdf_count_objects(ctx, doc); for (k = 1; k < len; k++) { obj = pdf_load_object(ctx, doc, k, 0); type = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); if (pdf_name_eq(ctx, type, PDF_NAME_Image)) { image = pdf_load_image(ctx, doc, obj); res_image_get_md5(ctx, image, digest); fz_drop_image(ctx, image); image = NULL; /* Don't allow overwrites. */ if (fz_hash_find(ctx, table->hash, digest) == NULL) fz_hash_insert(ctx, table->hash, digest, obj); } else { pdf_drop_obj(ctx, obj); } obj = NULL; } } fz_always(ctx) { fz_drop_image(ctx, image); pdf_drop_obj(ctx, obj); } fz_catch(ctx) { res_table_free(ctx, table); fz_rethrow(ctx); } }
static pdf_obj * res_image_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char *digest) { fz_image *image = item; fz_hash_table *hash = table->hash; pdf_obj *res; if (hash == NULL) res_image_init(ctx, doc, doc->resources->image); hash = doc->resources->image->hash; /* Create md5 and see if we have the item in our table */ res_image_get_md5(ctx, image, digest); res = fz_hash_find(ctx, hash, digest); if (res) pdf_keep_obj(ctx, res); return res; }
static pdf_obj * res_font_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char digest[16]) { fz_buffer *buffer = item; fz_hash_table *hash = table->hash; pdf_obj *res; if (hash == NULL) res_font_init(ctx, doc, doc->resources->font); hash = doc->resources->font->hash; /* Create md5 and see if we have the item in our table */ res_font_get_md5(ctx, buffer, digest); res = fz_hash_find(ctx, hash, digest); if (res) pdf_keep_obj(ctx, res); return res; }
static void fz_std_conv_pixmap(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src) { float srcv[FZ_MAX_COLORS]; float dstv[FZ_MAX_COLORS]; int srcn, dstn; int y, x, k, i; fz_colorspace *ss = src->colorspace; fz_colorspace *ds = dst->colorspace; unsigned char *s = src->samples; unsigned char *d = dst->samples; assert(src->w == dst->w && src->h == dst->h); assert(src->n == ss->n + 1); assert(dst->n == ds->n + 1); srcn = ss->n; dstn = ds->n; /* Special case for Lab colorspace (scaling of components to float) */ if (!strcmp(ss->name, "Lab") && srcn == 3) { for (y = 0; y < src->h; y++) { for (x = 0; x < src->w; x++) { srcv[0] = *s++ / 255.0f * 100; srcv[1] = *s++ - 128; srcv[2] = *s++ - 128; fz_convert_color(ctx, ds, dstv, ss, srcv); for (k = 0; k < dstn; k++) *d++ = dstv[k] * 255; *d++ = *s++; } } } /* Brute-force for small images */ else if (src->w * src->h < 256) { for (y = 0; y < src->h; y++) { for (x = 0; x < src->w; x++) { for (k = 0; k < srcn; k++) srcv[k] = *s++ / 255.0f; fz_convert_color(ctx, ds, dstv, ss, srcv); for (k = 0; k < dstn; k++) *d++ = dstv[k] * 255; *d++ = *s++; } } } /* 1-d lookup table for separation and similar colorspaces */ else if (srcn == 1) { unsigned char lookup[FZ_MAX_COLORS * 256]; for (i = 0; i < 256; i++) { srcv[0] = i / 255.0f; fz_convert_color(ctx, ds, dstv, ss, srcv); for (k = 0; k < dstn; k++) lookup[i * dstn + k] = dstv[k] * 255; } for (y = 0; y < src->h; y++) { for (x = 0; x < src->w; x++) { i = *s++; for (k = 0; k < dstn; k++) *d++ = lookup[i * dstn + k]; *d++ = *s++; } } } /* Memoize colors using a hash table for the general case */ else { fz_hash_table *lookup; unsigned char *color; lookup = fz_new_hash_table(ctx, 509, srcn, -1); for (y = 0; y < src->h; y++) { for (x = 0; x < src->w; x++) { color = fz_hash_find(ctx, lookup, s); if (color) { memcpy(d, color, dstn); s += srcn; d += dstn; *d++ = *s++; } else { for (k = 0; k < srcn; k++) srcv[k] = *s++ / 255.0f; fz_convert_color(ctx, ds, dstv, ss, srcv); for (k = 0; k < dstn; k++) *d++ = dstv[k] * 255; fz_hash_insert(ctx, lookup, s - srcn, d - dstn); *d++ = *s++; } } } fz_free_hash(ctx, lookup); } }