2012-01-06 14:49:06 -08:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
2010-09-30 09:34:38 -04:00
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2012-08-02 17:23:02 -07:00
|
|
|
// inline YUV<->RGB conversion function
|
2010-09-30 09:34:38 -04:00
|
|
|
//
|
2012-10-18 14:55:39 +02:00
|
|
|
// The exact naming is Y'CbCr, following the ITU-R BT.601 standard.
|
|
|
|
// More information at: http://en.wikipedia.org/wiki/YCbCr
|
|
|
|
// Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16
|
|
|
|
// U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128
|
|
|
|
// V = 0.4394 * R - 0.3679 * G - 0.0715 * B + 128
|
|
|
|
// We use 16bit fixed point operations for RGB->YUV conversion.
|
|
|
|
//
|
2010-09-30 09:34:38 -04:00
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
2011-09-02 21:30:08 +00:00
|
|
|
#ifndef WEBP_DSP_YUV_H_
|
|
|
|
#define WEBP_DSP_YUV_H_
|
2010-09-30 09:34:38 -04:00
|
|
|
|
2012-07-16 22:12:59 -07:00
|
|
|
#include "../dec/decode_vp8.h"
|
2010-09-30 09:34:38 -04:00
|
|
|
|
2012-10-18 14:55:39 +02:00
|
|
|
#if defined(WEBP_EXPERIMENTAL_FEATURES)
|
|
|
|
// Do NOT activate this feature for real compression. This is only experimental!
|
|
|
|
// This flag is for comparison purpose against JPEG's "YUVj" natural colorspace.
|
|
|
|
// This colorspace is close to Rec.601's Y'CbCr model with the notable
|
|
|
|
// difference of allowing larger range for luma/chroma.
|
|
|
|
// See http://en.wikipedia.org/wiki/YCbCr#JPEG_conversion paragraph, and its
|
|
|
|
// difference with http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
|
|
|
|
// #define USE_YUVj
|
|
|
|
#endif
|
|
|
|
|
2012-08-02 17:23:02 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// YUV -> RGB conversion
|
|
|
|
|
2010-09-30 09:34:38 -04:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum { YUV_FIX = 16, // fixed-point precision
|
|
|
|
YUV_RANGE_MIN = -227, // min value of r/g/b output
|
|
|
|
YUV_RANGE_MAX = 256 + 226 // max value of r/g/b output
|
|
|
|
};
|
|
|
|
extern int16_t VP8kVToR[256], VP8kUToB[256];
|
|
|
|
extern int32_t VP8kVToG[256], VP8kUToG[256];
|
|
|
|
extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
2011-07-13 09:08:58 -07:00
|
|
|
extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
|
2010-09-30 09:34:38 -04:00
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToRgb(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const rgb) {
|
2010-09-30 09:34:38 -04:00
|
|
|
const int r_off = VP8kVToR[v];
|
|
|
|
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
|
|
|
const int b_off = VP8kUToB[u];
|
|
|
|
rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN];
|
|
|
|
rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
|
|
|
|
rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN];
|
|
|
|
}
|
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToRgb565(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const rgb) {
|
2011-07-13 09:08:58 -07:00
|
|
|
const int r_off = VP8kVToR[v];
|
|
|
|
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
|
|
|
const int b_off = VP8kUToB[u];
|
|
|
|
rgb[0] = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) |
|
|
|
|
(VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5));
|
|
|
|
rgb[1] = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) |
|
|
|
|
(VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3));
|
|
|
|
}
|
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const argb) {
|
2011-07-13 09:08:58 -07:00
|
|
|
argb[0] = 0xff;
|
2012-06-27 10:00:48 -07:00
|
|
|
VP8YuvToRgb(y, u, v, argb + 1);
|
2011-07-13 09:08:58 -07:00
|
|
|
}
|
|
|
|
|
2012-06-27 10:00:48 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToRgba4444(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const argb) {
|
2011-07-13 09:08:58 -07:00
|
|
|
const int r_off = VP8kVToR[v];
|
|
|
|
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
|
|
|
const int b_off = VP8kUToB[u];
|
2012-06-27 10:00:48 -07:00
|
|
|
// Don't update alpha (last 4 bits of argb[1])
|
2011-07-14 11:41:23 -07:00
|
|
|
argb[0] = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) |
|
|
|
|
VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]);
|
2012-06-27 10:00:48 -07:00
|
|
|
argb[1] = 0x0f | (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4);
|
2011-07-13 09:08:58 -07:00
|
|
|
}
|
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToBgr(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const bgr) {
|
2010-09-30 09:34:38 -04:00
|
|
|
const int r_off = VP8kVToR[v];
|
|
|
|
const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
|
|
|
|
const int b_off = VP8kUToB[u];
|
|
|
|
bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN];
|
|
|
|
bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
|
|
|
|
bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN];
|
|
|
|
}
|
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const bgra) {
|
2010-09-30 09:34:38 -04:00
|
|
|
VP8YuvToBgr(y, u, v, bgra);
|
|
|
|
bgra[3] = 0xff;
|
|
|
|
}
|
|
|
|
|
2011-11-04 19:44:57 -07:00
|
|
|
static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
|
|
|
|
uint8_t* const rgba) {
|
2011-06-20 00:45:15 -07:00
|
|
|
VP8YuvToRgb(y, u, v, rgba);
|
|
|
|
rgba[3] = 0xff;
|
|
|
|
}
|
|
|
|
|
2010-09-30 09:34:38 -04:00
|
|
|
// Must be called before everything, to initialize the tables.
|
2011-03-25 15:04:11 -07:00
|
|
|
void VP8YUVInit(void);
|
2010-09-30 09:34:38 -04:00
|
|
|
|
2012-08-02 17:23:02 -07:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// RGB -> YUV conversion
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8ClipUV(int v) {
|
|
|
|
v = (v + (257 << (YUV_FIX + 2 - 1))) >> (YUV_FIX + 2);
|
|
|
|
return ((v & ~0xff) == 0) ? v : (v < 0) ? 0 : 255;
|
|
|
|
}
|
|
|
|
|
2012-10-18 14:55:39 +02:00
|
|
|
#ifndef USE_YUVj
|
|
|
|
|
2012-08-02 17:23:02 -07:00
|
|
|
static WEBP_INLINE int VP8RGBToY(int r, int g, int b) {
|
|
|
|
const int kRound = (1 << (YUV_FIX - 1)) + (16 << YUV_FIX);
|
|
|
|
const int luma = 16839 * r + 33059 * g + 6420 * b;
|
|
|
|
return (luma + kRound) >> YUV_FIX; // no need to clip
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8RGBToU(int r, int g, int b) {
|
2012-10-18 14:55:39 +02:00
|
|
|
const int u = -9719 * r - 19081 * g + 28800 * b;
|
|
|
|
return VP8ClipUV(u);
|
2012-08-02 17:23:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8RGBToV(int r, int g, int b) {
|
2012-10-18 14:55:39 +02:00
|
|
|
const int v = +28800 * r - 24116 * g - 4684 * b;
|
|
|
|
return VP8ClipUV(v);
|
2012-08-02 17:23:02 -07:00
|
|
|
}
|
|
|
|
|
2012-10-18 14:55:39 +02:00
|
|
|
#else
|
|
|
|
|
|
|
|
// This JPEG-YUV colorspace, only for comparison!
|
|
|
|
// These are also 16-bit precision coefficients from Rec.601, but with full
|
|
|
|
// [0..255] output range.
|
|
|
|
static WEBP_INLINE int VP8RGBToY(int r, int g, int b) {
|
|
|
|
const int kRound = (1 << (YUV_FIX - 1));
|
|
|
|
const int luma = 19595 * r + 38470 * g + 7471 * b;
|
|
|
|
return (luma + kRound) >> YUV_FIX; // no need to clip
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8RGBToU(int r, int g, int b) {
|
|
|
|
const int u = -11058 * r - 21710 * g + 32768 * b;
|
|
|
|
return VP8ClipUV(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int VP8RGBToV(int r, int g, int b) {
|
|
|
|
const int v = 32768 * r - 27439 * g - 5329 * b;
|
|
|
|
return VP8ClipUV(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_YUVj
|
|
|
|
|
2010-09-30 09:34:38 -04:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2011-09-07 05:31:28 +00:00
|
|
|
#endif /* WEBP_DSP_YUV_H_ */
|