1 /*
2 * Copyright (C) 2023 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 "image_converter.h"
17
18 #include "drawable_descriptor_log.h"
19 #ifndef PREVIEW
20 #include "image_utils.h"
21 #include "platform/image_native/image_type.h"
22 #endif
23
24 namespace OHOS::Ace::Napi {
25 #ifndef USE_ROSEN_DRAWING
PixelFormatToSkColorType(Media::PixelFormat pixelFormat)26 SkColorType ImageConverter::PixelFormatToSkColorType(Media::PixelFormat pixelFormat)
27 {
28 switch (pixelFormat) {
29 case Media::PixelFormat::BGRA_8888:
30 return SkColorType::kBGRA_8888_SkColorType;
31 case Media::PixelFormat::ARGB_8888:
32 case Media::PixelFormat::ALPHA_8:
33 case Media::PixelFormat::RGBA_8888:
34 case Media::PixelFormat::RGB_565:
35 case Media::PixelFormat::RGB_888:
36 case Media::PixelFormat::RGBA_F16:
37 case Media::PixelFormat::NV21:
38 case Media::PixelFormat::NV12:
39 case Media::PixelFormat::CMYK:
40 case Media::PixelFormat::UNKNOWN:
41 default:
42 return SkColorType::kUnknown_SkColorType;
43 }
44 }
45
AlphaTypeToSkAlphaType(Media::AlphaType alphaType)46 SkAlphaType ImageConverter::AlphaTypeToSkAlphaType(Media::AlphaType alphaType)
47 {
48 switch (alphaType) {
49 case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
50 return SkAlphaType::kOpaque_SkAlphaType;
51 case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
52 return SkAlphaType::kPremul_SkAlphaType;
53 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
54 return SkAlphaType::kUnpremul_SkAlphaType;
55 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
56 default:
57 return SkAlphaType::kUnknown_SkAlphaType;
58 }
59 }
60
PixelMapToBitmap(const std::shared_ptr<Media::PixelMap> & pixelMap)61 std::shared_ptr<SkBitmap> ImageConverter::PixelMapToBitmap(
62 const std::shared_ptr<Media::PixelMap>& pixelMap)
63 {
64 auto data = pixelMap->GetPixels();
65 SkBitmap bitmap;
66 SkColorType colorType = ImageConverter::PixelFormatToSkColorType(pixelMap->GetPixelFormat());
67 SkAlphaType alphaType = ImageConverter::AlphaTypeToSkAlphaType(pixelMap->GetAlphaType());
68 auto imageInfo = SkImageInfo::Make(pixelMap->GetWidth(), pixelMap->GetHeight(), colorType, alphaType);
69 bitmap.setInfo(imageInfo);
70 bitmap.setPixels(const_cast<uint8_t*>(data));
71 return std::make_shared<SkBitmap>(bitmap);
72 }
73
BitmapToPixelMap(const std::shared_ptr<SkBitmap> & bitMap,Media::InitializationOptions & opts)74 std::shared_ptr<Media::PixelMap> ImageConverter::BitmapToPixelMap(
75 const std::shared_ptr<SkBitmap>& bitMap, Media::InitializationOptions& opts)
76 {
77 auto data = bitMap->getPixels();
78 opts.size.width = static_cast<int32_t>(bitMap->width());
79 opts.size.height = static_cast<int32_t>(bitMap->height());
80 auto pixelMap = Media::PixelMap::Create(reinterpret_cast<uint32_t*>(data),
81 opts.size.width * opts.size.height, opts);
82 return pixelMap;
83 }
84 #else
85 Rosen::Drawing::ColorType ImageConverter::PixelFormatToColorType(Media::PixelFormat pixelFormat)
86 {
87 switch (pixelFormat) {
88 case Media::PixelFormat::BGRA_8888:
89 return Rosen::Drawing::ColorType::COLORTYPE_BGRA_8888;
90 case Media::PixelFormat::ARGB_8888:
91 case Media::PixelFormat::ALPHA_8:
92 case Media::PixelFormat::RGBA_8888:
93 case Media::PixelFormat::RGB_565:
94 case Media::PixelFormat::RGB_888:
95 case Media::PixelFormat::RGBA_F16:
96 case Media::PixelFormat::NV21:
97 case Media::PixelFormat::NV12:
98 case Media::PixelFormat::CMYK:
99 case Media::PixelFormat::UNKNOWN:
100 default:
101 return Rosen::Drawing::ColorType::COLORTYPE_UNKNOWN;
102 }
103 }
104
105 Rosen::Drawing::AlphaType ImageConverter::AlphaTypeToAlphaType(Media::AlphaType alphaType)
106 {
107 switch (alphaType) {
108 case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
109 return Rosen::Drawing::AlphaType::ALPHATYPE_OPAQUE;
110 case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
111 return Rosen::Drawing::AlphaType::ALPHATYPE_PREMUL;
112 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
113 return Rosen::Drawing::AlphaType::ALPHATYPE_UNPREMUL;
114 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
115 default:
116 return Rosen::Drawing::AlphaType::ALPHATYPE_UNKNOWN;
117 }
118 }
119
120 std::shared_ptr<Rosen::Drawing::Bitmap> ImageConverter::PixelMapToBitmap(
121 const std::shared_ptr<Media::PixelMap>& pixelMap)
122 {
123 if (!pixelMap) {
124 return nullptr;
125 }
126 auto data = pixelMap->GetPixels();
127 Rosen::Drawing::Bitmap bitmap;
128 Rosen::Drawing::ColorType colorType = ImageConverter::PixelFormatToColorType(pixelMap->GetPixelFormat());
129 Rosen::Drawing::AlphaType alphaType = ImageConverter::AlphaTypeToAlphaType(pixelMap->GetAlphaType());
130 Rosen::Drawing::ImageInfo imageInfo(pixelMap->GetWidth(), pixelMap->GetHeight(), colorType, alphaType);
131 bitmap.Build(imageInfo);
132 bitmap.SetPixels(const_cast<uint8_t*>(data));
133 return std::make_shared<Rosen::Drawing::Bitmap>(bitmap);
134 }
135
136 std::shared_ptr<Media::PixelMap> ImageConverter::BitmapToPixelMap(
137 const std::shared_ptr<Rosen::Drawing::Bitmap>& bitMap, Media::InitializationOptions& opts)
138 {
139 auto data = bitMap->GetPixels();
140 opts.size.width = static_cast<int32_t>(bitMap->GetWidth());
141 opts.size.height = static_cast<int32_t>(bitMap->GetHeight());
142 opts.editable = true;
143 auto pixelMap = Media::PixelMap::Create(opts);
144 if (!pixelMap) {
145 HILOGE("PixelMap is null, bitMap's Size = (%{public}d, %{public}d)", bitMap->GetWidth(), bitMap->GetHeight());
146 return pixelMap;
147 }
148 pixelMap->WritePixels(reinterpret_cast<uint8_t*>(data), opts.size.width * opts.size.height * sizeof(uint32_t));
149 return pixelMap;
150 }
151 #endif
152 } // namespace OHOS::Ace::Napi
153