Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dsp/lossless.c
Line
Count
Source
1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Image transforms and color space conversion methods for lossless decoder.
11
//
12
// Authors: Vikas Arora (vikaas.arora@gmail.com)
13
//          Jyrki Alakuijala (jyrki@google.com)
14
//          Urvang Joshi (urvang@google.com)
15
16
#include "src/dsp/lossless.h"
17
18
#include <assert.h>
19
#include <stdlib.h>
20
#include <string.h>
21
22
#include "src/dec/vp8li_dec.h"
23
#include "src/dsp/cpu.h"
24
#include "src/dsp/dsp.h"
25
#include "src/dsp/lossless_common.h"
26
#include "src/utils/endian_inl_utils.h"
27
#include "src/utils/utils.h"
28
#include "src/webp/decode.h"
29
#include "src/webp/format_constants.h"
30
#include "src/webp/types.h"
31
32
//------------------------------------------------------------------------------
33
// Image transforms.
34
35
20.8M
static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
36
20.8M
  return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
37
20.8M
}
38
39
2.07M
static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
40
2.07M
  return Average2(Average2(a0, a2), a1);
41
2.07M
}
42
43
static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1, uint32_t a2,
44
2.20M
                                     uint32_t a3) {
45
2.20M
  return Average2(Average2(a0, a1), Average2(a2, a3));
46
2.20M
}
47
48
18.5M
static WEBP_INLINE uint32_t Clip255(uint32_t a) {
49
18.5M
  if (a < 256) {
50
17.8M
    return a;
51
17.8M
  }
52
  // return 0, when a is a negative integer.
53
  // return 255, when a is positive.
54
711k
  return ~a >> 24;
55
18.5M
}
56
57
10.8M
static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
58
10.8M
  return Clip255((uint32_t)(a + b - c));
59
10.8M
}
60
61
static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
62
2.71M
                                                   uint32_t c2) {
63
2.71M
  const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
64
2.71M
  const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, (c1 >> 16) & 0xff,
65
2.71M
                                         (c2 >> 16) & 0xff);
66
2.71M
  const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, (c1 >> 8) & 0xff,
67
2.71M
                                         (c2 >> 8) & 0xff);
68
2.71M
  const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
69
2.71M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
70
2.71M
}
71
72
7.75M
static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
73
7.75M
  return Clip255((uint32_t)(a + (a - b) / 2));
74
7.75M
}
75
76
static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
77
1.93M
                                                   uint32_t c2) {
78
1.93M
  const uint32_t ave = Average2(c0, c1);
79
1.93M
  const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
80
1.93M
  const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
81
1.93M
  const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
82
1.93M
  const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
83
1.93M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
84
1.93M
}
85
86
// gcc <= 4.9 on ARM generates incorrect code in Select() when Sub3() is
87
// inlined.
88
#if defined(__arm__) && defined(__GNUC__) && LOCAL_GCC_VERSION <= 0x409
89
#define LOCAL_INLINE __attribute__((noinline))
90
#else
91
#define LOCAL_INLINE WEBP_INLINE
92
#endif
93
94
8.98M
static LOCAL_INLINE int Sub3(int a, int b, int c) {
95
8.98M
  const int pb = b - c;
96
8.98M
  const int pa = a - c;
97
8.98M
  return abs(pb) - abs(pa);
98
8.98M
}
99
100
#undef LOCAL_INLINE
101
102
2.24M
static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
103
2.24M
  const int pa_minus_pb =
104
2.24M
      Sub3((a >> 24), (b >> 24), (c >> 24)) +
105
2.24M
      Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
106
2.24M
      Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
107
2.24M
      Sub3((a) & 0xff, (b) & 0xff, (c) & 0xff);
108
2.24M
  return (pa_minus_pb <= 0) ? a : b;
