• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_map_ohos.h"
17 
18 #include <sstream>
19 
20 #include "base/log/log_wrapper.h"
21 #include "base/utils/utils.h"
22 
23 namespace OHOS::Ace {
24 
PixelFormatConverter(Media::PixelFormat pixelFormat)25 PixelFormat PixelMapOhos::PixelFormatConverter(Media::PixelFormat pixelFormat)
26 {
27     switch (pixelFormat) {
28         case Media::PixelFormat::RGB_565:
29             return PixelFormat::RGB_565;
30         case Media::PixelFormat::RGBA_8888:
31             return PixelFormat::RGBA_8888;
32         case Media::PixelFormat::BGRA_8888:
33             return PixelFormat::BGRA_8888;
34         case Media::PixelFormat::ALPHA_8:
35             return PixelFormat::ALPHA_8;
36         case Media::PixelFormat::RGBA_F16:
37             return PixelFormat::RGBA_F16;
38         case Media::PixelFormat::UNKNOWN:
39             return PixelFormat::UNKNOWN;
40         case Media::PixelFormat::ARGB_8888:
41             return PixelFormat::ARGB_8888;
42         case Media::PixelFormat::RGB_888:
43             return PixelFormat::RGB_888;
44         case Media::PixelFormat::NV21:
45             return PixelFormat::NV21;
46         case Media::PixelFormat::NV12:
47             return PixelFormat::NV12;
48         case Media::PixelFormat::CMYK:
49             return PixelFormat::CMYK;
50         default:
51             return PixelFormat::UNKNOWN;
52     }
53 }
54 
AlphaTypeConverter(Media::AlphaType alphaType)55 AlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType)
56 {
57     switch (alphaType) {
58         case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
59             return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
60         case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
61             return AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
62         case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
63             return AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
64         case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
65             return AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
66         default:
67             return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
68     }
69 }
70 
CreatePixelMap(void * rawPtr)71 RefPtr<PixelMap> PixelMap::CreatePixelMap(void* rawPtr)
72 {
73     std::shared_ptr<Media::PixelMap>* pixmapPtr = reinterpret_cast<std::shared_ptr<Media::PixelMap>*>(rawPtr);
74     if (pixmapPtr == nullptr || *pixmapPtr == nullptr) {
75         LOGW("pixmap pointer is nullptr when CreatePixelMap.");
76         return nullptr;
77     }
78     return AceType::MakeRefPtr<PixelMapOhos>(*pixmapPtr);
79 }
80 
CreatePixelMapFromDataAbility(void * ptr)81 RefPtr<PixelMap> PixelMap::CreatePixelMapFromDataAbility(void* ptr)
82 {
83     auto* pixmap = reinterpret_cast<Media::PixelMap*>(ptr);
84     CHECK_NULL_RETURN(pixmap, nullptr);
85     return AceType::MakeRefPtr<PixelMapOhos>(std::shared_ptr<Media::PixelMap>(pixmap));
86 }
87 
GetWidth() const88 int32_t PixelMapOhos::GetWidth() const
89 {
90     CHECK_NULL_RETURN(pixmap_, 0);
91     return pixmap_->GetWidth();
92 }
93 
GetHeight() const94 int32_t PixelMapOhos::GetHeight() const
95 {
96     CHECK_NULL_RETURN(pixmap_, 0);
97     return pixmap_->GetHeight();
98 }
99 
GetPixels() const100 const uint8_t* PixelMapOhos::GetPixels() const
101 {
102     CHECK_NULL_RETURN(pixmap_, nullptr);
103     return pixmap_->GetPixels();
104 }
105 
GetPixelFormat() const106 PixelFormat PixelMapOhos::GetPixelFormat() const
107 {
108     CHECK_NULL_RETURN(pixmap_, PixelFormat::UNKNOWN);
109     return PixelFormatConverter(pixmap_->GetPixelFormat());
110 }
111 
GetAlphaType() const112 AlphaType PixelMapOhos::GetAlphaType() const
113 {
114     CHECK_NULL_RETURN(pixmap_, AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN);
115     return AlphaTypeConverter(pixmap_->GetAlphaType());
116 }
117 
GetRowBytes() const118 int32_t PixelMapOhos::GetRowBytes() const
119 {
120     CHECK_NULL_RETURN(pixmap_, 0);
121     return pixmap_->GetRowBytes();
122 }
123 
GetByteCount() const124 int32_t PixelMapOhos::GetByteCount() const
125 {
126     CHECK_NULL_RETURN_NOLOG(pixmap_, 0);
127     return pixmap_->GetByteCount();
128 }
129 
GetPixelManager() const130 void* PixelMapOhos::GetPixelManager() const
131 {
132     Media::InitializationOptions opts;
133     CHECK_NULL_RETURN_NOLOG(pixmap_, nullptr);
134     auto newPixelMap = Media::PixelMap::Create(*pixmap_, opts);
135     return reinterpret_cast<void*>(new Media::PixelMapManager(newPixelMap.release()));
136 }
137 
GetRawPixelMapPtr() const138 void* PixelMapOhos::GetRawPixelMapPtr() const
139 {
140     CHECK_NULL_RETURN(pixmap_, nullptr);
141     return pixmap_.get();
142 }
143 
GetId()144 std::string PixelMapOhos::GetId()
145 {
146     // using pixmap addr
147     CHECK_NULL_RETURN(pixmap_, "nullptr");
148     std::stringstream strm;
149     strm << pixmap_.get();
150     return strm.str();
151 }
152 
GetModifyId()153 std::string PixelMapOhos::GetModifyId()
154 {
155     return std::string();
156 }
157 
GetPixelMapSharedPtr()158 std::shared_ptr<Media::PixelMap> PixelMapOhos::GetPixelMapSharedPtr()
159 {
160     return pixmap_;
161 }
162 
ConvertSkImageToPixmap(const uint32_t * colors,uint32_t colorLength,int32_t width,int32_t height)163 RefPtr<PixelMap> PixelMap::ConvertSkImageToPixmap(
164     const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height)
165 {
166     Media::InitializationOptions opts;
167     opts.size.width = width;
168     opts.size.height = height;
169     opts.editable = true;
170     std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(colors, colorLength, opts);
171     CHECK_NULL_RETURN(pixmap, nullptr);
172     std::shared_ptr<Media::PixelMap> sharedPixelmap(pixmap.release());
173     return AceType::MakeRefPtr<PixelMapOhos>(sharedPixelmap);
174 }
175 
176 } // namespace OHOS::Ace