summaryrefslogtreecommitdiff
path: root/source/pdf/pdf-resources.c
blob: 0772d3a46366844771372743a68ec1a5a555fa30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "mupdf/pdf.h"

static void
fz_md5_image(fz_context *ctx, fz_image *image, unsigned char digest[16])
{
	fz_pixmap *pixmap;
	fz_md5 state;
	int h;
	unsigned char *d;

	pixmap = fz_get_pixmap_from_image(ctx, image, NULL, NULL, 0, 0);
	fz_md5_init(&state);
	d = pixmap->samples;
	h = pixmap->h;
	while (h--)
	{
		fz_md5_update(&state, d, pixmap->w * pixmap->n);
		d += pixmap->stride;
	}
	fz_md5_final(&state, digest);
	fz_drop_pixmap(ctx, pixmap);
}

/* Image specific methods */
static void
pdf_preload_image_resources(fz_context *ctx, pdf_document *doc)
{
	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)
	{
		len = pdf_count_objects(ctx, doc);
		for (k = 1; k < len; k++)
		{
			obj = pdf_load_object(ctx, doc, k);
			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);
				fz_md5_image(ctx, image, digest);
				fz_drop_image(ctx, image);
				image = NULL;

				/* Don't allow overwrites. */
				if (!fz_hash_find(ctx, doc->resources.images, digest))
					fz_hash_insert(ctx, doc->resources.images, digest, pdf_keep_obj(ctx, obj));
			}
			pdf_drop_obj(ctx, obj);
			obj = NULL;
		}
	}
	fz_always(ctx)
	{
		fz_drop_image(ctx, image);
		pdf_drop_obj(ctx, obj);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}

pdf_obj *
pdf_find_image_resource(fz_context *ctx, pdf_document *doc, fz_image *item, unsigned char digest[16])
{
	pdf_obj *res;

	if (!doc->resources.images)
	{
		doc->resources.images = fz_new_hash_table(ctx, 4096, 16, -1);
		pdf_preload_image_resources(ctx, doc);
	}

	/* Create md5 and see if we have the item in our table */
	fz_md5_image(ctx, item, digest);
	res = fz_hash_find(ctx, doc->resources.images, digest);
	if (res)
		pdf_keep_obj(ctx, res);
	return res;
}

pdf_obj *
pdf_insert_image_resource(fz_context *ctx, pdf_document *doc, unsigned char digest[15], pdf_obj *obj)
{
	pdf_obj *res = fz_hash_insert(ctx, doc->resources.images, digest, obj);
	if (res)
		fz_warn(ctx, "warning: image resource already present");
	else
		res = pdf_keep_obj(ctx, obj);
	return res;
}

/* We do need to come up with an effective way to see what is already in the
 * file to avoid adding to what is already there. This is avoided for pdfwrite
 * as we check as we add each font. For adding text to an existing file though
 * it may be more problematic. */

pdf_obj *
pdf_find_font_resource(fz_context *ctx, pdf_document *doc, fz_buffer *item, unsigned char digest[16])
{
	pdf_obj *res;

	if (!doc->resources.fonts)
		doc->resources.fonts = fz_new_hash_table(ctx, 4096, 16, -1);

	/* Create md5 and see if we have the item in our table */
	fz_md5_buffer(ctx, item, digest);
	res = fz_hash_find(ctx, doc->resources.fonts, digest);
	if (res)
		pdf_keep_obj(ctx, res);
	return res;
}

pdf_obj *
pdf_insert_font_resource(fz_context *ctx, pdf_document *doc, unsigned char digest[15], pdf_obj *obj)
{
	pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, digest, obj);
	if (res)
		fz_warn(ctx, "warning: font resource already present");
	else
		res = pdf_keep_obj(ctx, obj);
	return res;
}

static void
res_table_free(fz_context *ctx, fz_hash_table *hash)
{
	int i, n;
	if (hash)
	{
		n = fz_hash_len(ctx, hash);
		for (i = 0; i < n; i++)
			pdf_drop_obj(ctx, fz_hash_get_val(ctx, hash, i));
		fz_drop_hash(ctx, hash);
	}
}

void
pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc)
{
	res_table_free(ctx, doc->resources.fonts);
	res_table_free(ctx, doc->resources.images);
}