109
2.24M
}
110
111
//------------------------------------------------------------------------------
112
// Predictors
113
114
static uint32_t VP8LPredictor0_C(const uint32_t* const left,
115
478M
                                 const uint32_t* const top) {
116
478M
  (void)top;
117
478M
  (void)left;
118
478M
  return ARGB_BLACK;
119
478M
}
120
static uint32_t VP8LPredictor1_C(const uint32_t* const left,
121
459M
                                 const uint32_t* const top) {
122
459M
  (void)top;
123
459M
  return *left;
124
459M
}
125
uint32_t VP8LPredictor2_C(const uint32_t* const left,
126
351M
                          const uint32_t* const top) {
127
351M
  (void)left;
128
351M
  return top[0];
129
351M
}
130
uint32_t VP8LPredictor3_C(const uint32_t* const left,
131
353M
                          const uint32_t* const top) {
132
353M
  (void)left;
133
353M
  return top[1];
134
353M
}
135
uint32_t VP8LPredictor4_C(const uint32_t* const left,
136
347M
                          const uint32_t* const top) {
137
347M
  (void)left;
138
347M
  return top[-1];
139
347M
}
140
uint32_t VP8LPredictor5_C(const uint32_t* const left,
141
2.07M
                          const uint32_t* const top) {
142
2.07M
  const uint32_t pred = Average3(*left, top[0], top[1]);
143
2.07M
  return pred;
144
2.07M
}
145
uint32_t VP8LPredictor6_C(const uint32_t* const left,
146
1.95M
                          const uint32_t* const top) {
147
1.95M
  const uint32_t pred = Average2(*left, top[-1]);
148
1.95M
  return pred;
149
1.95M
}
150
uint32_t VP8LPredictor7_C(const uint32_t* const left,
151
1.92M
                          const uint32_t* const top) {
152
1.92M
  const uint32_t pred = Average2(*left, top[0]);
153
1.92M
  return pred;
154
1.92M
}
155
uint32_t VP8LPredictor8_C(const uint32_t* const left,
156
2.01M
                          const uint32_t* const top) {
157
2.01M
  const uint32_t pred = Average2(top[-1], top[0]);
158
2.01M
  (void)left;
159
2.01M
  return pred;
160
2.01M
}
161
uint32_t VP8LPredictor9_C(const uint32_t* const left,
162
2.25M
                          const uint32_t* const top) {
163
2.25M
  const uint32_t pred = Average2(top[0], top[1]);
164
2.25M
  (void)left;
165
2.25M
  return pred;
166
2.25M
}
167
uint32_t VP8LPredictor10_C(const uint32_t* const left,
168
2.20M
                           const uint32_t* const top) {
169
2.20M
  const uint32_t pred = Average4(*left, top[-1], top[0], top[1]);
170
2.20M
  return pred;
171
2.20M
}
172
uint32_t VP8LPredictor11_C(const uint32_t* const left,
173
2.24M
                           const uint32_t* const top) {
174
2.24M
  const uint32_t pred = Select(top[0], *left, top[-1]);
175
2.24M
  return pred;
176
2.24M
}
177
uint32_t VP8LPredictor12_C(const uint32_t* const left,
178
2.71M
                           const uint32_t* const top) {
179
2.71M
  const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]);
180
2.71M
  return pred;
181
2.71M
}
182
uint32_t VP8LPredictor13_C(const uint32_t* const left,
183
1.93M
                           const uint32_t* const top) {
184
1.93M
  const uint32_t pred = ClampedAddSubtractHalf(*left, top[0], top[-1]);
185
1.93M
  return pred;
186
1.93M
}
187
188
static void PredictorAdd0_C(const uint32_t* in, const uint32_t* upper,
189
393k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
190
393k
  int x;
191
393k
  (void)upper;
192
1.38M
  for (x = 0; x < num_pixels; ++x) out[x] = VP8LAddPixels(in[x], ARGB_BLACK);
193
393k
}
194
static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper,
195
390k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
196
390k
  int i;
197
390k
  uint32_t left = out[-1];
198
390k
  (void)upper;
199
2.37M
  for (i = 0; i < num_pixels; ++i) {
200
1.98M
    out[i] = left = VP8LAddPixels(in[i], left);
201
1.98M
  }
