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 <fstream>
17 #include <string>
18 #include "directory_ex.h"
19 #include "hilog/log.h"
20 #include "image_packer.h"
21 #include "image_type.h"
22 #include "image_utils.h"
23 #include "log_tags.h"
24 #include "media_errors.h"
25 #include "pixel_map.h"
26 #include "image_source_util.h"
27
28 using namespace OHOS::Media;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::ImageSourceUtil;
31
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL_TEST = { LOG_CORE, LOG_TAG_DOMAIN_ID_IMAGE, "ImageSourceUtil" };
33 namespace OHOS {
34 namespace ImageSourceUtil {
35 constexpr uint32_t NUM_1 = 1;
36 constexpr uint32_t NUM_100 = 100;
37 constexpr int64_t BUFFER_SIZE = 2 * 1024 * 1024;
38
PackImage(const std::string & filePath,std::unique_ptr<PixelMap> pixelMap)39 int64_t PackImage(const std::string &filePath, std::unique_ptr<PixelMap> pixelMap)
40 {
41 ImagePacker imagePacker;
42 PackOption option;
43 option.format = "image/jpeg";
44 option.quality = NUM_100;
45 option.numberHint = NUM_1;
46 std::set<std::string> formats;
47 if (pixelMap == nullptr) {
48 HiLog::Error(LABEL_TEST, "pixelMap is nullptr");
49 return 0;
50 }
51 uint32_t ret = imagePacker.GetSupportedFormats(formats);
52 if (ret != SUCCESS) {
53 HiLog::Error(LABEL_TEST, "image packer get supported format failed, ret=%{public}u.", ret);
54 return 0;
55 }
56 imagePacker.StartPacking(filePath, option);
57 imagePacker.AddImage(*pixelMap);
58 int64_t packedSize = 0;
59 imagePacker.FinalizePacking(packedSize);
60 return static_cast<int64_t>(packedSize);
61 }
62
PackImage(std::unique_ptr<ImageSource> imageSource)63 int64_t PackImage(std::unique_ptr<ImageSource> imageSource)
64 {
65 ImagePacker imagePacker;
66 PackOption option;
67 option.format = "image/jpeg";
68 option.quality = NUM_100;
69 option.numberHint = 1;
70 std::set<std::string> formats;
71 if (imageSource == nullptr) {
72 HiLog::Error(LABEL_TEST, "imageSource is nullptr");
73 return 0;
74 }
75 uint32_t ret = imagePacker.GetSupportedFormats(formats);
76 if (ret != SUCCESS) {
77 HiLog::Error(LABEL_TEST, "image packer get supported format failed, ret=%{public}u.", ret);
78 return 0;
79 }
80 int64_t bufferSize = BUFFER_SIZE;
81 uint8_t *resultBuffer = reinterpret_cast<uint8_t *>(malloc(bufferSize));
82 if (resultBuffer == nullptr) {
83 HiLog::Error(LABEL_TEST, "image packer malloc buffer failed.");
84 return 0;
85 }
86 imagePacker.StartPacking(resultBuffer, bufferSize, option);
87 imagePacker.AddImage(*imageSource);
88 int64_t packedSize = 0;
89 imagePacker.FinalizePacking(packedSize);
90 return static_cast<int64_t>(packedSize);
91 }
92
ReadFileToBuffer(const std::string & filePath,uint8_t * buffer,size_t bufferSize)93 bool ReadFileToBuffer(const std::string &filePath, uint8_t *buffer, size_t bufferSize)
94 {
95 std::string realPath;
96 if (!OHOS::PathToRealPath(filePath, realPath)) {
97 HiLog::Error(LABEL_TEST, "file path to real path failed, file path=%{public}s.", filePath.c_str());
98 return false;
99 }
100
101 if (buffer == nullptr) {
102 HiLog::Error(LABEL_TEST, "buffer is nullptr");
103 return false;
104 }
105
106 FILE *fp = fopen(realPath.c_str(), "rb");
107 if (fp == nullptr) {
108 HiLog::Error(LABEL_TEST, "open file failed, real path=%{public}s.", realPath.c_str());
109 return false;
110 }
111 fseek(fp, 0, SEEK_END);
112 size_t fileSize = ftell(fp);
113 fseek(fp, 0, SEEK_SET);
114 if (bufferSize < fileSize) {
115 HiLog::Error(LABEL_TEST, "buffer size:(%{public}zu) is smaller than file size:(%{public}zu).", bufferSize,
116 fileSize);
117 fclose(fp);
118 return false;
119 }
120 size_t retSize = fread(buffer, 1, fileSize, fp);
121 if (retSize != fileSize) {
122 HiLog::Error(LABEL_TEST, "read file result size = %{public}zu, size = %{public}zu.", retSize, fileSize);
123 fclose(fp);
124 return false;
125 }
126 int ret = fclose(fp);
127 if (ret != 0) {
128 return true;
129 }
130 return true;
131 }
132 } // namespace ImageSourceUtil
133 } // namespace OHOS
134