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