202
390k
}
203
1.19M
GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C)
204
27.4k
GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C)
205
44.5k
GENERATE_PREDICTOR_ADD(VP8LPredictor4_C, PredictorAdd4_C)
206
0
GENERATE_PREDICTOR_ADD(VP8LPredictor5_C, PredictorAdd5_C)
207
0
GENERATE_PREDICTOR_ADD(VP8LPredictor6_C, PredictorAdd6_C)
208
0
GENERATE_PREDICTOR_ADD(VP8LPredictor7_C, PredictorAdd7_C)
209
28.0k
GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C)
210
111k
GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C)
211
79.8k
GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C)
212
93.4k
GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C)
213
256k
GENERATE_PREDICTOR_ADD(VP8LPredictor12_C, PredictorAdd12_C)
214
0
GENERATE_PREDICTOR_ADD(VP8LPredictor13_C, PredictorAdd13_C)
215
216
//------------------------------------------------------------------------------
217
218
// Inverse prediction.
219
static void PredictorInverseTransform_C(const VP8LTransform* const transform,
220
                                        int y_start, int y_end,
221
74.6k
                                        const uint32_t* in, uint32_t* out) {
222
74.6k
  const int width = transform->xsize;
223
74.6k
  if (y_start == 0) {  // First Row follows the L (mode=1) mode.
224
2.04k
    PredictorAdd0_C(in, NULL, 1, out);
225
2.04k
    PredictorAdd1_C(in + 1, NULL, width - 1, out + 1);
226
2.04k
    in += width;
227
2.04k
    out += width;
228
2.04k
    ++y_start;
229
2.04k
  }
230
231
74.6k
  {
232
74.6k
    int y = y_start;
233
74.6k
    const int tile_width = 1 << transform->bits;
234
74.6k
    const int mask = tile_width - 1;
235
74.6k
    const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
236
74.6k
    const uint32_t* pred_mode_base =
237
74.6k
        transform->data + (y >> transform->bits) * tiles_per_row;
238
239
1.24M
    while (y < y_end) {
240
1.17M
      const uint32_t* pred_mode_src = pred_mode_base;
241
1.17M
      int x = 1;
242
      // First pixel follows the T (mode=2) mode.
243
1.17M
      PredictorAdd2_C(in, out - width, 1, out);
244
      // .. the rest:
245
68.6M
      while (x < width) {
246
67.5M
        const VP8LPredictorAddSubFunc pred_func =
247
67.5M
            VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf];
248
67.5M
        int x_end = (x & ~mask) + tile_width;
249
67.5M
        if (x_end > width) x_end = width;
250
67.5M
        pred_func(in + x, out + x - width, x_end - x, out + x);
251
67.5M
        x = x_end;
252
67.5M
      }
253
1.17M
      in += width;
254
1.17M
      out += width;
255
1.17M
      ++y;
256
1.17M
      if ((y & mask) == 0) {  // Use the same mask, since tiles are squares.
257
56.8k
        pred_mode_base += tiles_per_row;
258
56.8k
      }
259
1.17M
    }
260
74.6k
  }
261
74.6k
}
262
263
// Add green to blue and red channels (i.e. perform the inverse transform of
264
// 'subtract green').
265
void VP8LAddGreenToBlueAndRed_C(const uint32_t* src, int num_pixels,
266
396
                                uint32_t* dst) {
267
396
  int i;
268
1.20k
  for (i = 0; i < num_pixels; ++i) {
269
807
    const uint32_t argb = src[i];
270
807
    const uint32_t green = ((argb >> 8) & 0xff);
271
807
    uint32_t red_blue = (argb & 0x00ff00ffu);
272
807
    red_blue += (green << 16) | green;
273
807
    red_blue &= 0x00ff00ffu;
274
807
    dst[i] = (argb & 0xff00ff00u) | red_blue;
275
807
  }
276
396
}
277
278
111k
static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) {
279
111k
  return ((int)color_pred * color) >> 5;
280
111k
}
281
282
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
283
2.71M
                                               VP8LMultipliers* const m) {
284
2.71M
  m->green_to_red = (color_code >> 0) & 0xff;
285
2.71M
  m->green_to_blue = (color_code >> 8) & 0xff;
286
2.71M
  m->red_to_blue = (color_code >> 16) & 0xff;
287
2.71M
}
288
289
void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
290
                                 const uint32_t* src, int num_pixels,
291
18.7k
                                 uint32_t* dst) {
292
18.7k
  int i;
293
56.0k
  for (i = 0; i < num_pixels; ++i) {
294
37.3k
    const uint32_t argb = src[i];
295
37.3k
    const int8_t green = (int8_t)(argb >> 8);
296
37.3k
    const uint32_t red = argb >> 16;
297
37.3k
    int new_red = red & 0xff;
298
37.3k
    int new_blue = argb & 0xff;
299
37.3k
    new_red += ColorTransformDelta((int8_t)m->green_to_red, green);
300
37.3k
    new_red &= 0xff;
301
37.3k
    new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green);
302
37.3k
    new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red);
303
37.3k
    new_blue &= 0xff;
304
37.3k
    dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
305
37.3k
  }
306
18.7k
}
307
308
// Color space inverse transform.
309
static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform,
310
                                         int y_start, int y_end,
