Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1 | #ifndef _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ |
| 2 | #define _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 3 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame^] | 4 | #include <android/data_space.h> |
| 5 | #include <jpegrecoverymap/recoverymap.h> |
| 6 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 7 | extern "C" { |
| 8 | #include "jpeglib.h" |
| 9 | #include "jerror.h" |
| 10 | } |
| 11 | |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 12 | class SkWStream; |
| 13 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 14 | class YuvToJpegEncoder { |
| 15 | public: |
| 16 | /** Create an encoder based on the YUV format. |
| 17 | * |
| 18 | * @param pixelFormat The yuv pixel format as defined in ui/PixelFormat.h. |
| 19 | * @param strides The number of row bytes in each image plane. |
| 20 | * @return an encoder based on the pixelFormat. |
| 21 | */ |
| 22 | static YuvToJpegEncoder* create(int pixelFormat, int* strides); |
| 23 | |
Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 24 | explicit YuvToJpegEncoder(int* strides); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 25 | |
| 26 | /** Encode YUV data to jpeg, which is output to a stream. |
| 27 | * |
| 28 | * @param stream The jpeg output stream. |
| 29 | * @param inYuv The input yuv data. |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame^] | 30 | * @param width Width of the Yuv data in terms of pixels. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 31 | * @param height Height of the Yuv data in terms of pixels. |
| 32 | * @param offsets The offsets in each image plane with respect to inYuv. |
| 33 | * @param jpegQuality Picture quality in [0, 100]. |
| 34 | * @return true if successfully compressed the stream. |
| 35 | */ |
| 36 | bool encode(SkWStream* stream, void* inYuv, int width, |
| 37 | int height, int* offsets, int jpegQuality); |
| 38 | |
| 39 | virtual ~YuvToJpegEncoder() {} |
| 40 | |
| 41 | protected: |
| 42 | int fNumPlanes; |
| 43 | int* fStrides; |
| 44 | void setJpegCompressStruct(jpeg_compress_struct* cinfo, int width, |
| 45 | int height, int quality); |
| 46 | virtual void configSamplingFactors(jpeg_compress_struct* cinfo) = 0; |
| 47 | virtual void compress(jpeg_compress_struct* cinfo, |
| 48 | uint8_t* yuv, int* offsets) = 0; |
| 49 | }; |
| 50 | |
| 51 | class Yuv420SpToJpegEncoder : public YuvToJpegEncoder { |
| 52 | public: |
Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 53 | explicit Yuv420SpToJpegEncoder(int* strides); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 54 | virtual ~Yuv420SpToJpegEncoder() {} |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 55 | |
| 56 | private: |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 57 | void configSamplingFactors(jpeg_compress_struct* cinfo); |
| 58 | void deinterleaveYuv(uint8_t* yuv, int width, int height, |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 59 | uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 60 | void deinterleave(uint8_t* vuPlanar, uint8_t* uRows, uint8_t* vRows, |
| 61 | int rowIndex, int width, int height); |
| 62 | void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class Yuv422IToJpegEncoder : public YuvToJpegEncoder { |
| 66 | public: |
Chih-Hung Hsieh | a654328 | 2016-08-29 14:46:35 -0700 | [diff] [blame] | 67 | explicit Yuv422IToJpegEncoder(int* strides); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 68 | virtual ~Yuv422IToJpegEncoder() {} |
| 69 | |
| 70 | private: |
| 71 | void configSamplingFactors(jpeg_compress_struct* cinfo); |
| 72 | void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets); |
| 73 | void deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, |
| 74 | uint8_t* vRows, int rowIndex, int width, int height); |
| 75 | }; |
| 76 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame^] | 77 | class P010Yuv420ToJpegREncoder { |
| 78 | public: |
| 79 | /** Encode YUV data to jpeg/r, which is output to a stream. |
| 80 | * This method will call RecoveryMap::EncodeJPEGR() method. If encoding failed, |
| 81 | * Corresponding error code (defined in jpegrerrorcode.h) will be printed and this |
| 82 | * method will be terminated and return false. |
| 83 | * |
| 84 | * @param env JNI environment. |
| 85 | * @param stream The jpeg output stream. |
| 86 | * @param hdr The input yuv data (p010 format). |
| 87 | * @param hdrColorSpaceId color space id for the input hdr. |
| 88 | * @param sdr The input yuv data (yuv420p format). |
| 89 | * @param sdrColorSpaceId color space id for the input sdr. |
| 90 | * @param width Width of the Yuv data in terms of pixels. |
| 91 | * @param height Height of the Yuv data in terms of pixels. |
| 92 | * @param jpegQuality Picture quality in [0, 100]. |
| 93 | * @return true if successfully compressed the stream. |
| 94 | */ |
| 95 | bool encode(JNIEnv* env, |
| 96 | SkWStream* stream, void* hdr, int hdrColorSpace, void* sdr, int sdrColorSpace, |
| 97 | int width, int height, int jpegQuality); |
| 98 | |
| 99 | /** Map data space (defined in DataSpace.java and data_space.h) to the color gamut |
| 100 | * used in JPEG/R |
| 101 | * |
| 102 | * @param env JNI environment. |
| 103 | * @param aDataSpace data space defined in data_space.h. |
| 104 | * @return color gamut for JPEG/R. |
| 105 | */ |
| 106 | static android::recoverymap::jpegr_color_gamut findColorGamut(JNIEnv* env, int aDataSpace); |
| 107 | |
| 108 | /** Map data space (defined in DataSpace.java and data_space.h) to the transfer function |
| 109 | * used in JPEG/R |
| 110 | * |
| 111 | * @param env JNI environment. |
| 112 | * @param aDataSpace data space defined in data_space.h. |
| 113 | * @return color gamut for JPEG/R. |
| 114 | */ |
| 115 | static android::recoverymap::jpegr_transfer_function findHdrTransferFunction( |
| 116 | JNIEnv* env, int aDataSpace); |
| 117 | }; |
| 118 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 119 | #endif // _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ |