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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_IMAGE_COMPRESS_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_IMAGE_COMPRESS_H
18 
19 #include <stdio.h>
20 #include <cstdlib>
21 #include <string>
22 #include <cmath>
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26     enum class ImageType {
27         JPEG = 1,
28         PNG = 2,
29         WORNG_TYPE = 3,
30     };
31 class ImageCompress {
32 public:
33     ImageCompress() = default;
34     bool IsPathValid(const std::string &srcPath);
35     bool IsImageNeedCompressBySize(size_t fileSize);
36     double CalculateRatio(size_t fileSize, const std::string &imageType);
37     ImageType GetImageType(const std::unique_ptr<uint8_t[]> &fileData, size_t fileLength);
38     bool GetImageTypeString(const std::unique_ptr<uint8_t[]> &fileData, size_t fileLength, std::string &imageType);
39     bool GetImageFileInfo(const std::string &srcFile, std::unique_ptr<uint8_t[]> &fileContent, int64_t &fileLength);
40     bool CompressImageByContent(const std::unique_ptr<uint8_t[]> &fileData, size_t fileSize,
41         std::unique_ptr<uint8_t[]> &compressedData, int64_t &compressedSize, std::string &imageType);
42 };
43 }
44 }
45 
46 #endif // FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_IMAGE_COMPRESS_H