311
1.71k
                                         const uint32_t* src, uint32_t* dst) {
312
1.71k
  const int width = transform->xsize;
313
1.71k
  const int tile_width = 1 << transform->bits;
314
1.71k
  const int mask = tile_width - 1;
315
1.71k
  const int safe_width = width & ~mask;
316
1.71k
  const int remaining_width = width - safe_width;
317
1.71k
  const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
318
1.71k
  int y = y_start;
319
1.71k
  const uint32_t* pred_row =
320
1.71k
      transform->data + (y >> transform->bits) * tiles_per_row;
321
322
28.0k
  while (y < y_end) {
323
26.3k
    const uint32_t* pred = pred_row;
324
26.3k
    VP8LMultipliers m = {0, 0, 0};
325
26.3k
    const uint32_t* const src_safe_end = src + safe_width;
326
26.3k
    const uint32_t* const src_end = src + width;
327
2.71M
    while (src < src_safe_end) {
328
2.69M
      ColorCodeToMultipliers(*pred++, &m);
329
2.69M
      VP8LTransformColorInverse(&m, src, tile_width, dst);
330
2.69M
      src += tile_width;
331
2.69M
      dst += tile_width;
332
2.69M
    }
333
26.3k
    if (src < src_end) {  // Left-overs using C-version.
334
20.1k
      ColorCodeToMultipliers(*pred++, &m);
335
20.1k
      VP8LTransformColorInverse(&m, src, remaining_width, dst);
336
20.1k
      src += remaining_width;
337
20.1k
      dst += remaining_width;
338
20.1k
    }
339
26.3k
    ++y;
340
26.3k
    if ((y & mask) == 0) pred_row += tiles_per_row;
341
26.3k
  }
342
1.71k
}
343
344
// Separate out pixels packed together using pixel-bundling.
345
// We define two methods for ARGB data (uint32_t) and alpha-only data (uint8_t).
346
// clang-format off
347
#define COLOR_INDEX_INVERSE(FUNC_NAME, F_NAME, STATIC_DECL, TYPE, BIT_SUFFIX,  \
348
                            GET_INDEX, GET_VALUE)                              \
349
static void F_NAME(const TYPE* src, const uint32_t* const color_map,           \
350
1.96k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
1.96k
  int y;                                                                       \
352
29.4k
  for (y = y_start; y < y_end; ++y) {                                          \
353
27.4k
    int x;                                                                     \
354
6.09M
    for (x = 0; x < width; ++x) {                                              \
355
6.06M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
6.06M
    }                                                                          \
357
27.4k
  }                                                                            \
358
1.96k
}                                                                              \
lossless.c:MapARGB_C
Line
Count
Source
350
1.45k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
1.45k
  int y;                                                                       \
352
22.3k
  for (y = y_start; y < y_end; ++y) {                                          \
353
20.9k
    int x;                                                                     \
354
5.53M
    for (x = 0; x < width; ++x) {                                              \
355
5.51M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
5.51M
    }                                                                          \
357
20.9k
  }                                                                            \
358
1.45k
}                                                                              \
lossless.c:MapAlpha_C
Line
Count
Source
350
513
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
513
  int y;                                                                       \
352
7.03k
  for (y = y_start; y < y_end; ++y) {                                          \
353
6.51k
    int x;                                                                     \
354
557k
    for (x = 0; x < width; ++x) {                                              \
355
550k
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
550k
    }                                                                          \
357
6.51k
  }                                                                            \
358
513
}                                                                              \
359
STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform,               \
360
                           int y_start, int y_end, const TYPE* src,            \
361
6.01k
                           TYPE* dst) {                                        \
362
6.01k
  int y;                                                                       \
363
6.01k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
6.01k
  const int width = transform->xsize;                                          \
365
6.01k
  const uint32_t* const color_map = transform->data;                           \
366
6.01k
  if (bits_per_pixel < 8) {                                                    \
367
4.04k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
4.04k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
59.4k
    for (y = y_start; y < y_end; ++y) {                                        \
370
55.4k
      int x;                                                                   \
371
7.72M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
7.66M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
7.66M
        if (bits_per_pixel == 1) {                                             \
374
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
912k
          packed >>= 1;                                                        \
376
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
912k
          packed >>= 1;                                                        \
378
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
912k
          packed >>= 1;                                                        \
380
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
912k
          packed >>= 1;                                                        \
382
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
912k
          packed >>= 1;                                                        \
384
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
912k
          packed >>= 1;                                                        \
386
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
912k
          packed >>= 1;                                                        \
388
912k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
6.75M
        } else if (bits_per_pixel == 2) {                                      \
390
3.17M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
3.17M
          packed >>= 2;                                                        \
392
3.17M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
3.17M
          packed >>= 2;                                                        \
394
3.17M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
3.17M
          packed >>= 2;                                                        \
396
3.17M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
3.58M
        } else {                                                               \
398
3.58M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
3.58M
          packed >>= 4;                                                        \
400
3.58M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
3.58M
        }                                                                      \
402
7.66M
      }                                                                        \
403
55.4k
      if (x < width) {                                                         \
404
32.8k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
81.5k
        for (; x < width; ++x) {                                               \
406
48.7k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
48.7k
          packed >>= bits_per_pixel;                                           \
408
48.7k
        }                                                                      \
409
32.8k
      }                                                                        \
410
55.4k
    }                                                                          \
411
4.04k
  } else {                                                                     \
412
1.96k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
1.96k
  }                                                                            \
