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 "convert_utils.h"
17
18 #include <chrono>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <iostream>
22 #include <fstream>
23
24 #include "pixel_map.h"
25 #include "image_packer.h"
26 #include "media_errors.h"
27 #include "image_log.h"
28
29 #undef LOG_DOMAIN
30 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_IMAGE
31
32 #undef LOG_TAG
33 #define LOG_TAG "CONVERT_UTILS"
34
35 static const int FOUR_BYTES_PER_PIXEL = 4;
36 static const int NUM_TWO = 2;
37 using namespace OHOS::Media;
38
ConvertDataToFd(const uint8_t * data,size_t size,std::string encodeFormat)39 int ConvertDataToFd(const uint8_t* data, size_t size, std::string encodeFormat)
40 {
41 if (size < FOUR_BYTES_PER_PIXEL) {
42 return -1;
43 }
44 int picturePixels = size / FOUR_BYTES_PER_PIXEL;
45 InitializationOptions opts = {};
46 if (picturePixels % NUM_TWO == 0) {
47 opts.size.width = picturePixels / NUM_TWO;
48 opts.size.height = NUM_TWO;
49 } else {
50 opts.size.width = picturePixels;
51 opts.size.height = 1;
52 }
53 auto pixelMap = PixelMap::Create(reinterpret_cast<const uint32_t*>(data),
54 static_cast<uint32_t>(picturePixels) * FOUR_BYTES_PER_PIXEL, opts);
55 if (pixelMap == nullptr) {
56 return -1;
57 }
58
59 std::string pathName = "/data/local/tmp/testFile_" + GetNowTimeStr() + ".dat";
60 IMAGE_LOGI("%{public}s pathName is %{public}s", __func__, pathName.c_str());
61 std::remove(pathName.c_str());
62 int fd = open(pathName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
63 if (fd < 0) {
64 return -1;
65 }
66 ImagePacker packer;
67 PackOption option;
68 option.format = encodeFormat;
69 packer.StartPacking(fd, option);
70 packer.AddImage(*(pixelMap.get()));
71 uint32_t errorCode = packer.FinalizePacking();
72 if (errorCode != SUCCESS) {
73 return -1;
74 }
75 return fd;
76 }
77
GetNowTimeStr()78 std::string GetNowTimeStr()
79 {
80 auto now = std::chrono::system_clock::now();
81 auto us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
82 return std::to_string(us.count());
83 }
84
WriteDataToFile(const uint8_t * data,size_t size,const std::string & filename)85 bool WriteDataToFile(const uint8_t* data, size_t size, const std::string& filename)
86 {
87 IMAGE_LOGI("%{public}s %{public}s IN", __func__, filename.c_str());
88 std::ifstream file(filename);
89 bool isFileExists = file.good();
90 file.close();
91 if (isFileExists) {
92 std::remove(filename.c_str());
93 }
94 std::ofstream newFile(filename);
95 if (!newFile.is_open()) {
96 IMAGE_LOGI("%{public}s failed, new file: %{public}s error", __func__, filename.c_str());
97 return false;
98 }
99 newFile.write(reinterpret_cast<const char*>(data), size);
100 newFile.close();
101 IMAGE_LOGI("%{public}s %{public}s SUCCESS", __func__, filename.c_str());
102 return true;
103 }
104