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 "common_fuzztest_function.h"
17
18 #define private public
19 #define protected public
20
21 #include <chrono>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <iostream>
25 #include <fstream>
26
27 #include "pixel_yuv.h"
28 #include "image_packer.h"
29 #include "media_errors.h"
30 #include "image_log.h"
31 #include "abs_image_decoder.h"
32 #include "image_type.h"
33
34 #undef LOG_DOMAIN
35 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
36
37 #undef LOG_TAG
38 #define LOG_TAG "COMMON_FUZZTEST_FUNCTION"
39
40 static const int FOUR_BYTES_PER_PIXEL = 4;
41 static const int NUM_TWO = 2;
42 static constexpr uint32_t PIXELFORMAT_MODULO = 105;
43 static constexpr uint32_t ALPHATYPE_MODULO = 4;
44 static constexpr uint32_t COLORSPACE_MODULO = 17;
45 static constexpr uint32_t DYNAMICRANGE_MODULO = 3;
46 static constexpr uint32_t RESOLUTION_MODULO = 4;
47 static constexpr uint32_t ALLOCATORTYPE_MODULO = 5;
48 using namespace OHOS::Media;
49
ConvertDataToFd(const uint8_t * data,size_t size,std::string encodeFormat)50 int ConvertDataToFd(const uint8_t* data, size_t size, std::string encodeFormat)
51 {
52 if (size < FOUR_BYTES_PER_PIXEL) {
53 return -1;
54 }
55 int picturePixels = size / FOUR_BYTES_PER_PIXEL;
56 InitializationOptions opts = {};
57 if (picturePixels % NUM_TWO == 0) {
58 opts.size.width = picturePixels / NUM_TWO;
59 opts.size.height = NUM_TWO;
60 } else {
61 opts.size.width = picturePixels;
62 opts.size.height = 1;
63 }
64 auto pixelMap = PixelMap::Create(reinterpret_cast<const uint32_t*>(data),
65 static_cast<uint32_t>(picturePixels) * FOUR_BYTES_PER_PIXEL, opts);
66 if (pixelMap == nullptr) {
67 return -1;
68 }
69
70 std::string pathName = "/data/local/tmp/testFile_" + GetNowTimeStr() + ".dat";
71 IMAGE_LOGI("%{public}s pathName is %{public}s", __func__, pathName.c_str());
72 std::remove(pathName.c_str());
73 int fd = open(pathName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
74 if (fd < 0) {
75 return -1;
76 }
77 ImagePacker packer;
78 PackOption option;
79 option.format = encodeFormat;
80 packer.StartPacking(fd, option);
81 packer.AddImage(*(pixelMap.get()));
82 uint32_t errorCode = packer.FinalizePacking();
83 if (errorCode != SUCCESS) {
84 return -1;
85 }
86 return fd;
87 }
88
GetNowTimeStr()89 std::string GetNowTimeStr()
90 {
91 auto now = std::chrono::system_clock::now();
92 auto us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
93 return std::to_string(us.count());
94 }
95
WriteDataToFile(const uint8_t * data,size_t size,const std::string & filename)96 bool WriteDataToFile(const uint8_t* data, size_t size, const std::string& filename)
97 {
98 IMAGE_LOGI("%{public}s %{public}s IN", __func__, filename.c_str());
99 std::ifstream file(filename);
100 bool isFileExists = file.good();
101 file.close();
102 if (isFileExists) {
103 std::remove(filename.c_str());
104 }
105 std::ofstream newFile(filename);
106 if (!newFile.is_open()) {
107 IMAGE_LOGI("%{public}s failed, new file: %{public}s error", __func__, filename.c_str());
108 return false;
109 }
110 newFile.write(reinterpret_cast<const char*>(data), size);
111 newFile.close();
112 IMAGE_LOGI("%{public}s %{public}s SUCCESS", __func__, filename.c_str());
113 return true;
114 }
115
PixelMapTest001(PixelMap * pixelMap)116 void PixelMapTest001(PixelMap* pixelMap)
117 {
118 IMAGE_LOGI("%{public}s IN", __func__);
119 pixelMap->SetTransformered(pixelMap->isTransformered_);
120 ImageInfo info;
121 pixelMap->GetYUVByteCount(info);
122 pixelMap->GetAllocatedByteCount(info);
123 InitializationOptions initOpts;
124 initOpts.size = {pixelMap->GetWidth(), pixelMap->GetHeight()};
125 initOpts.editable = true;
126 auto emptyPixelMap = PixelMap::Create(initOpts);
127 Rect srcRect;
128 int32_t errorCode;
129 PixelMap::Create(*(emptyPixelMap.get()), srcRect, initOpts, errorCode);
130 pixelMap->resize(1, 1);
131 pixelMap->CopyPixelMap(*pixelMap, *(emptyPixelMap.get()));
132 PixelFormat fromat = PixelFormat::RGBA_8888;
133 pixelMap->GetPixelFormatDetail(fromat);
134 fromat = PixelFormat::RGBA_1010102;
135 pixelMap->GetPixelFormatDetail(fromat);
136 fromat = PixelFormat::BGRA_8888;
137 pixelMap->GetPixelFormatDetail(fromat);
138 fromat = PixelFormat::ARGB_8888;
139 pixelMap->GetPixelFormatDetail(fromat);
140 fromat = PixelFormat::ALPHA_8;
141 pixelMap->GetPixelFormatDetail(fromat);
142 fromat = PixelFormat::RGB_565;
143 pixelMap->GetPixelFormatDetail(fromat);
144 fromat = PixelFormat::RGB_888;
145 pixelMap->GetPixelFormatDetail(fromat);
146 fromat = PixelFormat::NV21;
147 pixelMap->GetPixelFormatDetail(fromat);
148 fromat = PixelFormat::YCRCB_P010;
149 pixelMap->GetPixelFormatDetail(fromat);
150 fromat = PixelFormat::CMYK;
151 pixelMap->GetPixelFormatDetail(fromat);
152 fromat = PixelFormat::RGBA_F16;
153 pixelMap->GetPixelFormatDetail(fromat);
154 fromat = PixelFormat::ASTC_4x4;
155 pixelMap->GetPixelFormatDetail(fromat);
156 pixelMap->GetPixel8(0, 0);
157 pixelMap->GetPixel16(0, 0);
158 pixelMap->GetPixel32(0, 0);
159 pixelMap->GetPixel(0, 0);
160 uint32_t color = 0;
161 pixelMap->GetARGB32Color(0, 0, color);
162 IMAGE_LOGI("%{public}s SUCCESS", __func__);
163 }
164
PixelMapTest002(PixelMap * pixelMap)165 void PixelMapTest002(PixelMap* pixelMap)
166 {
167 IMAGE_LOGI("%{public}s IN", __func__);
168 pixelMap->GetPixelBytes();
169 pixelMap->GetRowBytes();
170 pixelMap->GetByteCount();
171 pixelMap->GetWidth();
172 pixelMap->GetHeight();
173 TransformData transformData;
174 pixelMap->GetTransformData(transformData);
175 pixelMap->SetTransformData(transformData);
176 pixelMap->GetBaseDensity();
177 ImageInfo imageInfo;
178 pixelMap->GetImageInfo(imageInfo);
179 pixelMap->GetPixelFormat();
180 pixelMap->GetColorSpace();
181 pixelMap->GetAlphaType();
182 pixelMap->GetPixels();
183 pixelMap->IsHdr();
184 uint32_t color = 0;
185 pixelMap->GetARGB32ColorA(color);
186 pixelMap->GetARGB32ColorR(color);
187 pixelMap->GetARGB32ColorG(color);
188 pixelMap->GetARGB32ColorB(color);
189 pixelMap->IsStrideAlignment();
190 pixelMap->GetAllocatorType();
191 pixelMap->GetFd();
192 #ifdef IMAGE_COLORSPACE_FLAG
193 OHOS::ColorManager::ColorSpace grColorSpace(OHOS::ColorManager::ColorSpaceName::SRGB);
194 pixelMap->ApplyColorSpace(grColorSpace);
195 #endif
196 pixelMap->ResetConfig(pixelMap->imageInfo_.size, pixelMap->imageInfo_.pixelFormat);
197 IMAGE_LOGI("%{public}s SUCCESS", __func__);
198 }
199
PixelYuvTest001(PixelMap * pixelMap)200 void PixelYuvTest001(PixelMap* pixelMap)
201 {
202 IMAGE_LOGI("%{public}s IN", __func__);
203 if (pixelMap == nullptr) {
204 IMAGE_LOGE("pixelMap is nullptr");
205 return;
206 }
207 auto pixelYuv = reinterpret_cast<PixelYuv*>(pixelMap);
208 pixelYuv->resize(0.1f, 0.1f);
209 Rect rect = {};
210 rect.top = 0;
211 rect.left = 0;
212 int32_t numTwo = 2;
213 rect.width = numTwo;
214 rect.height = numTwo;
215 pixelYuv->crop(rect);
216 pixelYuv->scale(0.5f, 0.5f);
217 pixelYuv->resize(0.5f, 0.5f);
218 int32_t dstW = pixelYuv->GetWidth() * 0.5f;
219 int32_t dstH = pixelYuv->GetHeight() * 0.5f;
220 pixelYuv->scale(dstW, dstH);
221 pixelYuv->resize(dstW, dstH);
222 pixelYuv->flip(false, false);
223 pixelYuv->flip(true, false);
224 pixelYuv->flip(true, true);
225 pixelYuv->flip(false, true);
226 pixelYuv->translate(0.5f, 0.5f);
227 pixelYuv->rotate(0.2f);
228 #ifdef IMAGE_COLORSPACE_FLAG
229 OHOS::ColorManager::ColorSpace grColorSpace(OHOS::ColorManager::ColorSpaceName::DISPLAY_P3);
230 pixelYuv->ApplyColorSpace(grColorSpace);
231 #endif
232 pixelYuv->GetPixel8(numTwo, numTwo);
233 pixelYuv->GetPixel16(numTwo, numTwo);
234 pixelYuv->GetPixel32(numTwo, numTwo);
235 uint32_t color = 0;
236 pixelYuv->GetARGB32Color(numTwo, numTwo, color);
237 pixelYuv->GetARGB32ColorA(color);
238 pixelYuv->GetARGB32ColorR(color);
239 pixelYuv->GetARGB32ColorG(color);
240 pixelYuv->GetARGB32ColorB(color);
241 pixelYuv->SetAlpha(0.5f);
242 pixelYuv->getPixelBytesNumber();
243 pixelYuv->GetByteCount();
244 pixelYuv->translate(0.5f, 0.5f);
245 IMAGE_LOGI("%{public}s SUCCESS", __func__);
246 }
247
PixelYuvTest002(PixelMap * pixelMap)248 void PixelYuvTest002(PixelMap* pixelMap)
249 {
250 IMAGE_LOGI("%{public}s IN", __func__);
251 if (pixelMap == nullptr) {
252 IMAGE_LOGE("pixelMap is nullptr");
253 return;
254 }
255 auto pixelYuv = reinterpret_cast<PixelYuv*>(pixelMap);
256 const uint64_t bufferSize = static_cast<uint64_t>(pixelYuv->GetByteCount());
257 auto srcBuffer = std::make_unique<uint8_t[]>(bufferSize);
258 uint8_t* srcPtr = srcBuffer.get();
259 const uint32_t offset = 0;
260 const uint32_t stride = 0;
261 const Rect region;
262 const Position pos;
263 pixelYuv->WritePixels(srcPtr, bufferSize, offset, stride, region);
264 pixelYuv->WritePixels(srcPtr, bufferSize);
265 pixelYuv->ReadPixels(bufferSize, offset, stride, region, srcPtr);
266 pixelYuv->ReadPixels(bufferSize, srcPtr);
267 pixelYuv->translate(0.5f, 0.5f);
268 uint32_t dstPixel = 0;
269 pixelYuv->ReadPixel(pos, dstPixel);
270 const uint32_t color = 0;
271 pixelYuv->WritePixels(color);
272 pixelYuv->WritePixel(pos, dstPixel);
273 IMAGE_LOGI("%{public}s SUCCESS", __func__);
274 }
275
SetFdpDecodeOptions(FuzzedDataProvider * fdp,OHOS::Media::DecodeOptions & decodeOpts)276 void SetFdpDecodeOptions(FuzzedDataProvider* fdp, OHOS::Media::DecodeOptions &decodeOpts)
277 {
278 decodeOpts.fitDensity = fdp->ConsumeIntegral<int32_t>();
279 decodeOpts.CropRect.left = fdp->ConsumeIntegral<int32_t>();
280 decodeOpts.CropRect.top = fdp->ConsumeIntegral<int32_t>();
281 decodeOpts.CropRect.width = fdp->ConsumeIntegral<int32_t>();
282 decodeOpts.CropRect.height = fdp->ConsumeIntegral<int32_t>();
283 decodeOpts.desiredSize.width = fdp->ConsumeIntegralInRange<uint16_t>(0, 0xfff);
284 decodeOpts.desiredSize.height = fdp->ConsumeIntegralInRange<uint16_t>(0, 0xfff);
285 decodeOpts.desiredRegion.left = fdp->ConsumeIntegral<int32_t>();
286 decodeOpts.desiredRegion.top = fdp->ConsumeIntegral<int32_t>();
287 decodeOpts.desiredRegion.width = fdp->ConsumeIntegral<int32_t>();
288 decodeOpts.desiredRegion.height = fdp->ConsumeIntegral<int32_t>();
289 decodeOpts.rotateDegrees = fdp->ConsumeFloatingPoint<float>();
290 decodeOpts.rotateNewDegrees = fdp->ConsumeIntegral<uint32_t>();
291 decodeOpts.sampleSize = fdp->ConsumeIntegral<uint32_t>();
292 decodeOpts.desiredPixelFormat = static_cast<PixelFormat>(fdp->ConsumeIntegral<uint8_t>() % PIXELFORMAT_MODULO);
293 decodeOpts.photoDesiredPixelFormat = static_cast<PixelFormat>(fdp->ConsumeIntegral<uint8_t>() % PIXELFORMAT_MODULO);
294 decodeOpts.desiredColorSpace = static_cast<ColorSpace>(fdp->ConsumeIntegral<uint8_t>() % COLORSPACE_MODULO);
295 decodeOpts.allowPartialImage = fdp->ConsumeBool();
296 decodeOpts.editable = fdp->ConsumeBool();
297 decodeOpts.preference = static_cast<MemoryUsagePreference>(fdp->ConsumeBool());
298 decodeOpts.fastAstc = fdp->ConsumeBool();
299 decodeOpts.invokeType = fdp->ConsumeIntegral<uint16_t>();
300 decodeOpts.desiredDynamicRange =
301 static_cast<DecodeDynamicRange>(fdp->ConsumeIntegral<uint8_t>() % DYNAMICRANGE_MODULO);
302 decodeOpts.resolutionQuality = static_cast<ResolutionQuality>(fdp->ConsumeIntegral<uint8_t>() % RESOLUTION_MODULO);
303 decodeOpts.isAisr = fdp->ConsumeBool();
304 decodeOpts.isAppUseAllocator = fdp->ConsumeBool();
305 decodeOpts.allocatorType = static_cast<AllocatorType>(fdp->ConsumeIntegral<uint32_t>() % ALLOCATORTYPE_MODULO);
306 }
307
SetFdpPixelDecodeOptions(FuzzedDataProvider * fdp,OHOS::ImagePlugin::PixelDecodeOptions & plOpts)308 void SetFdpPixelDecodeOptions(FuzzedDataProvider* fdp, OHOS::ImagePlugin::PixelDecodeOptions &plOpts)
309 {
310 plOpts.CropRect.left = fdp->ConsumeIntegral<int32_t>();
311 plOpts.CropRect.top = fdp->ConsumeIntegral<int32_t>();
312 plOpts.CropRect.width = fdp->ConsumeIntegral<int32_t>();
313 plOpts.CropRect.height = fdp->ConsumeIntegral<int32_t>();
314 plOpts.desiredSize.width = fdp->ConsumeIntegralInRange<uint16_t>(0, 0xfff);
315 plOpts.desiredSize.height = fdp->ConsumeIntegralInRange<uint16_t>(0, 0xfff);
316 plOpts.rotateDegrees = fdp->ConsumeFloatingPoint<float>();
317 plOpts.sampleSize = fdp->ConsumeIntegral<uint32_t>();
318 plOpts.desiredPixelFormat =
319 static_cast<OHOS::Media::PixelFormat>(fdp->ConsumeIntegral<uint8_t>() % PIXELFORMAT_MODULO);
320 plOpts.desiredColorSpace =
321 static_cast<OHOS::Media::ColorSpace>(fdp->ConsumeIntegral<uint8_t>() % COLORSPACE_MODULO);
322 plOpts.desireAlphaType = static_cast<OHOS::Media::AlphaType>(fdp->ConsumeIntegral<uint8_t>() % ALPHATYPE_MODULO);
323 plOpts.allowPartialImage = fdp->ConsumeBool();
324 plOpts.editable = fdp->ConsumeBool();
325 }
326