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 #ifndef JPEG_UTILS_H 17 #define JPEG_UTILS_H 18 19 #include <setjmp.h> 20 #include <stdio.h> 21 #include <cstdint> 22 #include <string> 23 #include "hilog/log.h" 24 #include "input_data_stream.h" 25 #include "jerror.h" 26 #include "jpeglib.h" 27 #include "log_tags.h" 28 #include "output_data_stream.h" 29 30 namespace OHOS { 31 namespace ImagePlugin { 32 static constexpr uint8_t SET_JUMP_VALUE = 1; 33 static constexpr uint8_t RW_LINE_NUM = 1; 34 static constexpr uint16_t JPEG_BUFFER_SIZE = 1024; 35 static constexpr uint32_t JPEG_IMAGE_NUM = 1; 36 static constexpr uint32_t PRINTF_SUCCESS = 0; 37 38 // redefine jpeg error manager struct. 39 struct ErrorMgr : jpeg_error_mgr { 40 struct jpeg_error_mgr pub; // public fields 41 42 #ifdef _WIN32 43 jmp_buf setjmp_buffer = {{0}}; // for return to caller 44 #else 45 jmp_buf setjmp_buffer; // for return to caller 46 #endif 47 }; 48 49 // redefine jpeg source manager struct. 50 struct JpegSrcMgr : jpeg_source_mgr { 51 explicit JpegSrcMgr(InputDataStream *stream); 52 53 InputDataStream *inputStream = nullptr; 54 uint16_t bufferSize = JPEG_BUFFER_SIZE; 55 ImagePlugin::DataStreamBuffer streamData; 56 }; 57 58 // redefine jpeg destination manager struct. 59 struct JpegDstMgr : jpeg_destination_mgr { 60 explicit JpegDstMgr(OutputDataStream *stream); 61 62 OutputDataStream *outputStream = nullptr; 63 uint16_t bufferSize = JPEG_BUFFER_SIZE; 64 uint8_t buffer[JPEG_BUFFER_SIZE] = { 0 }; 65 }; 66 67 // for jpeg error manager 68 void ErrorExit(j_common_ptr cinfo); 69 void OutputErrorMessage(j_common_ptr cinfo); 70 // for jpeg source manager 71 void InitSrcStream(j_decompress_ptr dinfo); 72 boolean FillInputBuffer(j_decompress_ptr dinfo); 73 void SkipInputData(j_decompress_ptr dinfo, long numBytes); 74 void TermSrcStream(j_decompress_ptr dinfo); 75 // for jpeg destination manager 76 void InitDstStream(j_compress_ptr cinfo); 77 boolean EmptyOutputBuffer(j_compress_ptr cinfo); 78 void TermDstStream(j_compress_ptr cinfo); 79 std::string DoubleToString(double num); 80 } // namespace ImagePlugin 81 } // namespace OHOS 82 83 #endif // JPEG_UTILS_H 84