forked from openslide/openslide
-
Notifications
You must be signed in to change notification settings - Fork 4
/
openslide-decode-jp2k.c
298 lines (264 loc) · 10.4 KB
/
openslide-decode-jp2k.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* OpenSlide, a library for reading whole slide image files
*
* Copyright (c) 2007-2015 Carnegie Mellon University
* Copyright (c) 2011 Google, Inc.
* Copyright (c) 2015 Benjamin Gilbert
* All rights reserved.
*
* OpenSlide is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 2.1.
*
* OpenSlide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with OpenSlide. If not, see
* <https://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include "openslide-private.h"
#include "openslide-decode-jp2k.h"
#include <openjpeg.h>
G_DEFINE_AUTOPTR_CLEANUP_FUNC(opj_codec_t, opj_destroy_codec)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(opj_image_t, opj_image_destroy)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(opj_stream_t, opj_stream_destroy)
struct buffer_state {
const uint8_t *data;
int32_t offset;
int32_t length;
};
static inline void write_pixel_ycbcr(uint32_t *dest, uint8_t Y,
int16_t R_chroma, int16_t G_chroma,
int16_t B_chroma) {
int16_t R = Y + R_chroma;
int16_t G = Y + G_chroma;
int16_t B = Y + B_chroma;
R = CLAMP(R, 0, 255);
G = CLAMP(G, 0, 255);
B = CLAMP(B, 0, 255);
*dest = 0xff000000 | ((uint8_t) R << 16) | ((uint8_t) G << 8) | ((uint8_t) B);
}
static inline void write_pixel_rgb(uint32_t *dest,
uint8_t R, uint8_t G, uint8_t B) {
*dest = 0xff000000 | R << 16 | G << 8 | B;
}
static void unpack_argb(enum _openslide_jp2k_colorspace space,
opj_image_comp_t *comps,
uint32_t *dest,
int32_t w, int32_t h) {
int c0_sub_x = w / comps[0].w;
int c1_sub_x = w / comps[1].w;
int c2_sub_x = w / comps[2].w;
int c0_sub_y = h / comps[0].h;
int c1_sub_y = h / comps[1].h;
int c2_sub_y = h / comps[2].h;
//g_debug("color space %d, subsamples x %d-%d-%d y %d-%d-%d", space, c0_sub_x, c1_sub_x, c2_sub_x, c0_sub_y, c1_sub_y, c2_sub_y);
if (space == OPENSLIDE_JP2K_YCBCR &&
c0_sub_x == 1 && c1_sub_x == 2 && c2_sub_x == 2 &&
c0_sub_y == 1 && c1_sub_y == 1 && c2_sub_y == 1) {
// Aperio 33003
for (int32_t y = 0; y < h; y++) {
int32_t c0_row_base = y * comps[0].w;
int32_t c1_row_base = y * comps[1].w;
int32_t c2_row_base = y * comps[2].w;
int32_t x;
for (x = 0; x < w - 1; x += 2) {
uint8_t c0 = comps[0].data[c0_row_base + x];
uint8_t c1 = comps[1].data[c1_row_base + (x / 2)];
uint8_t c2 = comps[2].data[c2_row_base + (x / 2)];
int16_t R_chroma = _openslide_R_Cr[c2];
int16_t G_chroma = (_openslide_G_Cb[c1] + _openslide_G_Cr[c2]) >> 16;
int16_t B_chroma = _openslide_B_Cb[c1];
write_pixel_ycbcr(dest++, c0, R_chroma, G_chroma, B_chroma);
c0 = comps[0].data[c0_row_base + x + 1];
write_pixel_ycbcr(dest++, c0, R_chroma, G_chroma, B_chroma);
}
if (x < w) {
uint8_t c0 = comps[0].data[c0_row_base + x];
uint8_t c1 = comps[1].data[c1_row_base + (x / 2)];
uint8_t c2 = comps[2].data[c2_row_base + (x / 2)];
int16_t R_chroma = _openslide_R_Cr[c2];
int16_t G_chroma = (_openslide_G_Cb[c1] + _openslide_G_Cr[c2]) >> 16;
int16_t B_chroma = _openslide_B_Cb[c1];
write_pixel_ycbcr(dest++, c0, R_chroma, G_chroma, B_chroma);
}
}
} else if (space == OPENSLIDE_JP2K_YCBCR) {
// Slow fallback
static gint warned_slowpath_ycbcr;
_openslide_performance_warn_once(&warned_slowpath_ycbcr,
"Decoding YCbCr JP2K image via "
"slow fallback, subsamples "
"x %d-%d-%d y %d-%d-%d",
c0_sub_x, c1_sub_x, c2_sub_x,
c0_sub_y, c1_sub_y, c2_sub_y);
for (int32_t y = 0; y < h; y++) {
int32_t c0_row_base = (y / c0_sub_y) * comps[0].w;
int32_t c1_row_base = (y / c1_sub_y) * comps[1].w;
int32_t c2_row_base = (y / c2_sub_y) * comps[2].w;
for (int32_t x = 0; x < w; x++) {
uint8_t c0 = comps[0].data[c0_row_base + (x / c0_sub_x)];
uint8_t c1 = comps[1].data[c1_row_base + (x / c1_sub_x)];
uint8_t c2 = comps[2].data[c2_row_base + (x / c2_sub_x)];
int16_t R_chroma = _openslide_R_Cr[c2];
int16_t G_chroma = (_openslide_G_Cb[c1] + _openslide_G_Cr[c2]) >> 16;
int16_t B_chroma = _openslide_B_Cb[c1];
write_pixel_ycbcr(dest++, c0, R_chroma, G_chroma, B_chroma);
}
}
} else if (space == OPENSLIDE_JP2K_RGB &&
c0_sub_x == 1 && c1_sub_x == 1 && c2_sub_x == 1 &&
c0_sub_y == 1 && c1_sub_y == 1 && c2_sub_y == 1) {
// Aperio 33005
for (int32_t y = 0; y < h; y++) {
int32_t c0_row_base = y * comps[0].w;
int32_t c1_row_base = y * comps[1].w;
int32_t c2_row_base = y * comps[2].w;
for (int32_t x = 0; x < w; x++) {
uint8_t c0 = comps[0].data[c0_row_base + x];
uint8_t c1 = comps[1].data[c1_row_base + x];
uint8_t c2 = comps[2].data[c2_row_base + x];
write_pixel_rgb(dest++, c0, c1, c2);
}
}
} else if (space == OPENSLIDE_JP2K_RGB) {
// Slow fallback
static gint warned_slowpath_rgb;
_openslide_performance_warn_once(&warned_slowpath_rgb,
"Decoding RGB JP2K image via "
"slow fallback, subsamples "
"x %d-%d-%d y %d-%d-%d",
c0_sub_x, c1_sub_x, c2_sub_x,
c0_sub_y, c1_sub_y, c2_sub_y);
for (int32_t y = 0; y < h; y++) {
int32_t c0_row_base = (y / c0_sub_y) * comps[0].w;
int32_t c1_row_base = (y / c1_sub_y) * comps[1].w;
int32_t c2_row_base = (y / c2_sub_y) * comps[2].w;
for (int32_t x = 0; x < w; x++) {
uint8_t c0 = comps[0].data[c0_row_base + (x / c0_sub_x)];
uint8_t c1 = comps[1].data[c1_row_base + (x / c1_sub_x)];
uint8_t c2 = comps[2].data[c2_row_base + (x / c2_sub_x)];
write_pixel_rgb(dest++, c0, c1, c2);
}
}
}
}
static void warning_callback(const char *msg, void *data G_GNUC_UNUSED) {
if (_openslide_debug(OPENSLIDE_DEBUG_DECODING)) {
g_autofree char *detail = g_strdup(msg);
g_strchomp(detail);
g_warning("OpenJPEG warning: %s", detail);
}
}
static void error_callback(const char *msg, void *data) {
GError **err = (GError **) data;
if (err && !*err) {
g_autofree char *detail = g_strdup(msg);
g_strchomp(detail);
// OpenJPEG can produce obscure error messages, so make sure to
// indicate where they came from
g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
"OpenJPEG error: %s", detail);
}
}
static OPJ_SIZE_T read_callback(void *buf, OPJ_SIZE_T count, void *data) {
struct buffer_state *state = data;
count = MIN(count, (OPJ_SIZE_T) (state->length - state->offset));
if (!count) {
return (OPJ_SIZE_T) -1;
}
memcpy(buf, state->data + state->offset, count);
state->offset += count;
return count;
}
static OPJ_OFF_T skip_callback(OPJ_OFF_T count, void *data) {
struct buffer_state *state = data;
int32_t orig_offset = state->offset;
state->offset = CLAMP(state->offset + count, 0, state->length);
if (count && state->offset == orig_offset) {
return -1;
}
return state->offset - orig_offset;
}
static OPJ_BOOL seek_callback(OPJ_OFF_T offset, void *data) {
struct buffer_state *state = data;
if (offset < 0 || offset > state->length) {
return OPJ_FALSE;
}
state->offset = offset;
return OPJ_TRUE;
}
bool _openslide_jp2k_decode_buffer(uint32_t *dest,
int32_t w, int32_t h,
const void *data, int32_t datalen,
enum _openslide_jp2k_colorspace space,
GError **err) {
g_assert(data != NULL);
g_assert(datalen >= 0);
// init stream
g_autoptr(opj_stream_t) stream = opj_stream_create(datalen, true);
struct buffer_state state = {
.data = data,
.length = datalen,
};
opj_stream_set_user_data(stream, &state, NULL);
opj_stream_set_user_data_length(stream, datalen);
opj_stream_set_read_function(stream, read_callback);
opj_stream_set_skip_function(stream, skip_callback);
opj_stream_set_seek_function(stream, seek_callback);
// init codec
g_autoptr(opj_codec_t) codec = opj_create_decompress(OPJ_CODEC_J2K);
opj_dparameters_t parameters;
opj_set_default_decoder_parameters(¶meters);
opj_setup_decoder(codec, ¶meters);
// enable error handlers
// note: don't use info_handler, it outputs lots of junk
GError *tmp_err = NULL;
opj_set_warning_handler(codec, warning_callback, &tmp_err);
opj_set_error_handler(codec, error_callback, &tmp_err);
// read header
g_autoptr(opj_image_t) image = NULL;
if (!opj_read_header(stream, codec, &image)) {
if (tmp_err) {
g_propagate_error(err, tmp_err);
} else {
g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
"opj_read_header() failed");
}
return false;
}
g_clear_error(&tmp_err); // clear any spurious message
// sanity checks
if (image->x1 != (OPJ_UINT32) w || image->y1 != (OPJ_UINT32) h) {
g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
"Dimensional mismatch reading JP2K, "
"expected %dx%d, got %ux%u",
w, h, image->x1, image->y1);
return false;
}
if (image->numcomps != 3) {
g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
"Expected 3 image components, found %u", image->numcomps);
return false;
}
// TODO more checks?
// decode
if (!opj_decode(codec, stream, image)) {
if (tmp_err) {
g_propagate_error(err, tmp_err);
} else {
g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
"opj_decode() failed");
}
return false;
}
g_clear_error(&tmp_err); // clear any spurious message
// copy pixels
unpack_argb(space, image->comps, dest, w, h);
return true;
}