• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "utils.h"
16 #include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/draw/surface.h"
17 #include <memory>
18 
19 namespace OHOS {
20 namespace Rosen {
21 namespace Drawing {
22 
23 static const int IMAGE_SIZE = 200;
24 
BuildRandomBitmap()25 Bitmap BuildRandomBitmap()
26 {
27     int seed = 2333;
28     srand(seed);
29     Bitmap bitmap;
30     bitmap.TryAllocPixels(ImageInfo::MakeN32Premul(IMAGE_SIZE, IMAGE_SIZE));
31     uint32_t* pixel = static_cast<uint32_t*>(bitmap.GetPixels());
32 
33     int mask = 255;
34     for (int i = 0; i < IMAGE_SIZE; i++) {
35         int r = rand() & mask;
36         int g = rand() & mask;
37         int b = rand() & mask;
38         int a = mask;
39         int x = r; // r: 0~7bit
40         x |= g << 8; // g: 8~15bit
41         x |= b << 16; // b: 16~23bit
42         x |= a << 24; // a: 24~31bit
43         for (int j = 0; j < IMAGE_SIZE; ++j) {
44             pixel[i * IMAGE_SIZE + j] = x;
45         }
46     }
47     return bitmap;
48 }
49 
MakeImageRaster()50 std::shared_ptr<Image> MakeImageRaster()
51 {
52     std::shared_ptr<Image> image = std::make_shared<Image>();
53     image->BuildFromBitmap(BuildRandomBitmap());
54     return image;
55 }
56 
MakeImageEncoded()57 std::shared_ptr<Image> MakeImageEncoded()
58 {
59     std::shared_ptr<Image> image = std::make_shared<Image>();
60     image->MakeFromEncoded(Data::MakeFromFileName("panda.png"));
61     return image;
62 }
63 
MakeImageGpu(std::shared_ptr<GPUContext> gpuContext)64 std::shared_ptr<Image> MakeImageGpu(std::shared_ptr<GPUContext> gpuContext)
65 {
66     auto surface = Surface::MakeRenderTarget(gpuContext.get(), true, ImageInfo::MakeN32Premul(200, 200)); // 200 * 200
67     Image image;
68     image.BuildFromBitmap(*gpuContext, BuildRandomBitmap());
69     auto canvas = surface->GetCanvas();
70     canvas->DrawImageRect(image, Rect(0, 0, 200, 200), SamplingOptions()); // 200 * 200
71     surface->FlushAndSubmit();
72     auto colorspace = ColorSpace::CreateSRGB();
73     std::shared_ptr<Image> dst = std::make_shared<Image>();
74     dst->BuildFromSurface(*gpuContext, *surface, TextureOrigin::TOP_LEFT,
75                           { ColorType::COLORTYPE_RGBA_8888, AlphaType::ALPHATYPE_PREMUL }, colorspace);
76     return dst;
77 }
78 
MakeImageYUVA(std::shared_ptr<GPUContext> gpuContext)79 std::shared_ptr<Image> MakeImageYUVA(std::shared_ptr<GPUContext> gpuContext)
80 {
81     Bitmap bitmap = BuildRandomBitmap();
82     YUVInfo yuvInfo = {
83         200,
84         200,
85         Drawing::YUVInfo::PlaneConfig::Y_UV,
86         Drawing::YUVInfo::SubSampling::K420,
87         Drawing::YUVInfo::YUVColorSpace::JPEG_FULL_YUVCOLORSPACE,
88         Drawing::YUVInfo::YUVDataType::UNORM_8,
89     };
90     auto image = Image::MakeFromYUVAPixmaps(*gpuContext, yuvInfo, bitmap.GetPixels());
91     return image;
92 }
93 
94 }
95 }
96 }