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 #include "test_common.h"
16 
17 #include <fcntl.h>
18 
19 #include "image/image.h"
20 #include "image_packer.h"
21 #include "utils/log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
Log(std::string info)25 void TestCommon::Log(std::string info)
26 {
27     LOGE("%{public}s", info.c_str());
28     std::cout << info.c_str() << std::endl;
29 }
30 
PackingPixmap(std::shared_ptr<OHOS::Media::PixelMap> pixmap,std::string fileName)31 int TestCommon::PackingPixmap(std::shared_ptr<OHOS::Media::PixelMap> pixmap, std::string fileName)
32 {
33     if (pixmap == nullptr || fileName == "") {
34         Log("invalied param");
35         return RET_FAILED;
36     }
37     OHOS::Media::ImagePacker imagePacker;
38     std::set<std::string> formats;
39     uint32_t ret = imagePacker.GetSupportedFormats(formats);
40     if (ret != 0) {
41         Log("failed to GetSupportedFormats");
42         return RET_FAILED;
43     }
44 
45     std::string path = "/data/test/" + fileName + ".png";
46     int32_t fileDes = open(path.c_str(), O_CREAT | O_RDWR);
47     if (fileDes <= 0) {
48         Log("failed to open");
49         return RET_FAILED;
50     }
51 
52     OHOS::Media::PackOption option;
53     option.format = "image/png";
54     uint32_t result = imagePacker.StartPacking(fileDes, option);
55     if (result != 0) {
56         Log("failed to StartPacking");
57         close(fileDes);
58         return RET_FAILED;
59     }
60     result = imagePacker.AddImage(*pixmap);
61     if (result != 0) {
62         Log("failed to AddImage");
63         close(fileDes);
64         return RET_FAILED;
65     }
66     int64_t packedSize = 0;
67     result = imagePacker.FinalizePacking(packedSize);
68     if (result != 0) {
69         Log("failed to FinalizePacking");
70         close(fileDes);
71         return RET_FAILED;
72     }
73 
74     close(fileDes);
75     return RET_OK;
76 }
ColorSpaceToDrawingColorSpace(OHOS::Media::ColorSpace colorSpace)77 std::shared_ptr<Drawing::ColorSpace> TestCommon::ColorSpaceToDrawingColorSpace(OHOS::Media::ColorSpace colorSpace)
78 {
79     switch (colorSpace) {
80         case OHOS::Media::ColorSpace::DISPLAY_P3:
81             return Drawing::ColorSpace::CreateRGB(
82                 Drawing::CMSTransferFuncType::SRGB, Drawing::CMSMatrixType::DCIP3);
83         case OHOS::Media::ColorSpace::LINEAR_SRGB:
84             return Drawing::ColorSpace::CreateSRGBLinear();
85         case OHOS::Media::ColorSpace::SRGB:
86         default:
87             return Drawing::ColorSpace::CreateSRGB();
88     }
89 }
PixelFormatToDrawingColorType(OHOS::Media::PixelFormat pixelFormat)90 Drawing::ColorType TestCommon::PixelFormatToDrawingColorType(OHOS::Media::PixelFormat pixelFormat)
91 {
92     switch (pixelFormat) {
93         case OHOS::Media::PixelFormat::RGB_565:
94             return Drawing::ColorType::COLORTYPE_RGB_565;
95         case OHOS::Media::PixelFormat::RGBA_8888:
96             return Drawing::ColorType::COLORTYPE_RGBA_8888;
97         case OHOS::Media::PixelFormat::BGRA_8888:
98             return Drawing::ColorType::COLORTYPE_BGRA_8888;
99         case OHOS::Media::PixelFormat::ALPHA_8:
100             return Drawing::ColorType::COLORTYPE_ALPHA_8;
101         case OHOS::Media::PixelFormat::RGBA_F16:
102             return Drawing::ColorType::COLORTYPE_RGBA_F16;
103         case OHOS::Media::PixelFormat::UNKNOWN:
104         case OHOS::Media::PixelFormat::ARGB_8888:
105         case OHOS::Media::PixelFormat::RGB_888:
106         case OHOS::Media::PixelFormat::NV21:
107         case OHOS::Media::PixelFormat::NV12:
108         case OHOS::Media::PixelFormat::CMYK:
109         default:
110             return Drawing::ColorType::COLORTYPE_UNKNOWN;
111     }
112 }
AlphaTypeToDrawingAlphaType(OHOS::Media::AlphaType alphaType)113 Drawing::AlphaType TestCommon::AlphaTypeToDrawingAlphaType(OHOS::Media::AlphaType alphaType)
114 {
115     switch (alphaType) {
116         case OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN:
117             return Drawing::AlphaType::ALPHATYPE_UNKNOWN;
118         case OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE:
119             return Drawing::AlphaType::ALPHATYPE_OPAQUE;
120         case OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL:
121             return Drawing::AlphaType::ALPHATYPE_PREMUL;
122         case OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL:
123             return Drawing::AlphaType::ALPHATYPE_UNPREMUL;
124         default:
125             return Drawing::AlphaType::ALPHATYPE_UNKNOWN;
126     }
127 }
128 } // namespace Rosen
129 } // namespace OHOS