1/*
2 * Copyright (c) 2023 Shenzhen Kaihong DID 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_HDI_CODEC_IMAGE_V2_0_CODECJPEGHELPER_H
17#define OHOS_HDI_CODEC_IMAGE_V2_0_CODECJPEGHELPER_H
18#include <cinttypes>
19#include "v2_0/icodec_image.h"
20#include <memory>
21namespace OHOS {
22namespace HDI {
23namespace Codec {
24namespace Image {
25namespace V2_0 {
26class CodecJpegHelper {
27public:
28    enum JpegMarker : int16_t {
29        SOF0 = 0xffc0,
30        DHT = 0xffc4,
31        SOI = 0xffd8,
32        EOI = 0xffd9,
33        SOS = 0xffda,
34        DQT = 0xffdb,
35        DRI = 0xffdd,
36        UNKNOWN = 0xffff
37    };
38    explicit CodecJpegHelper() = default;
39    ~CodecJpegHelper() = default;
40    int32_t JpegAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t fd);
41    bool DessambleJpeg(int8_t *buffer, size_t bufferLen, struct CodecJpegDecInfo &decInfo,
42                       std::unique_ptr<int8_t[]> &compressBuffer, uint32_t &comBufLen, uint32_t &dataStart);
43
44private:
45    int32_t FindMarker(int8_t *start);
46    int32_t JpegDqtAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t curPos);
47    int32_t JpegDriAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t curPos);
48    int32_t JpegDhtAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t curPos);
49    int32_t JpegDhtAssemble(const std::vector<CodecJpegHuffTable> &table, int8_t *buffer, int32_t curPos,
50                            bool dc = true);
51    int32_t JpegSofAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t curPos);
52    int32_t JpegSosAssemble(const struct CodecJpegDecInfo &decInfo, int8_t *buffer, int32_t curPos);
53    int32_t JpegDataAssemble(int8_t *buffer, int32_t curPos, int32_t fd);
54    int32_t DessambleSof(int8_t *buffer, struct CodecJpegDecInfo &decInfo);
55    int32_t DessambleSos(int8_t *buffer, struct CodecJpegDecInfo &decInfo);
56    int32_t DessambleCompressData(int8_t *buffer, std::unique_ptr<int8_t[]> &compressBuffer, uint32_t &comBufLen);
57    int32_t DessambleDqt(int8_t *buffer, struct CodecJpegDecInfo &decInfo);
58    int32_t DessambleDht(int8_t *buffer, struct CodecJpegDecInfo &decInfo);
59    int32_t PutInt16(int8_t *buffer, int32_t curPos, int16_t value);
60    int32_t PutInt8(int8_t *buffer, int32_t curPos, int8_t value);
61    int32_t GetInt8(int8_t *buffer);
62    int32_t GetInt16(int8_t *buffer);
63private:
64    static constexpr int32_t MAX_BUFFER_LEN = 128;
65};
66}
67}
68}
69}
70}
71#endif // OHOS_HDI_CODEC_IMAGE_V2_0_CODECJPEGHELPER_H
72