Add 10-12-14bit (integer) TIFF decoding support#21701
Conversation
An (slow) unpacking step is inserted when the native bpp is not equal to the dst_bpp Currently, I do not know if there can be several packing flavours in TIFF data.
modules/imgcodecs/test/test_tiff.cpp
Outdated
| cv::Mat tmp; | ||
| double diff = 0; | ||
|
|
||
| if (!img16UC1.empty()) |
There was a problem hiding this comment.
I was unsure about what to do, because the real test with actual images will fail on the buildbot as long as the sample files #962 are not commited
modules/imgcodecs/test/test_tiff.cpp
Outdated
| if (!img16UC1.empty()) | ||
| img8UC1.convertTo(tmp, img16UC1.type(), (1<<(16-8))); | ||
| diff = tmp.empty() ? 0. : cv::norm(tmp.reshape(1), img16UC1.reshape(1), cv::NORM_INF); | ||
| ASSERT_TRUE(img16UC1.empty() || (diff <= maxDiff)); |
There was a problem hiding this comment.
ASSERT_TRUE with complex checks provides almost useless messages about failure.
Use appropriate EXPECT_LE() macro instead.
modules/imgcodecs/src/grfmt_tiff.cpp
Outdated
| AutoBuffer<uchar> _buffer(buffer_size); | ||
| uchar* buffer = _buffer.data(); | ||
| ushort* buffer16 = (ushort*)buffer; | ||
| const size_t src_buffer_bytes_per_row = ((ncn * tile_width0 * bpp + bitsPerByte - 1)/bitsPerByte); |
| } | ||
| } | ||
|
|
||
| static void _unpack10To16(const uchar* src, const uchar* srcEnd, ushort* dst, ushort* dstEnd, size_t expectedDstElements) |
There was a problem hiding this comment.
expectedDstElements
We should have both limits here (dst and src) to ensure avoiding out of buffer access.
There was a problem hiding this comment.
This is already the case.
expectedDstElements is the width of the currently decoded tile, without any assumptions about src/dst bounds limits. Then, the fullPacketsCount computation ensures that no OOB will occur for the "fullPackets" loop, while the last loop with "remainingElements" performs explicit bounds checking
| for(size_t i = 0 ; i<fullPacketsCount ; ++i) | ||
| { | ||
| for(size_t j = 0 ; j<srcElementsPerPacket ; ++j) | ||
| buf.u8[srcElementsPerPacket-1-j] = *src++; |
There was a problem hiding this comment.
we need access limitation with buffer boundary check.
There was a problem hiding this comment.
the min() in fullPacketsCountcomputation ensures that no OOB will occur
modules/imgcodecs/test/test_tiff.cpp
Outdated
| // What about 32, 64 bit? | ||
| } | ||
|
|
||
| TEST(Imgcodecs_Tiff, decode_12) |
There was a problem hiding this comment.
decode_12
12
Below test cases for 10/14 bits too,
modules/imgcodecs/test/test_tiff.cpp
Outdated
| cv::Mat img10UC1 = imread(root + "readwrite/pattern_10uc1.tif", cv::IMREAD_UNCHANGED); | ||
| cv::Mat img10UC3 = imread(root + "readwrite/pattern_10uc3.tif", cv::IMREAD_UNCHANGED); | ||
| cv::Mat img10UC4 = imread(root + "readwrite/pattern_10uc4.tif", cv::IMREAD_UNCHANGED); | ||
| cv::Mat img12UC1 = imread(root + "readwrite/pattern_12uc1.tif", cv::IMREAD_UNCHANGED); | ||
| cv::Mat img12UC3 = imread(root + "readwrite/pattern_12uc3.tif", cv::IMREAD_UNCHANGED); | ||
| cv::Mat img12UC4 = imread(root + "readwrite/pattern_12uc4.tif", cv::IMREAD_UNCHANGED); |
There was a problem hiding this comment.
Please interleave reading and checks (group code of dedicated cases).
Add 10-12-14bit (integer) TIFF decoding support * Add 12bit (integer) TIFF decoding support An (slow) unpacking step is inserted when the native bpp is not equal to the dst_bpp Currently, I do not know if there can be several packing flavours in TIFF data. * added tests * move sample files to opencv_extra * added 10b and 14b unpacking * fix compilation for non MSVC compilers by using more standard typedefs * yet another typdef usage change to fix buildbot Mac compilation * fixed unpacking of partial packets * fixed warnings returned by buildbot * modifications as suggested by reviewer
Merge with extra: opencv/opencv_extra#962
Proposal for #21700
A (slow) unpacking step is inserted when the native bpp is not equal to the dst_bpp
Currently, I do not know if there can be several packing flavours in TIFF data.
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request