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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "plugins/gimp/file-jxl-load.h"
#define _PROFILE_ORIGIN_ JXL_COLOR_PROFILE_TARGET_ORIGINAL
#define _PROFILE_TARGET_ JXL_COLOR_PROFILE_TARGET_DATA
#define LOAD_PROC "file-jxl-load"
namespace jxl {
bool LoadJpegXlImage(const gchar *const filename, gint32 *const image_id) {
std::vector<uint8_t> icc_profile;
GimpColorProfile *profile_icc = nullptr;
GimpColorProfile *profile_int = nullptr;
bool is_linear = false;
gint32 layer;
gpointer pixels_buffer_1 = nullptr;
gpointer pixels_buffer_2 = nullptr;
size_t buffer_size = 0;
GimpImageBaseType image_type = GIMP_RGB;
GimpImageType layer_type = GIMP_RGB_IMAGE;
GimpPrecision precision = GIMP_PRECISION_U16_GAMMA;
JxlBasicInfo info = {};
JxlPixelFormat format = {};
format.num_channels = 4;
format.data_type = JXL_TYPE_FLOAT;
format.endianness = JXL_NATIVE_ENDIAN;
format.align = 0;
bool is_gray = false;
JpegXlGimpProgress gimp_load_progress(
("Opening JPEG XL file:" + std::string(filename)).c_str());
gimp_load_progress.update();
// read file
std::ifstream instream(filename, std::ios::in | std::ios::binary);
std::vector<uint8_t> compressed((std::istreambuf_iterator<char>(instream)),
std::istreambuf_iterator<char>());
instream.close();
gimp_load_progress.update();
// multi-threaded parallel runner.
auto runner = JxlResizableParallelRunnerMake(nullptr);
auto dec = JxlDecoderMake(nullptr);
if (JXL_DEC_SUCCESS !=
JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO |
JXL_DEC_COLOR_ENCODING |
JXL_DEC_FULL_IMAGE)) {
g_printerr(LOAD_PROC " Error: JxlDecoderSubscribeEvents failed\n");
return false;
}
if (JXL_DEC_SUCCESS != JxlDecoderSetParallelRunner(dec.get(),
JxlResizableParallelRunner,
runner.get())) {
g_printerr(LOAD_PROC " Error: JxlDecoderSetParallelRunner failed\n");
return false;
}
// grand decode loop...
JxlDecoderSetInput(dec.get(), compressed.data(), compressed.size());
while (true) {
gimp_load_progress.update();
JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
if (status == JXL_DEC_BASIC_INFO) {
if (JXL_DEC_SUCCESS != JxlDecoderGetBasicInfo(dec.get(), &info)) {
g_printerr(LOAD_PROC " Error: JxlDecoderGetBasicInfo failed\n");
return false;
}
JxlResizableParallelRunnerSetThreads(
runner.get(),
JxlResizableParallelRunnerSuggestThreads(info.xsize, info.ysize));
} else if (status == JXL_DEC_COLOR_ENCODING) {
// check for ICC profile
size_t icc_size = 0;
JxlColorEncoding color_encoding;
if (JXL_DEC_SUCCESS !=
JxlDecoderGetColorAsEncodedProfile(
dec.get(), &format, _PROFILE_ORIGIN_, &color_encoding)) {
// Attempt to load ICC profile when no internal color encoding
if (JXL_DEC_SUCCESS != JxlDecoderGetICCProfileSize(dec.get(), &format,
_PROFILE_ORIGIN_,
&icc_size)) {
g_printerr(LOAD_PROC
" Warning: JxlDecoderGetICCProfileSize failed\n");
}
if (icc_size > 0) {
icc_profile.resize(icc_size);
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(
dec.get(), &format, _PROFILE_ORIGIN_,
icc_profile.data(), icc_profile.size())) {
g_printerr(LOAD_PROC
" Warning: JxlDecoderGetColorAsICCProfile failed\n");
}
profile_icc = gimp_color_profile_new_from_icc_profile(
icc_profile.data(), icc_profile.size(), nullptr);
if (profile_icc) {
is_linear = gimp_color_profile_is_linear(profile_icc);
g_printerr(LOAD_PROC " Info: Color profile is_linear = %d\n",
is_linear);
} else {
g_printerr(LOAD_PROC " Warning: Failed to read ICC profile.\n");
}
} else {
g_printerr(LOAD_PROC " Warning: Empty ICC data.\n");
}
}
// Internal color profile detection...
if (JXL_DEC_SUCCESS ==
JxlDecoderGetColorAsEncodedProfile(
dec.get(), &format, _PROFILE_TARGET_, &color_encoding)) {
g_printerr(LOAD_PROC " Info: Internal color encoding detected.\n");
// figure out linearity of internal profile
switch (color_encoding.transfer_function) {
case JXL_TRANSFER_FUNCTION_LINEAR:
is_linear = true;
break;
case JXL_TRANSFER_FUNCTION_709:
case JXL_TRANSFER_FUNCTION_PQ:
case JXL_TRANSFER_FUNCTION_HLG:
case JXL_TRANSFER_FUNCTION_GAMMA:
case JXL_TRANSFER_FUNCTION_DCI:
case JXL_TRANSFER_FUNCTION_SRGB:
is_linear = false;
break;
case JXL_TRANSFER_FUNCTION_UNKNOWN:
default:
if (profile_icc) {
g_printerr(LOAD_PROC
" Info: Unknown transfer function. "
"ICC profile is present.");
} else {
g_printerr(LOAD_PROC
" Info: Unknown transfer function. "
"No ICC profile present.");
}
break;
}
switch (color_encoding.color_space) {
case JXL_COLOR_SPACE_RGB:
if (color_encoding.white_point == JXL_WHITE_POINT_D65 &&
color_encoding.primaries == JXL_PRIMARIES_SRGB) {
if (is_linear) {
profile_int = gimp_color_profile_new_rgb_srgb_linear();
} else {
profile_int = gimp_color_profile_new_rgb_srgb();
}
} else if (!is_linear &&
color_encoding.white_point == JXL_WHITE_POINT_D65 &&
(color_encoding.primaries_green_xy[0] == 0.2100 ||
color_encoding.primaries_green_xy[1] == 0.7100)) {
// Probably Adobe RGB
profile_int = gimp_color_profile_new_rgb_adobe();
} else if (profile_icc) {
g_printerr(LOAD_PROC
" Info: Unknown RGB colorspace. "
"Using ICC profile.\n");
} else {
g_printerr(LOAD_PROC
" Info: Unknown RGB colorspace. "
"Treating as sRGB.\n");
if (is_linear) {
profile_int = gimp_color_profile_new_rgb_srgb_linear();
} else {
profile_int = gimp_color_profile_new_rgb_srgb();
}
}
break;
case JXL_COLOR_SPACE_GRAY:
is_gray = true;
if (!profile_icc ||
color_encoding.white_point == JXL_WHITE_POINT_D65) {
if (is_linear) {
profile_int = gimp_color_profile_new_d65_gray_linear();
} else {
profile_int = gimp_color_profile_new_d65_gray_srgb_trc();
}
}
break;
case JXL_COLOR_SPACE_XYB:
case JXL_COLOR_SPACE_UNKNOWN:
default:
if (profile_icc) {
g_printerr(LOAD_PROC
" Info: Unknown colorspace. Using ICC profile.\n");
} else {
g_error(
LOAD_PROC
" Warning: Unknown colorspace. Treating as sRGB profile.\n");
if (is_linear) {
profile_int = gimp_color_profile_new_rgb_srgb_linear();
} else {
profile_int = gimp_color_profile_new_rgb_srgb();
}
}
break;
}
}
// set pixel format
if (info.num_color_channels > 1) {
if (info.alpha_bits == 0) {
image_type = GIMP_RGB;
layer_type = GIMP_RGB_IMAGE;
format.num_channels = info.num_color_channels;
} else {
image_type = GIMP_RGB;
layer_type = GIMP_RGBA_IMAGE;
format.num_channels = info.num_color_channels + 1;
}
} else if (info.num_color_channels == 1) {
if (info.alpha_bits == 0) {
image_type = GIMP_GRAY;
layer_type = GIMP_GRAY_IMAGE;
format.num_channels = info.num_color_channels;
} else {
image_type = GIMP_GRAY;
layer_type = GIMP_GRAYA_IMAGE;
format.num_channels = info.num_color_channels + 1;
}
}
// Set image bit depth and linearity
if (info.bits_per_sample <= 8) {
if (is_linear) {
precision = GIMP_PRECISION_U8_LINEAR;
} else {
precision = GIMP_PRECISION_U8_GAMMA;
}
} else if (info.bits_per_sample <= 16) {
if (info.exponent_bits_per_sample > 0) {
if (is_linear) {
precision = GIMP_PRECISION_HALF_LINEAR;
} else {
precision = GIMP_PRECISION_HALF_GAMMA;
}
} else if (is_linear) {
precision = GIMP_PRECISION_U16_LINEAR;
} else {
precision = GIMP_PRECISION_U16_GAMMA;
}
} else {
if (info.exponent_bits_per_sample > 0) {
if (is_linear) {
precision = GIMP_PRECISION_FLOAT_LINEAR;
} else {
precision = GIMP_PRECISION_FLOAT_GAMMA;
}
} else if (is_linear) {
precision = GIMP_PRECISION_U32_LINEAR;
} else {
precision = GIMP_PRECISION_U32_GAMMA;
}
}
// create new image
if (is_linear) {
*image_id = gimp_image_new_with_precision(
info.xsize, info.ysize, image_type, GIMP_PRECISION_FLOAT_LINEAR);
} else {
*image_id = gimp_image_new_with_precision(
info.xsize, info.ysize, image_type, GIMP_PRECISION_FLOAT_GAMMA);
}
if (profile_int) {
gimp_image_set_color_profile(*image_id, profile_int);
} else if (!profile_icc) {
g_printerr(LOAD_PROC " Warning: No color profile.\n");
}
} else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
// get image from decoder in FLOAT
format.data_type = JXL_TYPE_FLOAT;
if (JXL_DEC_SUCCESS !=
JxlDecoderImageOutBufferSize(dec.get(), &format, &buffer_size)) {
g_printerr(LOAD_PROC " Error: JxlDecoderImageOutBufferSize failed\n");
return false;
}
pixels_buffer_1 = g_malloc(buffer_size);
if (JXL_DEC_SUCCESS != JxlDecoderSetImageOutBuffer(dec.get(), &format,
pixels_buffer_1,
buffer_size)) {
g_printerr(LOAD_PROC " Error: JxlDecoderSetImageOutBuffer failed\n");
return false;
}
} else if (status == JXL_DEC_FULL_IMAGE || status == JXL_DEC_FRAME) {
// create and insert layer
layer = gimp_layer_new(*image_id, "Background", info.xsize, info.ysize,
layer_type, /*opacity=*/100,
gimp_image_get_default_new_layer_mode(*image_id));
gimp_image_insert_layer(*image_id, layer, /*parent_id=*/-1,
/*position=*/0);
pixels_buffer_2 = g_malloc(buffer_size);
GeglBuffer *buffer = gimp_drawable_get_buffer(layer);
const Babl *destination_format = gegl_buffer_set_format(buffer, nullptr);
std::string babl_format_str = "";
if (is_gray) {
babl_format_str += "Y'";
} else {
babl_format_str += "R'G'B'";
}
if (info.alpha_bits > 0) {
babl_format_str += "A";
}
babl_format_str += " float";
const Babl *source_format = babl_format(babl_format_str.c_str());
babl_process(babl_fish(source_format, destination_format),
pixels_buffer_1, pixels_buffer_2, info.xsize * info.ysize);
gegl_buffer_set(buffer, GEGL_RECTANGLE(0, 0, info.xsize, info.ysize), 0,
nullptr, pixels_buffer_2, GEGL_AUTO_ROWSTRIDE);
g_clear_object(&buffer);
} else if (status == JXL_DEC_SUCCESS) {
// All decoding successfully finished.
// It's not required to call JxlDecoderReleaseInput(dec.get())
// since the decoder will be destroyed.
break;
} else if (status == JXL_DEC_NEED_MORE_INPUT) {
g_printerr(LOAD_PROC " Error: Already provided all input\n");
return false;
} else if (status == JXL_DEC_ERROR) {
g_printerr(LOAD_PROC " Error: Decoder error\n");
return false;
} else {
g_printerr(LOAD_PROC " Error: Unknown decoder status\n");
return false;
}
} // end grand decode loop
gimp_load_progress.update();
if (profile_icc) {
gimp_image_set_color_profile(*image_id, profile_icc);
}
gimp_load_progress.update();
// TODO(xiota): Add option to keep image as float
if (info.bits_per_sample < 32) {
gimp_image_convert_precision(*image_id, precision);
}
gimp_image_set_filename(*image_id, filename);
gimp_load_progress.finished();
return true;
}
} // namespace jxl
|