1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "pixel_convert_adapter.h"
17 #include "pixel_yuv_utils.h"
18 #include <map>
19 
20 #include "image_log.h"
21 #include "include/core/SkBitmap.h"
22 #include "include/core/SkCanvas.h"
23 #include "include/core/SkColor.h"
24 #include "include/core/SkColorSpace.h"
25 #include "include/core/SkImageInfo.h"
26 #include "include/core/SkPaint.h"
27 #include "include/core/SkPixmap.h"
28 #include "media_errors.h"
29 #ifdef _WIN32
30 #include <iomanip>
31 #endif
32 
33 #undef LOG_DOMAIN
34 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
35 
36 #undef LOG_TAG
37 #define LOG_TAG "PixelConvertAdapter"
38 
39 namespace OHOS {
40 namespace Media {
41 using namespace std;
42 
43 static const uint8_t NUM_0 = 0;
44 static const uint8_t NUM_1 = 1;
45 static const uint8_t NUM_2 = 2;
46 static const uint8_t NUM_3 = 3;
47 static const uint8_t NUM_4 = 4;
48 
49 const std::map<PixelFormat, AVPixelFormat> PixelConvertAdapter::FFMPEG_PIXEL_FORMAT_MAP = {
50     {PixelFormat::UNKNOWN, AV_PIX_FMT_NONE},
51     {PixelFormat::NV12, AV_PIX_FMT_NV12},
52     {PixelFormat::NV21, AV_PIX_FMT_NV21},
53     {PixelFormat::RGB_565, AV_PIX_FMT_RGB565},
54     {PixelFormat::RGBA_8888, AV_PIX_FMT_RGBA},
55     {PixelFormat::BGRA_8888, AV_PIX_FMT_BGRA},
56     {PixelFormat::ARGB_8888, AV_PIX_FMT_ARGB},
57     {PixelFormat::RGBA_F16, AV_PIX_FMT_RGBA64},
58     {PixelFormat::RGB_888, AV_PIX_FMT_RGB24},
59     {PixelFormat::YCRCB_P010, AV_PIX_FMT_P010LE},
60     {PixelFormat::YCBCR_P010, AV_PIX_FMT_P010LE},
61 };
62 
63 static const map<PixelFormat, SkColorType> PIXEL_FORMAT_MAP = {
64     { PixelFormat::UNKNOWN, SkColorType::kUnknown_SkColorType},
65     { PixelFormat::ARGB_8888, SkColorType::kRGBA_8888_SkColorType},
66     { PixelFormat::ALPHA_8, SkColorType::kAlpha_8_SkColorType},
67     { PixelFormat::RGB_565, SkColorType::kRGB_565_SkColorType},
68     { PixelFormat::RGBA_F16, SkColorType::kRGBA_F16_SkColorType},
69     { PixelFormat::RGBA_8888, SkColorType::kRGBA_8888_SkColorType},
70     { PixelFormat::BGRA_8888, SkColorType::kBGRA_8888_SkColorType},
71     { PixelFormat::RGB_888, SkColorType::kRGB_888x_SkColorType},
72     { PixelFormat::RGBA_1010102, SkColorType::kRGBA_1010102_SkColorType},
73     { PixelFormat::RGBA_U16, SkColorType::kR16G16B16A16_unorm_SkColorType},
74 };
75 
PixelFormatConvert(const PixelFormat & pixelFormat)76 static SkColorType PixelFormatConvert(const PixelFormat &pixelFormat)
77 {
78     auto formatSearch = PIXEL_FORMAT_MAP.find(pixelFormat);
79     return (formatSearch != PIXEL_FORMAT_MAP.end()) ? formatSearch->second : SkColorType::kUnknown_SkColorType;
80 }
81 
ARGBToRGBA(uint8_t * srcPixels,uint8_t * dstPixels,uint32_t byteCount)82 static void ARGBToRGBA(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount)
83 {
84     if (byteCount % NUM_4 != NUM_0) {
85         IMAGE_LOGE("Pixel count must multiple of 4.");
86         return;
87     }
88     uint8_t *src = srcPixels;
89     uint8_t *dst = dstPixels;
90     for (uint32_t i = NUM_0 ; i < byteCount; i += NUM_4) {
91         // 0-R 1-G 2-B 3-A
92         dst[NUM_0] = src[NUM_1];
93         dst[NUM_1] = src[NUM_2];
94         dst[NUM_2] = src[NUM_3];
95         dst[NUM_3] = src[NUM_0];
96         src += NUM_4;
97         dst += NUM_4;
98     }
99 }
100 
RGBAToARGB(uint8_t * srcPixels,uint8_t * dstPixels,uint32_t byteCount)101 static void RGBAToARGB(uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount)
102 {
103     if (byteCount % NUM_4 != NUM_0) {
104         IMAGE_LOGE("Pixel count must multiple of 4.");
105         return;
106     }
107     uint8_t *src = srcPixels;
108     uint8_t *dst = dstPixels;
109     for (uint32_t i = NUM_0 ; i < byteCount; i += NUM_4) {
110         // 0-A 1-R 2-G 3-B
111         dst[NUM_0] = src[NUM_3];
112         dst[NUM_1] = src[NUM_0];
113         dst[NUM_2] = src[NUM_1];
114         dst[NUM_3] = src[NUM_2];
115         src += NUM_4;
116         dst += NUM_4;
117     }
118 }
119 
RGBxToRGB(const uint8_t * srcPixels,uint8_t * dstPixels,uint32_t byteCount)120 static void RGBxToRGB(const uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount)
121 {
122     if (byteCount % NUM_4 != NUM_0) {
123         IMAGE_LOGE("Pixel count must multiple of 4.");
124         return;
125     }
126     const uint8_t *src = srcPixels;
127     uint8_t *dst = dstPixels;
128     for (uint32_t i = NUM_0 ; i < byteCount; i += NUM_4) {
129         // 0-R 1-G 2-B
130         dst[NUM_0] = src[NUM_0];
131         dst[NUM_1] = src[NUM_1];
132         dst[NUM_2] = src[NUM_2];
133         src += NUM_4;
134         dst += NUM_3;
135     }
136 }
137 
RGBToRGBx(const uint8_t * srcPixels,uint8_t * dstPixels,uint32_t byteCount)138 static void RGBToRGBx(const uint8_t* srcPixels, uint8_t* dstPixels, uint32_t byteCount)
139 {
140     if (byteCount % NUM_3 != NUM_0) {
141         IMAGE_LOGE("Pixel count must multiple of 3.");
142         return;
143     }
144     const uint8_t *src = srcPixels;
145     uint8_t *dst = dstPixels;
146     for (uint32_t i = NUM_0 ; i < byteCount; i += NUM_3) {
147         // 0-R 1-G 2-B
148         dst[NUM_0] = src[NUM_0];
149         dst[NUM_1] = src[NUM_1];
150         dst[NUM_2] = src[NUM_2];
151         dst[NUM_3] = 0;
152         src += NUM_3;
153         dst += NUM_4;
154     }
155 }
156 
GetRGBxRowBytes(const ImageInfo & imgInfo)157 static int32_t GetRGBxRowBytes(const ImageInfo &imgInfo)
158 {
159     return imgInfo.size.width * NUM_4;
160 }
161 
GetRGBxSize(const ImageInfo & imgInfo)162 static int32_t GetRGBxSize(const ImageInfo &imgInfo)
163 {
164     return imgInfo.size.height * GetRGBxRowBytes(imgInfo);
165 }
166 
WritePixelsConvert(const void * srcPixels,uint32_t srcRowBytes,const ImageInfo & srcInfo,void * dstPixels,const Position & dstPos,uint32_t dstRowBytes,const ImageInfo & dstInfo)167 bool PixelConvertAdapter::WritePixelsConvert(const void *srcPixels, uint32_t srcRowBytes, const ImageInfo &srcInfo,
168                                              void *dstPixels, const Position &dstPos, uint32_t dstRowBytes,
169                                              const ImageInfo &dstInfo)
170 {
171     // basic valid check, other parameters valid check in writePixels method
172     if (srcPixels == nullptr || dstPixels == nullptr) {
173         IMAGE_LOGE("src or dst pixels invalid.");
174         return false;
175     }
176 
177     SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
178     SkAlphaType dstAlphaType = static_cast<SkAlphaType>(dstInfo.alphaType);
179     SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
180     SkColorType dstColorType = PixelFormatConvert(dstInfo.pixelFormat);
181     SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
182     SkImageInfo dstImageInfo = SkImageInfo::Make(dstInfo.size.width, dstInfo.size.height, dstColorType, dstAlphaType);
183 
184     int32_t dstRGBxSize = (dstInfo.pixelFormat == PixelFormat::RGB_888) ?
185         GetRGBxSize(dstInfo) : static_cast<int32_t>(NUM_1);
186     auto dstRGBxPixels = std::make_unique<uint8_t[]>(dstRGBxSize);
187     auto keepDstPixels = dstPixels;
188     dstPixels = (dstInfo.pixelFormat == PixelFormat::RGB_888) ? &dstRGBxPixels[0] : dstPixels;
189     dstRowBytes = (dstInfo.pixelFormat == PixelFormat::RGB_888) ?
190         static_cast<uint32_t>(GetRGBxRowBytes(dstInfo)) : dstRowBytes;
191 
192     int32_t srcRGBxSize = (srcInfo.pixelFormat == PixelFormat::RGB_888) ? GetRGBxSize(srcInfo) : NUM_1;
193     auto srcRGBxPixels = std::make_unique<uint8_t[]>(srcRGBxSize);
194     if (srcInfo.pixelFormat == PixelFormat::RGB_888) {
195         RGBToRGBx(static_cast<const uint8_t*>(srcPixels), &srcRGBxPixels[0], srcRowBytes * srcInfo.size.height);
196         srcPixels = &srcRGBxPixels[0];
197         srcRowBytes = static_cast<uint32_t>(GetRGBxRowBytes(srcInfo));
198     }
199     SkPixmap srcPixmap(srcImageInfo, srcPixels, srcRowBytes);
200     if (srcInfo.pixelFormat == PixelFormat::ARGB_8888) {
201         uint8_t* src = static_cast<uint8_t*>(srcPixmap.writable_addr());
202         ARGBToRGBA(src, src, srcRowBytes * srcInfo.size.height);
203     }
204 
205     SkBitmap dstBitmap;
206     if (!dstBitmap.installPixels(dstImageInfo, dstPixels, dstRowBytes)) {
207         IMAGE_LOGE("WritePixelsConvert dst bitmap install pixels failed.");
208         return false;
209     }
210     if (!dstBitmap.writePixels(srcPixmap, dstPos.x, dstPos.y)) {
211         IMAGE_LOGE("WritePixelsConvert dst bitmap write pixels by source failed.");
212         return false;
213     }
214 
215     if (dstInfo.pixelFormat == PixelFormat::ARGB_8888) {
216         uint32_t dstSize = dstRowBytes * dstInfo.size.height;
217         RGBAToARGB(static_cast<uint8_t*>(dstPixels), static_cast<uint8_t*>(dstPixels), dstSize);
218     } else if (dstInfo.pixelFormat == PixelFormat::RGB_888) {
219         RGBxToRGB(&dstRGBxPixels[0], static_cast<uint8_t*>(keepDstPixels), dstRGBxSize);
220     }
221 
222     return true;
223 }
224 
ReadPixelsConvert(const void * srcPixels,const Position & srcPos,uint32_t srcRowBytes,const ImageInfo & srcInfo,void * dstPixels,uint32_t dstRowBytes,const ImageInfo & dstInfo)225 bool PixelConvertAdapter::ReadPixelsConvert(const void *srcPixels, const Position &srcPos, uint32_t srcRowBytes,
226                                             const ImageInfo &srcInfo, void *dstPixels, uint32_t dstRowBytes,
227                                             const ImageInfo &dstInfo)
228 {
229     // basic valid check, other parameters valid check in readPixels method
230     if (srcPixels == nullptr || dstPixels == nullptr) {
231         IMAGE_LOGE("src or dst pixels invalid.");
232         return false;
233     }
234     SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
235     SkAlphaType dstAlphaType = static_cast<SkAlphaType>(dstInfo.alphaType);
236     SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
237     SkColorType dstColorType = PixelFormatConvert(dstInfo.pixelFormat);
238     SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
239     SkImageInfo dstImageInfo = SkImageInfo::Make(dstInfo.size.width, dstInfo.size.height, dstColorType, dstAlphaType);
240 
241     SkBitmap srcBitmap;
242     if (!srcBitmap.installPixels(srcImageInfo, const_cast<void *>(srcPixels), srcRowBytes)) {
243         IMAGE_LOGE("ReadPixelsConvert src bitmap install pixels failed.");
244         return false;
245     }
246     if (!srcBitmap.readPixels(dstImageInfo, dstPixels, dstRowBytes, srcPos.x, srcPos.y)) {
247         IMAGE_LOGE("ReadPixelsConvert read dst pixels from source failed.");
248         return false;
249     }
250     return true;
251 }
252 
EraseBitmap(const void * srcPixels,uint32_t srcRowBytes,const ImageInfo & srcInfo,uint32_t color)253 bool PixelConvertAdapter::EraseBitmap(const void *srcPixels, uint32_t srcRowBytes, const ImageInfo &srcInfo,
254                                       uint32_t color)
255 {
256     if (srcPixels == nullptr) {
257         IMAGE_LOGE("srcPixels is null.");
258         return false;
259     }
260     SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
261     SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
262     SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
263     SkBitmap srcBitmap;
264     if (!srcBitmap.installPixels(srcImageInfo, const_cast<void *>(srcPixels), srcRowBytes)) {
265         IMAGE_LOGE("ReadPixelsConvert src bitmap install pixels failed.");
266         return false;
267     }
268     const SkColor4f skColor = SkColor4f::FromColor(color);
269     SkPaint paint;
270     paint.setColor4f(skColor, SkColorSpace::MakeSRGB().get());
271     paint.setBlendMode(SkBlendMode::kSrc);
272     SkCanvas canvas(srcBitmap);
273     canvas.drawPaint(paint);
274     return true;
275 }
276 
YUV420ToRGB888(const uint8_t * in,YuvImageInfo & srcInfo,uint8_t * out,YuvImageInfo & dstInfo)277 bool PixelConvertAdapter::YUV420ToRGB888(const uint8_t *in, YuvImageInfo &srcInfo, uint8_t *out, YuvImageInfo &dstInfo)
278 {
279     if (PixelYuvUtils::YuvScale(const_cast<uint8_t *>(in), srcInfo, out, dstInfo, SWS_BICUBIC) != SUCCESS) {
280         IMAGE_LOGE("YUV420ToRGB888 failed");
281         return false;
282     }
283     return true;
284 }
285 
286 } // namespace Media
287 } // namespace OHOS
288