Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include "mupdf/fitz.h"
2
3
#define DPI 72.0f
4
5
typedef struct img_document_s img_document;
6
typedef struct img_page_s img_page;
7
8
struct img_page_s
9
{
10
fz_page super;
11
fz_image *image;
12
};
13
14
struct img_document_s
15
{
16
fz_document super;
17
fz_image *image;
18
};
19
20
static void
21
img_close_document(fz_context *ctx, img_document *doc)
22
{
23
fz_drop_image(ctx, doc->image);
24
fz_free(ctx, doc);
25
}
26
27
static int
28
img_count_pages(fz_context *ctx, img_document *doc)
29
{
30
return 1;
31
}
32
33
static fz_rect *
34
img_bound_page(fz_context *ctx, img_page *page, fz_rect *bbox)
35
{
36
fz_image *image = page->image;
37
int xres, yres;
38
fz_image_get_sanitised_res(image, &xres, &yres);
39
bbox->x0 = bbox->y0 = 0;
40
bbox->x1 = image->w * DPI / xres;
41
bbox->y1 = image->h * DPI / yres;
42
return bbox;
43
}
44
45
static void
46
img_run_page(fz_context *ctx, img_page *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
47
{
48
fz_matrix local_ctm = *ctm;
49
fz_image *image = page->image;
50
int xres, yres;
51
float w, h;
52
fz_image_get_sanitised_res(image, &xres, &yres);
53
w = image->w * DPI / xres;
54
h = image->h * DPI / yres;
55
fz_pre_scale(&local_ctm, w, h);
56
fz_fill_image(ctx, dev, image, &local_ctm, 1);
57
}
58
59
static void
60
img_drop_page_imp(fz_context *ctx, img_page *page)
61
{
62
fz_drop_image(ctx, page->image);
63
}
64
65
static img_page *
66
img_load_page(fz_context *ctx, img_document *doc, int number)
67
{
68
img_page *page;
69
70
if (number != 0)
71
return NULL;
72
73
page = fz_new_page(ctx, sizeof *page);
74
75
page->super.bound_page = (fz_page_bound_page_fn *)img_bound_page;
76
page->super.run_page_contents = (fz_page_run_page_contents_fn *)img_run_page;
77
page->super.drop_page_imp = (fz_page_drop_page_imp_fn *)img_drop_page_imp;
78
79
page->image = fz_keep_image(ctx, doc->image);
80
81
return page;
82
}
83
84
static int
85
img_lookup_metadata(fz_context *ctx, img_document *doc, const char *key, char *buf, int size)
86
{
87
if (!strcmp(key, "format"))
88
return fz_strlcpy(buf, "Image", size);
89
return -1;
90
}
91
92
static img_document *
93
img_new_document(fz_context *ctx, fz_image *image)
94
{
95
img_document *doc = fz_new_document(ctx, sizeof *doc);
96
97
doc->super.close = (fz_document_close_fn *)img_close_document;
98
doc->super.count_pages = (fz_document_count_pages_fn *)img_count_pages;
99
doc->super.load_page = (fz_document_load_page_fn *)img_load_page;
100
doc->super.lookup_metadata = (fz_document_lookup_metadata_fn *)img_lookup_metadata;
101
102
doc->image = fz_keep_image(ctx, image);
103
104
return doc;
105
}
106
107
static img_document *
108
img_open_document_with_stream(fz_context *ctx, fz_stream *stm)
109
{
110
fz_buffer *buffer = NULL;
111
fz_image *image = NULL;
112
img_document *doc;
113
114
fz_var(buffer);
115
fz_var(image);
116
117
fz_try(ctx)
118
{
119
buffer = fz_read_all(ctx, stm, 1024);
120
image = fz_new_image_from_buffer(ctx, buffer);
121
doc = img_new_document(ctx, image);
122
}
123
fz_always(ctx)
124
{
125
fz_drop_image(ctx, image);
126
fz_drop_buffer(ctx, buffer);
127
}
128
fz_catch(ctx)
129
fz_rethrow(ctx);
130
131
return doc;
132
}
133
134
static img_document *
135
img_open_document(fz_context *ctx, const char *filename)
136
{
137
fz_stream *stm;
138
img_document *doc;
139
140
stm = fz_open_file(ctx, filename);
141
142
fz_try(ctx)
143
doc = img_open_document_with_stream(ctx, stm);
144
fz_always(ctx)
145
fz_drop_stream(ctx, stm);
146
fz_catch(ctx)
147
fz_rethrow(ctx);
148
149
return doc;
150
}
151
152
static int
153
img_recognize(fz_context *doc, const char *magic)
154
{
155
char *ext = strrchr(magic, '.');
156
157
if (ext)
158
{
159
if (!fz_strcasecmp(ext, ".png") || !fz_strcasecmp(ext, ".jpg") ||
160
!fz_strcasecmp(ext, ".jpeg") || !fz_strcasecmp(ext, ".jfif") ||
161
!fz_strcasecmp(ext, ".jfif-tbnl") || !fz_strcasecmp(ext, ".jpe"))
162
return 100;
163
}
164
if (!strcmp(magic, "png") || !strcmp(magic, "image/png") ||
165
!strcmp(magic, "jpg") || !strcmp(magic, "image/jpeg") ||
166
!strcmp(magic, "jpeg") || !strcmp(magic, "image/pjpeg") ||
167
!strcmp(magic, "jpe") || !strcmp(magic, "jfif"))
168
return 100;
169
170
return 0;
171
}
172
173
fz_document_handler img_document_handler =
174
{
175
(fz_document_recognize_fn *)&img_recognize,
176
(fz_document_open_fn *)&img_open_document,
177
(fz_document_open_with_stream_fn *)&img_open_document_with_stream
178
};
179
180