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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#include "fitz-internal.h"
#define OPJ_STATIC
#include <openjpeg.h>
static void fz_opj_error_callback(const char *msg, void *client_data)
{
fz_context *ctx = (fz_context *)client_data;
fz_warn(ctx, "openjpeg error: %s", msg);
}
static void fz_opj_warning_callback(const char *msg, void *client_data)
{
fz_context *ctx = (fz_context *)client_data;
fz_warn(ctx, "openjpeg warning: %s", msg);
}
static void fz_opj_info_callback(const char *msg, void *client_data)
{
/* fz_warn("openjpeg info: %s", msg); */
}
fz_pixmap *
fz_load_jpx(fz_context *ctx, unsigned char *data, int size, fz_colorspace *defcs, int indexed)
{
fz_pixmap *img;
fz_colorspace *origcs;
opj_event_mgr_t evtmgr;
opj_dparameters_t params;
opj_dinfo_t *info;
opj_cio_t *cio;
opj_image_t *jpx;
fz_colorspace *colorspace;
unsigned char *p;
int format;
int a, n, w, h, depth, sgnd;
int x, y, k, v;
if (size < 2)
fz_throw(ctx, "not enough data to determine image format");
/* Check for SOC marker -- if found we have a bare J2K stream */
if (data[0] == 0xFF && data[1] == 0x4F)
format = CODEC_J2K;
else
format = CODEC_JP2;
memset(&evtmgr, 0, sizeof(evtmgr));
evtmgr.error_handler = fz_opj_error_callback;
evtmgr.warning_handler = fz_opj_warning_callback;
evtmgr.info_handler = fz_opj_info_callback;
opj_set_default_decoder_parameters(¶ms);
if (indexed)
params.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
info = opj_create_decompress(format);
opj_set_event_mgr((opj_common_ptr)info, &evtmgr, ctx);
opj_setup_decoder(info, ¶ms);
cio = opj_cio_open((opj_common_ptr)info, data, size);
jpx = opj_decode(info, cio);
opj_cio_close(cio);
opj_destroy_decompress(info);
if (!jpx)
fz_throw(ctx, "opj_decode failed");
for (k = 1; k < jpx->numcomps; k++)
{
if (jpx->comps[k].w != jpx->comps[0].w)
{
opj_image_destroy(jpx);
fz_throw(ctx, "image components have different width");
}
if (jpx->comps[k].h != jpx->comps[0].h)
{
opj_image_destroy(jpx);
fz_throw(ctx, "image components have different height");
}
if (jpx->comps[k].prec != jpx->comps[0].prec)
{
opj_image_destroy(jpx);
fz_throw(ctx, "image components have different precision");
}
}
n = jpx->numcomps;
w = jpx->comps[0].w;
h = jpx->comps[0].h;
depth = jpx->comps[0].prec;
sgnd = jpx->comps[0].sgnd;
if (jpx->color_space == CLRSPC_SRGB && n == 4) { n = 3; a = 1; }
else if (jpx->color_space == CLRSPC_SYCC && n == 4) { n = 3; a = 1; }
else if (n == 2) { n = 1; a = 1; }
else if (n > 4) { n = 4; a = 1; }
else { a = 0; }
origcs = defcs;
if (defcs)
{
if (defcs->n == n)
{
colorspace = defcs;
}
else
{
fz_warn(ctx, "jpx file and dict colorspaces do not match");
defcs = NULL;
}
}
if (!defcs)
{
switch (n)
{
case 1: colorspace = fz_device_gray; break;
case 3: colorspace = fz_device_rgb; break;
case 4: colorspace = fz_device_cmyk; break;
}
}
fz_try(ctx)
{
img = fz_new_pixmap(ctx, colorspace, w, h);
}
fz_catch(ctx)
{
opj_image_destroy(jpx);
fz_throw(ctx, "out of memory");
}
p = img->samples;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
{
for (k = 0; k < n + a; k++)
{
v = jpx->comps[k].data[y * w + x];
if (sgnd)
v = v + (1 << (depth - 1));
if (depth > 8)
v = v >> (depth - 8);
*p++ = v;
}
if (!a)
*p++ = 255;
}
}
opj_image_destroy(jpx);
if (a)
{
if (n == 4)
{
fz_pixmap *tmp = fz_new_pixmap(ctx, fz_device_rgb, w, h);
fz_convert_pixmap(ctx, tmp, img);
fz_drop_pixmap(ctx, img);
img = tmp;
}
fz_premultiply_pixmap(ctx, img);
}
if (origcs != defcs)
{
fz_pixmap *tmp = fz_new_pixmap(ctx, origcs, w, h);
fz_convert_pixmap(ctx, tmp, img);
fz_drop_pixmap(ctx, img);
img = tmp;
}
return img;
}
|