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 "include/core/SkBitmap.h"
18 #include "include/core/SkCanvas.h"
19 #include "include/core/SkColor.h"
20 #include "include/core/SkColorSpace.h"
21 #include "include/core/SkImageInfo.h"
22 #include "include/core/SkPaint.h"
23 #include "include/core/SkPixmap.h"
24 #ifdef _WIN32
25 #include <iomanip>
26 #endif
27
28 namespace OHOS {
29 namespace Media {
30 using namespace OHOS::HiviewDFX;
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "PixelConvertAdapter" };
33
PixelFormatConvert(const PixelFormat & pixelFormat)34 static SkColorType PixelFormatConvert(const PixelFormat &pixelFormat)
35 {
36 SkColorType colorType;
37 switch (pixelFormat) {
38 case PixelFormat::BGRA_8888:
39 colorType = SkColorType::kBGRA_8888_SkColorType;
40 break;
41 case PixelFormat::RGBA_8888:
42 colorType = SkColorType::kRGBA_8888_SkColorType;
43 break;
44 case PixelFormat::RGB_565:
45 colorType = SkColorType::kRGB_565_SkColorType;
46 break;
47 case PixelFormat::ALPHA_8:
48 colorType = SkColorType::kAlpha_8_SkColorType;
49 break;
50 default:
51 colorType = SkColorType::kUnknown_SkColorType;
52 break;
53 }
54 return colorType;
55 }
56
WritePixelsConvert(const void * srcPixels,uint32_t srcRowBytes,const ImageInfo & srcInfo,void * dstPixels,const Position & dstPos,uint32_t dstRowBytes,const ImageInfo & dstInfo)57 bool PixelConvertAdapter::WritePixelsConvert(const void *srcPixels, uint32_t srcRowBytes, const ImageInfo &srcInfo,
58 void *dstPixels, const Position &dstPos, uint32_t dstRowBytes,
59 const ImageInfo &dstInfo)
60 {
61 // basic valid check, other parameters valid check in writePixels method
62 if (srcPixels == nullptr || dstPixels == nullptr) {
63 HiLog::Error(LABEL, "src or dst pixels invalid.");
64 return false;
65 }
66 SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
67 SkAlphaType dstAlphaType = static_cast<SkAlphaType>(dstInfo.alphaType);
68 SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
69 SkColorType dstColorType = PixelFormatConvert(dstInfo.pixelFormat);
70 SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
71 SkImageInfo dstImageInfo = SkImageInfo::Make(dstInfo.size.width, dstInfo.size.height, dstColorType, dstAlphaType);
72
73 SkBitmap dstBitmap;
74 SkPixmap srcPixmap(srcImageInfo, srcPixels, srcRowBytes);
75 if (!dstBitmap.installPixels(dstImageInfo, dstPixels, dstRowBytes)) {
76 HiLog::Error(LABEL, "WritePixelsConvert dst bitmap install pixels failed.");
77 return false;
78 }
79 if (!dstBitmap.writePixels(srcPixmap, dstPos.x, dstPos.y)) {
80 HiLog::Error(LABEL, "WritePixelsConvert dst bitmap write pixels by source failed.");
81 return false;
82 }
83
84 return true;
85 }
86
ReadPixelsConvert(const void * srcPixels,const Position & srcPos,uint32_t srcRowBytes,const ImageInfo & srcInfo,void * dstPixels,uint32_t dstRowBytes,const ImageInfo & dstInfo)87 bool PixelConvertAdapter::ReadPixelsConvert(const void *srcPixels, const Position &srcPos, uint32_t srcRowBytes,
88 const ImageInfo &srcInfo, void *dstPixels, uint32_t dstRowBytes,
89 const ImageInfo &dstInfo)
90 {
91 // basic valid check, other parameters valid check in readPixels method
92 if (srcPixels == nullptr || dstPixels == nullptr) {
93 HiLog::Error(LABEL, "src or dst pixels invalid.");
94 return false;
95 }
96 SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
97 SkAlphaType dstAlphaType = static_cast<SkAlphaType>(dstInfo.alphaType);
98 SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
99 SkColorType dstColorType = PixelFormatConvert(dstInfo.pixelFormat);
100 SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
101 SkImageInfo dstImageInfo = SkImageInfo::Make(dstInfo.size.width, dstInfo.size.height, dstColorType, dstAlphaType);
102
103 SkBitmap srcBitmap;
104 if (!srcBitmap.installPixels(srcImageInfo, const_cast<void *>(srcPixels), srcRowBytes)) {
105 HiLog::Error(LABEL, "ReadPixelsConvert src bitmap install pixels failed.");
106 return false;
107 }
108 if (!srcBitmap.readPixels(dstImageInfo, dstPixels, dstRowBytes, srcPos.x, srcPos.y)) {
109 HiLog::Error(LABEL, "ReadPixelsConvert read dst pixels from source failed.");
110 return false;
111 }
112 return true;
113 }
114
EraseBitmap(const void * srcPixels,uint32_t srcRowBytes,const ImageInfo & srcInfo,uint32_t color)115 bool PixelConvertAdapter::EraseBitmap(const void *srcPixels, uint32_t srcRowBytes, const ImageInfo &srcInfo,
116 uint32_t color)
117 {
118 if (srcPixels == nullptr) {
119 HiLog::Error(LABEL, "srcPixels is null.");
120 return false;
121 }
122 SkAlphaType srcAlphaType = static_cast<SkAlphaType>(srcInfo.alphaType);
123 SkColorType srcColorType = PixelFormatConvert(srcInfo.pixelFormat);
124 SkImageInfo srcImageInfo = SkImageInfo::Make(srcInfo.size.width, srcInfo.size.height, srcColorType, srcAlphaType);
125 SkBitmap srcBitmap;
126 if (!srcBitmap.installPixels(srcImageInfo, const_cast<void *>(srcPixels), srcRowBytes)) {
127 HiLog::Error(LABEL, "ReadPixelsConvert src bitmap install pixels failed.");
128 return false;
129 }
130 const SkColor4f skColor = SkColor4f::FromColor(color);
131 SkPaint paint;
132 paint.setColor4f(skColor, SkColorSpace::MakeSRGB().get());
133 paint.setBlendMode(SkBlendMode::kSrc);
134 SkCanvas canvas(srcBitmap);
135 canvas.drawPaint(paint);
136 return true;
137 }
138 } // namespace Media
139 } // namespace OHOS
140