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 {
PixelFormatToSkColorType(Media::PixelFormat pixelFormat)23 SkColorType ImageConverter::PixelFormatToSkColorType(Media::PixelFormat pixelFormat)
24 {
25 switch (pixelFormat) {
26 case Media::PixelFormat::BGRA_8888:
27 return SkColorType::kBGRA_8888_SkColorType;
28 case Media::PixelFormat::ARGB_8888:
29 case Media::PixelFormat::ALPHA_8:
30 case Media::PixelFormat::RGBA_8888:
31 case Media::PixelFormat::RGB_565:
32 case Media::PixelFormat::RGB_888:
33 case Media::PixelFormat::RGBA_F16:
34 case Media::PixelFormat::NV21:
35 case Media::PixelFormat::NV12:
36 case Media::PixelFormat::CMYK:
37 case Media::PixelFormat::UNKNOWN:
38 default:
39 return SkColorType::kUnknown_SkColorType;
40 }
41 }
42
AlphaTypeToSkAlphaType(Media::AlphaType alphaType)43 SkAlphaType ImageConverter::AlphaTypeToSkAlphaType(Media::AlphaType alphaType)
44 {
45 switch (alphaType) {
46 case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
47 return SkAlphaType::kOpaque_SkAlphaType;
48 case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
49 return SkAlphaType::kPremul_SkAlphaType;
50 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
51 return SkAlphaType::kUnpremul_SkAlphaType;
52 case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
53 default:
54 return SkAlphaType::kUnknown_SkAlphaType;
55 }
56 }
57
PixelMapToBitmap(const std::shared_ptr<Media::PixelMap> & pixelMap)58 std::shared_ptr<SkBitmap> ImageConverter::PixelMapToBitmap(
59 const std::shared_ptr<Media::PixelMap>& pixelMap)
60 {
61 auto data = pixelMap->GetPixels();
62 SkBitmap bitmap;
63 SkColorType colorType = ImageConverter::PixelFormatToSkColorType(pixelMap->GetPixelFormat());
64 SkAlphaType alphaType = ImageConverter::AlphaTypeToSkAlphaType(pixelMap->GetAlphaType());
65 auto imageInfo = SkImageInfo::Make(pixelMap->GetWidth(), pixelMap->GetHeight(), colorType, alphaType);
66 bitmap.setInfo(imageInfo);
67 bitmap.setPixels(const_cast<uint8_t*>(data));
68 return std::make_shared<SkBitmap>(bitmap);
69 }
70
BitmapToPixelMap(const std::shared_ptr<SkBitmap> & bitMap,Media::InitializationOptions & opts)71 std::shared_ptr<Media::PixelMap> ImageConverter::BitmapToPixelMap(
72 const std::shared_ptr<SkBitmap>& bitMap, Media::InitializationOptions& opts)
73 {
74 auto data = bitMap->getPixels();
75 opts.size.width = static_cast<int32_t>(bitMap->width());
76 opts.size.height = static_cast<int32_t>(bitMap->height());
77 auto pixelMap = Media::PixelMap::Create(reinterpret_cast<uint32_t*>(data),
78 opts.size.width * opts.size.height, opts);
79 return pixelMap;
80 }
81 } // namespace OHOS::Ace::Napi
82