414
6.01k
}
VP8LColorIndexInverseTransformAlpha
Line
Count
Source
361
1.93k
                           TYPE* dst) {                                        \
362
1.93k
  int y;                                                                       \
363
1.93k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
1.93k
  const int width = transform->xsize;                                          \
365
1.93k
  const uint32_t* const color_map = transform->data;                           \
366
1.93k
  if (bits_per_pixel < 8) {                                                    \
367
1.42k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
1.42k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
16.8k
    for (y = y_start; y < y_end; ++y) {                                        \
370
15.4k
      int x;                                                                   \
371
1.71M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
1.70M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
1.70M
        if (bits_per_pixel == 1) {                                             \
374
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
113k
          packed >>= 1;                                                        \
376
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
113k
          packed >>= 1;                                                        \
378
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
113k
          packed >>= 1;                                                        \
380
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
113k
          packed >>= 1;                                                        \
382
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
113k
          packed >>= 1;                                                        \
384
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
113k
          packed >>= 1;                                                        \
386
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
113k
          packed >>= 1;                                                        \
388
113k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
1.59M
        } else if (bits_per_pixel == 2) {                                      \
390
1.59M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
1.59M
          packed >>= 2;                                                        \
392
1.59M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
1.59M
          packed >>= 2;                                                        \
394
1.59M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
1.59M
          packed >>= 2;                                                        \
396
1.59M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
1.59M
        } else {                                                               \
398
248
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
248
          packed >>= 4;                                                        \
400
248
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
248
        }                                                                      \
402
1.70M
      }                                                                        \
403
15.4k
      if (x < width) {                                                         \
404
4.90k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
11.9k
        for (; x < width; ++x) {                                               \
406
7.04k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
7.04k
          packed >>= bits_per_pixel;                                           \
408
7.04k
        }                                                                      \
409
4.90k
      }                                                                        \
410
15.4k
    }                                                                          \
411
1.42k
  } else {                                                                     \
412
513
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
513
  }                                                                            \
414
1.93k
}
lossless.c:ColorIndexInverseTransform_C
Line
Count
Source
361
4.07k
                           TYPE* dst) {                                        \
362
4.07k
  int y;                                                                       \
363
4.07k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
4.07k
  const int width = transform->xsize;                                          \
365
4.07k
  const uint32_t* const color_map = transform->data;                           \
366
4.07k
  if (bits_per_pixel < 8) {                                                    \
367
2.61k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
2.61k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
42.6k
    for (y = y_start; y < y_end; ++y) {                                        \
370
40.0k
      int x;                                                                   \
371
6.00M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
5.96M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
5.96M
        if (bits_per_pixel == 1) {                                             \
374
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
799k
          packed >>= 1;                                                        \
376
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
799k
          packed >>= 1;                                                        \
378
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
799k
          packed >>= 1;                                                        \
380
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
799k
          packed >>= 1;                                                        \
382
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
799k
          packed >>= 1;                                                        \
384
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
799k
          packed >>= 1;                                                        \
386
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
799k
          packed >>= 1;                                                        \
388
799k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
5.16M
        } else if (bits_per_pixel == 2) {                                      \
390
1.58M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
1.58M
          packed >>= 2;                                                        \
392
1.58M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
1.58M
          packed >>= 2;                                                        \
394
1.58M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
1.58M
          packed >>= 2;                                                        \
396
1.58M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
3.58M
        } else {                                                               \
398
3.58M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
3.58M
          packed >>= 4;                                                        \
400
3.58M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
3.58M
        }                                                                      \
402
5.96M
      }                                                                        \
403
40.0k
      if (x < width) {                                                         \
404
27.8k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
69.6k
        for (; x < width; ++x) {                                               \
406
41.7k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
41.7k
          packed >>= bits_per_pixel;                                           \
408
41.7k
        }                                                                      \
409
27.8k
      }                                                                        \
410
40.0k
    }                                                                          \
411
2.61k
  } else {                                                                     \
412
1.45k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
1.45k
  }                                                                            \
414
4.07k
}
415
// clang-format on
416
417
COLOR_INDEX_INVERSE(ColorIndexInverseTransform_C, MapARGB_C, static, uint32_t,
418
                    32b, VP8GetARGBIndex, VP8GetARGBValue)
419
COLOR_INDEX_INVERSE(VP8LColorIndexInverseTransformAlpha, MapAlpha_C, , uint8_t,
420
                    8b, VP8GetAlphaIndex, VP8GetAlphaValue)
