• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "rs_graphic_test_img.h"
17 
18 #include "drawing/draw/core_canvas.h"
19 #include "image/bitmap.h"
20 #include "image/image.h"
21 #include "modifier/rs_extended_modifier.h"
22 #include "modifier/rs_property_modifier.h"
23 #include "utils/sampling_options.h"
24 
25 #define WIDTH_INDEX 2
26 #define HEIGHT_INDEX 3
27 
28 namespace OHOS {
29 namespace Rosen {
30 
DecodePixelMap(const std::string & pathName,const Media::AllocatorType & allocatorType)31 std::shared_ptr<Media::PixelMap> DecodePixelMap(const std::string& pathName, const Media::AllocatorType& allocatorType)
32 {
33     uint32_t errCode = 0;
34     std::unique_ptr<Media::ImageSource> imageSource =
35         Media::ImageSource::CreateImageSource(pathName, Media::SourceOptions(), errCode);
36     if (imageSource == nullptr || errCode != 0) {
37         std::cout << "imageSource : " << (imageSource != nullptr) << ", err:" << errCode << std::endl;
38         return nullptr;
39     }
40     Media::DecodeOptions decodeOpt;
41     decodeOpt.allocatorType = allocatorType;
42     std::shared_ptr<Media::PixelMap> pixelmap = imageSource->CreatePixelMap(decodeOpt, errCode);
43     if (pixelmap == nullptr || errCode != 0) {
44         std::cout << "pixelmap == nullptr, err:" << errCode << std::endl;
45         return nullptr;
46     }
47     return pixelmap;
48 }
49 
DecodePixelMap(const uint8_t * data,uint32_t size,const Media::AllocatorType & allocatorType)50 std::shared_ptr<Media::PixelMap> DecodePixelMap(
51     const uint8_t* data, uint32_t size, const Media::AllocatorType& allocatorType)
52 {
53     uint32_t errCode = 0;
54     std::unique_ptr<Media::ImageSource> imageSource =
55         Media::ImageSource::CreateImageSource(data, size, Media::SourceOptions(), errCode);
56     if (imageSource == nullptr || errCode != 0) {
57         std::cout << "imageSource : " << (imageSource != nullptr) << ", err:" << errCode << std::endl;
58         return nullptr;
59     }
60     Media::DecodeOptions decodeOpt;
61     decodeOpt.allocatorType = allocatorType;
62     std::shared_ptr<Media::PixelMap> pixelmap = imageSource->CreatePixelMap(decodeOpt, errCode);
63     if (pixelmap == nullptr || errCode != 0) {
64         std::cout << "pixelmap == nullptr, err:" << errCode << std::endl;
65         return nullptr;
66     }
67     return pixelmap;
68 }
69 
SetUpNodeBgImage(const std::string & pathName,const Rosen::Vector4f bounds)70 std::shared_ptr<Rosen::RSCanvasNode> SetUpNodeBgImage(const std::string& pathName, const Rosen::Vector4f bounds)
71 {
72     std::shared_ptr<Media::PixelMap> pixelmap = DecodePixelMap(pathName, Media::AllocatorType::SHARE_MEM_ALLOC);
73     auto image = std::make_shared<Rosen::RSImage>();
74     image->SetPixelMap(pixelmap);
75     image->SetImageFit((int)ImageFit::FILL);
76     auto node = Rosen::RSCanvasNode::Create();
77     node->SetBounds(bounds);
78     node->SetBgImageSize(bounds[WIDTH_INDEX], bounds[HEIGHT_INDEX]);
79     node->SetBgImage(image);
80     return node;
81 }
82 
SetUpNodeBgImage(const uint8_t * data,uint32_t size,const Rosen::Vector4f bounds)83 std::shared_ptr<Rosen::RSCanvasNode> SetUpNodeBgImage(const uint8_t* data, uint32_t size, const Rosen::Vector4f bounds)
84 {
85     std::shared_ptr<Media::PixelMap> pixelmap = DecodePixelMap(data, size, Media::AllocatorType::SHARE_MEM_ALLOC);
86     auto image = std::make_shared<Rosen::RSImage>();
87     image->SetPixelMap(pixelmap);
88     image->SetImageFit((int)ImageFit::FILL);
89     auto node = Rosen::RSCanvasNode::Create();
90     node->SetBounds(bounds);
91     node->SetBgImageSize(bounds[WIDTH_INDEX], bounds[HEIGHT_INDEX]);
92     node->SetBgImage(image);
93     return node;
94 }
95 
SetWidth(int32_t width)96 void ImageCustomModifier::SetWidth(int32_t width)
97 {
98     if (width_ == nullptr) {
99         width_ = std::make_shared<RSProperty<int32_t>>(width);
100         AttachProperty(width_);
101     } else {
102         width_->Set(width);
103     }
104 }
105 
SetHeight(int32_t height)106 void ImageCustomModifier::SetHeight(int32_t height)
107 {
108     if (height_ == nullptr) {
109         height_ = std::make_shared<RSProperty<int32_t>>(height);
110         AttachProperty(height_);
111     } else {
112         height_->Set(height);
113     }
114 }
115 
SetPixelMapPath(std::string pathName)116 void ImageCustomModifier::SetPixelMapPath(std::string pathName)
117 {
118     if (pathName_ == nullptr) {
119         pathName_ = std::make_shared<RSProperty<std::string>>(pathName);
120         AttachProperty(pathName_);
121     } else {
122         pathName_->Set(pathName);
123     }
124 }
125 
Draw(RSDrawingContext & context) const126 void ImageCustomModifier::Draw(RSDrawingContext& context) const
127 {
128     if (!width_ || !height_ || !pathName_) {
129         std::cout << "Has nullptr, Draw none\n";
130         Drawing::Rect rect;
131         Drawing::Brush brush;
132         context.canvas->AttachBrush(brush);
133         context.canvas->DrawRect(rect);
134         context.canvas->DetachBrush();
135         return;
136     }
137     int32_t width = width_->Get();
138     int32_t height = height_->Get();
139     std::string pathName = pathName_->Get();
140     auto pixelmap_ = DecodePixelMap(pathName, Media::AllocatorType::SHARE_MEM_ALLOC);
141     if (pixelmap_ == nullptr) {
142         std::cout << "***Test*** : pixelmap_ == nullptr\n";
143         return;
144     }
145     auto image = std::make_shared<Rosen::RSImage>();
146     image->SetPixelMap(pixelmap_);
147     image->SetImageFit((int)ImageFit::FILL);
148     image->CanvasDrawImage(*context.canvas, Drawing::Rect(0, 0, width, height), Drawing::SamplingOptions());
149     return;
150 }
151 } // namespace Rosen
152 } // namespace OHOS