• 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 "drawable_descriptor.h"
21 #include "pixel_map_manager.h"
22 
23 #include "base/log/log_wrapper.h"
24 #include "base/utils/utils.h"
25 #include "core/image/image_file_cache.h"
26 
27 namespace OHOS::Ace {
28 
PixelFormatConverter(Media::PixelFormat pixelFormat)29 PixelFormat PixelMapOhos::PixelFormatConverter(Media::PixelFormat pixelFormat)
30 {
31     switch (pixelFormat) {
32         case Media::PixelFormat::RGB_565:
33             return PixelFormat::RGB_565;
34         case Media::PixelFormat::RGBA_8888:
35             return PixelFormat::RGBA_8888;
36         case Media::PixelFormat::BGRA_8888:
37             return PixelFormat::BGRA_8888;
38         case Media::PixelFormat::ALPHA_8:
39             return PixelFormat::ALPHA_8;
40         case Media::PixelFormat::RGBA_F16:
41             return PixelFormat::RGBA_F16;
42         case Media::PixelFormat::UNKNOWN:
43             return PixelFormat::UNKNOWN;
44         case Media::PixelFormat::ARGB_8888:
45             return PixelFormat::ARGB_8888;
46         case Media::PixelFormat::RGB_888:
47             return PixelFormat::RGB_888;
48         case Media::PixelFormat::NV21:
49             return PixelFormat::NV21;
50         case Media::PixelFormat::NV12:
51             return PixelFormat::NV12;
52         case Media::PixelFormat::CMYK:
53             return PixelFormat::CMYK;
54         default:
55             return PixelFormat::UNKNOWN;
56     }
57 }
58 
AlphaTypeConverter(Media::AlphaType alphaType)59 AlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType)
60 {
61     switch (alphaType) {
62         case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
63             return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
64         case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
65             return AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
66         case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
67             return AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
68         case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
69             return AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
70         default:
71             return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
72     }
73 }
74 
Create(std::unique_ptr<Media::PixelMap> && pixmap)75 RefPtr<PixelMap> PixelMap::Create(std::unique_ptr<Media::PixelMap>&& pixmap)
76 {
77     return AceType::MakeRefPtr<PixelMapOhos>(std::move(pixmap));
78 }
79 
CreatePixelMap(void * rawPtr)80 RefPtr<PixelMap> PixelMap::CreatePixelMap(void* rawPtr)
81 {
82     auto* pixmapPtr = reinterpret_cast<std::shared_ptr<Media::PixelMap>*>(rawPtr);
83     if (pixmapPtr == nullptr || *pixmapPtr == nullptr) {
84         LOGW("pixmap pointer is nullptr when CreatePixelMap.");
85         return nullptr;
86     }
87     return AceType::MakeRefPtr<PixelMapOhos>(*pixmapPtr);
88 }
89 
GetFromDrawable(void * ptr)90 RefPtr<PixelMap> PixelMap::GetFromDrawable(void* ptr)
91 {
92     CHECK_NULL_RETURN(ptr, nullptr);
93     auto* drawable = reinterpret_cast<Napi::DrawableDescriptor*>(ptr);
94     return AceType::MakeRefPtr<PixelMapOhos>(drawable->GetPixelMap());
95 }
96 
CreatePixelMapFromDataAbility(void * ptr)97 RefPtr<PixelMap> PixelMap::CreatePixelMapFromDataAbility(void* ptr)
98 {
99     auto* pixmap = reinterpret_cast<Media::PixelMap*>(ptr);
100     CHECK_NULL_RETURN(pixmap, nullptr);
101     return AceType::MakeRefPtr<PixelMapOhos>(std::shared_ptr<Media::PixelMap>(pixmap));
102 }
103 
GetWidth() const104 int32_t PixelMapOhos::GetWidth() const
105 {
106     CHECK_NULL_RETURN(pixmap_, 0);
107     return pixmap_->GetWidth();
108 }
109 
GetHeight() const110 int32_t PixelMapOhos::GetHeight() const
111 {
112     CHECK_NULL_RETURN(pixmap_, 0);
113     return pixmap_->GetHeight();
114 }
115 
GetPixels() const116 const uint8_t* PixelMapOhos::GetPixels() const
117 {
118     CHECK_NULL_RETURN(pixmap_, nullptr);
119     return pixmap_->GetPixels();
120 }
121 
GetPixelFormat() const122 PixelFormat PixelMapOhos::GetPixelFormat() const
123 {
124     CHECK_NULL_RETURN(pixmap_, PixelFormat::UNKNOWN);
125     return PixelFormatConverter(pixmap_->GetPixelFormat());
126 }
127 
GetAlphaType() const128 AlphaType PixelMapOhos::GetAlphaType() const
129 {
130     CHECK_NULL_RETURN(pixmap_, AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN);
131     return AlphaTypeConverter(pixmap_->GetAlphaType());
132 }
133 
GetRowStride() const134 int32_t PixelMapOhos::GetRowStride() const
135 {
136     CHECK_NULL_RETURN(pixmap_, 0);
137     return pixmap_->GetRowStride();
138 }
139 
GetRowBytes() const140 int32_t PixelMapOhos::GetRowBytes() const
141 {
142     CHECK_NULL_RETURN(pixmap_, 0);
143     return pixmap_->GetRowBytes();
144 }
145 
GetByteCount() const146 int32_t PixelMapOhos::GetByteCount() const
147 {
148     CHECK_NULL_RETURN(pixmap_, 0);
149     return pixmap_->GetByteCount();
150 }
151 
GetPixelManager() const152 void* PixelMapOhos::GetPixelManager() const
153 {
154     Media::InitializationOptions opts;
155     CHECK_NULL_RETURN(pixmap_, nullptr);
156     auto newPixelMap = Media::PixelMap::Create(*pixmap_, opts);
157     return reinterpret_cast<void*>(new Media::PixelMapManager(newPixelMap.release()));
158 }
159 
GetRawPixelMapPtr() const160 void* PixelMapOhos::GetRawPixelMapPtr() const
161 {
162     CHECK_NULL_RETURN(pixmap_, nullptr);
163     return pixmap_.get();
164 }
165 
Scale(float xAxis,float yAxis)166 void PixelMapOhos::Scale(float xAxis, float yAxis)
167 {
168     CHECK_NULL_VOID(pixmap_);
169     pixmap_->scale(xAxis, yAxis);
170 }
171 
Scale(float xAxis,float yAxis,const AceAntiAliasingOption & option)172 void PixelMapOhos::Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option)
173 {
174     CHECK_NULL_VOID(pixmap_);
175     switch (option) {
176         case AceAntiAliasingOption::NONE:
177             pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::NONE);
178             break;
179         case AceAntiAliasingOption::LOW:
180             pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::LOW);
181             break;
182         case AceAntiAliasingOption::MEDIUM:
183             pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::MEDIUM);
184             break;
185         case AceAntiAliasingOption::HIGH:
186             pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::HIGH);
187             break;
188         default:
189             pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::NONE);
190             break;
191     }
192 }
193 
GetId()194 std::string PixelMapOhos::GetId()
195 {
196     // using pixmap addr
197     CHECK_NULL_RETURN(pixmap_, "nullptr");
198     std::stringstream strm;
199     strm << pixmap_.get();
200     return strm.str();
201 }
202 
GetModifyId()203 std::string PixelMapOhos::GetModifyId()
204 {
205     return {};
206 }
207 
GetPixelMapSharedPtr()208 std::shared_ptr<Media::PixelMap> PixelMapOhos::GetPixelMapSharedPtr()
209 {
210     return pixmap_;
211 }
212 
GetWritablePixels() const213 void* PixelMapOhos::GetWritablePixels() const
214 {
215     CHECK_NULL_RETURN(pixmap_, nullptr);
216     return pixmap_->GetWritablePixels();
217 }
218 
ConvertSkImageToPixmap(const uint32_t * colors,uint32_t colorLength,int32_t width,int32_t height)219 RefPtr<PixelMap> PixelMap::ConvertSkImageToPixmap(
220     const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height)
221 {
222     Media::InitializationOptions opts;
223     opts.size.width = width;
224     opts.size.height = height;
225     opts.editable = true;
226     std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(colors, colorLength, opts);
227     CHECK_NULL_RETURN(pixmap, nullptr);
228     std::shared_ptr<Media::PixelMap> sharedPixelmap(pixmap.release());
229     return AceType::MakeRefPtr<PixelMapOhos>(sharedPixelmap);
230 }
231 
SavePixelMapToFile(const std::string & dst) const232 void PixelMapOhos::SavePixelMapToFile(const std::string& dst) const
233 {
234     int32_t w = pixmap_->GetWidth();
235     int32_t h = pixmap_->GetHeight();
236     int32_t totalSize = pixmap_->GetByteCount();
237     auto rowStride = pixmap_->GetRowStride();
238     uint64_t nowTime = static_cast<uint64_t>(
239         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
240             .count());
241     std::string filename = std::to_string(nowTime) + "_w" + std::to_string(w) + "_h" + std::to_string(h) +
242                            "_rowStride" + std::to_string(rowStride) + "_byteCount" + std::to_string(totalSize) + dst +
243                            ".dat";
244     auto path = ImageFileCache::GetInstance().ConstructCacheFilePath(filename);
245     std::ofstream outFile(path, std::fstream::out);
246     if (!outFile.is_open()) {
247         TAG_LOGW(AceLogTag::ACE_IMAGE, "write error, path=%{public}s", path.c_str());
248     }
249     outFile.write(reinterpret_cast<const char*>(pixmap_->GetPixels()), totalSize);
250     TAG_LOGI(AceLogTag::ACE_IMAGE, "write success, path=%{public}s", path.c_str());
251 }
252 
253 } // namespace OHOS::Ace
254