421
422
#undef COLOR_INDEX_INVERSE
423
424
void VP8LInverseTransform(const VP8LTransform* const transform, int row_start,
425
                          int row_end, const uint32_t* const in,
426
90.1k
                          uint32_t* const out) {
427
90.1k
  const int width = transform->xsize;
428
90.1k
  assert(row_start < row_end);
429
90.1k
  assert(row_end <= transform->ysize);
430
90.1k
  switch (transform->type) {
431
9.71k
    case SUBTRACT_GREEN_TRANSFORM:
432
9.71k
      VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out);
433
9.71k
      break;
434
74.6k
    case PREDICTOR_TRANSFORM:
435
74.6k
      PredictorInverseTransform_C(transform, row_start, row_end, in, out);
436
74.6k
      if (row_end != transform->ysize) {
437
        // The last predicted row in this iteration will be the top-pred row
438
        // for the first row in next iteration.
439
72.6k
        memcpy(out - width, out + (row_end - row_start - 1) * width,
440
72.6k
               width * sizeof(*out));
441
72.6k
      }
442
74.6k
      break;
443
1.71k
    case CROSS_COLOR_TRANSFORM:
444
1.71k
      ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out);
445
1.71k
      break;
446
4.07k
    case COLOR_INDEXING_TRANSFORM:
447
4.07k
      if (in == out && transform->bits > 0) {
448
        // Move packed pixels to the end of unpacked region, so that unpacking
449
        // can occur seamlessly.
450
        // Also, note that this is the only transform that applies on
451
        // the effective width of VP8LSubSampleSize(xsize, bits). All other
452
        // transforms work on effective width of 'xsize'.
453
1.26k
        const int out_stride = (row_end - row_start) * width;
454
1.26k
        const int in_stride =
455
1.26k
            (row_end - row_start) *
456
1.26k
            VP8LSubSampleSize(transform->xsize, transform->bits);
457
1.26k
        uint32_t* const src = out + out_stride - in_stride;
458
1.26k
        memmove(src, out, in_stride * sizeof(*src));
459
1.26k
        ColorIndexInverseTransform_C(transform, row_start, row_end, src, out);
460
2.80k
      } else {
461
2.80k
        ColorIndexInverseTransform_C(transform, row_start, row_end, in, out);
462
2.80k
      }
463
4.07k
      break;
464
90.1k
  }
