Path: blob/master/3rdparty/libwebp/src/enc/picture_csp_enc.c
16344 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// WebPPicture utils for colorspace conversion10//11// Author: Skal ([email protected])1213#include <assert.h>14#include <stdlib.h>15#include <math.h>1617#include "src/enc/vp8i_enc.h"18#include "src/utils/random_utils.h"19#include "src/utils/utils.h"20#include "src/dsp/dsp.h"21#include "src/dsp/lossless.h"22#include "src/dsp/yuv.h"2324// Uncomment to disable gamma-compression during RGB->U/V averaging25#define USE_GAMMA_COMPRESSION2627// If defined, use table to compute x / alpha.28#define USE_INVERSE_ALPHA_TABLE2930#ifdef WORDS_BIGENDIAN31#define ALPHA_OFFSET 0 // uint32_t 0xff000000 is 0xff,00,00,00 in memory32#else33#define ALPHA_OFFSET 3 // uint32_t 0xff000000 is 0x00,00,00,ff in memory34#endif3536//------------------------------------------------------------------------------37// Detection of non-trivial transparency3839// Returns true if alpha[] has non-0xff values.40static int CheckNonOpaque(const uint8_t* alpha, int width, int height,41int x_step, int y_step) {42if (alpha == NULL) return 0;43WebPInitAlphaProcessing();44if (x_step == 1) {45for (; height-- > 0; alpha += y_step) {46if (WebPHasAlpha8b(alpha, width)) return 1;47}48} else {49for (; height-- > 0; alpha += y_step) {50if (WebPHasAlpha32b(alpha, width)) return 1;51}52}53return 0;54}5556// Checking for the presence of non-opaque alpha.57int WebPPictureHasTransparency(const WebPPicture* picture) {58if (picture == NULL) return 0;59if (!picture->use_argb) {60return CheckNonOpaque(picture->a, picture->width, picture->height,611, picture->a_stride);62} else {63const int alpha_offset = ALPHA_OFFSET;64return CheckNonOpaque((const uint8_t*)picture->argb + alpha_offset,65picture->width, picture->height,664, picture->argb_stride * sizeof(*picture->argb));67}68return 0;69}7071//------------------------------------------------------------------------------72// Code for gamma correction7374#if defined(USE_GAMMA_COMPRESSION)7576// gamma-compensates loss of resolution during chroma subsampling77#define kGamma 0.80 // for now we use a different gamma value than kGammaF78#define kGammaFix 12 // fixed-point precision for linear values79#define kGammaScale ((1 << kGammaFix) - 1)80#define kGammaTabFix 7 // fixed-point fractional bits precision81#define kGammaTabScale (1 << kGammaTabFix)82#define kGammaTabRounder (kGammaTabScale >> 1)83#define kGammaTabSize (1 << (kGammaFix - kGammaTabFix))8485static int kLinearToGammaTab[kGammaTabSize + 1];86static uint16_t kGammaToLinearTab[256];87static volatile int kGammaTablesOk = 0;8889static WEBP_TSAN_IGNORE_FUNCTION void InitGammaTables(void) {90if (!kGammaTablesOk) {91int v;92const double scale = (double)(1 << kGammaTabFix) / kGammaScale;93const double norm = 1. / 255.;94for (v = 0; v <= 255; ++v) {95kGammaToLinearTab[v] =96(uint16_t)(pow(norm * v, kGamma) * kGammaScale + .5);97}98for (v = 0; v <= kGammaTabSize; ++v) {99kLinearToGammaTab[v] = (int)(255. * pow(scale * v, 1. / kGamma) + .5);100}101kGammaTablesOk = 1;102}103}104105static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {106return kGammaToLinearTab[v];107}108109static WEBP_INLINE int Interpolate(int v) {110const int tab_pos = v >> (kGammaTabFix + 2); // integer part111const int x = v & ((kGammaTabScale << 2) - 1); // fractional part112const int v0 = kLinearToGammaTab[tab_pos];113const int v1 = kLinearToGammaTab[tab_pos + 1];114const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate115assert(tab_pos + 1 < kGammaTabSize + 1);116return y;117}118119// Convert a linear value 'v' to YUV_FIX+2 fixed-point precision120// U/V value, suitable for RGBToU/V calls.121static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {122const int y = Interpolate(base_value << shift); // final uplifted value123return (y + kGammaTabRounder) >> kGammaTabFix; // descale124}125126#else127128static void InitGammaTables(void) {}129static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }130static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {131return (int)(base_value << shift);132}133134#endif // USE_GAMMA_COMPRESSION135136//------------------------------------------------------------------------------137// RGB -> YUV conversion138139static int RGBToY(int r, int g, int b, VP8Random* const rg) {140return (rg == NULL) ? VP8RGBToY(r, g, b, YUV_HALF)141: VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));142}143144static int RGBToU(int r, int g, int b, VP8Random* const rg) {145return (rg == NULL) ? VP8RGBToU(r, g, b, YUV_HALF << 2)146: VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));147}148149static int RGBToV(int r, int g, int b, VP8Random* const rg) {150return (rg == NULL) ? VP8RGBToV(r, g, b, YUV_HALF << 2)151: VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));152}153154//------------------------------------------------------------------------------155// Sharp RGB->YUV conversion156157static const int kNumIterations = 4;158static const int kMinDimensionIterativeConversion = 4;159160// We could use SFIX=0 and only uint8_t for fixed_y_t, but it produces some161// banding sometimes. Better use extra precision.162#define SFIX 2 // fixed-point precision of RGB and Y/W163typedef int16_t fixed_t; // signed type with extra SFIX precision for UV164typedef uint16_t fixed_y_t; // unsigned type with extra SFIX precision for W165166#define SHALF (1 << SFIX >> 1)167#define MAX_Y_T ((256 << SFIX) - 1)168#define SROUNDER (1 << (YUV_FIX + SFIX - 1))169170#if defined(USE_GAMMA_COMPRESSION)171172// We use tables of different size and precision for the Rec709 / BT2020173// transfer function.174#define kGammaF (1./0.45)175static uint32_t kLinearToGammaTabS[kGammaTabSize + 2];176#define GAMMA_TO_LINEAR_BITS 14177static uint32_t kGammaToLinearTabS[MAX_Y_T + 1]; // size scales with Y_FIX178static volatile int kGammaTablesSOk = 0;179180static WEBP_TSAN_IGNORE_FUNCTION void InitGammaTablesS(void) {181assert(2 * GAMMA_TO_LINEAR_BITS < 32); // we use uint32_t intermediate values182if (!kGammaTablesSOk) {183int v;184const double norm = 1. / MAX_Y_T;185const double scale = 1. / kGammaTabSize;186const double a = 0.09929682680944;187const double thresh = 0.018053968510807;188const double final_scale = 1 << GAMMA_TO_LINEAR_BITS;189for (v = 0; v <= MAX_Y_T; ++v) {190const double g = norm * v;191double value;192if (g <= thresh * 4.5) {193value = g / 4.5;194} else {195const double a_rec = 1. / (1. + a);196value = pow(a_rec * (g + a), kGammaF);197}198kGammaToLinearTabS[v] = (uint32_t)(value * final_scale + .5);199}200for (v = 0; v <= kGammaTabSize; ++v) {201const double g = scale * v;202double value;203if (g <= thresh) {204value = 4.5 * g;205} else {206value = (1. + a) * pow(g, 1. / kGammaF) - a;207}208// we already incorporate the 1/2 rounding constant here209kLinearToGammaTabS[v] =210(uint32_t)(MAX_Y_T * value) + (1 << GAMMA_TO_LINEAR_BITS >> 1);211}212// to prevent small rounding errors to cause read-overflow:213kLinearToGammaTabS[kGammaTabSize + 1] = kLinearToGammaTabS[kGammaTabSize];214kGammaTablesSOk = 1;215}216}217218// return value has a fixed-point precision of GAMMA_TO_LINEAR_BITS219static WEBP_INLINE uint32_t GammaToLinearS(int v) {220return kGammaToLinearTabS[v];221}222223static WEBP_INLINE uint32_t LinearToGammaS(uint32_t value) {224// 'value' is in GAMMA_TO_LINEAR_BITS fractional precision225const uint32_t v = value * kGammaTabSize;226const uint32_t tab_pos = v >> GAMMA_TO_LINEAR_BITS;227// fractional part, in GAMMA_TO_LINEAR_BITS fixed-point precision228const uint32_t x = v - (tab_pos << GAMMA_TO_LINEAR_BITS); // fractional part229// v0 / v1 are in GAMMA_TO_LINEAR_BITS fixed-point precision (range [0..1])230const uint32_t v0 = kLinearToGammaTabS[tab_pos + 0];231const uint32_t v1 = kLinearToGammaTabS[tab_pos + 1];232// Final interpolation. Note that rounding is already included.233const uint32_t v2 = (v1 - v0) * x; // note: v1 >= v0.234const uint32_t result = v0 + (v2 >> GAMMA_TO_LINEAR_BITS);235return result;236}237238#else239240static void InitGammaTablesS(void) {}241static WEBP_INLINE uint32_t GammaToLinearS(int v) {242return (v << GAMMA_TO_LINEAR_BITS) / MAX_Y_T;243}244static WEBP_INLINE uint32_t LinearToGammaS(uint32_t value) {245return (MAX_Y_T * value) >> GAMMA_TO_LINEAR_BITS;246}247248#endif // USE_GAMMA_COMPRESSION249250//------------------------------------------------------------------------------251252static uint8_t clip_8b(fixed_t v) {253return (!(v & ~0xff)) ? (uint8_t)v : (v < 0) ? 0u : 255u;254}255256static fixed_y_t clip_y(int y) {257return (!(y & ~MAX_Y_T)) ? (fixed_y_t)y : (y < 0) ? 0 : MAX_Y_T;258}259260//------------------------------------------------------------------------------261262static int RGBToGray(int r, int g, int b) {263const int luma = 13933 * r + 46871 * g + 4732 * b + YUV_HALF;264return (luma >> YUV_FIX);265}266267static uint32_t ScaleDown(int a, int b, int c, int d) {268const uint32_t A = GammaToLinearS(a);269const uint32_t B = GammaToLinearS(b);270const uint32_t C = GammaToLinearS(c);271const uint32_t D = GammaToLinearS(d);272return LinearToGammaS((A + B + C + D + 2) >> 2);273}274275static WEBP_INLINE void UpdateW(const fixed_y_t* src, fixed_y_t* dst, int w) {276int i;277for (i = 0; i < w; ++i) {278const uint32_t R = GammaToLinearS(src[0 * w + i]);279const uint32_t G = GammaToLinearS(src[1 * w + i]);280const uint32_t B = GammaToLinearS(src[2 * w + i]);281const uint32_t Y = RGBToGray(R, G, B);282dst[i] = (fixed_y_t)LinearToGammaS(Y);283}284}285286static void UpdateChroma(const fixed_y_t* src1, const fixed_y_t* src2,287fixed_t* dst, int uv_w) {288int i;289for (i = 0; i < uv_w; ++i) {290const int r = ScaleDown(src1[0 * uv_w + 0], src1[0 * uv_w + 1],291src2[0 * uv_w + 0], src2[0 * uv_w + 1]);292const int g = ScaleDown(src1[2 * uv_w + 0], src1[2 * uv_w + 1],293src2[2 * uv_w + 0], src2[2 * uv_w + 1]);294const int b = ScaleDown(src1[4 * uv_w + 0], src1[4 * uv_w + 1],295src2[4 * uv_w + 0], src2[4 * uv_w + 1]);296const int W = RGBToGray(r, g, b);297dst[0 * uv_w] = (fixed_t)(r - W);298dst[1 * uv_w] = (fixed_t)(g - W);299dst[2 * uv_w] = (fixed_t)(b - W);300dst += 1;301src1 += 2;302src2 += 2;303}304}305306static void StoreGray(const fixed_y_t* rgb, fixed_y_t* y, int w) {307int i;308for (i = 0; i < w; ++i) {309y[i] = RGBToGray(rgb[0 * w + i], rgb[1 * w + i], rgb[2 * w + i]);310}311}312313//------------------------------------------------------------------------------314315static WEBP_INLINE fixed_y_t Filter2(int A, int B, int W0) {316const int v0 = (A * 3 + B + 2) >> 2;317return clip_y(v0 + W0);318}319320//------------------------------------------------------------------------------321322static WEBP_INLINE fixed_y_t UpLift(uint8_t a) { // 8bit -> SFIX323return ((fixed_y_t)a << SFIX) | SHALF;324}325326static void ImportOneRow(const uint8_t* const r_ptr,327const uint8_t* const g_ptr,328const uint8_t* const b_ptr,329int step,330int pic_width,331fixed_y_t* const dst) {332int i;333const int w = (pic_width + 1) & ~1;334for (i = 0; i < pic_width; ++i) {335const int off = i * step;336dst[i + 0 * w] = UpLift(r_ptr[off]);337dst[i + 1 * w] = UpLift(g_ptr[off]);338dst[i + 2 * w] = UpLift(b_ptr[off]);339}340if (pic_width & 1) { // replicate rightmost pixel341dst[pic_width + 0 * w] = dst[pic_width + 0 * w - 1];342dst[pic_width + 1 * w] = dst[pic_width + 1 * w - 1];343dst[pic_width + 2 * w] = dst[pic_width + 2 * w - 1];344}345}346347static void InterpolateTwoRows(const fixed_y_t* const best_y,348const fixed_t* prev_uv,349const fixed_t* cur_uv,350const fixed_t* next_uv,351int w,352fixed_y_t* out1,353fixed_y_t* out2) {354const int uv_w = w >> 1;355const int len = (w - 1) >> 1; // length to filter356int k = 3;357while (k-- > 0) { // process each R/G/B segments in turn358// special boundary case for i==0359out1[0] = Filter2(cur_uv[0], prev_uv[0], best_y[0]);360out2[0] = Filter2(cur_uv[0], next_uv[0], best_y[w]);361362WebPSharpYUVFilterRow(cur_uv, prev_uv, len, best_y + 0 + 1, out1 + 1);363WebPSharpYUVFilterRow(cur_uv, next_uv, len, best_y + w + 1, out2 + 1);364365// special boundary case for i == w - 1 when w is even366if (!(w & 1)) {367out1[w - 1] = Filter2(cur_uv[uv_w - 1], prev_uv[uv_w - 1],368best_y[w - 1 + 0]);369out2[w - 1] = Filter2(cur_uv[uv_w - 1], next_uv[uv_w - 1],370best_y[w - 1 + w]);371}372out1 += w;373out2 += w;374prev_uv += uv_w;375cur_uv += uv_w;376next_uv += uv_w;377}378}379380static WEBP_INLINE uint8_t ConvertRGBToY(int r, int g, int b) {381const int luma = 16839 * r + 33059 * g + 6420 * b + SROUNDER;382return clip_8b(16 + (luma >> (YUV_FIX + SFIX)));383}384385static WEBP_INLINE uint8_t ConvertRGBToU(int r, int g, int b) {386const int u = -9719 * r - 19081 * g + 28800 * b + SROUNDER;387return clip_8b(128 + (u >> (YUV_FIX + SFIX)));388}389390static WEBP_INLINE uint8_t ConvertRGBToV(int r, int g, int b) {391const int v = +28800 * r - 24116 * g - 4684 * b + SROUNDER;392return clip_8b(128 + (v >> (YUV_FIX + SFIX)));393}394395static int ConvertWRGBToYUV(const fixed_y_t* best_y, const fixed_t* best_uv,396WebPPicture* const picture) {397int i, j;398uint8_t* dst_y = picture->y;399uint8_t* dst_u = picture->u;400uint8_t* dst_v = picture->v;401const fixed_t* const best_uv_base = best_uv;402const int w = (picture->width + 1) & ~1;403const int h = (picture->height + 1) & ~1;404const int uv_w = w >> 1;405const int uv_h = h >> 1;406for (best_uv = best_uv_base, j = 0; j < picture->height; ++j) {407for (i = 0; i < picture->width; ++i) {408const int off = (i >> 1);409const int W = best_y[i];410const int r = best_uv[off + 0 * uv_w] + W;411const int g = best_uv[off + 1 * uv_w] + W;412const int b = best_uv[off + 2 * uv_w] + W;413dst_y[i] = ConvertRGBToY(r, g, b);414}415best_y += w;416best_uv += (j & 1) * 3 * uv_w;417dst_y += picture->y_stride;418}419for (best_uv = best_uv_base, j = 0; j < uv_h; ++j) {420for (i = 0; i < uv_w; ++i) {421const int off = i;422const int r = best_uv[off + 0 * uv_w];423const int g = best_uv[off + 1 * uv_w];424const int b = best_uv[off + 2 * uv_w];425dst_u[i] = ConvertRGBToU(r, g, b);426dst_v[i] = ConvertRGBToV(r, g, b);427}428best_uv += 3 * uv_w;429dst_u += picture->uv_stride;430dst_v += picture->uv_stride;431}432return 1;433}434435//------------------------------------------------------------------------------436// Main function437438#define SAFE_ALLOC(W, H, T) ((T*)WebPSafeMalloc((W) * (H), sizeof(T)))439440static int PreprocessARGB(const uint8_t* r_ptr,441const uint8_t* g_ptr,442const uint8_t* b_ptr,443int step, int rgb_stride,444WebPPicture* const picture) {445// we expand the right/bottom border if needed446const int w = (picture->width + 1) & ~1;447const int h = (picture->height + 1) & ~1;448const int uv_w = w >> 1;449const int uv_h = h >> 1;450uint64_t prev_diff_y_sum = ~0;451int j, iter;452453// TODO(skal): allocate one big memory chunk. But for now, it's easier454// for valgrind debugging to have several chunks.455fixed_y_t* const tmp_buffer = SAFE_ALLOC(w * 3, 2, fixed_y_t); // scratch456fixed_y_t* const best_y_base = SAFE_ALLOC(w, h, fixed_y_t);457fixed_y_t* const target_y_base = SAFE_ALLOC(w, h, fixed_y_t);458fixed_y_t* const best_rgb_y = SAFE_ALLOC(w, 2, fixed_y_t);459fixed_t* const best_uv_base = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);460fixed_t* const target_uv_base = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);461fixed_t* const best_rgb_uv = SAFE_ALLOC(uv_w * 3, 1, fixed_t);462fixed_y_t* best_y = best_y_base;463fixed_y_t* target_y = target_y_base;464fixed_t* best_uv = best_uv_base;465fixed_t* target_uv = target_uv_base;466const uint64_t diff_y_threshold = (uint64_t)(3.0 * w * h);467int ok;468469if (best_y_base == NULL || best_uv_base == NULL ||470target_y_base == NULL || target_uv_base == NULL ||471best_rgb_y == NULL || best_rgb_uv == NULL ||472tmp_buffer == NULL) {473ok = WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);474goto End;475}476assert(picture->width >= kMinDimensionIterativeConversion);477assert(picture->height >= kMinDimensionIterativeConversion);478479WebPInitConvertARGBToYUV();480481// Import RGB samples to W/RGB representation.482for (j = 0; j < picture->height; j += 2) {483const int is_last_row = (j == picture->height - 1);484fixed_y_t* const src1 = tmp_buffer + 0 * w;485fixed_y_t* const src2 = tmp_buffer + 3 * w;486487// prepare two rows of input488ImportOneRow(r_ptr, g_ptr, b_ptr, step, picture->width, src1);489if (!is_last_row) {490ImportOneRow(r_ptr + rgb_stride, g_ptr + rgb_stride, b_ptr + rgb_stride,491step, picture->width, src2);492} else {493memcpy(src2, src1, 3 * w * sizeof(*src2));494}495StoreGray(src1, best_y + 0, w);496StoreGray(src2, best_y + w, w);497498UpdateW(src1, target_y, w);499UpdateW(src2, target_y + w, w);500UpdateChroma(src1, src2, target_uv, uv_w);501memcpy(best_uv, target_uv, 3 * uv_w * sizeof(*best_uv));502best_y += 2 * w;503best_uv += 3 * uv_w;504target_y += 2 * w;505target_uv += 3 * uv_w;506r_ptr += 2 * rgb_stride;507g_ptr += 2 * rgb_stride;508b_ptr += 2 * rgb_stride;509}510511// Iterate and resolve clipping conflicts.512for (iter = 0; iter < kNumIterations; ++iter) {513const fixed_t* cur_uv = best_uv_base;514const fixed_t* prev_uv = best_uv_base;515uint64_t diff_y_sum = 0;516517best_y = best_y_base;518best_uv = best_uv_base;519target_y = target_y_base;520target_uv = target_uv_base;521for (j = 0; j < h; j += 2) {522fixed_y_t* const src1 = tmp_buffer + 0 * w;523fixed_y_t* const src2 = tmp_buffer + 3 * w;524{525const fixed_t* const next_uv = cur_uv + ((j < h - 2) ? 3 * uv_w : 0);526InterpolateTwoRows(best_y, prev_uv, cur_uv, next_uv, w, src1, src2);527prev_uv = cur_uv;528cur_uv = next_uv;529}530531UpdateW(src1, best_rgb_y + 0 * w, w);532UpdateW(src2, best_rgb_y + 1 * w, w);533UpdateChroma(src1, src2, best_rgb_uv, uv_w);534535// update two rows of Y and one row of RGB536diff_y_sum += WebPSharpYUVUpdateY(target_y, best_rgb_y, best_y, 2 * w);537WebPSharpYUVUpdateRGB(target_uv, best_rgb_uv, best_uv, 3 * uv_w);538539best_y += 2 * w;540best_uv += 3 * uv_w;541target_y += 2 * w;542target_uv += 3 * uv_w;543}544// test exit condition545if (iter > 0) {546if (diff_y_sum < diff_y_threshold) break;547if (diff_y_sum > prev_diff_y_sum) break;548}549prev_diff_y_sum = diff_y_sum;550}551// final reconstruction552ok = ConvertWRGBToYUV(best_y_base, best_uv_base, picture);553554End:555WebPSafeFree(best_y_base);556WebPSafeFree(best_uv_base);557WebPSafeFree(target_y_base);558WebPSafeFree(target_uv_base);559WebPSafeFree(best_rgb_y);560WebPSafeFree(best_rgb_uv);561WebPSafeFree(tmp_buffer);562return ok;563}564#undef SAFE_ALLOC565566//------------------------------------------------------------------------------567// "Fast" regular RGB->YUV568569#define SUM4(ptr, step) LinearToGamma( \570GammaToLinear((ptr)[0]) + \571GammaToLinear((ptr)[(step)]) + \572GammaToLinear((ptr)[rgb_stride]) + \573GammaToLinear((ptr)[rgb_stride + (step)]), 0) \574575#define SUM2(ptr) \576LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)577578#define SUM2ALPHA(ptr) ((ptr)[0] + (ptr)[rgb_stride])579#define SUM4ALPHA(ptr) (SUM2ALPHA(ptr) + SUM2ALPHA((ptr) + 4))580581#if defined(USE_INVERSE_ALPHA_TABLE)582583static const int kAlphaFix = 19;584// Following table is (1 << kAlphaFix) / a. The (v * kInvAlpha[a]) >> kAlphaFix585// formula is then equal to v / a in most (99.6%) cases. Note that this table586// and constant are adjusted very tightly to fit 32b arithmetic.587// In particular, they use the fact that the operands for 'v / a' are actually588// derived as v = (a0.p0 + a1.p1 + a2.p2 + a3.p3) and a = a0 + a1 + a2 + a3589// with ai in [0..255] and pi in [0..1<<kGammaFix). The constraint to avoid590// overflow is: kGammaFix + kAlphaFix <= 31.591static const uint32_t kInvAlpha[4 * 0xff + 1] = {5920, /* alpha = 0 */593524288, 262144, 174762, 131072, 104857, 87381, 74898, 65536,59458254, 52428, 47662, 43690, 40329, 37449, 34952, 32768,59530840, 29127, 27594, 26214, 24966, 23831, 22795, 21845,59620971, 20164, 19418, 18724, 18078, 17476, 16912, 16384,59715887, 15420, 14979, 14563, 14169, 13797, 13443, 13107,59812787, 12483, 12192, 11915, 11650, 11397, 11155, 10922,59910699, 10485, 10280, 10082, 9892, 9709, 9532, 9362,6009198, 9039, 8886, 8738, 8594, 8456, 8322, 8192,6018065, 7943, 7825, 7710, 7598, 7489, 7384, 7281,6027182, 7084, 6990, 6898, 6808, 6721, 6636, 6553,6036472, 6393, 6316, 6241, 6168, 6096, 6026, 5957,6045890, 5825, 5761, 5698, 5637, 5577, 5518, 5461,6055405, 5349, 5295, 5242, 5190, 5140, 5090, 5041,6064993, 4946, 4899, 4854, 4809, 4766, 4723, 4681,6074639, 4599, 4559, 4519, 4481, 4443, 4405, 4369,6084332, 4297, 4262, 4228, 4194, 4161, 4128, 4096,6094064, 4032, 4002, 3971, 3942, 3912, 3883, 3855,6103826, 3799, 3771, 3744, 3718, 3692, 3666, 3640,6113615, 3591, 3566, 3542, 3518, 3495, 3472, 3449,6123426, 3404, 3382, 3360, 3339, 3318, 3297, 3276,6133256, 3236, 3216, 3196, 3177, 3158, 3139, 3120,6143102, 3084, 3066, 3048, 3030, 3013, 2995, 2978,6152962, 2945, 2928, 2912, 2896, 2880, 2864, 2849,6162833, 2818, 2803, 2788, 2774, 2759, 2744, 2730,6172716, 2702, 2688, 2674, 2661, 2647, 2634, 2621,6182608, 2595, 2582, 2570, 2557, 2545, 2532, 2520,6192508, 2496, 2484, 2473, 2461, 2449, 2438, 2427,6202416, 2404, 2394, 2383, 2372, 2361, 2351, 2340,6212330, 2319, 2309, 2299, 2289, 2279, 2269, 2259,6222250, 2240, 2231, 2221, 2212, 2202, 2193, 2184,6232175, 2166, 2157, 2148, 2139, 2131, 2122, 2114,6242105, 2097, 2088, 2080, 2072, 2064, 2056, 2048,6252040, 2032, 2024, 2016, 2008, 2001, 1993, 1985,6261978, 1971, 1963, 1956, 1949, 1941, 1934, 1927,6271920, 1913, 1906, 1899, 1892, 1885, 1879, 1872,6281865, 1859, 1852, 1846, 1839, 1833, 1826, 1820,6291814, 1807, 1801, 1795, 1789, 1783, 1777, 1771,6301765, 1759, 1753, 1747, 1741, 1736, 1730, 1724,6311718, 1713, 1707, 1702, 1696, 1691, 1685, 1680,6321675, 1669, 1664, 1659, 1653, 1648, 1643, 1638,6331633, 1628, 1623, 1618, 1613, 1608, 1603, 1598,6341593, 1588, 1583, 1579, 1574, 1569, 1565, 1560,6351555, 1551, 1546, 1542, 1537, 1533, 1528, 1524,6361519, 1515, 1510, 1506, 1502, 1497, 1493, 1489,6371485, 1481, 1476, 1472, 1468, 1464, 1460, 1456,6381452, 1448, 1444, 1440, 1436, 1432, 1428, 1424,6391420, 1416, 1413, 1409, 1405, 1401, 1398, 1394,6401390, 1387, 1383, 1379, 1376, 1372, 1368, 1365,6411361, 1358, 1354, 1351, 1347, 1344, 1340, 1337,6421334, 1330, 1327, 1323, 1320, 1317, 1314, 1310,6431307, 1304, 1300, 1297, 1294, 1291, 1288, 1285,6441281, 1278, 1275, 1272, 1269, 1266, 1263, 1260,6451257, 1254, 1251, 1248, 1245, 1242, 1239, 1236,6461233, 1230, 1227, 1224, 1222, 1219, 1216, 1213,6471210, 1208, 1205, 1202, 1199, 1197, 1194, 1191,6481188, 1186, 1183, 1180, 1178, 1175, 1172, 1170,6491167, 1165, 1162, 1159, 1157, 1154, 1152, 1149,6501147, 1144, 1142, 1139, 1137, 1134, 1132, 1129,6511127, 1125, 1122, 1120, 1117, 1115, 1113, 1110,6521108, 1106, 1103, 1101, 1099, 1096, 1094, 1092,6531089, 1087, 1085, 1083, 1081, 1078, 1076, 1074,6541072, 1069, 1067, 1065, 1063, 1061, 1059, 1057,6551054, 1052, 1050, 1048, 1046, 1044, 1042, 1040,6561038, 1036, 1034, 1032, 1030, 1028, 1026, 1024,6571022, 1020, 1018, 1016, 1014, 1012, 1010, 1008,6581006, 1004, 1002, 1000, 998, 996, 994, 992,659991, 989, 987, 985, 983, 981, 979, 978,660976, 974, 972, 970, 969, 967, 965, 963,661961, 960, 958, 956, 954, 953, 951, 949,662948, 946, 944, 942, 941, 939, 937, 936,663934, 932, 931, 929, 927, 926, 924, 923,664921, 919, 918, 916, 914, 913, 911, 910,665908, 907, 905, 903, 902, 900, 899, 897,666896, 894, 893, 891, 890, 888, 887, 885,667884, 882, 881, 879, 878, 876, 875, 873,668872, 870, 869, 868, 866, 865, 863, 862,669860, 859, 858, 856, 855, 853, 852, 851,670849, 848, 846, 845, 844, 842, 841, 840,671838, 837, 836, 834, 833, 832, 830, 829,672828, 826, 825, 824, 823, 821, 820, 819,673817, 816, 815, 814, 812, 811, 810, 809,674807, 806, 805, 804, 802, 801, 800, 799,675798, 796, 795, 794, 793, 791, 790, 789,676788, 787, 786, 784, 783, 782, 781, 780,677779, 777, 776, 775, 774, 773, 772, 771,678769, 768, 767, 766, 765, 764, 763, 762,679760, 759, 758, 757, 756, 755, 754, 753,680752, 751, 750, 748, 747, 746, 745, 744,681743, 742, 741, 740, 739, 738, 737, 736,682735, 734, 733, 732, 731, 730, 729, 728,683727, 726, 725, 724, 723, 722, 721, 720,684719, 718, 717, 716, 715, 714, 713, 712,685711, 710, 709, 708, 707, 706, 705, 704,686703, 702, 701, 700, 699, 699, 698, 697,687696, 695, 694, 693, 692, 691, 690, 689,688688, 688, 687, 686, 685, 684, 683, 682,689681, 680, 680, 679, 678, 677, 676, 675,690674, 673, 673, 672, 671, 670, 669, 668,691667, 667, 666, 665, 664, 663, 662, 661,692661, 660, 659, 658, 657, 657, 656, 655,693654, 653, 652, 652, 651, 650, 649, 648,694648, 647, 646, 645, 644, 644, 643, 642,695641, 640, 640, 639, 638, 637, 637, 636,696635, 634, 633, 633, 632, 631, 630, 630,697629, 628, 627, 627, 626, 625, 624, 624,698623, 622, 621, 621, 620, 619, 618, 618,699617, 616, 616, 615, 614, 613, 613, 612,700611, 611, 610, 609, 608, 608, 607, 606,701606, 605, 604, 604, 603, 602, 601, 601,702600, 599, 599, 598, 597, 597, 596, 595,703595, 594, 593, 593, 592, 591, 591, 590,704589, 589, 588, 587, 587, 586, 585, 585,705584, 583, 583, 582, 581, 581, 580, 579,706579, 578, 578, 577, 576, 576, 575, 574,707574, 573, 572, 572, 571, 571, 570, 569,708569, 568, 568, 567, 566, 566, 565, 564,709564, 563, 563, 562, 561, 561, 560, 560,710559, 558, 558, 557, 557, 556, 555, 555,711554, 554, 553, 553, 552, 551, 551, 550,712550, 549, 548, 548, 547, 547, 546, 546,713545, 544, 544, 543, 543, 542, 542, 541,714541, 540, 539, 539, 538, 538, 537, 537,715536, 536, 535, 534, 534, 533, 533, 532,716532, 531, 531, 530, 530, 529, 529, 528,717527, 527, 526, 526, 525, 525, 524, 524,718523, 523, 522, 522, 521, 521, 520, 520,719519, 519, 518, 518, 517, 517, 516, 516,720515, 515, 514, 514721};722723// Note that LinearToGamma() expects the values to be premultiplied by 4,724// so we incorporate this factor 4 inside the DIVIDE_BY_ALPHA macro directly.725#define DIVIDE_BY_ALPHA(sum, a) (((sum) * kInvAlpha[(a)]) >> (kAlphaFix - 2))726727#else728729#define DIVIDE_BY_ALPHA(sum, a) (4 * (sum) / (a))730731#endif // USE_INVERSE_ALPHA_TABLE732733static WEBP_INLINE int LinearToGammaWeighted(const uint8_t* src,734const uint8_t* a_ptr,735uint32_t total_a, int step,736int rgb_stride) {737const uint32_t sum =738a_ptr[0] * GammaToLinear(src[0]) +739a_ptr[step] * GammaToLinear(src[step]) +740a_ptr[rgb_stride] * GammaToLinear(src[rgb_stride]) +741a_ptr[rgb_stride + step] * GammaToLinear(src[rgb_stride + step]);742assert(total_a > 0 && total_a <= 4 * 0xff);743#if defined(USE_INVERSE_ALPHA_TABLE)744assert((uint64_t)sum * kInvAlpha[total_a] < ((uint64_t)1 << 32));745#endif746return LinearToGamma(DIVIDE_BY_ALPHA(sum, total_a), 0);747}748749static WEBP_INLINE void ConvertRowToY(const uint8_t* const r_ptr,750const uint8_t* const g_ptr,751const uint8_t* const b_ptr,752int step,753uint8_t* const dst_y,754int width,755VP8Random* const rg) {756int i, j;757for (i = 0, j = 0; i < width; i += 1, j += step) {758dst_y[i] = RGBToY(r_ptr[j], g_ptr[j], b_ptr[j], rg);759}760}761762static WEBP_INLINE void AccumulateRGBA(const uint8_t* const r_ptr,763const uint8_t* const g_ptr,764const uint8_t* const b_ptr,765const uint8_t* const a_ptr,766int rgb_stride,767uint16_t* dst, int width) {768int i, j;769// we loop over 2x2 blocks and produce one R/G/B/A value for each.770for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * 4, dst += 4) {771const uint32_t a = SUM4ALPHA(a_ptr + j);772int r, g, b;773if (a == 4 * 0xff || a == 0) {774r = SUM4(r_ptr + j, 4);775g = SUM4(g_ptr + j, 4);776b = SUM4(b_ptr + j, 4);777} else {778r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 4, rgb_stride);779g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 4, rgb_stride);780b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 4, rgb_stride);781}782dst[0] = r;783dst[1] = g;784dst[2] = b;785dst[3] = a;786}787if (width & 1) {788const uint32_t a = 2u * SUM2ALPHA(a_ptr + j);789int r, g, b;790if (a == 4 * 0xff || a == 0) {791r = SUM2(r_ptr + j);792g = SUM2(g_ptr + j);793b = SUM2(b_ptr + j);794} else {795r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 0, rgb_stride);796g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 0, rgb_stride);797b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 0, rgb_stride);798}799dst[0] = r;800dst[1] = g;801dst[2] = b;802dst[3] = a;803}804}805806static WEBP_INLINE void AccumulateRGB(const uint8_t* const r_ptr,807const uint8_t* const g_ptr,808const uint8_t* const b_ptr,809int step, int rgb_stride,810uint16_t* dst, int width) {811int i, j;812for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * step, dst += 4) {813dst[0] = SUM4(r_ptr + j, step);814dst[1] = SUM4(g_ptr + j, step);815dst[2] = SUM4(b_ptr + j, step);816}817if (width & 1) {818dst[0] = SUM2(r_ptr + j);819dst[1] = SUM2(g_ptr + j);820dst[2] = SUM2(b_ptr + j);821}822}823824static WEBP_INLINE void ConvertRowsToUV(const uint16_t* rgb,825uint8_t* const dst_u,826uint8_t* const dst_v,827int width,828VP8Random* const rg) {829int i;830for (i = 0; i < width; i += 1, rgb += 4) {831const int r = rgb[0], g = rgb[1], b = rgb[2];832dst_u[i] = RGBToU(r, g, b, rg);833dst_v[i] = RGBToV(r, g, b, rg);834}835}836837static int ImportYUVAFromRGBA(const uint8_t* r_ptr,838const uint8_t* g_ptr,839const uint8_t* b_ptr,840const uint8_t* a_ptr,841int step, // bytes per pixel842int rgb_stride, // bytes per scanline843float dithering,844int use_iterative_conversion,845WebPPicture* const picture) {846int y;847const int width = picture->width;848const int height = picture->height;849const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);850const int is_rgb = (r_ptr < b_ptr); // otherwise it's bgr851852picture->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;853picture->use_argb = 0;854855// disable smart conversion if source is too small (overkill).856if (width < kMinDimensionIterativeConversion ||857height < kMinDimensionIterativeConversion) {858use_iterative_conversion = 0;859}860861if (!WebPPictureAllocYUVA(picture, width, height)) {862return 0;863}864if (has_alpha) {865assert(step == 4);866#if defined(USE_GAMMA_COMPRESSION) && defined(USE_INVERSE_ALPHA_TABLE)867assert(kAlphaFix + kGammaFix <= 31);868#endif869}870871if (use_iterative_conversion) {872InitGammaTablesS();873if (!PreprocessARGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, picture)) {874return 0;875}876if (has_alpha) {877WebPExtractAlpha(a_ptr, rgb_stride, width, height,878picture->a, picture->a_stride);879}880} else {881const int uv_width = (width + 1) >> 1;882int use_dsp = (step == 3); // use special function in this case883// temporary storage for accumulated R/G/B values during conversion to U/V884uint16_t* const tmp_rgb =885(uint16_t*)WebPSafeMalloc(4 * uv_width, sizeof(*tmp_rgb));886uint8_t* dst_y = picture->y;887uint8_t* dst_u = picture->u;888uint8_t* dst_v = picture->v;889uint8_t* dst_a = picture->a;890891VP8Random base_rg;892VP8Random* rg = NULL;893if (dithering > 0.) {894VP8InitRandom(&base_rg, dithering);895rg = &base_rg;896use_dsp = 0; // can't use dsp in this case897}898WebPInitConvertARGBToYUV();899InitGammaTables();900901if (tmp_rgb == NULL) return 0; // malloc error902903// Downsample Y/U/V planes, two rows at a time904for (y = 0; y < (height >> 1); ++y) {905int rows_have_alpha = has_alpha;906if (use_dsp) {907if (is_rgb) {908WebPConvertRGB24ToY(r_ptr, dst_y, width);909WebPConvertRGB24ToY(r_ptr + rgb_stride,910dst_y + picture->y_stride, width);911} else {912WebPConvertBGR24ToY(b_ptr, dst_y, width);913WebPConvertBGR24ToY(b_ptr + rgb_stride,914dst_y + picture->y_stride, width);915}916} else {917ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);918ConvertRowToY(r_ptr + rgb_stride,919g_ptr + rgb_stride,920b_ptr + rgb_stride, step,921dst_y + picture->y_stride, width, rg);922}923dst_y += 2 * picture->y_stride;924if (has_alpha) {925rows_have_alpha &= !WebPExtractAlpha(a_ptr, rgb_stride, width, 2,926dst_a, picture->a_stride);927dst_a += 2 * picture->a_stride;928}929// Collect averaged R/G/B(/A)930if (!rows_have_alpha) {931AccumulateRGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, tmp_rgb, width);932} else {933AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, rgb_stride, tmp_rgb, width);934}935// Convert to U/V936if (rg == NULL) {937WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);938} else {939ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);940}941dst_u += picture->uv_stride;942dst_v += picture->uv_stride;943r_ptr += 2 * rgb_stride;944b_ptr += 2 * rgb_stride;945g_ptr += 2 * rgb_stride;946if (has_alpha) a_ptr += 2 * rgb_stride;947}948if (height & 1) { // extra last row949int row_has_alpha = has_alpha;950if (use_dsp) {951if (r_ptr < b_ptr) {952WebPConvertRGB24ToY(r_ptr, dst_y, width);953} else {954WebPConvertBGR24ToY(b_ptr, dst_y, width);955}956} else {957ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);958}959if (row_has_alpha) {960row_has_alpha &= !WebPExtractAlpha(a_ptr, 0, width, 1, dst_a, 0);961}962// Collect averaged R/G/B(/A)963if (!row_has_alpha) {964// Collect averaged R/G/B965AccumulateRGB(r_ptr, g_ptr, b_ptr, step, /* rgb_stride = */ 0,966tmp_rgb, width);967} else {968AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, /* rgb_stride = */ 0,969tmp_rgb, width);970}971if (rg == NULL) {972WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);973} else {974ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);975}976}977WebPSafeFree(tmp_rgb);978}979return 1;980}981982#undef SUM4983#undef SUM2984#undef SUM4ALPHA985#undef SUM2ALPHA986987//------------------------------------------------------------------------------988// call for ARGB->YUVA conversion989990static int PictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace,991float dithering, int use_iterative_conversion) {992if (picture == NULL) return 0;993if (picture->argb == NULL) {994return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);995} else if ((colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {996return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);997} else {998const uint8_t* const argb = (const uint8_t*)picture->argb;999const uint8_t* const a = argb + (0 ^ ALPHA_OFFSET);1000const uint8_t* const r = argb + (1 ^ ALPHA_OFFSET);1001const uint8_t* const g = argb + (2 ^ ALPHA_OFFSET);1002const uint8_t* const b = argb + (3 ^ ALPHA_OFFSET);10031004picture->colorspace = WEBP_YUV420;1005return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,1006dithering, use_iterative_conversion, picture);1007}1008}10091010int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,1011float dithering) {1012return PictureARGBToYUVA(picture, colorspace, dithering, 0);1013}10141015int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {1016return PictureARGBToYUVA(picture, colorspace, 0.f, 0);1017}10181019int WebPPictureSharpARGBToYUVA(WebPPicture* picture) {1020return PictureARGBToYUVA(picture, WEBP_YUV420, 0.f, 1);1021}1022// for backward compatibility1023int WebPPictureSmartARGBToYUVA(WebPPicture* picture) {1024return WebPPictureSharpARGBToYUVA(picture);1025}10261027//------------------------------------------------------------------------------1028// call for YUVA -> ARGB conversion10291030int WebPPictureYUVAToARGB(WebPPicture* picture) {1031if (picture == NULL) return 0;1032if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {1033return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);1034}1035if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {1036return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);1037}1038if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {1039return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);1040}1041// Allocate a new argb buffer (discarding the previous one).1042if (!WebPPictureAllocARGB(picture, picture->width, picture->height)) return 0;1043picture->use_argb = 1;10441045// Convert1046{1047int y;1048const int width = picture->width;1049const int height = picture->height;1050const int argb_stride = 4 * picture->argb_stride;1051uint8_t* dst = (uint8_t*)picture->argb;1052const uint8_t *cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;1053WebPUpsampleLinePairFunc upsample =1054WebPGetLinePairConverter(ALPHA_OFFSET > 0);10551056// First row, with replicated top samples.1057upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);1058cur_y += picture->y_stride;1059dst += argb_stride;1060// Center rows.1061for (y = 1; y + 1 < height; y += 2) {1062const uint8_t* const top_u = cur_u;1063const uint8_t* const top_v = cur_v;1064cur_u += picture->uv_stride;1065cur_v += picture->uv_stride;1066upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,1067dst, dst + argb_stride, width);1068cur_y += 2 * picture->y_stride;1069dst += 2 * argb_stride;1070}1071// Last row (if needed), with replicated bottom samples.1072if (height > 1 && !(height & 1)) {1073upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);1074}1075// Insert alpha values if needed, in replacement for the default 0xff ones.1076if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {1077for (y = 0; y < height; ++y) {1078uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;1079const uint8_t* const src = picture->a + y * picture->a_stride;1080int x;1081for (x = 0; x < width; ++x) {1082argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);1083}1084}1085}1086}1087return 1;1088}10891090//------------------------------------------------------------------------------1091// automatic import / conversion10921093static int Import(WebPPicture* const picture,1094const uint8_t* rgb, int rgb_stride,1095int step, int swap_rb, int import_alpha) {1096int y;1097// swap_rb -> b,g,r,a , !swap_rb -> r,g,b,a1098const uint8_t* r_ptr = rgb + (swap_rb ? 2 : 0);1099const uint8_t* g_ptr = rgb + 1;1100const uint8_t* b_ptr = rgb + (swap_rb ? 0 : 2);1101const int width = picture->width;1102const int height = picture->height;11031104if (!picture->use_argb) {1105const uint8_t* a_ptr = import_alpha ? rgb + 3 : NULL;1106return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,11070.f /* no dithering */, 0, picture);1108}1109if (!WebPPictureAlloc(picture)) return 0;11101111VP8LDspInit();1112WebPInitAlphaProcessing();11131114if (import_alpha) {1115// dst[] byte order is {a,r,g,b} for big-endian, {b,g,r,a} for little endian1116uint32_t* dst = picture->argb;1117const int do_copy = (ALPHA_OFFSET == 3) && swap_rb;1118assert(step == 4);1119if (do_copy) {1120for (y = 0; y < height; ++y) {1121memcpy(dst, rgb, width * 4);1122rgb += rgb_stride;1123dst += picture->argb_stride;1124}1125} else {1126for (y = 0; y < height; ++y) {1127#ifdef WORDS_BIGENDIAN1128// BGRA or RGBA input order.1129const uint8_t* a_ptr = rgb + 3;1130WebPPackARGB(a_ptr, r_ptr, g_ptr, b_ptr, width, dst);1131r_ptr += rgb_stride;1132g_ptr += rgb_stride;1133b_ptr += rgb_stride;1134#else1135// RGBA input order. Need to swap R and B.1136VP8LConvertBGRAToRGBA((const uint32_t*)rgb, width, (uint8_t*)dst);1137#endif1138rgb += rgb_stride;1139dst += picture->argb_stride;1140}1141}1142} else {1143uint32_t* dst = picture->argb;1144assert(step >= 3);1145for (y = 0; y < height; ++y) {1146WebPPackRGB(r_ptr, g_ptr, b_ptr, width, step, dst);1147r_ptr += rgb_stride;1148g_ptr += rgb_stride;1149b_ptr += rgb_stride;1150dst += picture->argb_stride;1151}1152}1153return 1;1154}11551156// Public API11571158#if !defined(WEBP_REDUCE_CSP)11591160int WebPPictureImportBGR(WebPPicture* picture,1161const uint8_t* rgb, int rgb_stride) {1162return (picture != NULL && rgb != NULL)1163? Import(picture, rgb, rgb_stride, 3, 1, 0)1164: 0;1165}11661167int WebPPictureImportBGRA(WebPPicture* picture,1168const uint8_t* rgba, int rgba_stride) {1169return (picture != NULL && rgba != NULL)1170? Import(picture, rgba, rgba_stride, 4, 1, 1)1171: 0;1172}117311741175int WebPPictureImportBGRX(WebPPicture* picture,1176const uint8_t* rgba, int rgba_stride) {1177return (picture != NULL && rgba != NULL)1178? Import(picture, rgba, rgba_stride, 4, 1, 0)1179: 0;1180}11811182#endif // WEBP_REDUCE_CSP11831184int WebPPictureImportRGB(WebPPicture* picture,1185const uint8_t* rgb, int rgb_stride) {1186return (picture != NULL && rgb != NULL)1187? Import(picture, rgb, rgb_stride, 3, 0, 0)1188: 0;1189}11901191int WebPPictureImportRGBA(WebPPicture* picture,1192const uint8_t* rgba, int rgba_stride) {1193return (picture != NULL && rgba != NULL)1194? Import(picture, rgba, rgba_stride, 4, 0, 1)1195: 0;1196}11971198int WebPPictureImportRGBX(WebPPicture* picture,1199const uint8_t* rgba, int rgba_stride) {1200return (picture != NULL && rgba != NULL)1201? Import(picture, rgba, rgba_stride, 4, 0, 0)1202: 0;1203}12041205//------------------------------------------------------------------------------120612071208