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