blob: 7e7b935df2761a61c1e4cb04f7980f6b705683d9 [file] [log] [blame]
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001#ifndef _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_
2#define _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08003
4#include "SkTypes.h"
5#include "SkStream.h"
6extern "C" {
7 #include "jpeglib.h"
8 #include "jerror.h"
9}
10
11class YuvToJpegEncoder {
12public:
13 /** Create an encoder based on the YUV format.
14 *
15 * @param pixelFormat The yuv pixel format as defined in ui/PixelFormat.h.
16 * @param strides The number of row bytes in each image plane.
17 * @return an encoder based on the pixelFormat.
18 */
19 static YuvToJpegEncoder* create(int pixelFormat, int* strides);
20
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070021 explicit YuvToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080022
23 /** Encode YUV data to jpeg, which is output to a stream.
24 *
25 * @param stream The jpeg output stream.
26 * @param inYuv The input yuv data.
27 * @param width Width of the the Yuv data in terms of pixels.
28 * @param height Height of the Yuv data in terms of pixels.
29 * @param offsets The offsets in each image plane with respect to inYuv.
30 * @param jpegQuality Picture quality in [0, 100].
31 * @return true if successfully compressed the stream.
32 */
33 bool encode(SkWStream* stream, void* inYuv, int width,
34 int height, int* offsets, int jpegQuality);
35
36 virtual ~YuvToJpegEncoder() {}
37
38protected:
39 int fNumPlanes;
40 int* fStrides;
41 void setJpegCompressStruct(jpeg_compress_struct* cinfo, int width,
42 int height, int quality);
43 virtual void configSamplingFactors(jpeg_compress_struct* cinfo) = 0;
44 virtual void compress(jpeg_compress_struct* cinfo,
45 uint8_t* yuv, int* offsets) = 0;
46};
47
48class Yuv420SpToJpegEncoder : public YuvToJpegEncoder {
49public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070050 explicit Yuv420SpToJpegEncoder(int* strides);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080051 virtual ~Yuv420SpToJpegEncoder() {}
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080052
53private:
Andreas Gampeed6b9df2014-11-20 22:02:20 -080054 void configSamplingFactors(jpeg_compress_struct* cinfo);
55 void deinterleaveYuv(uint8_t* yuv, int width, int height,
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080056 uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar);
Andreas Gampeed6b9df2014-11-20 22:02:20 -080057 void deinterleave(uint8_t* vuPlanar, uint8_t* uRows, uint8_t* vRows,
58 int rowIndex, int width, int height);
59 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080060};
61
62class Yuv422IToJpegEncoder : public YuvToJpegEncoder {
63public:
Chih-Hung Hsieha6543282016-08-29 14:46:35 -070064 explicit Yuv422IToJpegEncoder(int* strides);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080065 virtual ~Yuv422IToJpegEncoder() {}
66
67private:
68 void configSamplingFactors(jpeg_compress_struct* cinfo);
69 void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
70 void deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows,
71 uint8_t* vRows, int rowIndex, int width, int height);
72};
73
Andreas Gampeed6b9df2014-11-20 22:02:20 -080074#endif // _ANDROID_GRAPHICS_YUV_TO_JPEG_ENCODER_H_