Skip to content

Commit

Permalink
detect broken ICC when encoding jpg (libjxl#3833)
Browse files Browse the repository at this point in the history
* detect broken ICC when encoding jpg

* Update lib/jxl/encode.cc

Co-authored-by: Jon Sneyers <jon@cloudinary.com>

* fix format

---------

Co-authored-by: Jon Sneyers <jon@cloudinary.com>
(cherry picked from commit 635eb58)
  • Loading branch information
mo271 committed Nov 26, 2024
1 parent 9212d84 commit 23f992e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/jxl/encode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2097,8 +2097,13 @@ JxlEncoderStatus JxlEncoderAddJPEGFrame(
}

if (!frame_settings->enc->color_encoding_set) {
SetColorEncodingFromJpegData(
*io.Main().jpeg_data, &frame_settings->enc->metadata.m.color_encoding);
if (!SetColorEncodingFromJpegData(
*io.Main().jpeg_data,
&frame_settings->enc->metadata.m.color_encoding)) {
return JXL_API_ERROR(
frame_settings->enc, JXL_ENC_ERR_BAD_INPUT,
"Error decoding the ICC profile embedded in the input JPEG");
}
frame_settings->enc->color_encoding_set = true;
}

Expand Down
12 changes: 8 additions & 4 deletions lib/jxl/jpeg/enc_jpeg_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "lib/jxl/jpeg/enc_jpeg_data.h"

#include <brotli/encode.h>
#include <jxl/cms.h>
#include <jxl/memory_manager.h>
#include <jxl/types.h>

Expand Down Expand Up @@ -222,8 +223,8 @@ inline bool IsJPG(const Span<const uint8_t> bytes) {

} // namespace

void SetColorEncodingFromJpegData(const jpeg::JPEGData& jpg,
ColorEncoding* color_encoding) {
Status SetColorEncodingFromJpegData(const jpeg::JPEGData& jpg,
ColorEncoding* color_encoding) {
IccBytes icc_profile;
if (!ParseChunkedMarker(jpg, kApp2, ByteSpan(kIccProfileTag), &icc_profile)) {
JXL_WARNING("ReJPEG: corrupted ICC profile\n");
Expand All @@ -234,8 +235,10 @@ void SetColorEncodingFromJpegData(const jpeg::JPEGData& jpg,
bool is_gray = (jpg.components.size() == 1);
*color_encoding = ColorEncoding::SRGB(is_gray);
} else {
color_encoding->SetICCRaw(std::move(icc_profile));
JXL_RETURN_IF_ERROR(
color_encoding->SetICC(std::move(icc_profile), JxlGetDefaultCms()));
}
return true;
}

Status SetChromaSubsamplingFromJpegData(const JPEGData& jpg,
Expand Down Expand Up @@ -398,7 +401,8 @@ Status DecodeImageJPG(const Span<const uint8_t> bytes, CodecInOut* io) {
jpeg_data)) {
return JXL_FAILURE("Error reading JPEG");
}
SetColorEncodingFromJpegData(*jpeg_data, &io->metadata.m.color_encoding);
JXL_RETURN_IF_ERROR(
SetColorEncodingFromJpegData(*jpeg_data, &io->metadata.m.color_encoding));
JXL_RETURN_IF_ERROR(SetBlobsFromJpegData(*jpeg_data, &io->blobs));
JXL_RETURN_IF_ERROR(SetChromaSubsamplingFromJpegData(
*jpeg_data, &io->Main().chroma_subsampling));
Expand Down
4 changes: 2 additions & 2 deletions lib/jxl/jpeg/enc_jpeg_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ Status EncodeJPEGData(JxlMemoryManager* memory_manager, JPEGData& jpeg_data,
std::vector<uint8_t>* bytes,
const CompressParams& cparams);

void SetColorEncodingFromJpegData(const jpeg::JPEGData& jpg,
ColorEncoding* color_encoding);
Status SetColorEncodingFromJpegData(const jpeg::JPEGData& jpg,
ColorEncoding* color_encoding);
Status SetChromaSubsamplingFromJpegData(const JPEGData& jpg,
YCbCrChromaSubsampling* cs);
Status SetColorTransformFromJpegData(const JPEGData& jpg,
Expand Down

0 comments on commit 23f992e

Please sign in to comment.