465
90.1k
}
466
467
//------------------------------------------------------------------------------
468
// Color space conversion.
469
470
0
static int is_big_endian(void) {
471
0
  static const union {
472
0
    uint16_t w;
473
0
    uint8_t b[2];
474
0
  } tmp = {1};
475
0
  return (tmp.b[0] != 1);
476
0
}
477
478
void VP8LConvertBGRAToRGB_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
479
0
                            uint8_t* WEBP_RESTRICT dst) {
480
0
  const uint32_t* const src_end = src + num_pixels;
481
0
  while (src < src_end) {
482
0
    const uint32_t argb = *src++;
483
0
    *dst++ = (argb >> 16) & 0xff;
484
0
    *dst++ = (argb >> 8) & 0xff;
485
0
    *dst++ = (argb >> 0) & 0xff;
486
0
  }
487
0
}
488
489
void VP8LConvertBGRAToRGBA_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
490
1.49M
                             uint8_t* WEBP_RESTRICT dst) {
491
1.49M
  const uint32_t* const src_end = src + num_pixels;
492
7.10M
  while (src < src_end) {
493
5.61M
    const uint32_t argb = *src++;
494
5.61M
    *dst++ = (argb >> 16) & 0xff;
495
5.61M
    *dst++ = (argb >> 8) & 0xff;
496
5.61M
    *dst++ = (argb >> 0) & 0xff;
497
5.61M
    *dst++ = (argb >> 24) & 0xff;
498
5.61M
  }
499
1.49M
}
500
501
void VP8LConvertBGRAToRGBA4444_C(const uint32_t* WEBP_RESTRICT src,
502
0
                                 int num_pixels, uint8_t* WEBP_RESTRICT dst) {
503
0
  const uint32_t* const src_end = src + num_pixels;
504
0
  while (src < src_end) {
505
0
    const uint32_t argb = *src++;
506
0
    const uint8_t rg = ((argb >> 16) & 0xf0) | ((argb >> 12) & 0xf);
507
0
    const uint8_t ba = ((argb >> 0) & 0xf0) | ((argb >> 28) & 0xf);
508
#if (WEBP_SWAP_16BIT_CSP == 1)
509
    *dst++ = ba;
510
    *dst++ = rg;
511
#else
512
0
    *dst++ = rg;
513
0
    *dst++ = ba;
514
0
#endif
515
0
  }
516
0
}
517
518
void VP8LConvertBGRAToRGB565_C(const uint32_t* WEBP_RESTRICT src,
519
0
                               int num_pixels, uint8_t* WEBP_RESTRICT dst) {
520
0
  const uint32_t* const src_end = src + num_pixels;
521
0
  while (src < src_end) {
522
0
    const uint32_t argb = *src++;
523
0
    const uint8_t rg = ((argb >> 16) & 0xf8) | ((argb >> 13) & 0x7);
524
0
    const uint8_t gb = ((argb >> 5) & 0xe0) | ((argb >> 3) & 0x1f);
525
#if (WEBP_SWAP_16BIT_CSP == 1)
526
    *dst++ = gb;
527
    *dst++ = rg;
528
#else
529
0
    *dst++ = rg;
530
0
    *dst++ = gb;
531
0
#endif
532
0
  }
533
0
}
534
535
void VP8LConvertBGRAToBGR_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
536
0
                            uint8_t* WEBP_RESTRICT dst) {
537
0
  const uint32_t* const src_end = src + num_pixels;
538
0
  while (src < src_end) {
539
0
    const uint32_t argb = *src++;
540
0
    *dst++ = (argb >> 0) & 0xff;
541
0
    *dst++ = (argb >> 8) & 0xff;
542
0
    *dst++ = (argb >> 16) & 0xff;
543
0
  }
544
0
}
545
546
static void CopyOrSwap(const uint32_t* WEBP_RESTRICT src, int num_pixels,
547
0
                       uint8_t* WEBP_RESTRICT dst, int swap_on_big_endian) {
548
0
  if (is_big_endian() == swap_on_big_endian) {
549
0
    const uint32_t* const src_end = src + num_pixels;
550
0
    while (src < src_end) {
551
0
      const uint32_t argb = *src++;
552
0
      WebPUint32ToMem(dst, BSwap32(argb));
553
0
      dst += sizeof(argb);
554
0
    }
555
0
  } else {
556
0
    memcpy(dst, src, num_pixels * sizeof(*src));
557
0
  }
558
0
}
559
560
void VP8LConvertFromBGRA(const uint32_t* const in_data, int num_pixels,
561
1.74M
                         WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
562
1.74M
  switch (out_colorspace) {
563
0
    case MODE_RGB:
564
0
      VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
565
0
      break;
566
1.74M
    case MODE_RGBA:
567
1.74M
      VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
568
1.74M
      break;
569
0
    case MODE_rgbA:
570
0
      VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
571
0
      WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
572
0
      break;
573
0
    case MODE_BGR:
574
0
      VP8LConvertBGRAToBGR(in_data, num_pixels, rgba);
575
0
      break;
576
0
    case MODE_BGRA:
577
0
      CopyOrSwap(in_data, num_pixels, rgba, 1);
578
0
      break;
579
0
    case MODE_bgrA:
580
0
      CopyOrSwap(in_data, num_pixels, rgba, 1);
581
0
      WebPApplyAlphaMultiply(rgba, 0, num_pixels, 1, 0);
582
0
      break;
583
0
    case MODE_ARGB:
584
0
      CopyOrSwap(in_data, num_pixels, rgba, 0);
585
0
      break;
586
0
    case MODE_Argb:
587
0
      CopyOrSwap(in_data, num_pixels, rgba, 0);
588
0
      WebPApplyAlphaMultiply(rgba, 1, num_pixels, 1, 0);
589
0
      break;
590
0
    case MODE_RGBA_4444:
591
0
      VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
592
0
      break;
593
0
    case MODE_rgbA_4444:
594
0
      VP8LConvertBGRAToRGBA4444(in_data, num_pixels, rgba);
595
0
      WebPApplyAlphaMultiply4444(rgba, num_pixels, 1, 0);
596
0
      break;
597
0
    case MODE_RGB_565:
598
0
      VP8LConvertBGRAToRGB565(in_data, num_pixels, rgba);
599
0
      break;
600
0
    default:
601
0
      assert(0);  // Code flow should not reach here.
602
1.74M
  }
603
1.74M
}
604
605
//------------------------------------------------------------------------------
606
607
VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed;
608
VP8LProcessDecBlueAndRedFunc VP8LAddGreenToBlueAndRed_SSE;
609
VP8LPredictorAddSubFunc VP8LPredictorsAdd[16];
610
VP8LPredictorAddSubFunc VP8LPredictorsAdd_SSE[16];
611
VP8LPredictorFunc VP8LPredictors[16];
612
613
// exposed plain-C implementations
614
VP8LPredictorAddSubFunc VP8LPredictorsAdd_C[16];
615
616
VP8LTransformColorInverseFunc VP8LTransformColorInverse;
617
VP8LTransformColorInverseFunc VP8LTransformColorInverse_SSE;
618
619
VP8LConvertFunc VP8LConvertBGRAToRGB;
620
VP8LConvertFunc VP8LConvertBGRAToRGB_SSE;
621
VP8LConvertFunc VP8LConvertBGRAToRGBA;
622
VP8LConvertFunc VP8LConvertBGRAToRGBA_SSE;
623
VP8LConvertFunc VP8LConvertBGRAToRGBA4444;
624
VP8LConvertFunc VP8LConvertBGRAToRGB565;
625
VP8LConvertFunc VP8LConvertBGRAToBGR;
626
VP8LConvertFunc VP8LConvertBGRAToBGR_SSE;
627
628
VP8LMapARGBFunc VP8LMapColor32b;
629
VP8LMapAlphaFunc VP8LMapColor8b;
630
631
extern VP8CPUInfo VP8GetCPUInfo;
632
extern void VP8LDspInitSSE2(void);
633
extern void VP8LDspInitSSE41(void);
634
extern void VP8LDspInitAVX2(void);
635
extern void VP8LDspInitNEON(void);
636
extern void VP8LDspInitMIPSdspR2(void);
637
extern void VP8LDspInitMSA(void);
638
639
#define COPY_PREDICTOR_ARRAY(IN, OUT)                       \
640
9
  do {                                                      \
641
9
    (OUT)[0] = IN##0_C;                                     \
642
9
    (OUT)[1] = IN##1_C;                                     \
643
9
    (OUT)[2] = IN##2_C;                                     \
644
9
    (OUT)[3] = IN##3_C;                                     \
645
9
    (OUT)[4] = IN##4_C;                                     \
646
9
    (OUT)[5] = IN##5_C;                                     \
647
9
    (OUT)[6] = IN##6_C;                                     \
648
9
    (OUT)[7] = IN##7_C;                                     \
649
9
    (OUT)[8] = IN##8_C;                                     \
650
9
    (OUT)[9] = IN##9_C;                                     \
651
9
    (OUT)[10] = IN##10_C;                                   \
652
9
    (OUT)[11] = IN##11_C;                                   \
653
9
    (OUT)[12] = IN##12_C;                                   \
654
9
    (OUT)[13] = IN##13_C;                                   \
655
9
    (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \
656
9
    (OUT)[15] = IN##0_C;                                    \
657
9
  } while (0);
658
659
3
WEBP_DSP_INIT_FUNC(VP8LDspInit) {
660
3
  COPY_PREDICTOR_ARRAY(VP8LPredictor, VP8LPredictors)
661
3
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd)
662
3
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C)
663
664
3
#if !WEBP_NEON_OMIT_C_CODE
665
3
  VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
