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_packer_native.h"
17
18 #include "common_utils.h"
19 #include "image_packer.h"
20 #include "image_packer_native_impl.h"
21 #include "image_source_native_impl.h"
22 #include "pixelmap_native_impl.h"
23 #ifndef _WIN32
24 #include "securec.h"
25 #else
26 #include "memory.h"
27 #endif
28
29 using namespace OHOS;
30 using namespace Media;
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 constexpr size_t SIZE_ZERO = 0;
36 constexpr int32_t IMAGE_BASE = 62980096;
37 constexpr int32_t MASK_2 = 0x3;
38 constexpr int32_t MASK_16 = 0xffff;
39 static constexpr int32_t IMAGE_BASE_19 = 19;
40 static constexpr int32_t IMAGE_BASE_16 = 16;
41 static constexpr int32_t IMAGE_BASE_17 = 17;
42 static constexpr int32_t IMAGE_BASE_26 = 26;
43 static constexpr int32_t IMAGE_BASE_31 = 31;
44 static constexpr int32_t IMAGE_BASE_152 = 152;
45 static constexpr int32_t IMAGE_BASE_27 = 27;
46 static constexpr int32_t IMAGE_BASE_12 = 12;
47 static constexpr int32_t IMAGE_BASE_13 = 13;
48 static constexpr int32_t IMAGE_BASE_6 = 6;
49 static constexpr int32_t IMAGE_BASE_14 = 14;
50 static constexpr int32_t IMAGE_BASE_4 = 4;
51 static constexpr int32_t IMAGE_BASE_9 = 9;
52 static constexpr int32_t IMAGE_BASE_20 = 20;
53 static constexpr int32_t IMAGE_BASE_22 = 22;
54 static constexpr int32_t IMAGE_BASE_23 = 23;
55
56 struct OH_PackingOptions {
57 Image_MimeType mimeType;
58 int quality;
59 int32_t desiredDynamicRange = IMAGE_PACKER_DYNAMIC_RANGE_SDR;
60 uint16_t loop;
61 uint16_t* delayTimes;
62 uint32_t delayTimesSize;
63 uint16_t* disposalTypes;
64 uint32_t disposalTypesSize;
65 bool needsPackProperties = false;
66 };
67
68 struct OH_PackingOptionsForSequence {
69 int32_t frameCount;
70 int32_t* delayTimeList;
71 size_t delayTimeListLength;
72 uint32_t* disposalTypes;
73 size_t disposalTypesLength;
74 uint32_t loopCount = 1;
75 };
76
ToNewErrorCode(int code)77 static Image_ErrorCode ToNewErrorCode(int code)
78 {
79 switch (code) {
80 case 0:
81 return IMAGE_SUCCESS;
82 case IMAGE_BASE + IMAGE_BASE_19:
83 return IMAGE_BAD_PARAMETER;
84 case IMAGE_BASE + IMAGE_BASE_16:
85 case IMAGE_BASE + IMAGE_BASE_17:
86 case IMAGE_BASE + IMAGE_BASE_26:
87 return IMAGE_UNKNOWN_MIME_TYPE;
88 case IMAGE_BASE + IMAGE_BASE_31:
89 return IMAGE_TOO_LARGE;
90 case IMAGE_BASE + IMAGE_BASE_152:
91 return IMAGE_UNSUPPORTED_OPERATION;
92 case IMAGE_BASE + IMAGE_BASE_27:
93 return IMAGE_UNSUPPORTED_METADATA;
94 case IMAGE_BASE + IMAGE_BASE_12:
95 return IMAGE_UNSUPPORTED_CONVERSION;
96 case IMAGE_BASE + IMAGE_BASE_13:
97 return IMAGE_INVALID_REGION;
98 case IMAGE_BASE + IMAGE_BASE_6:
99 return IMAGE_ALLOC_FAILED;
100 case IMAGE_BASE + IMAGE_BASE_14:
101 return IMAGE_BAD_SOURCE;
102 case IMAGE_BASE + IMAGE_BASE_4:
103 case IMAGE_BASE + IMAGE_BASE_9:
104 case IMAGE_BASE + IMAGE_BASE_20:
105 case IMAGE_BASE + IMAGE_BASE_22:
106 return IMAGE_DECODE_FAILED;
107 case IMAGE_BASE + IMAGE_BASE_23:
108 return IMAGE_ENCODE_FAILED;
109 default:
110 return IMAGE_UNKNOWN_ERROR;
111 }
112 };
113
ParseDynamicRange(int32_t val)114 static EncodeDynamicRange ParseDynamicRange(int32_t val)
115 {
116 if (val <= static_cast<int32_t>(EncodeDynamicRange::SDR)) {
117 return EncodeDynamicRange(val);
118 }
119
120 return EncodeDynamicRange::SDR;
121 }
122
CopyPackingOptions(const OH_PackingOptions * options,PackOption & packOption)123 static Image_ErrorCode CopyPackingOptions(const OH_PackingOptions *options, PackOption &packOption)
124 {
125 if (options == nullptr) {
126 return IMAGE_BAD_PARAMETER;
127 }
128
129 std::string format(options->mimeType.data, options->mimeType.size);
130 if (format.empty()) {
131 return IMAGE_BAD_PARAMETER;
132 }
133 packOption.format = format;
134 packOption.quality = options->quality;
135 packOption.needsPackProperties = options->needsPackProperties;
136 packOption.desiredDynamicRange = ParseDynamicRange(options->desiredDynamicRange);
137 return IMAGE_SUCCESS;
138 }
139
140 MIDK_EXPORT
OH_PackingOptions_Create(OH_PackingOptions ** options)141 Image_ErrorCode OH_PackingOptions_Create(OH_PackingOptions **options)
142 {
143 *options = new OH_PackingOptions();
144 if (*options == nullptr) {
145 return IMAGE_BAD_PARAMETER;
146 }
147 return IMAGE_SUCCESS;
148 }
149
150 MIDK_EXPORT
OH_PackingOptions_GetMimeType(OH_PackingOptions * options,Image_MimeType * format)151 Image_ErrorCode OH_PackingOptions_GetMimeType(OH_PackingOptions *options,
152 Image_MimeType *format)
153 {
154 if (options == nullptr || format == nullptr) {
155 return IMAGE_BAD_PARAMETER;
156 }
157
158 if (format->size != SIZE_ZERO && format->size < options->mimeType.size) {
159 return IMAGE_BAD_PARAMETER;
160 }
161
162 format->size = (format->size == SIZE_ZERO) ? options->mimeType.size : format->size;
163 format->data = static_cast<char *>(malloc(format->size));
164 if (format->data == nullptr) {
165 return IMAGE_ALLOC_FAILED;
166 }
167
168 if (memcpy_s(format->data, format->size, options->mimeType.data, options->mimeType.size) != 0) {
169 free(format->data);
170 format->data = nullptr;
171 format->size = 0;
172 return IMAGE_COPY_FAILED;
173 }
174 return IMAGE_SUCCESS;
175 }
176
177 MIDK_EXPORT
OH_PackingOptions_SetMimeType(OH_PackingOptions * options,Image_MimeType * format)178 Image_ErrorCode OH_PackingOptions_SetMimeType(OH_PackingOptions *options,
179 Image_MimeType *format)
180 {
181 if (options == nullptr || format->data == nullptr || format->size == 0) {
182 return IMAGE_BAD_PARAMETER;
183 }
184 if (options->mimeType.data != nullptr) {
185 free(options->mimeType.data);
186 options->mimeType.data = nullptr;
187 }
188 options->mimeType.size = format->size;
189 options->mimeType.data = static_cast<char *>(malloc(options->mimeType.size));
190 if (options->mimeType.data == nullptr) {
191 return IMAGE_ALLOC_FAILED;
192 }
193 if (memcpy_s(options->mimeType.data, options->mimeType.size, format->data, format->size) != 0) {
194 free(options->mimeType.data);
195 options->mimeType.data = nullptr;
196 options->mimeType.size = 0;
197 return IMAGE_COPY_FAILED;
198 }
199 return IMAGE_SUCCESS;
200 }
201
202 MIDK_EXPORT
OH_PackingOptions_GetQuality(OH_PackingOptions * options,uint32_t * quality)203 Image_ErrorCode OH_PackingOptions_GetQuality(OH_PackingOptions *options, uint32_t *quality)
204 {
205 if (options == nullptr || quality == nullptr) {
206 return IMAGE_BAD_PARAMETER;
207 }
208 *quality = options->quality;
209 return IMAGE_SUCCESS;
210 }
211
212 MIDK_EXPORT
OH_PackingOptions_SetQuality(OH_PackingOptions * options,uint32_t quality)213 Image_ErrorCode OH_PackingOptions_SetQuality(OH_PackingOptions *options, uint32_t quality)
214 {
215 if (options == nullptr) {
216 return IMAGE_BAD_PARAMETER;
217 }
218 options->quality = static_cast<int>(quality);
219 return IMAGE_SUCCESS;
220 }
221
222 MIDK_EXPORT
OH_PackingOptions_GetNeedsPackProperties(OH_PackingOptions * options,bool * needsPackProperties)223 Image_ErrorCode OH_PackingOptions_GetNeedsPackProperties(OH_PackingOptions *options, bool *needsPackProperties)
224 {
225 if (options == nullptr || needsPackProperties == nullptr) {
226 return IMAGE_BAD_PARAMETER;
227 }
228 *needsPackProperties = options->needsPackProperties;
229 return IMAGE_SUCCESS;
230 }
231
232 MIDK_EXPORT
OH_PackingOptions_SetNeedsPackProperties(OH_PackingOptions * options,bool needsPackProperties)233 Image_ErrorCode OH_PackingOptions_SetNeedsPackProperties(OH_PackingOptions *options, bool needsPackProperties)
234 {
235 if (options == nullptr) {
236 return IMAGE_BAD_PARAMETER;
237 }
238 options->needsPackProperties = needsPackProperties;
239 return IMAGE_SUCCESS;
240 }
241
242 MIDK_EXPORT
OH_PackingOptions_GetDesiredDynamicRange(OH_PackingOptions * options,int32_t * desiredDynamicRange)243 Image_ErrorCode OH_PackingOptions_GetDesiredDynamicRange(OH_PackingOptions *options, int32_t* desiredDynamicRange)
244 {
245 if (options == nullptr || desiredDynamicRange == nullptr) {
246 return IMAGE_BAD_PARAMETER;
247 }
248 *desiredDynamicRange = options->desiredDynamicRange;
249 return IMAGE_SUCCESS;
250 }
251
252 MIDK_EXPORT
OH_PackingOptions_SetDesiredDynamicRange(OH_PackingOptions * options,int32_t desiredDynamicRange)253 Image_ErrorCode OH_PackingOptions_SetDesiredDynamicRange(OH_PackingOptions *options, int32_t desiredDynamicRange)
254 {
255 if (options == nullptr) {
256 return IMAGE_BAD_PARAMETER;
257 }
258 options->desiredDynamicRange = desiredDynamicRange;
259 return IMAGE_SUCCESS;
260 }
261
262 MIDK_EXPORT
OH_PackingOptions_SetLoop(OH_PackingOptions * options,uint16_t loop)263 Image_ErrorCode OH_PackingOptions_SetLoop(OH_PackingOptions *options, uint16_t loop)
264 {
265 if (options == nullptr) {
266 return IMAGE_BAD_PARAMETER;
267 }
268 options->loop = loop;
269 return IMAGE_SUCCESS;
270 }
271
272 MIDK_EXPORT
OH_PackingOptions_GetLoop(OH_PackingOptions * options,uint16_t * loop)273 Image_ErrorCode OH_PackingOptions_GetLoop(OH_PackingOptions *options, uint16_t* loop)
274 {
275 if (options == nullptr || loop == nullptr) {
276 return IMAGE_BAD_PARAMETER;
277 }
278 *loop = options->loop;
279 return IMAGE_SUCCESS;
280 }
281
282 MIDK_EXPORT
OH_PackingOptions_SetDelayTimes(OH_PackingOptions * options,uint16_t * delayTimes,uint32_t delayTimesSize)283 Image_ErrorCode OH_PackingOptions_SetDelayTimes(OH_PackingOptions *options, uint16_t* delayTimes,
284 uint32_t delayTimesSize)
285 {
286 if (options == nullptr) {
287 return IMAGE_BAD_PARAMETER;
288 }
289 options->delayTimes = delayTimes;
290 options->delayTimesSize = delayTimesSize;
291 return IMAGE_SUCCESS;
292 }
293
294 MIDK_EXPORT
OH_PackingOptions_GetDelayTimes(OH_PackingOptions * options,uint16_t * delayTimes,uint32_t * delayTimesSize)295 Image_ErrorCode OH_PackingOptions_GetDelayTimes(OH_PackingOptions *options, uint16_t* delayTimes,
296 uint32_t* delayTimesSize)
297 {
298 if (options == nullptr || delayTimes == nullptr || delayTimesSize == nullptr) {
299 return IMAGE_BAD_PARAMETER;
300 }
301 delayTimes = options->delayTimes;
302 *delayTimesSize = options->delayTimesSize;
303 return IMAGE_SUCCESS;
304 }
305
306 MIDK_EXPORT
OH_PackingOptions_SetDisposalTypes(OH_PackingOptions * options,uint16_t * disposalTypes,uint32_t disposalTypesSize)307 Image_ErrorCode OH_PackingOptions_SetDisposalTypes(OH_PackingOptions *options, uint16_t* disposalTypes,
308 uint32_t disposalTypesSize)
309 {
310 if (options == nullptr) {
311 return IMAGE_BAD_PARAMETER;
312 }
313 options->disposalTypes = disposalTypes;
314 options->disposalTypesSize = disposalTypesSize;
315 return IMAGE_SUCCESS;
316 }
317
318 MIDK_EXPORT
OH_PackingOptions_GetDisposalTypes(OH_PackingOptions * options,uint16_t * disposalTypes,uint32_t * disposalTypesSize)319 Image_ErrorCode OH_PackingOptions_GetDisposalTypes(OH_PackingOptions *options, uint16_t* disposalTypes,
320 uint32_t* disposalTypesSize)
321 {
322 if (options == nullptr || disposalTypes == nullptr || disposalTypesSize == nullptr) {
323 return IMAGE_BAD_PARAMETER;
324 }
325 disposalTypes = options->disposalTypes;
326 *disposalTypesSize = options->disposalTypesSize;
327 return IMAGE_SUCCESS;
328 }
329
330 MIDK_EXPORT
OH_PackingOptions_Release(OH_PackingOptions * options)331 Image_ErrorCode OH_PackingOptions_Release(OH_PackingOptions *options)
332 {
333 if (options == nullptr) {
334 return IMAGE_BAD_PARAMETER;
335 }
336 if (options->mimeType.data) {
337 free(options->mimeType.data);
338 options->mimeType.data = nullptr;
339 }
340 delete options;
341 return IMAGE_SUCCESS;
342 }
343
344 MIDK_EXPORT
OH_PackingOptionsForSequence_Create(OH_PackingOptionsForSequence ** options)345 Image_ErrorCode OH_PackingOptionsForSequence_Create(OH_PackingOptionsForSequence **options)
346 {
347 *options = new OH_PackingOptionsForSequence();
348 if (*options == nullptr) {
349 return IMAGE_BAD_PARAMETER;
350 }
351 return IMAGE_SUCCESS;
352 }
353
354 MIDK_EXPORT
OH_PackingOptionsForSequence_SetFrameCount(OH_PackingOptionsForSequence * options,uint32_t frameCount)355 Image_ErrorCode OH_PackingOptionsForSequence_SetFrameCount(OH_PackingOptionsForSequence *options,
356 uint32_t frameCount)
357 {
358 if (options == nullptr) {
359 return IMAGE_BAD_PARAMETER;
360 }
361 options->frameCount = static_cast<int32_t>(frameCount);
362 return IMAGE_SUCCESS;
363 }
364
365 MIDK_EXPORT
OH_PackingOptionsForSequence_GetFrameCount(OH_PackingOptionsForSequence * options,uint32_t * frameCount)366 Image_ErrorCode OH_PackingOptionsForSequence_GetFrameCount(OH_PackingOptionsForSequence *options,
367 uint32_t *frameCount)
368 {
369 if (options == nullptr || frameCount == nullptr) {
370 return IMAGE_BAD_PARAMETER;
371 }
372 *frameCount = static_cast<uint32_t>(options->frameCount);
373 return IMAGE_SUCCESS;
374 }
375
376 MIDK_EXPORT
OH_PackingOptionsForSequence_SetDelayTimeList(OH_PackingOptionsForSequence * options,int32_t * delayTimeList,size_t delayTimeListLength)377 Image_ErrorCode OH_PackingOptionsForSequence_SetDelayTimeList(OH_PackingOptionsForSequence *options,
378 int32_t *delayTimeList, size_t delayTimeListLength)
379 {
380 if (options == nullptr) {
381 return IMAGE_BAD_PARAMETER;
382 }
383 options->delayTimeList = delayTimeList;
384 options->delayTimeListLength = delayTimeListLength;
385 return IMAGE_SUCCESS;
386 }
387
388 MIDK_EXPORT
OH_PackingOptionsForSequence_GetDelayTimeList(OH_PackingOptionsForSequence * options,int32_t * delayTimeList,size_t delayTimeListLength)389 Image_ErrorCode OH_PackingOptionsForSequence_GetDelayTimeList(OH_PackingOptionsForSequence *options,
390 int32_t *delayTimeList, size_t delayTimeListLength)
391 {
392 if (options == nullptr || delayTimeList == nullptr || delayTimeListLength == 0) {
393 return IMAGE_BAD_PARAMETER;
394 }
395 size_t minDelayTimeListLength = std::min(delayTimeListLength, options->delayTimeListLength);
396 if (memcpy_s(delayTimeList, delayTimeListLength * sizeof(int32_t),
397 options->delayTimeList, minDelayTimeListLength * sizeof(int32_t)) != 0) {
398 return IMAGE_COPY_FAILED;
399 }
400 return IMAGE_SUCCESS;
401 }
402
403 MIDK_EXPORT
OH_PackingOptionsForSequence_SetDisposalTypes(OH_PackingOptionsForSequence * options,uint32_t * disposalTypes,size_t disposalTypesLength)404 Image_ErrorCode OH_PackingOptionsForSequence_SetDisposalTypes(OH_PackingOptionsForSequence *options,
405 uint32_t *disposalTypes, size_t disposalTypesLength)
406 {
407 if (options == nullptr) {
408 return IMAGE_BAD_PARAMETER;
409 }
410 options->disposalTypes = disposalTypes;
411 options->disposalTypesLength = disposalTypesLength;
412 return IMAGE_SUCCESS;
413 }
414
415 MIDK_EXPORT
OH_PackingOptionsForSequence_GetDisposalTypes(OH_PackingOptionsForSequence * options,uint32_t * disposalTypes,size_t disposalTypesLength)416 Image_ErrorCode OH_PackingOptionsForSequence_GetDisposalTypes(OH_PackingOptionsForSequence *options,
417 uint32_t *disposalTypes, size_t disposalTypesLength)
418 {
419 if (options == nullptr || disposalTypes == nullptr || disposalTypesLength == 0) {
420 return IMAGE_BAD_PARAMETER;
421 }
422 size_t minDisposalTypesLength = std::min(disposalTypesLength, options->disposalTypesLength);
423 if (memcpy_s(disposalTypes, disposalTypesLength * sizeof(uint32_t),
424 options->disposalTypes, minDisposalTypesLength * sizeof(uint32_t)) != 0) {
425 return IMAGE_COPY_FAILED;
426 }
427 return IMAGE_SUCCESS;
428 }
429
430 MIDK_EXPORT
OH_PackingOptionsForSequence_SetLoopCount(OH_PackingOptionsForSequence * options,uint32_t loopCount)431 Image_ErrorCode OH_PackingOptionsForSequence_SetLoopCount(OH_PackingOptionsForSequence *options, uint32_t loopCount)
432 {
433 if (options == nullptr) {
434 return IMAGE_BAD_PARAMETER;
435 }
436 options->loopCount = loopCount;
437 return IMAGE_SUCCESS;
438 }
439
440 MIDK_EXPORT
OH_PackingOptionsForSequence_GetLoopCount(OH_PackingOptionsForSequence * options,uint32_t * loopCount)441 Image_ErrorCode OH_PackingOptionsForSequence_GetLoopCount(OH_PackingOptionsForSequence *options, uint32_t *loopCount)
442 {
443 if (options == nullptr || loopCount == nullptr) {
444 return IMAGE_BAD_PARAMETER;
445 }
446 *loopCount = options->loopCount;
447 return IMAGE_SUCCESS;
448 }
449
450 MIDK_EXPORT
OH_PackingOptionsForSequence_Release(OH_PackingOptionsForSequence * options)451 Image_ErrorCode OH_PackingOptionsForSequence_Release(OH_PackingOptionsForSequence *options)
452 {
453 if (options == nullptr) {
454 return IMAGE_BAD_PARAMETER;
455 }
456 delete options;
457 return IMAGE_SUCCESS;
458 }
459
460 MIDK_EXPORT
OH_ImagePackerNative_Create(OH_ImagePackerNative ** imagePacker)461 Image_ErrorCode OH_ImagePackerNative_Create(OH_ImagePackerNative **imagePacker)
462 {
463 auto imagePacker2 = new OH_ImagePackerNative();
464 if (imagePacker2 == nullptr || imagePacker2->GetInnerImagePacker() == nullptr) {
465 if (imagePacker2) {
466 delete imagePacker2;
467 }
468 return IMAGE_BAD_PARAMETER;
469 }
470 *imagePacker = imagePacker2;
471 return IMAGE_SUCCESS;
472 }
473
474 MIDK_EXPORT
OH_ImagePackerNative_PackToDataFromImageSource(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_ImageSourceNative * imageSource,uint8_t * outData,size_t * size)475 Image_ErrorCode OH_ImagePackerNative_PackToDataFromImageSource(OH_ImagePackerNative *imagePacker,
476 OH_PackingOptions *options, OH_ImageSourceNative *imageSource, uint8_t *outData, size_t *size)
477 {
478 if (imagePacker == nullptr || options == nullptr || imageSource == nullptr || outData == nullptr) {
479 return IMAGE_BAD_PARAMETER;
480 }
481
482 PackOption packOption;
483 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
484 if (errorCode != IMAGE_SUCCESS) {
485 return errorCode;
486 }
487 return ToNewErrorCode(imagePacker->PackingFromImageSource(&packOption, imageSource,
488 outData, reinterpret_cast<int64_t*>(size)));
489 }
490
491 MIDK_EXPORT
OH_ImagePackerNative_PackToDataFromPixelmap(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_PixelmapNative * pixelmap,uint8_t * outData,size_t * size)492 Image_ErrorCode OH_ImagePackerNative_PackToDataFromPixelmap(OH_ImagePackerNative *imagePacker,
493 OH_PackingOptions *options, OH_PixelmapNative *pixelmap, uint8_t *outData, size_t *size)
494 {
495 if (imagePacker == nullptr || options == nullptr || pixelmap == nullptr || outData == nullptr) {
496 return IMAGE_BAD_PARAMETER;
497 }
498
499 PackOption packOption;
500 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
501 if (errorCode != IMAGE_SUCCESS) {
502 return errorCode;
503 }
504 return ToNewErrorCode(imagePacker->PackingFromPixelmap(&packOption, pixelmap, outData,
505 reinterpret_cast<int64_t*>(size)));
506 }
507
508 MIDK_EXPORT
OH_ImagePackerNative_PackToDataFromPicture(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_PictureNative * picture,uint8_t * outData,size_t * size)509 Image_ErrorCode OH_ImagePackerNative_PackToDataFromPicture(OH_ImagePackerNative *imagePacker,
510 OH_PackingOptions *options, OH_PictureNative *picture, uint8_t *outData, size_t *size)
511 {
512 if (imagePacker == nullptr || options == nullptr || picture == nullptr || outData == nullptr) {
513 return IMAGE_BAD_PARAMETER;
514 }
515
516 PackOption packOption;
517 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
518 if (errorCode != IMAGE_SUCCESS) {
519 return errorCode;
520 }
521 return ToNewErrorCode(imagePacker->PackToDataFromPicture(&packOption, picture, outData,
522 reinterpret_cast<int64_t*>(size)));
523 }
524
525 MIDK_EXPORT
OH_ImagePackerNative_PackToFileFromImageSource(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_ImageSourceNative * imageSource,int32_t fd)526 Image_ErrorCode OH_ImagePackerNative_PackToFileFromImageSource(OH_ImagePackerNative *imagePacker,
527 OH_PackingOptions *options, OH_ImageSourceNative *imageSource, int32_t fd)
528 {
529 if (imagePacker == nullptr || options == nullptr || imageSource == nullptr) {
530 return IMAGE_BAD_PARAMETER;
531 }
532
533 PackOption packOption;
534 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
535 if (errorCode != IMAGE_SUCCESS) {
536 return errorCode;
537 }
538 return ToNewErrorCode(imagePacker->PackToFileFromImageSource(&packOption, imageSource, fd));
539 }
540
541 MIDK_EXPORT
OH_ImagePackerNative_PackToFileFromPixelmap(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_PixelmapNative * pixelmap,int32_t fd)542 Image_ErrorCode OH_ImagePackerNative_PackToFileFromPixelmap(OH_ImagePackerNative *imagePacker,
543 OH_PackingOptions *options, OH_PixelmapNative *pixelmap, int32_t fd)
544 {
545 if (imagePacker == nullptr || options == nullptr || pixelmap == nullptr) {
546 return IMAGE_BAD_PARAMETER;
547 }
548
549 PackOption packOption;
550 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
551 if (errorCode != IMAGE_SUCCESS) {
552 return errorCode;
553 }
554 return ToNewErrorCode(imagePacker->PackToFileFromPixelmap(&packOption, pixelmap, fd));
555 }
556
557 MIDK_EXPORT
OH_ImagePackerNative_PackToFileFromPicture(OH_ImagePackerNative * imagePacker,OH_PackingOptions * options,OH_PictureNative * picture,int32_t fd)558 Image_ErrorCode OH_ImagePackerNative_PackToFileFromPicture(OH_ImagePackerNative *imagePacker,
559 OH_PackingOptions *options, OH_PictureNative *picture, int32_t fd)
560 {
561 if (imagePacker == nullptr || options == nullptr || picture == nullptr) {
562 return IMAGE_BAD_PARAMETER;
563 }
564
565 PackOption packOption;
566 Image_ErrorCode errorCode = CopyPackingOptions(options, packOption);
567 if (errorCode != IMAGE_SUCCESS) {
568 return errorCode;
569 }
570 return ToNewErrorCode(imagePacker->PackToFileFromPicture(&packOption, picture, fd));
571 }
572
HandlePackingOptionsForSequence(OH_PackingOptionsForSequence * options,PackOption * packOption)573 static bool HandlePackingOptionsForSequence(OH_PackingOptionsForSequence *options, PackOption *packOption)
574 {
575 packOption->format = "image/gif";
576 packOption->loop = static_cast<uint16_t>(options->loopCount & MASK_16);
577 for (uint32_t i = 0; i < options->delayTimeListLength; i++) {
578 if (options->delayTimeList[i] <= 0 || options->delayTimeList[i] > MASK_16) {
579 return false;
580 }
581 packOption->delayTimes.push_back(
582 static_cast<uint16_t>(options->delayTimeList[i]) & static_cast<uint16_t>(MASK_16));
583 }
584 if (options->delayTimeListLength < static_cast<size_t>(options->frameCount)) {
585 for (size_t i = options->delayTimeListLength; i < static_cast<size_t>(options->frameCount); i++) {
586 packOption->delayTimes.push_back(
587 static_cast<uint16_t>(options->delayTimeList[options->delayTimeListLength - 1]) &
588 static_cast<uint16_t>(MASK_16));
589 }
590 }
591 for (uint32_t i = 0; i < options->disposalTypesLength; i++) {
592 if (options->disposalTypes[i] > MASK_2) {
593 return false;
594 }
595 packOption->disposalTypes.push_back(options->disposalTypes[i] & MASK_2);
596 }
597 return true;
598 }
599
600 MIDK_EXPORT
OH_ImagePackerNative_PackToDataFromPixelmapSequence(OH_ImagePackerNative * imagePacker,OH_PackingOptionsForSequence * options,OH_PixelmapNative ** pixelmapSequence,size_t sequenceLength,uint8_t * outData,size_t * outDataSize)601 Image_ErrorCode OH_ImagePackerNative_PackToDataFromPixelmapSequence(OH_ImagePackerNative *imagePacker,
602 OH_PackingOptionsForSequence *options, OH_PixelmapNative **pixelmapSequence,
603 size_t sequenceLength, uint8_t *outData, size_t *outDataSize)
604 {
605 if (imagePacker == nullptr || options == nullptr || pixelmapSequence == nullptr || outData == nullptr ||
606 options->delayTimeListLength == 0 || sequenceLength == 0 || options->loopCount > MASK_16 ||
607 options->frameCount <= 0) {
608 return IMAGE_BAD_PARAMETER;
609 }
610 std::vector<OH_PixelmapNative*> pixelmaps;
611 PackOption packOption;
612 if (!HandlePackingOptionsForSequence(options, &packOption)) {
613 return IMAGE_BAD_PARAMETER;
614 }
615 if (sequenceLength >= static_cast<size_t>(options->frameCount)) {
616 for (int i = 0; i < options->frameCount; i++) {
617 pixelmaps.push_back(pixelmapSequence[i]);
618 }
619 } else {
620 for (size_t i = 0; i < sequenceLength; i++) {
621 pixelmaps.push_back(pixelmapSequence[i]);
622 }
623 for (size_t i = sequenceLength; i < static_cast<size_t>(options->frameCount); i++) {
624 pixelmaps.push_back(pixelmapSequence[sequenceLength - 1]);
625 }
626 }
627 return ToNewErrorCode(imagePacker->PackToDataMultiFrames(&packOption, pixelmaps, outData,
628 reinterpret_cast<int64_t*>(outDataSize)));
629 }
630
631 MIDK_EXPORT
OH_ImagePackerNative_PackToFileFromPixelmapSequence(OH_ImagePackerNative * imagePacker,OH_PackingOptionsForSequence * options,OH_PixelmapNative ** pixelmapSequence,size_t sequenceLength,int32_t fd)632 Image_ErrorCode OH_ImagePackerNative_PackToFileFromPixelmapSequence(OH_ImagePackerNative *imagePacker,
633 OH_PackingOptionsForSequence *options, OH_PixelmapNative **pixelmapSequence, size_t sequenceLength, int32_t fd)
634 {
635 if (imagePacker == nullptr || options == nullptr || pixelmapSequence == nullptr ||
636 options->delayTimeListLength == 0 || sequenceLength == 0 || options->loopCount > MASK_16 ||
637 options->frameCount <= 0) {
638 return IMAGE_BAD_PARAMETER;
639 }
640 std::vector<OH_PixelmapNative*> pixelmaps;
641 PackOption packOption;
642 if (!HandlePackingOptionsForSequence(options, &packOption)) {
643 return IMAGE_BAD_PARAMETER;
644 }
645 if (sequenceLength >= static_cast<size_t>(options->frameCount)) {
646 for (int i = 0; i < options->frameCount; i++) {
647 pixelmaps.push_back(pixelmapSequence[i]);
648 }
649 } else {
650 for (size_t i = 0; i < sequenceLength; i++) {
651 pixelmaps.push_back(pixelmapSequence[i]);
652 }
653 for (size_t i = sequenceLength; i < static_cast<size_t>(options->frameCount); i++) {
654 pixelmaps.push_back(pixelmapSequence[sequenceLength - 1]);
655 }
656 }
657 return ToNewErrorCode(imagePacker->PackToFileMultiFrames(&packOption, pixelmaps, fd));
658 }
659
660 MIDK_EXPORT
OH_ImagePackerNative_Release(OH_ImagePackerNative * imagePacker)661 Image_ErrorCode OH_ImagePackerNative_Release(OH_ImagePackerNative *imagePacker)
662 {
663 if (imagePacker == nullptr) {
664 return IMAGE_BAD_PARAMETER;
665 }
666 delete imagePacker;
667 return IMAGE_SUCCESS;
668 }
669
670 #ifdef __cplusplus
671 };
672 #endif