imgcodecs: fix JPEG 2000 encoding of images smaller than 32 pixels 🤖🤖🤖 - #29926
`imwrite()` and `imencode()` cannot write a JPEG 2000 file for any image narrower or shorter than 32 pixels. The call fails with
```
OpenJPEG2000: Number of resolutions is too high in comparison to the size of tiles
imencode(): can't encode data: OpenCV(4.15.0-dev) .../grfmt_jpeg2000_openjpeg.cpp:802:
error: (-213:The function/feature is not implemented) OpenJPEG2000: Can not start
compression in function 'cv::Jpeg2KOpjEncoder::write'
```
and returns `false`. Every other encoder in the module writes an 8x8 image without complaint, so this is a gap specific to the OpenJPEG path.
`setupEncoderParameters()` never touched `opj_cparameters::numresolution`, leaving it at the OpenJPEG default of 6. `opj_j2k_encoding_validation()` (`3rdparty/openjpeg/openjp2/j2k.c:8840-8851`) requires each tile to be at least `1 << (numresolutions - 1)` in both directions, and because OpenCV does not enable tiling the single tile is the whole image. The threshold is therefore exactly `2^5 = 32` px, which matches the observed boundary: a 32x32 image encodes, a 31x31 image does not.
This patch lowers `numresolution` to what the image size allows. It only ever lowers it, so an image that is at least 32x32 keeps the default of 6 and its encoded bytes are unchanged. No public API is changed and no new test data is needed.
### Validation
Built `opencv_test_imgcodecs` on Windows 10.0.26200 x64 with MSVC 19.51.36256 (Visual Studio 18), Release, `-DBUILD_LIST=core,imgproc,imgcodecs,ts -DCPU_DISPATCH=` (baseline SSE3), bundled OpenJPEG 2.5.3, on `4.x` at `0aa49a8e`.
The test is in the first commit and the fix in the second. Against unmodified `0aa49a8e` the new test fails on the very first size:
```
[ RUN ] Imgcodecs_Jpeg2000.encode_small_image
[ERROR:0@0.034] global grfmt_jpeg2000_openjpeg.cpp:299 cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Number of resolutions is too high in comparison to the size of tiles
modules\imgcodecs\test\test_read_write.cpp(341): error: Value of: imencode(".jp2", src, buf)
Actual: false
Expected: true
imencode() failed for .jp2
Google Test trace:
modules\imgcodecs\test\test_read_write.cpp(336): 1x1
[ FAILED ] Imgcodecs_Jpeg2000.encode_small_image (3 ms)
```
With the fix applied, all six sizes (1x1, 8x8, 31x31, 64x8, 8x64, 32x32) encode, decode back to the same size and type, and round-trip bit-exactly:
```
[ RUN ] Imgcodecs_Jpeg2000.encode_small_image
[ OK ] Imgcodecs_Jpeg2000.encode_small_image (23 ms)
[ PASSED ] 1 test.
```
The 64x8 and 8x64 cases cover the tile-height and tile-width conditions separately; 32x32 pins the boundary where the default is still used.
No other result in the module changed. `opencv_test_imgcodecs` with no filter:
| | tests run | passed | failed |
|---|---:|---:|---:|
| unmodified `0aa49a8e` | 757 | 231 | 526 |
| with this patch | 758 | 232 | 526 |
The patch adds exactly one test, it passes, and the failed count is identical. Those 526 failures are pre-existing on this machine and unrelated to this change: `opencv_extra` is not checked out here, so every test that calls `findDataFile()` fails. I did not run the suite against `opencv_extra` data, and I have not tested a JasPer or a non-x86 build.
As a direct check that images at or above the threshold are untouched, I encoded the same fixed inputs before and after the patch and compared the output sizes. They are byte-for-byte identical above the threshold, and only the previously-failing cases change:
```
before after
8x8 CV_8UC3 (failed) 250 bytes
8x8 CV_16UC3 (failed) 249 bytes
32x32 CV_8UC3 755 bytes 755 bytes
32x32 CV_16UC3 1542 bytes 1542 bytes
64x64 CV_8UC3 2952 bytes 2952 bytes
129x129 CV_8UC3 11981 bytes 11981 bytes
129x129 CV_16UC3 24922 bytes 24922 bytes
```
`git diff --check` is clean. `5.x` carries the identical code and the identical defect; this is proposed to `4.x` per the branch policy, to be ported by the core team.
I did not find an existing issue for this, so the PR references none.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work *(no existing report found)*
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable *(accuracy test added in the first commit; self-contained, no new test data)*
- [ ] The feature is well documented and sample code can be built with the project CMake *(not applicable: bug fix, no API or documented behaviour change)*
SunRasterDecoder::readData() only called CvtPaletteToGray() when the
file contained a color palette (m_maptype == RMT_EQUAL_RGB). But
SunRasterEncoder always writes RMT_NONE (no palette), so gray_palette[]
stayed zero-initialized and FillGrayRow8() mapped every pixel to 0.
Fix: call CvtPaletteToGray() whenever reading as grayscale, regardless
of palette type. readHeader() already fills m_palette correctly via
FillGrayPalette() for the RMT_NONE case.
Add regression test Imgcodecs_SunRaster.imread_grayscale_roundtrip
that writes a full 0-255 ramp, reads it back with IMREAD_GRAYSCALE,
and checks NORM_INF == 0. Without the fix, NORM_INF == 255 (all black).
Remove the "broken (black result)" skip for ras in read_write_GRAYSCALE.
imgcodecs: implement imencodemulti() #26211Close#26207
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
imgcodecs: Add rgb flag for imread and imdecode #25809
Try to `imread` images by RGB to save R-B swapping costs.
## How to use it?
```
img_rgb = cv2.imread("PATH", IMREAD_COLOR_RGB) # OpenCV decode the image by RGB format.
```
## TODO
- [x] Fix the broken code
- [x] Add imread rgb test
- [x] Speed test of rgb mode.
## Performance test
| file name | IMREAD_COLOR | IMREAD_COLOR_RGB |
| --------- | ------ | --------- |
| jpg01 | 284 ms | 277 ms |
| jpg02 | 376 ms | 366 ms |
| png01 | 62 ms | 60 ms |
| Png02 | 97 ms | 94 ms |
Test with [image_test.zip](https://github.com/user-attachments/files/15982949/image_test.zip)
```.cpp
string img_path = "/Users/mzh/work/data/image_test/png02.png";
int loop = 20;
TickMeter t;
double t0 = 10000;
for (int i = 0; i < loop; i++)
{
t.reset();
t.start();
img_bgr = imread(img_path, IMREAD_COLOR);
t.stop();
if (t.getTimeMilli() < t0) t0 = t.getTimeMilli();
}
std::cout<<"bgr time = "<<t0<<std::endl;
t0 = 10000;
for (int i = 0; i < loop; i++)
{
t.reset();
t.start();
img_rgb = imread(img_path, IMREAD_COLOR_RGB);
t.stop();
if (t.getTimeMilli() < t0) t0 = t.getTimeMilli();
}
std::cout<<"rgb time = "<<t0<<std::endl;
```
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Jpeg2000 OpenJPEG port
* OpenJPEG based JPEG2000 decoder implementation
Currently, the following input color spaces and depth conversions are
supported:
- 8 bit -> 8 bit
- 16 bit -> 16 bit (IMREAD_UNCHANGED, IMREAD_ANYDEPTH)
- RGB(a) -> BGR
- RGBA -> BGRA (IMREAD_UNCHANGED)
- Y(a) -> Y(a) (IMREAD_ANYCOLOR, IMREAD_GRAY, IMREAD_UNCHANGED))
- YCC -> Y (IMREAD_GRAY)
* Check for OpenJPEG availability
This enables OpenJPEG based JPEG2000 imread support by default, which
can be disabled by -DWITH_OPENJPEG=OFF. In case OpenJPEG is enabled
and found, any checks for Jasper are skipped.
* Implement precision downscaling for precision > 8 without IMREAD_UNCHANGED
With IMREAD_UNCHANGED, values are kept from the input image, without it
components are downscaled to CV_8U range.
* Enable Jpeg2K tests when OpenJPEG is available
* Add support for some more color conversions
Support IMREAD_GRAY when input color space is RGB or unspecified.
Support YUV input color space for BGR output.
* fix: problems with unmanaged memory
* fix: CMake warning - HAVE_OPENJPEG is undefined
Removed trailing whitespaces
* fix: CMake find_package OpenJPEG add minimal version
* Basic JPEG2K encoder
Images with depth CV_8U and CV_16U are supported, with 1 to 4 channels.
* feature: Improved code for OpenJPEG2000 encoder/decoder
- Removed code duplication
- Added error handlers
- Extracted functions
* feature: Update conversion openjpeg array from/to Mat
* feature: Extend ChannelsIterator to fulfill RandomAccessIterator named requirements
- Removed channels split in copyFromMatImpl. With ChannelsIterator no allocations are performed.
- Split whole loop into 2 parts in copyToMat -> where std::copy and std::transforms are called.
* fix: Applied review comments.
- Changed `nullptr` in CV_LOG* functions to `NULL`
- Added `falls through` comment in decoder color space `switch`
- Added warning about unsupported parameters for the encoder
* feature: Added decode from in-memory buffers.
Co-authored-by: Vadim Levin <vadim.levin@xperience.ai>
* fix cv::imwrite to allow cv::UMat and vectors of it to be written
* cv::imwrite failure handling improvements
* Unit test for regression testing imwrite support for and fix whitespace
- removed tr1 usage (dropped in C++17)
- moved includes of vector/map/iostream/limits into ts.hpp
- require opencv_test + anonymous namespace (added compile check)
- fixed norm() usage (must be from cvtest::norm for checks) and other conflict functions
- added missing license headers
general:
- all iterative tests have been replaced with parameterized tests
- old-style try..catch tests have been modified to use EXPECT_/ASSERT_ gtest macros
- added temporary files cleanup
- modified MatComparator error message formatting
imgcodecs:
- test_grfmt.cpp split to test_jpg.cpp, test_png.cpp, test_tiff.cpp, etc.
videoio:
- added public HAVE_VIDEO_INPUT, HAVE_VIDEO_OUTPUT definitions to cvconfig.h
- built-in MotionJPEG codec could not be tested on some platforms (read_write test was disabled if ffmpeg is off, encoding/decoding was handled by ffmpeg otherwise).
- image-related tests moved to imgcodecs (Videoio_Image)
- several property get/set tests have been combined into one
- added MotionJPEG test video to opencv_extra