…lines 1–153 unchanged (153 lines)
154 buff[8+4+2] = 'B';
155 buff[8+4+3] = 'I';
156 EXPECT_NO_THROW(img = imdecode(buff, IMREAD_COLOR));
157 EXPECT_TRUE(img.empty());
158}
159
160// The program must not crash even when decoding a corrupted APNG image.
161// See https://github.com/opencv/opencv/issues/27744
162#if defined(HAVE_PNG) // APNG is supported only with using libpng
163TEST(Imgcodecs_Png, decode_regression27744)
164{
165 // Create APNG stream
166 Animation anim;
167 for(size_t i = 0 ; i < 3 ; i++) {
168 Mat frame(120, 120, CV_8UC3, Scalar(0,0,0));
169 putText(frame, cv::format("%d", static_cast<int>(i)), Point(5, 28), FONT_HERSHEY_SIMPLEX, .5, Scalar(100, 255, 0, 255), 2);
170 anim.frames.push_back(frame);
171 anim.durations.push_back(30);
172 }
173 bool ret = false;
174 vector<uchar> buff;
175 EXPECT_NO_THROW(ret = imencodeanimation(".png", anim, buff));
176 ASSERT_TRUE(ret) << "imencodeanimation() returns false";
177
178 // Find IDAT chunk
179 const vector<uchar> IDAT = {'I', 'D', 'A', 'T' };
180 std::vector<uchar>::iterator it = std::search(buff.begin(), buff.end(), IDAT.begin(), IDAT.end());
181 ASSERT_FALSE(it == buff.end()) << "IDAT chunk not found";
182
183 // Determine the range to test
184 // APNG stream contains as { len0, len1, len2, len3, 'I', 'D', 'A' 'T', ... }
185 size_t idx = std::distance(buff.begin(), it); // 'I' position
186 size_t len = (buff[idx-4] << 24) + (buff[idx-3] << 16) +
187 (buff[idx-2] << 8) + (buff[idx-1]); // IDAT chunk length
188 idx = idx + 4; // Move to IDAT body
189
190 // Test
191 for(size_t i = 0; i < len; i++, idx++) {
192 vector<uint8_t> work = buff;
193 work[idx] = static_cast<uint8_t>((static_cast<uint32_t>(work[idx]) + 1) & 0xff);
194
195 Mat dst;
196 EXPECT_NO_THROW(dst = imdecode(work, cv::IMREAD_COLOR));
197 if(dst.empty()) {
198 // libpng detects some error, but the program is not crashed. Test is passed.
199 break;
200 }
201 }
202}
203#endif
204
205typedef testing::TestWithParam<string> Imgcodecs_Png_PngSuite;
206
207// Parameterized test for decoding PNG files from the PNGSuite test set
208TEST_P(Imgcodecs_Png_PngSuite, decode)
209{
…lines 210–640 unchanged (431 lines)