1 #ifndef _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ 2 #define _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ 3 4 #include <android/data_space.h> 5 #include <ultrahdr/jpegr.h> 6 7 extern "C" { 8 #include "jpeglib.h" 9 #include "jerror.h" 10 } 11 12 class SkWStream; 13 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 24 explicit YuvToJpegEncoder(int* strides); 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. 30 * @param width Width of the Yuv data in terms of pixels. 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 ~YuvToJpegEncoder()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: 53 explicit Yuv420SpToJpegEncoder(int* strides); ~Yuv420SpToJpegEncoder()54 virtual ~Yuv420SpToJpegEncoder() {} 55 56 private: 57 void configSamplingFactors(jpeg_compress_struct* cinfo); 58 void deinterleaveYuv(uint8_t* yuv, int width, int height, 59 uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar); 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); 63 }; 64 65 class Yuv422IToJpegEncoder : public YuvToJpegEncoder { 66 public: 67 explicit Yuv422IToJpegEncoder(int* strides); ~Yuv422IToJpegEncoder()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 77 class P010Yuv420ToJpegREncoder { 78 public: 79 /** Encode YUV data to jpeg/r, which is output to a stream. 80 * This method will call JpegR::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::ultrahdr::ultrahdr_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::ultrahdr::ultrahdr_transfer_function findHdrTransferFunction( 116 JNIEnv* env, int aDataSpace); 117 }; 118 119 #endif // _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_ 120