diff --git a/README b/README index 52f4b8f2..395ed733 100644 --- a/README +++ b/README @@ -263,6 +263,20 @@ size_t WebPEncodeBGRA(const uint8_t* bgra, int width, int height, int stride, They will convert raw RGB samples to a WebP data. The only control supplied is the quality factor. +There are some variants for using the lossless format: + +size_t WebPEncodeLosslessRGB(const uint8_t* rgb, int width, int height, + int stride, uint8_t** output); +size_t WebPEncodeLosslessBGR(const uint8_t* bgr, int width, int height, + int stride, uint8_t** output); +size_t WebPEncodeLosslessRGBA(const uint8_t* rgba, int width, int height, + int stride, uint8_t** output); +size_t WebPEncodeLosslessBGRA(const uint8_t* bgra, int width, int height, + int stride, uint8_t** output); + +Of course in this case, no quality factor is needed since the compression +occurs without loss of the input values, at the expense of larger output sizes. + Advanced encoding API: ---------------------- diff --git a/src/enc/picture.c b/src/enc/picture.c index 451d9aeb..d8c8dc2d 100644 --- a/src/enc/picture.c +++ b/src/enc/picture.c @@ -1008,7 +1008,8 @@ int WebPPictureDistortion(const WebPPicture* const pic1, typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int); static size_t Encode(const uint8_t* rgba, int width, int height, int stride, - Importer import, float quality_factor, uint8_t** output) { + Importer import, float quality_factor, int lossless, + uint8_t** output) { WebPPicture pic; WebPConfig config; WebPMemoryWriter wrt; @@ -1019,6 +1020,8 @@ static size_t Encode(const uint8_t* rgba, int width, int height, int stride, return 0; // shouldn't happen, except if system installation is broken } + config.lossless = !!lossless; + pic.use_argb_input = !!lossless; pic.width = width; pic.height = height; pic.writer = WebPMemoryWrite; @@ -1036,10 +1039,10 @@ static size_t Encode(const uint8_t* rgba, int width, int height, int stride, return wrt.size; } -#define ENCODE_FUNC(NAME, IMPORTER) \ -size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \ - uint8_t** out) { \ - return Encode(in, w, h, bps, IMPORTER, q, out); \ +#define ENCODE_FUNC(NAME, IMPORTER) \ +size_t NAME(const uint8_t* in, int w, int h, int bps, float q, \ + uint8_t** out) { \ + return Encode(in, w, h, bps, IMPORTER, q, 0, out); \ } ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB); @@ -1049,6 +1052,19 @@ ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA); #undef ENCODE_FUNC +#define LOSSLESS_DEFAULT_QUALITY 70. +#define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER) \ +size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) { \ + return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out); \ +} + +LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB); +LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR); +LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA); +LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA); + +#undef LOSSLESS_ENCODE_FUNC + //------------------------------------------------------------------------------ #if defined(__cplusplus) || defined(c_plusplus) diff --git a/src/webp/encode.h b/src/webp/encode.h index b61de12d..8cee9973 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -30,6 +30,9 @@ WEBP_EXTERN(int) WebPGetEncoderVersion(void); // Returns the size of the compressed data (pointed to by *output), or 0 if // an error occurred. The compressed data must be released by the caller // using the call 'free(*output)'. +// These functions compress using the lossy format, and the quality_factor +// can go from 0 (smaller output, lower quality) to 100 (best quality, +// larger output). WEBP_EXTERN(size_t) WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output); @@ -43,6 +46,22 @@ WEBP_EXTERN(size_t) WebPEncodeBGRA(const uint8_t* bgra, int width, int height, int stride, float quality_factor, uint8_t** output); +// These functions are the equivalent of the above, but compressing in a +// lossless manner. Files are usually larger than lossy format, but will +// not suffer any compression loss. +WEBP_EXTERN(size_t) WebPEncodeLosslessRGB(const uint8_t* rgb, + int width, int height, int stride, + uint8_t** output); +WEBP_EXTERN(size_t) WebPEncodeLosslessBGR(const uint8_t* bgr, + int width, int height, int stride, + uint8_t** output); +WEBP_EXTERN(size_t) WebPEncodeLosslessRGBA(const uint8_t* rgba, + int width, int height, int stride, + uint8_t** output); +WEBP_EXTERN(size_t) WebPEncodeLosslessBGRA(const uint8_t* bgra, + int width, int height, int stride, + uint8_t** output); + //------------------------------------------------------------------------------ // Coding parameters