1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
6 // This C++ example decodes a JPEG XL image in one shot (all input bytes
7 // available at once). The example outputs the pixels and color information to a
8 // floating point image and an ICC profile on disk.
10 #include <jxl/decode.h>
11 #include <jxl/decode_cxx.h>
18 bool DecodeJpegXlExif(const uint8_t* jxl
, size_t size
,
19 std::vector
<uint8_t>* exif
) {
20 auto dec
= JxlDecoderMake(nullptr);
22 // We're only interested in the Exif boxes in this example, so don't
23 // subscribe to events related to pixel data.
24 if (JXL_DEC_SUCCESS
!= JxlDecoderSubscribeEvents(dec
.get(), JXL_DEC_BOX
)) {
25 fprintf(stderr
, "JxlDecoderSubscribeEvents failed\n");
28 bool support_decompression
= true;
29 if (JXL_DEC_SUCCESS
!= JxlDecoderSetDecompressBoxes(dec
.get(), JXL_TRUE
)) {
31 "NOTE: decompressing brob boxes not supported with the currently "
32 "used jxl library.\n");
33 support_decompression
= false;
36 JxlDecoderSetInput(dec
.get(), jxl
, size
);
37 JxlDecoderCloseInput(dec
.get());
39 const constexpr size_t kChunkSize
= 65536;
40 size_t output_pos
= 0;
43 JxlDecoderStatus status
= JxlDecoderProcessInput(dec
.get());
44 if (status
== JXL_DEC_ERROR
) {
45 fprintf(stderr
, "Decoder error\n");
47 } else if (status
== JXL_DEC_NEED_MORE_INPUT
) {
48 fprintf(stderr
, "Error, already provided all input\n");
50 } else if (status
== JXL_DEC_BOX
) {
52 size_t remaining
= JxlDecoderReleaseBoxBuffer(dec
.get());
53 exif
->resize(exif
->size() - remaining
);
54 // No need to wait for JXL_DEC_SUCCESS or decode other boxes.
58 if (JXL_DEC_SUCCESS
!=
59 JxlDecoderGetBoxType(dec
.get(), type
, support_decompression
)) {
60 fprintf(stderr
, "Error, failed to get box type\n");
63 if (!memcmp(type
, "Exif", 4)) {
64 exif
->resize(kChunkSize
);
65 JxlDecoderSetBoxBuffer(dec
.get(), exif
->data(), exif
->size());
67 } else if (status
== JXL_DEC_BOX_NEED_MORE_OUTPUT
) {
68 size_t remaining
= JxlDecoderReleaseBoxBuffer(dec
.get());
69 output_pos
+= kChunkSize
- remaining
;
70 exif
->resize(exif
->size() + kChunkSize
);
71 JxlDecoderSetBoxBuffer(dec
.get(), exif
->data() + output_pos
,
72 exif
->size() - output_pos
);
73 } else if (status
== JXL_DEC_SUCCESS
) {
75 size_t remaining
= JxlDecoderReleaseBoxBuffer(dec
.get());
76 exif
->resize(exif
->size() - remaining
);
81 fprintf(stderr
, "Unknown decoder status\n");
87 bool LoadFile(const char* filename
, std::vector
<uint8_t>* out
) {
88 FILE* file
= fopen(filename
, "rb");
93 if (fseek(file
, 0, SEEK_END
) != 0) {
98 long size
= ftell(file
);
99 // Avoid invalid file or directory.
100 if (size
>= LONG_MAX
|| size
< 0) {
105 if (fseek(file
, 0, SEEK_SET
) != 0) {
111 size_t readsize
= fread(out
->data(), 1, size
, file
);
112 if (fclose(file
) != 0) {
116 return readsize
== static_cast<size_t>(size
);
119 bool WriteFile(const char* filename
, const uint8_t* data
, size_t size
) {
120 FILE* file
= fopen(filename
, "wb");
122 fprintf(stderr
, "Could not open %s for writing", filename
);
125 fwrite(data
, 1, size
, file
);
126 if (fclose(file
) != 0) {
132 int main(int argc
, char* argv
[]) {
135 "Usage: %s <jxl> <exif>\n"
137 " jxl = input JPEG XL image filename\n"
138 " exif = output exif filename\n"
139 "Output files will be overwritten.\n",
144 const char* jxl_filename
= argv
[1];
145 const char* exif_filename
= argv
[2];
147 std::vector
<uint8_t> jxl
;
148 if (!LoadFile(jxl_filename
, &jxl
)) {
149 fprintf(stderr
, "couldn't load %s\n", jxl_filename
);
153 std::vector
<uint8_t> exif
;
154 if (!DecodeJpegXlExif(jxl
.data(), jxl
.size(), &exif
)) {
155 fprintf(stderr
, "Error while decoding the jxl file\n");
159 printf("No exif data present in this image\n");
161 // TODO(lode): the exif box data contains the 4-byte TIFF header at the
162 // beginning, check whether this is desired to be part of the output, or
163 // should be removed.
164 if (!WriteFile(exif_filename
, exif
.data(), exif
.size())) {
165 fprintf(stderr
, "Error while writing the exif file\n");
168 printf("Successfully wrote %s\n", exif_filename
);