123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include "video_preprocess_common.hpp"
- bool ConvertColorSpace(size_t width, size_t height, VideoPixelFmt src_fmt, VideoPixelFmt dst_fmt,
- uint8_t* src_img_data, cv::Mat* dst_img) {
- cv::Mat src_img;
- bool cvt_ret = true;
- switch (src_fmt) {
- case VideoPixelFmt::NV12: {
- src_img = cv::Mat(height * 3 / 2, width, CV_8UC1, src_img_data);
- switch (dst_fmt) {
- case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2RGB_NV12); break;
- case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGR_NV12); break;
- case VideoPixelFmt::RGBA:
- case VideoPixelFmt::BGRA:
- case VideoPixelFmt::ARGB:
- case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGRA_NV12); break;
- default: cvt_ret = false; break;
- }
- break;
- }
- case VideoPixelFmt::NV21: {
- src_img = cv::Mat(height * 3 / 2, width, CV_8UC1, src_img_data);
- switch (dst_fmt) {
- case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2RGB_NV21); break;
- case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGR_NV21); break;
- case VideoPixelFmt::RGBA:
- case VideoPixelFmt::BGRA:
- case VideoPixelFmt::ARGB:
- case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_YUV2BGRA_NV21); break;
- default: cvt_ret = false; break;
- }
- break;
- }
- case VideoPixelFmt::RGB24: {
- src_img = cv::Mat(height, width, CV_8UC3, src_img_data);
- switch (dst_fmt) {
- case VideoPixelFmt::RGB24: *dst_img = src_img; break;
- case VideoPixelFmt::BGR24: cv::cvtColor(src_img, *dst_img, cv::COLOR_RGB2BGR); break;
- case VideoPixelFmt::RGBA:
- case VideoPixelFmt::BGRA:
- case VideoPixelFmt::ARGB:
- case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_RGB2BGRA); break;
- default: cvt_ret = false; break;
- }
- break;
- }
- case VideoPixelFmt::BGR24: {
- src_img = cv::Mat(height, width, CV_8UC3, src_img_data);
- switch (dst_fmt) {
- case VideoPixelFmt::RGB24: cv::cvtColor(src_img, *dst_img, cv::COLOR_BGR2RGB); break;
- case VideoPixelFmt::BGR24: *dst_img = src_img; break;
- case VideoPixelFmt::RGBA:
- case VideoPixelFmt::BGRA:
- case VideoPixelFmt::ARGB:
- case VideoPixelFmt::ABGR: cv::cvtColor(src_img, *dst_img, cv::COLOR_BGR2BGRA); break;
- default: cvt_ret = false; break;
- }
- break;
- }
- default: cvt_ret = false; break;
- }
- if (!cvt_ret) {
- return false;
- }
- switch (dst_fmt) {
- case VideoPixelFmt::RGBA: {
- cv::Mat rgba(dst_img->size(), dst_img->type());
-
- int from_to[] = {0, 2, 1, 1, 2, 0, 3, 3};
- cv::mixChannels(dst_img, 1, &rgba, 1, from_to, 4);
- *dst_img = rgba;
- break;
- }
- case VideoPixelFmt::ARGB: {
- cv::Mat argb(dst_img->size(), dst_img->type());
-
- int from_to[] = {0, 3, 1, 2, 2, 1, 3, 0};
- cv::mixChannels(dst_img, 1, &argb, 1, from_to, 4);
- *dst_img = argb;
- break;
- }
- case VideoPixelFmt::ABGR: {
- cv::Mat abgr(dst_img->size(), dst_img->type());
-
- int from_to[] = {0, 1, 1, 2, 2, 3, 3, 0};
- cv::mixChannels(dst_img, 1, &abgr, 1, from_to, 4);
- *dst_img = abgr;
- break;
- }
- default: break;
- }
- return true;
- }
|