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 "pixelmap_helper.h"
17 #include "common_utils.h"
18 #include <multimedia/image_framework/image/image_source_native.h>
19 #include <multimedia/image_framework/image/image_packer_native.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <hilog/log.h>
23
24 #define IMAGE_EFFECT_HELPER_100 100
25
CreateDecodingOptions()26 std::shared_ptr<OH_DecodingOptions> CreateDecodingOptions()
27 {
28 OH_DecodingOptions *options = nullptr;
29 Image_ErrorCode errorCode = OH_DecodingOptions_Create(&options);
30 std::shared_ptr<OH_DecodingOptions> optionsPtr(options, [](OH_DecodingOptions *options) {
31 OH_DecodingOptions_Release(options);
32 });
33
34 return optionsPtr;
35 }
36
Decode(std::string & pathName)37 std::shared_ptr<OH_PixelmapNative> PixelMapHelper::Decode(std::string &pathName)
38 {
39 int fd = open(pathName.c_str(), O_RDWR);
40 std::shared_ptr<int> fdPtr(&fd, [](int *fd) { close(*fd); });
41 OH_ImageSourceNative *imageSource = nullptr;
42 Image_ErrorCode errorCode = OH_ImageSourceNative_CreateFromFd(*fdPtr.get(), &imageSource);
43
44 std::shared_ptr<OH_ImageSourceNative> imageSourcePtr(imageSource, [](OH_ImageSourceNative *imageSource) {
45 OH_ImageSourceNative_Release(imageSource);
46 });
47
48 std::shared_ptr<OH_DecodingOptions> optionsPtr = CreateDecodingOptions();
49 OH_PixelmapNative *pixelmap = nullptr;
50 errorCode = OH_ImageSourceNative_CreatePixelmap(imageSourcePtr.get(), optionsPtr.get(), &pixelmap);
51
52 std::shared_ptr<OH_PixelmapNative> pixelmapPtr(pixelmap, [](OH_PixelmapNative *pixelmap) {
53 OH_PixelmapNative_Release(pixelmap);
54 });
55
56 return pixelmapPtr;
57 }
58
CreatePackingOptions()59 std::shared_ptr<OH_PackingOptions> CreatePackingOptions()
60 {
61 OH_PackingOptions *options = nullptr;
62 Image_ErrorCode errorCode = OH_PackingOptions_Create(&options);
63
64 std::shared_ptr<OH_PackingOptions> optionsPtr(options, [](OH_PackingOptions *options) {
65 OH_PackingOptions_Release(options);
66 });
67
68 OH_PackingOptions_SetQuality(optionsPtr.get(), IMAGE_EFFECT_HELPER_100);
69 Image_MimeType format = { .data = const_cast<char *>(MIME_TYPE_JPEG), .size = strlen(MIME_TYPE_JPEG) };
70 OH_PackingOptions_SetMimeType(optionsPtr.get(), &format);
71 return optionsPtr;
72 }
73
Encode(OH_PixelmapNative * pixelmap,std::string & path)74 bool PixelMapHelper::Encode(OH_PixelmapNative *pixelmap, std::string &path)
75 {
76 OH_ImagePackerNative *imagePacker = nullptr;
77 Image_ErrorCode errorCode = OH_ImagePackerNative_Create(&imagePacker);
78 std::shared_ptr<OH_ImagePackerNative> imagePackerPtr(imagePacker, [](OH_ImagePackerNative *imagePacker) {
79 OH_ImagePackerNative_Release(imagePacker);
80 });
81
82 std::shared_ptr<OH_PackingOptions> optionsPtr = CreatePackingOptions();
83
84 int fd = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
85 errorCode = OH_ImagePackerNative_PackToFileFromPixelmap(imagePackerPtr.get(), optionsPtr.get(), pixelmap, fd);
86 close(fd);
87
88 return true;
89 }