123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "pixel_map_ohos.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci#include "drawable_descriptor.h" 1923b3eb3cSopenharmony_ci#include "pixel_map_manager.h" 2023b3eb3cSopenharmony_ci 2123b3eb3cSopenharmony_ci#include "core/image/image_file_cache.h" 2223b3eb3cSopenharmony_ci 2323b3eb3cSopenharmony_cinamespace OHOS::Ace { 2423b3eb3cSopenharmony_ci 2523b3eb3cSopenharmony_ciPixelFormat PixelMapOhos::PixelFormatConverter(Media::PixelFormat pixelFormat) 2623b3eb3cSopenharmony_ci{ 2723b3eb3cSopenharmony_ci switch (pixelFormat) { 2823b3eb3cSopenharmony_ci case Media::PixelFormat::RGB_565: 2923b3eb3cSopenharmony_ci return PixelFormat::RGB_565; 3023b3eb3cSopenharmony_ci case Media::PixelFormat::RGBA_8888: 3123b3eb3cSopenharmony_ci return PixelFormat::RGBA_8888; 3223b3eb3cSopenharmony_ci case Media::PixelFormat::RGBA_1010102: 3323b3eb3cSopenharmony_ci return PixelFormat::RGBA_1010102; 3423b3eb3cSopenharmony_ci case Media::PixelFormat::BGRA_8888: 3523b3eb3cSopenharmony_ci return PixelFormat::BGRA_8888; 3623b3eb3cSopenharmony_ci case Media::PixelFormat::ALPHA_8: 3723b3eb3cSopenharmony_ci return PixelFormat::ALPHA_8; 3823b3eb3cSopenharmony_ci case Media::PixelFormat::RGBA_F16: 3923b3eb3cSopenharmony_ci return PixelFormat::RGBA_F16; 4023b3eb3cSopenharmony_ci case Media::PixelFormat::UNKNOWN: 4123b3eb3cSopenharmony_ci return PixelFormat::UNKNOWN; 4223b3eb3cSopenharmony_ci case Media::PixelFormat::ARGB_8888: 4323b3eb3cSopenharmony_ci return PixelFormat::ARGB_8888; 4423b3eb3cSopenharmony_ci case Media::PixelFormat::RGB_888: 4523b3eb3cSopenharmony_ci return PixelFormat::RGB_888; 4623b3eb3cSopenharmony_ci case Media::PixelFormat::NV21: 4723b3eb3cSopenharmony_ci return PixelFormat::NV21; 4823b3eb3cSopenharmony_ci case Media::PixelFormat::NV12: 4923b3eb3cSopenharmony_ci return PixelFormat::NV12; 5023b3eb3cSopenharmony_ci case Media::PixelFormat::CMYK: 5123b3eb3cSopenharmony_ci return PixelFormat::CMYK; 5223b3eb3cSopenharmony_ci default: 5323b3eb3cSopenharmony_ci return PixelFormat::UNKNOWN; 5423b3eb3cSopenharmony_ci } 5523b3eb3cSopenharmony_ci} 5623b3eb3cSopenharmony_ci 5723b3eb3cSopenharmony_ciAlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType) 5823b3eb3cSopenharmony_ci{ 5923b3eb3cSopenharmony_ci switch (alphaType) { 6023b3eb3cSopenharmony_ci case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN: 6123b3eb3cSopenharmony_ci return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 6223b3eb3cSopenharmony_ci case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE: 6323b3eb3cSopenharmony_ci return AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; 6423b3eb3cSopenharmony_ci case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL: 6523b3eb3cSopenharmony_ci return AlphaType::IMAGE_ALPHA_TYPE_PREMUL; 6623b3eb3cSopenharmony_ci case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL: 6723b3eb3cSopenharmony_ci return AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL; 6823b3eb3cSopenharmony_ci default: 6923b3eb3cSopenharmony_ci return AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN; 7023b3eb3cSopenharmony_ci } 7123b3eb3cSopenharmony_ci} 7223b3eb3cSopenharmony_ci 7323b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::Create(std::unique_ptr<Media::PixelMap>&& pixmap) 7423b3eb3cSopenharmony_ci{ 7523b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(std::move(pixmap)); 7623b3eb3cSopenharmony_ci} 7723b3eb3cSopenharmony_ci 7823b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::CreatePixelMap(void* rawPtr) 7923b3eb3cSopenharmony_ci{ 8023b3eb3cSopenharmony_ci auto* pixmapPtr = reinterpret_cast<std::shared_ptr<Media::PixelMap>*>(rawPtr); 8123b3eb3cSopenharmony_ci if (pixmapPtr == nullptr || *pixmapPtr == nullptr) { 8223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_IMAGE, "pixmap pointer is nullptr when CreatePixelMap."); 8323b3eb3cSopenharmony_ci return nullptr; 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(*pixmapPtr); 8623b3eb3cSopenharmony_ci} 8723b3eb3cSopenharmony_ci 8823b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::CopyPixelMap(const RefPtr<PixelMap>& pixelMap) 8923b3eb3cSopenharmony_ci{ 9023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixelMap, nullptr); 9123b3eb3cSopenharmony_ci OHOS::Media::InitializationOptions opts; 9223b3eb3cSopenharmony_ci auto mediaPixelMap = pixelMap->GetPixelMapSharedPtr(); 9323b3eb3cSopenharmony_ci std::unique_ptr<Media::PixelMap> uniquePixelMap = Media::PixelMap::Create(*mediaPixelMap, opts); 9423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(uniquePixelMap, nullptr); 9523b3eb3cSopenharmony_ci Media::PixelMap* pixelMapRelease = uniquePixelMap.release(); 9623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixelMapRelease, nullptr); 9723b3eb3cSopenharmony_ci std::shared_ptr<Media::PixelMap> newPixelMap(pixelMapRelease); 9823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(newPixelMap, nullptr); 9923b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(newPixelMap); 10023b3eb3cSopenharmony_ci} 10123b3eb3cSopenharmony_ci 10223b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::DecodeTlv(std::vector<uint8_t>& buff) 10323b3eb3cSopenharmony_ci{ 10423b3eb3cSopenharmony_ci Media::PixelMap* pixelMapRelease = OHOS::Media::PixelMap::DecodeTlv(buff); 10523b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixelMapRelease, nullptr); 10623b3eb3cSopenharmony_ci std::shared_ptr<Media::PixelMap> newPixelMap(pixelMapRelease); 10723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(newPixelMap, nullptr); 10823b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(newPixelMap); 10923b3eb3cSopenharmony_ci} 11023b3eb3cSopenharmony_ci 11123b3eb3cSopenharmony_cibool PixelMapOhos::EncodeTlv(std::vector<uint8_t>& buff) 11223b3eb3cSopenharmony_ci{ 11323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, false); 11423b3eb3cSopenharmony_ci return pixmap_->EncodeTlv(buff); 11523b3eb3cSopenharmony_ci} 11623b3eb3cSopenharmony_ci 11723b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::GetFromDrawable(void* ptr) 11823b3eb3cSopenharmony_ci{ 11923b3eb3cSopenharmony_ci CHECK_NULL_RETURN(ptr, nullptr); 12023b3eb3cSopenharmony_ci auto* drawable = reinterpret_cast<Napi::DrawableDescriptor*>(ptr); 12123b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(drawable->GetPixelMap()); 12223b3eb3cSopenharmony_ci} 12323b3eb3cSopenharmony_ci 12423b3eb3cSopenharmony_cibool PixelMap::GetPxielMapListFromAnimatedDrawable(void* ptr, std::vector<RefPtr<PixelMap>>& pixelMaps, 12523b3eb3cSopenharmony_ci int32_t& duration, int32_t& iterations) 12623b3eb3cSopenharmony_ci{ 12723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(ptr, false); 12823b3eb3cSopenharmony_ci auto* drawable = reinterpret_cast<Napi::DrawableDescriptor*>(ptr); 12923b3eb3cSopenharmony_ci auto drawableType = drawable->GetDrawableType(); 13023b3eb3cSopenharmony_ci if (drawableType != Napi::DrawableDescriptor::DrawableType::ANIMATED) { 13123b3eb3cSopenharmony_ci return false; 13223b3eb3cSopenharmony_ci } 13323b3eb3cSopenharmony_ci auto* animatedDrawable = static_cast<Napi::AnimatedDrawableDescriptor*>(drawable); 13423b3eb3cSopenharmony_ci std::vector<std::shared_ptr<Media::PixelMap>> pixelMapList = animatedDrawable->GetPixelMapList(); 13523b3eb3cSopenharmony_ci for (uint32_t i = 0; i < pixelMapList.size(); i++) { 13623b3eb3cSopenharmony_ci pixelMaps.push_back(AceType::MakeRefPtr<PixelMapOhos>(std::move(pixelMapList[i]))); 13723b3eb3cSopenharmony_ci } 13823b3eb3cSopenharmony_ci duration = animatedDrawable->GetDuration(); 13923b3eb3cSopenharmony_ci iterations = animatedDrawable->GetIterations(); 14023b3eb3cSopenharmony_ci return true; 14123b3eb3cSopenharmony_ci} 14223b3eb3cSopenharmony_ci 14323b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::CreatePixelMapFromDataAbility(void* ptr) 14423b3eb3cSopenharmony_ci{ 14523b3eb3cSopenharmony_ci auto* pixmap = reinterpret_cast<Media::PixelMap*>(ptr); 14623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap, nullptr); 14723b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(std::shared_ptr<Media::PixelMap>(pixmap)); 14823b3eb3cSopenharmony_ci} 14923b3eb3cSopenharmony_ci 15023b3eb3cSopenharmony_ciint32_t PixelMapOhos::GetWidth() const 15123b3eb3cSopenharmony_ci{ 15223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, 0); 15323b3eb3cSopenharmony_ci return pixmap_->GetWidth(); 15423b3eb3cSopenharmony_ci} 15523b3eb3cSopenharmony_ci 15623b3eb3cSopenharmony_ciint32_t PixelMapOhos::GetHeight() const 15723b3eb3cSopenharmony_ci{ 15823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, 0); 15923b3eb3cSopenharmony_ci return pixmap_->GetHeight(); 16023b3eb3cSopenharmony_ci} 16123b3eb3cSopenharmony_ci 16223b3eb3cSopenharmony_ciconst uint8_t* PixelMapOhos::GetPixels() const 16323b3eb3cSopenharmony_ci{ 16423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, nullptr); 16523b3eb3cSopenharmony_ci return pixmap_->GetPixels(); 16623b3eb3cSopenharmony_ci} 16723b3eb3cSopenharmony_ci 16823b3eb3cSopenharmony_ciPixelFormat PixelMapOhos::GetPixelFormat() const 16923b3eb3cSopenharmony_ci{ 17023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, PixelFormat::UNKNOWN); 17123b3eb3cSopenharmony_ci return PixelFormatConverter(pixmap_->GetPixelFormat()); 17223b3eb3cSopenharmony_ci} 17323b3eb3cSopenharmony_ci 17423b3eb3cSopenharmony_ciAlphaType PixelMapOhos::GetAlphaType() const 17523b3eb3cSopenharmony_ci{ 17623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN); 17723b3eb3cSopenharmony_ci return AlphaTypeConverter(pixmap_->GetAlphaType()); 17823b3eb3cSopenharmony_ci} 17923b3eb3cSopenharmony_ci 18023b3eb3cSopenharmony_ciint32_t PixelMapOhos::GetRowStride() const 18123b3eb3cSopenharmony_ci{ 18223b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, 0); 18323b3eb3cSopenharmony_ci return pixmap_->GetRowStride(); 18423b3eb3cSopenharmony_ci} 18523b3eb3cSopenharmony_ci 18623b3eb3cSopenharmony_ciint32_t PixelMapOhos::GetRowBytes() const 18723b3eb3cSopenharmony_ci{ 18823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, 0); 18923b3eb3cSopenharmony_ci return pixmap_->GetRowBytes(); 19023b3eb3cSopenharmony_ci} 19123b3eb3cSopenharmony_ci 19223b3eb3cSopenharmony_ciint32_t PixelMapOhos::GetByteCount() const 19323b3eb3cSopenharmony_ci{ 19423b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, 0); 19523b3eb3cSopenharmony_ci return pixmap_->GetByteCount(); 19623b3eb3cSopenharmony_ci} 19723b3eb3cSopenharmony_ci 19823b3eb3cSopenharmony_civoid* PixelMapOhos::GetPixelManager() const 19923b3eb3cSopenharmony_ci{ 20023b3eb3cSopenharmony_ci Media::InitializationOptions opts; 20123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, nullptr); 20223b3eb3cSopenharmony_ci auto newPixelMap = Media::PixelMap::Create(*pixmap_, opts); 20323b3eb3cSopenharmony_ci return reinterpret_cast<void*>(new Media::PixelMapManager(newPixelMap.release())); 20423b3eb3cSopenharmony_ci} 20523b3eb3cSopenharmony_ci 20623b3eb3cSopenharmony_civoid* PixelMapOhos::GetRawPixelMapPtr() const 20723b3eb3cSopenharmony_ci{ 20823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, nullptr); 20923b3eb3cSopenharmony_ci return pixmap_.get(); 21023b3eb3cSopenharmony_ci} 21123b3eb3cSopenharmony_ci 21223b3eb3cSopenharmony_civoid PixelMapOhos::Scale(float xAxis, float yAxis) 21323b3eb3cSopenharmony_ci{ 21423b3eb3cSopenharmony_ci CHECK_NULL_VOID(pixmap_); 21523b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis); 21623b3eb3cSopenharmony_ci} 21723b3eb3cSopenharmony_ci 21823b3eb3cSopenharmony_civoid PixelMapOhos::Scale(float xAxis, float yAxis, const AceAntiAliasingOption &option) 21923b3eb3cSopenharmony_ci{ 22023b3eb3cSopenharmony_ci CHECK_NULL_VOID(pixmap_); 22123b3eb3cSopenharmony_ci switch (option) { 22223b3eb3cSopenharmony_ci case AceAntiAliasingOption::NONE: 22323b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::NONE); 22423b3eb3cSopenharmony_ci break; 22523b3eb3cSopenharmony_ci case AceAntiAliasingOption::LOW: 22623b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::LOW); 22723b3eb3cSopenharmony_ci break; 22823b3eb3cSopenharmony_ci case AceAntiAliasingOption::MEDIUM: 22923b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::MEDIUM); 23023b3eb3cSopenharmony_ci break; 23123b3eb3cSopenharmony_ci case AceAntiAliasingOption::HIGH: 23223b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::HIGH); 23323b3eb3cSopenharmony_ci break; 23423b3eb3cSopenharmony_ci default: 23523b3eb3cSopenharmony_ci pixmap_->scale(xAxis, yAxis, Media::AntiAliasingOption::NONE); 23623b3eb3cSopenharmony_ci break; 23723b3eb3cSopenharmony_ci } 23823b3eb3cSopenharmony_ci} 23923b3eb3cSopenharmony_ci 24023b3eb3cSopenharmony_cistd::string PixelMapOhos::GetId() 24123b3eb3cSopenharmony_ci{ 24223b3eb3cSopenharmony_ci // using pixmap addr 24323b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, "nullptr"); 24423b3eb3cSopenharmony_ci std::stringstream strm; 24523b3eb3cSopenharmony_ci strm << pixmap_.get(); 24623b3eb3cSopenharmony_ci return strm.str(); 24723b3eb3cSopenharmony_ci} 24823b3eb3cSopenharmony_ci 24923b3eb3cSopenharmony_cistd::string PixelMapOhos::GetModifyId() 25023b3eb3cSopenharmony_ci{ 25123b3eb3cSopenharmony_ci return {}; 25223b3eb3cSopenharmony_ci} 25323b3eb3cSopenharmony_ci 25423b3eb3cSopenharmony_cistd::shared_ptr<Media::PixelMap> PixelMapOhos::GetPixelMapSharedPtr() 25523b3eb3cSopenharmony_ci{ 25623b3eb3cSopenharmony_ci return pixmap_; 25723b3eb3cSopenharmony_ci} 25823b3eb3cSopenharmony_ci 25923b3eb3cSopenharmony_civoid* PixelMapOhos::GetWritablePixels() const 26023b3eb3cSopenharmony_ci{ 26123b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, nullptr); 26223b3eb3cSopenharmony_ci return pixmap_->GetWritablePixels(); 26323b3eb3cSopenharmony_ci} 26423b3eb3cSopenharmony_ci 26523b3eb3cSopenharmony_cibool PixelMapOhos::GetPixelsVec(std::vector<uint8_t>& data) const 26623b3eb3cSopenharmony_ci{ 26723b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap_, false); 26823b3eb3cSopenharmony_ci data.resize(pixmap_->GetByteCount()); 26923b3eb3cSopenharmony_ci uint8_t* dst = data.data(); 27023b3eb3cSopenharmony_ci uint32_t errCode = pixmap_->ReadPixels(pixmap_->GetByteCount(), dst); 27123b3eb3cSopenharmony_ci if (errCode) { 27223b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_IMAGE, "GetPixelsVec error, errCode=%{public}d", errCode); 27323b3eb3cSopenharmony_ci return false; 27423b3eb3cSopenharmony_ci } 27523b3eb3cSopenharmony_ci return true; 27623b3eb3cSopenharmony_ci} 27723b3eb3cSopenharmony_ci 27823b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMap::ConvertSkImageToPixmap( 27923b3eb3cSopenharmony_ci const uint32_t* colors, uint32_t colorLength, int32_t width, int32_t height) 28023b3eb3cSopenharmony_ci{ 28123b3eb3cSopenharmony_ci Media::InitializationOptions opts; 28223b3eb3cSopenharmony_ci opts.size.width = width; 28323b3eb3cSopenharmony_ci opts.size.height = height; 28423b3eb3cSopenharmony_ci opts.editable = true; 28523b3eb3cSopenharmony_ci std::unique_ptr<Media::PixelMap> pixmap = Media::PixelMap::Create(colors, colorLength, opts); 28623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pixmap, nullptr); 28723b3eb3cSopenharmony_ci std::shared_ptr<Media::PixelMap> sharedPixelmap(pixmap.release()); 28823b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(sharedPixelmap); 28923b3eb3cSopenharmony_ci} 29023b3eb3cSopenharmony_ci 29123b3eb3cSopenharmony_civoid PixelMapOhos::SavePixelMapToFile(const std::string& dst) const 29223b3eb3cSopenharmony_ci{ 29323b3eb3cSopenharmony_ci int32_t w = pixmap_->GetWidth(); 29423b3eb3cSopenharmony_ci int32_t h = pixmap_->GetHeight(); 29523b3eb3cSopenharmony_ci int32_t totalSize = static_cast<int32_t>(pixmap_->GetCapacity()); 29623b3eb3cSopenharmony_ci auto rowStride = pixmap_->GetRowStride(); 29723b3eb3cSopenharmony_ci uint64_t nowTime = static_cast<uint64_t>( 29823b3eb3cSopenharmony_ci std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()) 29923b3eb3cSopenharmony_ci .count()); 30023b3eb3cSopenharmony_ci std::string filename = std::to_string(nowTime) + "_w" + std::to_string(w) + "_h" + std::to_string(h) + 30123b3eb3cSopenharmony_ci "_rowStride" + std::to_string(rowStride) + "_byteCount" + std::to_string(totalSize) + dst + 30223b3eb3cSopenharmony_ci ".dat"; 30323b3eb3cSopenharmony_ci auto path = ImageFileCache::GetInstance().ConstructCacheFilePath(filename); 30423b3eb3cSopenharmony_ci std::ofstream outFile(path, std::fstream::out); 30523b3eb3cSopenharmony_ci if (!outFile.is_open()) { 30623b3eb3cSopenharmony_ci TAG_LOGW(AceLogTag::ACE_IMAGE, "write error, path=%{public}s", path.c_str()); 30723b3eb3cSopenharmony_ci } 30823b3eb3cSopenharmony_ci outFile.write(reinterpret_cast<const char*>(pixmap_->GetPixels()), totalSize); 30923b3eb3cSopenharmony_ci TAG_LOGI(AceLogTag::ACE_IMAGE, "write success, path=%{public}s", path.c_str()); 31023b3eb3cSopenharmony_ci} 31123b3eb3cSopenharmony_ci 31223b3eb3cSopenharmony_ciRefPtr<PixelMap> PixelMapOhos::GetCropPixelMap(const Rect& srcRect) 31323b3eb3cSopenharmony_ci{ 31423b3eb3cSopenharmony_ci Media::InitializationOptions options; 31523b3eb3cSopenharmony_ci options.size.width = static_cast<int32_t>(srcRect.Width()); 31623b3eb3cSopenharmony_ci options.size.height = static_cast<int32_t>(srcRect.Height()); 31723b3eb3cSopenharmony_ci options.pixelFormat = Media::PixelFormat::RGBA_8888; 31823b3eb3cSopenharmony_ci options.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; 31923b3eb3cSopenharmony_ci options.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE; 32023b3eb3cSopenharmony_ci 32123b3eb3cSopenharmony_ci Media::Rect rect {srcRect.Left(), srcRect.Top(), srcRect.Width(), srcRect.Height()}; 32223b3eb3cSopenharmony_ci auto resPixelmap = OHOS::Media::PixelMap::Create(*pixmap_, rect, options); 32323b3eb3cSopenharmony_ci return AceType::MakeRefPtr<PixelMapOhos>(std::move(resPixelmap)); 32423b3eb3cSopenharmony_ci} 32523b3eb3cSopenharmony_ci 32623b3eb3cSopenharmony_ci} // namespace OHOS::Ace 327