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