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