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