opencv/modules/imgcodecs/test/test_precomp.hpp
zihaomu 934e6899f8
Merge pull request #25809 from zihaomu:imread_rgb_flag
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
2024-07-03 10:58:25 +03:00

82 lines
2.4 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc_c.h"
namespace cv {
static inline
void PrintTo(const ImreadModes& val, std::ostream* os)
{
int v = val;
if (v == IMREAD_UNCHANGED && (v & IMREAD_IGNORE_ORIENTATION) != 0)
{
CV_Assert(IMREAD_UNCHANGED == -1);
*os << "IMREAD_UNCHANGED";
return;
}
if ((v & IMREAD_COLOR) != 0)
{
CV_Assert(IMREAD_COLOR == 1);
v &= ~IMREAD_COLOR;
*os << "IMREAD_COLOR" << (v == 0 ? "" : " | ");
}
else
{
CV_Assert(IMREAD_GRAYSCALE == 0);
*os << "IMREAD_GRAYSCALE" << (v == 0 ? "" : " | ");
}
if ((v & IMREAD_ANYDEPTH) != 0)
{
v &= ~IMREAD_ANYDEPTH;
*os << "IMREAD_ANYDEPTH" << (v == 0 ? "" : " | ");
}
if ((v & IMREAD_ANYCOLOR) != 0)
{
v &= ~IMREAD_ANYCOLOR;
*os << "IMREAD_ANYCOLOR" << (v == 0 ? "" : " | ");
}
if ((v & IMREAD_LOAD_GDAL) != 0)
{
v &= ~IMREAD_LOAD_GDAL;
*os << "IMREAD_LOAD_GDAL" << (v == 0 ? "" : " | ");
}
if ((v & IMREAD_IGNORE_ORIENTATION) != 0)
{
v &= ~IMREAD_IGNORE_ORIENTATION;
*os << "IMREAD_IGNORE_ORIENTATION" << (v == 0 ? "" : " | ");
}
if ((v & IMREAD_COLOR_RGB) != 0)
{
v &= ~IMREAD_COLOR_RGB;
*os << "IMREAD_COLOR_RGB" << (v == 0 ? "" : " | ");
}
switch (v)
{
case IMREAD_UNCHANGED: return;
case IMREAD_GRAYSCALE: return;
case IMREAD_COLOR: return;
case IMREAD_ANYDEPTH: return;
case IMREAD_ANYCOLOR: return;
case IMREAD_LOAD_GDAL: return;
case IMREAD_REDUCED_GRAYSCALE_2: // fallthru
case IMREAD_REDUCED_COLOR_2: *os << "REDUCED_2"; return;
case IMREAD_REDUCED_GRAYSCALE_4: // fallthru
case IMREAD_REDUCED_COLOR_4: *os << "REDUCED_4"; return;
case IMREAD_REDUCED_GRAYSCALE_8: // fallthru
case IMREAD_REDUCED_COLOR_8: *os << "REDUCED_8"; return;
case IMREAD_IGNORE_ORIENTATION: return;
case IMREAD_COLOR_RGB: return;
} // don't use "default:" to emit compiler warnings
*os << "IMREAD_UNKNOWN(" << (int)v << ")";
}
} // namespace
#endif