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