/src/libultrahdr/lib/include/ultrahdr/jpegencoderhelper.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022 The Android Open Source Project |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 | | * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 | | * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
7 | | * option. This file may not be copied, modified, or distributed |
8 | | * except according to those terms. |
9 | | */ |
10 | | |
11 | | #ifndef ULTRAHDR_JPEGENCODERHELPER_H |
12 | | #define ULTRAHDR_JPEGENCODERHELPER_H |
13 | | |
14 | | #include <stdio.h> // For jpeglib.h. |
15 | | |
16 | | // C++ build requires extern C for jpeg internals. |
17 | | #ifdef __cplusplus |
18 | | extern "C" { |
19 | | #endif |
20 | | |
21 | | #include <jerror.h> |
22 | | #include <jpeglib.h> |
23 | | |
24 | | #ifdef __cplusplus |
25 | | } // extern "C" |
26 | | #endif |
27 | | |
28 | | #include <cstdint> |
29 | | #include <vector> |
30 | | |
31 | | #include "ultrahdr_api.h" |
32 | | |
33 | | namespace ultrahdr { |
34 | | |
35 | | /*!\brief module for managing output */ |
36 | | struct destination_mgr_impl : jpeg_destination_mgr { |
37 | | static const int kBlockSize = 16384; // result buffer resize step |
38 | | std::vector<JOCTET> mResultBuffer; // buffer to store encoded data |
39 | | }; |
40 | | |
41 | | /*!\brief Encapsulates a converter from raw to jpg image format. This class is not thread-safe */ |
42 | | class JpegEncoderHelper { |
43 | | public: |
44 | 0 | JpegEncoderHelper() = default; |
45 | 0 | ~JpegEncoderHelper() = default; |
46 | | |
47 | | /*!\brief This function encodes the raw image that is passed to it and stores the results |
48 | | * internally. The result is accessible via getter functions. |
49 | | * |
50 | | * \param[in] img image to encode |
51 | | * \param[in] qfactor quality factor [1 - 100, 1 being poorest and 100 being best quality] |
52 | | * \param[in] iccBuffer pointer to icc segment that needs to be added to the compressed image |
53 | | * \param[in] iccSize size of icc segment |
54 | | * |
55 | | * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. |
56 | | */ |
57 | | uhdr_error_info_t compressImage(const uhdr_raw_image_t* img, const int qfactor, |
58 | | const void* iccBuffer, const size_t iccSize); |
59 | | |
60 | | /*!\brief This function encodes the raw image that is passed to it and stores the results |
61 | | * internally. The result is accessible via getter functions. |
62 | | * |
63 | | * \param[in] planes pointers of all planes of input image |
64 | | * \param[in] strides strides of all planes of input image |
65 | | * \param[in] width image width |
66 | | * \param[in] height image height |
67 | | * \param[in] format input raw image format |
68 | | * \param[in] qfactor quality factor [1 - 100, 1 being poorest and 100 being best quality] |
69 | | * \param[in] iccBuffer pointer to icc segment that needs to be added to the compressed image |
70 | | * \param[in] iccSize size of icc segment |
71 | | * |
72 | | * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. |
73 | | */ |
74 | | uhdr_error_info_t compressImage(const uint8_t* planes[3], const unsigned int strides[3], |
75 | | const int width, const int height, const uhdr_img_fmt_t format, |
76 | | const int qfactor, const void* iccBuffer, const size_t iccSize); |
77 | | |
78 | | /*! Below public methods are only effective if a call to compressImage() is made and it returned |
79 | | * true. */ |
80 | | |
81 | | /*!\brief returns pointer to compressed image output */ |
82 | | uhdr_compressed_image_t getCompressedImage(); |
83 | | |
84 | | /*!\brief returns pointer to compressed image output |
85 | | * \deprecated This function is deprecated instead use getCompressedImage(). |
86 | | */ |
87 | 0 | void* getCompressedImagePtr() { return mDestMgr.mResultBuffer.data(); } |
88 | | |
89 | | /*!\brief returns size of compressed image |
90 | | * \deprecated This function is deprecated instead use getCompressedImage(). |
91 | | */ |
92 | 0 | size_t getCompressedImageSize() { return mDestMgr.mResultBuffer.size(); } |
93 | | |
94 | | private: |
95 | | // max number of components supported |
96 | | static constexpr int kMaxNumComponents = 3; |
97 | | |
98 | | uhdr_error_info_t encode(const uint8_t* planes[3], const unsigned int strides[3], const int width, |
99 | | const int height, const uhdr_img_fmt_t format, const int qfactor, |
100 | | const void* iccBuffer, const size_t iccSize); |
101 | | |
102 | | uhdr_error_info_t compressYCbCr(jpeg_compress_struct* cinfo, const uint8_t* planes[3], |
103 | | const unsigned int strides[3]); |
104 | | |
105 | | destination_mgr_impl mDestMgr; // object for managing output |
106 | | |
107 | | // temporary storage |
108 | | std::unique_ptr<uint8_t[]> mPlanesMCURow[kMaxNumComponents]; |
109 | | |
110 | | unsigned int mPlaneWidth[kMaxNumComponents]; |
111 | | unsigned int mPlaneHeight[kMaxNumComponents]; |
112 | | }; |
113 | | |
114 | | } /* namespace ultrahdr */ |
115 | | |
116 | | #endif // ULTRAHDR_JPEGENCODERHELPER_H |