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 #ifndef OHOS_RESTOOL_COMPRESSION_PARSER_H 17 #define OHOS_RESTOOL_COMPRESSION_PARSER_H 18 19 #include <chrono> 20 #include <cJSON.h> 21 #ifdef __WIN32 22 #include <windows.h> 23 #else 24 #include <dlfcn.h> 25 #endif 26 #include "resource_util.h" 27 28 namespace OHOS { 29 namespace Global { 30 namespace Restool { 31 32 enum class TranscodeError { 33 SUCCESS = 0, 34 INVALID_PARAMETERS, 35 IMAGE_ERROR, 36 ANIMATED_IMAGE_SKIP, 37 MALLOC_FAILED, 38 ENCODE_ASTC_FAILED, 39 SUPER_COMPRESS_FAILED, 40 NOT_MATCH_BASE = 100, 41 IMAGE_SIZE_NOT_MATCH = 100, 42 IMAGE_RESOLUTION_NOT_MATCH, 43 EXCLUDE_MATCH, 44 NOT_MATCH_BUTT = 199, 45 LOAD_COMPRESS_FAILED 46 }; 47 48 struct TranscodeResult { 49 size_t originSize; 50 int32_t size; 51 int32_t width; 52 int32_t height; 53 }; 54 55 struct ImageSize { 56 size_t width; 57 size_t height; 58 }; 59 60 typedef TranscodeError (*ITranscodeImages) (const std::string &imagePath, const bool extAppend, 61 std::string &outputPath, TranscodeResult &result); 62 typedef bool (*ISetTranscodeOptions) (const std::string &optionJson, const std::string &optionJsonExclude); 63 typedef TranscodeError (*IScaleImage) (const std::string &imagePath, std::string &outputPath, ImageSize size); 64 65 class CompressionParser { 66 public: 67 static std::shared_ptr<CompressionParser> GetCompressionParser(const std::string &filePath); 68 static std::shared_ptr<CompressionParser> GetCompressionParser(); 69 CompressionParser(); 70 explicit CompressionParser(const std::string &filePath); 71 virtual ~CompressionParser(); 72 uint32_t Init(); 73 bool CopyAndTranscode(const std::string &src, std::string &dst, const bool extAppend = false); 74 bool GetMediaSwitch(); 75 std::string PrintTransMessage(); 76 bool GetDefaultCompress(); 77 void SetOutPath(const std::string &path); 78 bool ScaleIconEnable(); 79 bool CheckAndScaleIcon(const std::string &src, const std::string &originDst, std::string &scaleDst); 80 private: 81 bool ParseContext(const cJSON *contextNode); 82 bool ParseCompression(const cJSON *compressionNode); 83 bool ParseFilters(const cJSON *filtersNode); 84 bool LoadImageTranscoder(); 85 bool SetTranscodeOptions(const std::string &optionJson, const std::string &optionJsonExclude); 86 TranscodeError TranscodeImages(const std::string &imagePath, const bool extAppend, 87 std::string &outputPath, TranscodeResult &result); 88 TranscodeError ScaleImage(const std::string &imagePath, std::string &outputPath); 89 std::vector<std::string> ParsePath(const cJSON *pathNode); 90 std::string ParseRules(const cJSON *rulesNode); 91 std::string ParseJsonStr(const cJSON *node); 92 bool CheckPath(const std::string &src, const std::vector<std::string> &paths); 93 bool IsInPath(const std::string &src, const std::shared_ptr<CompressFilter> &compressFilter); 94 bool IsInExcludePath(const std::string &src, const std::shared_ptr<CompressFilter> &compressFilter); 95 void CollectTime(uint32_t &count, unsigned long long &time, 96 std::chrono::time_point<std::chrono::steady_clock> &start); 97 void CollectTimeAndSize(TranscodeError res, std::chrono::time_point<std::chrono::steady_clock> &start, 98 TranscodeResult &result); 99 std::string GetMethod(const std::shared_ptr<CompressFilter> &compressFilter); 100 std::string GetRules(const std::shared_ptr<CompressFilter> &compressFilter); 101 std::string GetExcludeRules(const std::shared_ptr<CompressFilter> &compressFilter); 102 std::string GetFileRules(const std::string &rules, const std::string &method); 103 bool CheckAndTranscode(const std::string &src, std::string &dst, std::string &output, 104 const std::shared_ptr<CompressFilter> &compressFilter, const bool extAppend); 105 bool CopyForTrans(const std::string &src, const std::string &originDst, const std::string &dst); 106 bool IsDefaultCompress(); 107 std::string filePath_; 108 std::string extensionPath_; 109 std::vector<std::shared_ptr<CompressFilter>> compressFilters_; 110 bool mediaSwitch_; 111 cJSON *root_; 112 bool defaultCompress_; 113 std::string outPath_; 114 unsigned long long totalTime_ = 0; 115 uint32_t totalCounts_ = 0; 116 unsigned long long compressTime_ = 0; 117 uint32_t compressCounts_ = 0; 118 unsigned long long successTime_ = 0; 119 uint32_t successCounts_ = 0; 120 unsigned long long originalSize_ = 0; 121 unsigned long long successSize_ = 0; 122 #ifdef __WIN32 123 HMODULE handle_ = nullptr; 124 #else 125 void *handle_ = nullptr; 126 #endif 127 }; 128 } 129 } 130 } 131 #endif