666
667
3
  VP8LTransformColorInverse = VP8LTransformColorInverse_C;
668
669
3
  VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
670
3
  VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
671
3
  VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
672
3
#endif
673
674
3
  VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
675
3
  VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
676
677
3
  VP8LMapColor32b = MapARGB_C;
678
3
  VP8LMapColor8b = MapAlpha_C;
679
680
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
681
3
  if (VP8GetCPUInfo != NULL) {
682
3
#if defined(WEBP_HAVE_SSE2)
683
3
    if (VP8GetCPUInfo(kSSE2)) {
684
3
      VP8LDspInitSSE2();
685
3
#if defined(WEBP_HAVE_SSE41)
686
3
      if (VP8GetCPUInfo(kSSE4_1)) {
687
3
        VP8LDspInitSSE41();
688
3
#if defined(WEBP_HAVE_AVX2)
689
3
        if (VP8GetCPUInfo(kAVX2)) {
690
3
          VP8LDspInitAVX2();
691
3
        }
692
3
#endif
693
3
      }
694
3
#endif
695
3
    }
696
3
#endif
697
#if defined(WEBP_USE_MIPS_DSP_R2)
698
    if (VP8GetCPUInfo(kMIPSdspR2)) {
699
      VP8LDspInitMIPSdspR2();
700
    }
701
#endif
702
#if defined(WEBP_USE_MSA)
703
    if (VP8GetCPUInfo(kMSA)) {
704
      VP8LDspInitMSA();
705
    }
706
#endif
707
3
  }
708
709
#if defined(WEBP_HAVE_NEON)
710
  if (WEBP_NEON_OMIT_C_CODE ||
711
      (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
712
    VP8LDspInitNEON();
713
  }
714
#endif
715
716
3
  assert(VP8LAddGreenToBlueAndRed != NULL);
717
3
  assert(VP8LTransformColorInverse != NULL);
718
3
  assert(VP8LConvertBGRAToRGBA != NULL);
719
3
  assert(VP8LConvertBGRAToRGB != NULL);
720
3
  assert(VP8LConvertBGRAToBGR != NULL);
721
3
  assert(VP8LConvertBGRAToRGBA4444 != NULL);
722
3
  assert(VP8LConvertBGRAToRGB565 != NULL);
723
3
  assert(VP8LMapColor32b != NULL);
724
  assert(VP8LMapColor8b != NULL);
725
3
}
726
#undef COPY_PREDICTOR_ARRAY
727
728
//------------------------------------------------------------------------------