diff --git a/lib/extras/enc/jpg.cc b/lib/extras/enc/jpg.cc index 43fb24b0..35b01b61 100644 --- a/lib/extras/enc/jpg.cc +++ b/lib/extras/enc/jpg.cc @@ -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, diff --git a/lib/extras/jpegli_test.cc b/lib/extras/jpegli_test.cc index 65541c58..80c01d0d 100644 --- a/lib/extras/jpegli_test.cc +++ b/lib/extras/jpegli_test.cc @@ -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(orig_xsize), ppf_in.frames[0].color.xsize); + ASSERT_EQ(static_cast(orig_ysize), ppf_in.frames[0].color.ysize); + + std::vector 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;