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
|
// 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.
// This C++ example decodes a JPEG XL image in one shot (all input bytes
// available at once). The example outputs the pixels and color information to a
// floating point image and an ICC profile on disk.
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include "jxl/decode.h"
#include "jxl/decode_cxx.h"
bool DecodeJpegXlExif(const uint8_t* jxl, size_t size,
std::vector<uint8_t>* exif) {
auto dec = JxlDecoderMake(nullptr);
// We're only interested in the Exif boxes in this example, so don't
// subscribe to events related to pixel data.
if (JXL_DEC_SUCCESS != JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BOX)) {
fprintf(stderr, "JxlDecoderSubscribeEvents failed\n");
return false;
}
bool support_decompression = true;
if (JXL_DEC_SUCCESS != JxlDecoderSetDecompressBoxes(dec.get(), JXL_TRUE)) {
fprintf(stderr,
"NOTE: decompressing brob boxes not supported with the currently "
"used jxl library.\n");
support_decompression = false;
}
JxlDecoderSetInput(dec.get(), jxl, size);
JxlDecoderCloseInput(dec.get());
const constexpr size_t kChunkSize = 65536;
size_t output_pos = 0;
for (;;) {
JxlDecoderStatus status = JxlDecoderProcessInput(dec.get());
if (status == JXL_DEC_ERROR) {
fprintf(stderr, "Decoder error\n");
return false;
} else if (status == JXL_DEC_NEED_MORE_INPUT) {
fprintf(stderr, "Error, already provided all input\n");
return false;
} else if (status == JXL_DEC_BOX) {
if (!exif->empty()) {
size_t remaining = JxlDecoderReleaseBoxBuffer(dec.get());
exif->resize(exif->size() - remaining);
// No need to wait for JXL_DEC_SUCCESS or decode other boxes.
return true;
}
JxlBoxType type;
if (JXL_DEC_SUCCESS !=
JxlDecoderGetBoxType(dec.get(), type, support_decompression)) {
fprintf(stderr, "Error, failed to get box type\n");
return false;
}
if (!memcmp(type, "Exif", 4)) {
exif->resize(kChunkSize);
JxlDecoderSetBoxBuffer(dec.get(), exif->data(), exif->size());
}
} else if (status == JXL_DEC_BOX_NEED_MORE_OUTPUT) {
size_t remaining = JxlDecoderReleaseBoxBuffer(dec.get());
output_pos += kChunkSize - remaining;
exif->resize(exif->size() + kChunkSize);
JxlDecoderSetBoxBuffer(dec.get(), exif->data() + output_pos,
exif->size() - output_pos);
} else if (status == JXL_DEC_SUCCESS) {
if (!exif->empty()) {
size_t remaining = JxlDecoderReleaseBoxBuffer(dec.get());
exif->resize(exif->size() - remaining);
return true;
}
return true;
} else {
fprintf(stderr, "Unknown decoder status\n");
return false;
}
}
}
bool LoadFile(const char* filename, std::vector<uint8_t>* out) {
FILE* file = fopen(filename, "rb");
if (!file) {
return false;
}
if (fseek(file, 0, SEEK_END) != 0) {
fclose(file);
return false;
}
long size = ftell(file);
// Avoid invalid file or directory.
if (size >= LONG_MAX || size < 0) {
fclose(file);
return false;
}
if (fseek(file, 0, SEEK_SET) != 0) {
fclose(file);
return false;
}
out->resize(size);
size_t readsize = fread(out->data(), 1, size, file);
if (fclose(file) != 0) {
return false;
}
return readsize == static_cast<size_t>(size);
}
bool WriteFile(const char* filename, const uint8_t* data, size_t size) {
FILE* file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Could not open %s for writing", filename);
return false;
}
fwrite(data, 1, size, file);
if (fclose(file) != 0) {
return false;
}
return true;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
fprintf(stderr,
"Usage: %s <jxl> <exif>\n"
"Where:\n"
" jxl = input JPEG XL image filename\n"
" exif = output exif filename\n"
"Output files will be overwritten.\n",
argv[0]);
return 1;
}
const char* jxl_filename = argv[1];
const char* exif_filename = argv[2];
std::vector<uint8_t> jxl;
if (!LoadFile(jxl_filename, &jxl)) {
fprintf(stderr, "couldn't load %s\n", jxl_filename);
return 1;
}
std::vector<uint8_t> exif;
if (!DecodeJpegXlExif(jxl.data(), jxl.size(), &exif)) {
fprintf(stderr, "Error while decoding the jxl file\n");
return 1;
}
if (exif.empty()) {
printf("No exif data present in this image\n");
} else {
// TODO(lode): the exif box data contains the 4-byte TIFF header at the
// beginning, check whether this is desired to be part of the output, or
// should be removed.
if (!WriteFile(exif_filename, exif.data(), exif.size())) {
fprintf(stderr, "Error while writing the exif file\n");
return 1;
}
printf("Successfully wrote %s\n", exif_filename);
}
return 0;
}
|