Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/extras/enc/jpg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ class JPEGEncoder : public Encoder {
encoded_image->bitstreams.reserve(ppf.frames.size());
for (const auto& frame : ppf.frames) {
JPEGLI_RETURN_IF_ERROR(VerifyPackedImage(frame.color, ppf.info));
if (frame.color.xsize != ppf.info.xsize ||
frame.color.ysize != ppf.info.ysize) {
return JPEGLI_FAILURE("Frame size does not match image size.");
}
encoded_image->bitstreams.emplace_back();
JPEGLI_RETURN_IF_ERROR(EncodeImageJPG(
frame.color, ppf.info, ppf.icc, ppf.metadata.exif, jpeg_encoder,
Expand Down
32 changes: 32 additions & 0 deletions lib/extras/jpegli_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,38 @@ TEST(JpegliTest, JpegliEncodeRejectsMismatchedFrameSize) {
EXPECT_FALSE(EncodeJpeg(ppf_in, settings, nullptr, &compressed));
}

TEST(JpegliTest, JpegliLibjpegEncodeRejectsMismatchedFrameSize) {
TEST_LIBJPEG_SUPPORT();
std::string testimage = "jxl/flower/flower_small.rgb.depth8.ppm";
PackedPixelFile ppf_in;
ASSERT_TRUE(ReadTestImage(testimage, &ppf_in));
ASSERT_FALSE(ppf_in.frames.empty());
const uint32_t orig_xsize = ppf_in.info.xsize;
const uint32_t orig_ysize = ppf_in.info.ysize;
ASSERT_EQ(static_cast<size_t>(orig_xsize), ppf_in.frames[0].color.xsize);
ASSERT_EQ(static_cast<size_t>(orig_ysize), ppf_in.frames[0].color.ysize);

std::vector<uint8_t> compressed;

// Sanity: original ppf encodes successfully.
EXPECT_TRUE(EncodeWithLibjpeg(ppf_in, 90, &compressed));

// Mismatch ysize (info > frame): pre-fix this read past the pixel buffer
// in the encoder copy loops.
ppf_in.info.ysize = orig_ysize + 1;
EXPECT_FALSE(EncodeWithLibjpeg(ppf_in, 90, &compressed));

// Mismatch xsize (info > frame).
ppf_in.info.ysize = orig_ysize;
ppf_in.info.xsize = orig_xsize + 1;
EXPECT_FALSE(EncodeWithLibjpeg(ppf_in, 90, &compressed));

// Mismatch in the other direction (info < frame).
ppf_in.info.xsize = orig_xsize - 1;
ppf_in.info.ysize = orig_ysize;
EXPECT_FALSE(EncodeWithLibjpeg(ppf_in, 90, &compressed));
}

struct TestConfig {
int num_colors;
int passes;
Expand Down