• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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     uint32_t ret = imagePacker.GetSupportedFormats(formats);
48     if (ret != SUCCESS) {
49         HiLog::Error(LABEL_TEST, "image packer get supported format failed, ret=%{public}u.", ret);
50         return 0;
51     }
52     imagePacker.StartPacking(filePath, option);
53     imagePacker.AddImage(*pixelMap);
54     int64_t packedSize = 0;
55     imagePacker.FinalizePacking(packedSize);
56     HiLog::Debug(LABEL_TEST, "packedSize=%{public}lld.", static_cast<long long>(packedSize));
57     return packedSize;
58 }
59 
PackImage(std::unique_ptr<ImageSource> imageSource)60 int64_t PackImage(std::unique_ptr<ImageSource> imageSource)
61 {
62     ImagePacker imagePacker;
63     PackOption option;
64     option.format = "image/jpeg";
65     option.quality = NUM_100;
66     option.numberHint = 1;
67     std::set<std::string> formats;
68     uint32_t ret = imagePacker.GetSupportedFormats(formats);
69     if (ret != SUCCESS) {
70         HiLog::Error(LABEL_TEST, "image packer get supported format failed, ret=%{public}u.", ret);
71         return 0;
72     }
73     int64_t bufferSize = BUFFER_SIZE;
74     uint8_t *resultBuffer = (uint8_t *)malloc(bufferSize);
75     if (resultBuffer == nullptr) {
76         HiLog::Error(LABEL_TEST, "image packer malloc buffer failed.");
77         return 0;
78     }
79     imagePacker.StartPacking(resultBuffer, bufferSize, option);
80     imagePacker.AddImage(*imageSource);
81     int64_t packedSize = 0;
82     imagePacker.FinalizePacking(packedSize);
83     HiLog::Debug(LABEL_TEST, "packedSize=%{public}lld.", static_cast<long long>(packedSize));
84     return packedSize;
85 }
86 
ReadFileToBuffer(const std::string & filePath,uint8_t * buffer,size_t bufferSize)87 bool ReadFileToBuffer(const std::string &filePath, uint8_t *buffer, size_t bufferSize)
88 {
89     std::string realPath;
90     if (!OHOS::PathToRealPath(filePath, realPath)) {
91         HiLog::Error(LABEL_TEST, "file path to real path failed, file path=%{public}s.", filePath.c_str());
92         return false;
93     }
94     FILE *fp = fopen(realPath.c_str(), "rb");
95     if (fp == nullptr) {
96         HiLog::Error(LABEL_TEST, "open file failed, real path=%{public}s.", realPath.c_str());
97         return false;
98     }
99     fseek(fp, 0, SEEK_END);
100     size_t fileSize = ftell(fp);
101     fseek(fp, 0, SEEK_SET);
102     if (bufferSize < fileSize) {
103         HiLog::Error(LABEL_TEST, "buffer size:(%{public}zu) is smaller than file size:(%{public}zu).", bufferSize,
104                      fileSize);
105         fclose(fp);
106         return false;
107     }
108     size_t retSize = fread(buffer, 1, fileSize, fp);
109     if (retSize != fileSize) {
110         HiLog::Error(LABEL_TEST, "read file result size = %{public}zu, size = %{public}zu.", retSize, fileSize);
111         fclose(fp);
112         return false;
113     }
114     fclose(fp);
115     return true;
116 }
117 }
118 } // namespace
119