1 /*
2 * Copyright (c) 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 "image_test.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
21 static constexpr int32_t BITMAP_WIDTH = 300;
22 static constexpr int32_t BITMAP_HEIGHT = 300;
23 static constexpr scalar RECT1_POSITION_LEFT = 100;
24 static constexpr scalar RECT1_POSITION_TOP = 100;
25 static constexpr scalar RECT1_POSITION_RIGHT = 200;
26 static constexpr scalar RECT1_POSITION_BOTTOM = 200;
27 static constexpr scalar RECT2_POSITION_LEFT = 300;
28 static constexpr scalar RECT2_POSITION_TOP = 300;
29 static constexpr scalar RECT2_POSITION_RIGHT = 500;
30 static constexpr scalar RECT2_POSITION_BOTTOM = 500;
TestDrawImage(Canvas & canvas,uint32_t width,uint32_t height)31 void ImageTest::TestDrawImage(Canvas& canvas, uint32_t width, uint32_t height)
32 {
33 LOGI("+++++++ TestDrawImage");
34 Bitmap bmp;
35 BitmapFormat format { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
36 bmp.Build(BITMAP_WIDTH, BITMAP_HEIGHT, format); // bitmap width and height
37 bmp.ClearWithColor(Drawing::Color::COLOR_BLUE);
38
39 Image image;
40 image.BuildFromBitmap(bmp);
41 int imageWidth = image.GetWidth();
42 int imageHeight = image.GetHeight();
43 LOGI("image width = %{public}d, image height = %{public}d", imageWidth, imageHeight);
44 Matrix matrix;
45 // Set matrix to rotate by degrees 45 about a pivot point at (0, 0).
46 matrix.Rotate(45, 0, 0);
47 SamplingOptions sampling = SamplingOptions(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NEAREST);
48 auto e = ShaderEffect::CreateImageShader(image, TileMode::REPEAT, TileMode::MIRROR, sampling, matrix);
49 LOGI("sampling useCubic = %{public}d, filter = %{public}d, mipmap = %{public}d", sampling.GetUseCubic(),
50 sampling.GetFilterMode(), sampling.GetMipmapMode());
51 auto c = Drawing::ColorSpace::CreateRefImage(image);
52
53 Pen pen;
54 pen.SetAntiAlias(true);
55 pen.SetColor(Drawing::Color::COLOR_BLUE);
56 pen.SetColor(pen.GetColor4f(), c);
57 pen.SetWidth(10); // The thickness of the pen is 10
58 pen.SetShaderEffect(e);
59 canvas.AttachPen(pen);
60 canvas.DrawImage(image, 500, 500, sampling); // Draw image at (500,500)
61
62 LOGI("------- TestDrawImage");
63 }
64
TestDrawImageRect(Canvas & canvas,uint32_t width,uint32_t height)65 void ImageTest::TestDrawImageRect(Canvas& canvas, uint32_t width, uint32_t height)
66 {
67 LOGI("+++++++ TestDrawImageRect");
68 Bitmap bmp;
69 BitmapFormat format { COLORTYPE_RGBA_8888, ALPHATYPE_OPAQUE };
70 bmp.Build(BITMAP_WIDTH, BITMAP_HEIGHT, format); // bitmap width and height
71 bmp.ClearWithColor(Drawing::Color::COLOR_BLUE);
72
73 Image image;
74 image.BuildFromBitmap(bmp);
75 Drawing::Rect r1(RECT1_POSITION_LEFT, RECT1_POSITION_TOP, RECT1_POSITION_RIGHT, RECT1_POSITION_BOTTOM);
76 Drawing::Rect r2(RECT2_POSITION_LEFT, RECT2_POSITION_TOP, RECT2_POSITION_RIGHT, RECT2_POSITION_BOTTOM);
77 SamplingOptions sampling = SamplingOptions(Drawing::FilterMode::NEAREST, Drawing::MipmapMode::NEAREST);
78
79 Brush brush;
80 brush.SetColor(Drawing::Color::COLOR_RED);
81 canvas.AttachBrush(brush);
82 canvas.DrawImageRect(image, r1, r2, sampling, SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
83
84 LOGI("------- TestDrawImageRect");
85 }
86
ImageTestCase()87 std::vector<ImageTest::TestFunc> ImageTest::ImageTestCase()
88 {
89 std::vector<TestFunc> testFuncVec;
90 testFuncVec.push_back(TestDrawImage);
91 testFuncVec.push_back(TestDrawImageRect);
92 return testFuncVec;
93 }
94 } // namespace Drawing
95 } // namespace Rosen
96 } // namespace OHOS