• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 constexpr int EXPECTED_ONE_HUNDRED = 100;
24 
CreateDecodingOptions()25 std::shared_ptr<OH_DecodingOptions> CreateDecodingOptions()
26 {
27     OH_DecodingOptions *options = nullptr;
28     Image_ErrorCode errorCode = OH_DecodingOptions_Create(&options);
29     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, nullptr,
30         "OH_DecodingOptions_Create fail! errorCode=%{public}d", errorCode);
31 
32     std::shared_ptr<OH_DecodingOptions> optionsPtr(options, [](OH_DecodingOptions *options) {
33         OH_DecodingOptions_Release(options);
34     });
35 
36     return optionsPtr;
37 }
38 
Decode(std::string & pathName)39 std::shared_ptr<OH_PixelmapNative> PixelMapHelper::Decode(std::string &pathName)
40 {
41     int fd = open(pathName.c_str(), O_RDWR);
42     CHECK_AND_RETURN_RET_LOG(fd != -1, nullptr, "Open path fail! pathName=%{public}s", pathName.c_str());
43 
44     std::shared_ptr<int> fdPtr(&fd, [](int *fd) { close(*fd); });
45     OH_ImageSourceNative *imageSource = nullptr;
46     Image_ErrorCode errorCode = OH_ImageSourceNative_CreateFromFd(*fdPtr.get(), &imageSource);
47     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, nullptr,
48         "OH_ImageSourceNative_CreateFromFd fail! errorCode=%{public}d", errorCode);
49 
50     std::shared_ptr<OH_ImageSourceNative> imageSourcePtr(imageSource, [](OH_ImageSourceNative *imageSource) {
51         OH_ImageSourceNative_Release(imageSource);
52     });
53 
54     std::shared_ptr<OH_DecodingOptions> optionsPtr = CreateDecodingOptions();
55     CHECK_AND_RETURN_RET_LOG(optionsPtr != nullptr, nullptr, "CreateDecodingOptions fail!");
56 
57     OH_PixelmapNative *pixelmap = nullptr;
58     errorCode = OH_ImageSourceNative_CreatePixelmap(imageSourcePtr.get(), optionsPtr.get(), &pixelmap);
59     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, nullptr,
60         "OH_ImageSourceNative_CreatePixelmap fail! errorCode=%{public}d", errorCode);
61 
62     std::shared_ptr<OH_PixelmapNative> pixelmapPtr(pixelmap, [](OH_PixelmapNative *pixelmap) {
63         OH_PixelmapNative_Release(pixelmap);
64     });
65 
66     return pixelmapPtr;
67 }
68 
CreatePackingOptions()69 std::shared_ptr<OH_PackingOptions> CreatePackingOptions()
70 {
71     OH_PackingOptions *options = nullptr;
72     Image_ErrorCode errorCode = OH_PackingOptions_Create(&options);
73     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, nullptr,
74         "OH_PackingOptions_Create fail! errorCode=%{public}d", errorCode);
75 
76     std::shared_ptr<OH_PackingOptions> optionsPtr(options, [](OH_PackingOptions *options) {
77         OH_PackingOptions_Release(options);
78     });
79 
80     OH_PackingOptions_SetQuality(optionsPtr.get(), EXPECTED_ONE_HUNDRED);
81     Image_MimeType format = { .data = const_cast<char *>(MIME_TYPE_JPEG), .size = strlen(MIME_TYPE_JPEG) };
82     OH_PackingOptions_SetMimeType(optionsPtr.get(), &format);
83     return optionsPtr;
84 }
85 
Encode(OH_PixelmapNative * pixelmap,std::string & path)86 bool PixelMapHelper::Encode(OH_PixelmapNative *pixelmap, std::string &path)
87 {
88     OH_ImagePackerNative *imagePacker = nullptr;
89     Image_ErrorCode errorCode = OH_ImagePackerNative_Create(&imagePacker);
90     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, false,
91         "OH_ImagePackerNative_Create fail! errorCode=%{public}d", errorCode);
92 
93     std::shared_ptr<OH_ImagePackerNative> imagePackerPtr(imagePacker, [](OH_ImagePackerNative *imagePacker) {
94         OH_ImagePackerNative_Release(imagePacker);
95     });
96 
97     std::shared_ptr<OH_PackingOptions> optionsPtr = CreatePackingOptions();
98     CHECK_AND_RETURN_RET_LOG(optionsPtr != nullptr, false, "CreatePackingOptions fail!");
99 
100     int fd = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
101     CHECK_AND_RETURN_RET_LOG(fd != -1, false, "Open path fail! path=%{public}s", path.c_str());
102     errorCode = OH_ImagePackerNative_PackToFileFromPixelmap(imagePackerPtr.get(), optionsPtr.get(), pixelmap, fd);
103     close(fd);
104     CHECK_AND_RETURN_RET_LOG(errorCode == Image_ErrorCode::IMAGE_SUCCESS, false,
105         "OH_ImagePackerNative_PackToFileFromPixelmap fail! errorCode=%{public}d", errorCode);
106 
107     return true;
108 }