Coverage Report

Created: 2026-06-16 07:20

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
37.8M
static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
36
37.8M
  return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
37
37.8M
}
38
39
3.54M
static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
40
3.54M
  return Average2(Average2(a0, a2), a1);
41
3.54M
}
42
43
static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1, uint32_t a2,
44
4.15M
                                     uint32_t a3) {
45
4.15M
  return Average2(Average2(a0, a1), Average2(a2, a3));
46
4.15M
}
47
48
31.3M
static WEBP_INLINE uint32_t Clip255(uint32_t a) {
49
31.3M
  if (a < 256) {
50
30.6M
    return a;
51
30.6M
  }
52
  // return 0, when a is a negative integer.
53
  // return 255, when a is positive.
54
654k
  return ~a >> 24;
55
31.3M
}
56
57
17.1M
static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
58
17.1M
  return Clip255((uint32_t)(a + b - c));
59
17.1M
}
60
61
static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
62
4.28M
                                                   uint32_t c2) {
63
4.28M
  const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
64
4.28M
  const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, (c1 >> 16) & 0xff,
65
4.28M
                                         (c2 >> 16) & 0xff);
66
4.28M
  const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, (c1 >> 8) & 0xff,
67
4.28M
                                         (c2 >> 8) & 0xff);
68
4.28M
  const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
69
4.28M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
70
4.28M
}
71
72
14.1M
static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
73
14.1M
  return Clip255((uint32_t)(a + (a - b) / 2));
74
14.1M
}
75
76
static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
77
3.54M
                                                   uint32_t c2) {
78
3.54M
  const uint32_t ave = Average2(c0, c1);
79
3.54M
  const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
80
3.54M
  const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
81
3.54M
  const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
82
3.54M
  const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
83
3.54M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
84
3.54M
}
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
15.6M
static LOCAL_INLINE int Sub3(int a, int b, int c) {
95
15.6M
  const int pb = b - c;
96
15.6M
  const int pa = a - c;
97
15.6M
  return abs(pb) - abs(pa);
98
15.6M
}
99
100
#undef LOCAL_INLINE
101
102
3.91M
static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
103
3.91M
  const int pa_minus_pb =
104
3.91M
      Sub3((a >> 24), (b >> 24), (c >> 24)) +
105
3.91M
      Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
106
3.91M
      Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
107
3.91M
      Sub3((a) & 0xff, (b) & 0xff, (c) & 0xff);
108
3.91M
  return (pa_minus_pb <= 0) ? a : b;
109
3.91M
}
110
111
//------------------------------------------------------------------------------
112
// Predictors
113
114
static uint32_t VP8LPredictor0_C(const uint32_t* const left,
115
0
                                 const uint32_t* const top) {
116
0
  (void)top;
117
0
  (void)left;
118
0
  return ARGB_BLACK;
119
0
}
120
static uint32_t VP8LPredictor1_C(const uint32_t* const left,
121
0
                                 const uint32_t* const top) {
122
0
  (void)top;
123
0
  return *left;
124
0
}
125
uint32_t VP8LPredictor2_C(const uint32_t* const left,
126
18.4M
                          const uint32_t* const top) {
127
18.4M
  (void)left;
128
18.4M
  return top[0];
129
18.4M
}
130
uint32_t VP8LPredictor3_C(const uint32_t* const left,
131
3.78M
                          const uint32_t* const top) {
132
3.78M
  (void)left;
133
3.78M
  return top[1];
134
3.78M
}
135
uint32_t VP8LPredictor4_C(const uint32_t* const left,
136
3.97M
                          const uint32_t* const top) {
137
3.97M
  (void)left;
138
3.97M
  return top[-1];
139
3.97M
}
140
uint32_t VP8LPredictor5_C(const uint32_t* const left,
141
3.54M
                          const uint32_t* const top) {
142
3.54M
  const uint32_t pred = Average3(*left, top[0], top[1]);
143
3.54M
  return pred;
144
3.54M
}
145
uint32_t VP8LPredictor6_C(const uint32_t* const left,
146
3.70M
                          const uint32_t* const top) {
147
3.70M
  const uint32_t pred = Average2(*left, top[-1]);
148
3.70M
  return pred;
149
3.70M
}
150
uint32_t VP8LPredictor7_C(const uint32_t* const left,
151
3.51M
                          const uint32_t* const top) {
152
3.51M
  const uint32_t pred = Average2(*left, top[0]);
153
3.51M
  return pred;
154
3.51M
}
155
uint32_t VP8LPredictor8_C(const uint32_t* const left,
156
3.74M
                          const uint32_t* const top) {
157
3.74M
  const uint32_t pred = Average2(top[-1], top[0]);
158
3.74M
  (void)left;
159
3.74M
  return pred;
160
3.74M
}
161
uint32_t VP8LPredictor9_C(const uint32_t* const left,
162
3.72M
                          const uint32_t* const top) {
163
3.72M
  const uint32_t pred = Average2(top[0], top[1]);
164
3.72M
  (void)left;
165
3.72M
  return pred;
166
3.72M
}
167
uint32_t VP8LPredictor10_C(const uint32_t* const left,
168
4.15M
                           const uint32_t* const top) {
169
4.15M
  const uint32_t pred = Average4(*left, top[-1], top[0], top[1]);
170
4.15M
  return pred;
171
4.15M
}
172
uint32_t VP8LPredictor11_C(const uint32_t* const left,
173
3.91M
                           const uint32_t* const top) {
174
3.91M
  const uint32_t pred = Select(top[0], *left, top[-1]);
175
3.91M
  return pred;
176
3.91M
}
177
uint32_t VP8LPredictor12_C(const uint32_t* const left,
178
4.28M
                           const uint32_t* const top) {
179
4.28M
  const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]);
180
4.28M
  return pred;
181
4.28M
}
182
uint32_t VP8LPredictor13_C(const uint32_t* const left,
183
3.54M
                           const uint32_t* const top) {
184
3.54M
  const uint32_t pred = ClampedAddSubtractHalf(*left, top[0], top[-1]);
185
3.54M
  return pred;
186
3.54M
}
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.33M
  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
415k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
196
415k
  int i;
197
415k
  uint32_t left = out[-1];
198
415k
  (void)upper;
199
2.52M
  for (i = 0; i < num_pixels; ++i) {
200
2.11M
    out[i] = left = VP8LAddPixels(in[i], left);
201
2.11M
  }
202
415k
}
203
1.40M
GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C)
204
31.2k
GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C)
205
103k
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
56.6k
GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C)
210
46.6k
GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C)
211
145k
GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C)
212
81.3k
GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C)
213
237k
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
84.4k
                                        const uint32_t* in, uint32_t* out) {
222
84.4k
  const int width = transform->xsize;
223
84.4k
  if (y_start == 0) {  // First Row follows the L (mode=1) mode.
224
2.79k
    PredictorAdd0_C(in, NULL, 1, out);
225
2.79k
    PredictorAdd1_C(in + 1, NULL, width - 1, out + 1);
226
2.79k
    in += width;
227
2.79k
    out += width;
228
2.79k
    ++y_start;
229
2.79k
  }
230
231
84.4k
  {
232
84.4k
    int y = y_start;
233
84.4k
    const int tile_width = 1 << transform->bits;
234
84.4k
    const int mask = tile_width - 1;
235
84.4k
    const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
236
84.4k
    const uint32_t* pred_mode_base =
237
84.4k
        transform->data + (y >> transform->bits) * tiles_per_row;
238
239
1.40M
    while (y < y_end) {
240
1.31M
      const uint32_t* pred_mode_src = pred_mode_base;
241
1.31M
      int x = 1;
242
      // First pixel follows the T (mode=2) mode.
243
1.31M
      PredictorAdd2_C(in, out - width, 1, out);
244
      // .. the rest:
245
18.8M
      while (x < width) {
246
17.5M
        const VP8LPredictorAddSubFunc pred_func =
247
17.5M
            VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf];
248
17.5M
        int x_end = (x & ~mask) + tile_width;
249
17.5M
        if (x_end > width) x_end = width;
250
17.5M
        pred_func(in + x, out + x - width, x_end - x, out + x);
251
17.5M
        x = x_end;
252
17.5M
      }
253
1.31M
      in += width;
254
1.31M
      out += width;
255
1.31M
      ++y;
256
1.31M
      if ((y & mask) == 0) {  // Use the same mask, since tiles are squares.
257
23.5k
        pred_mode_base += tiles_per_row;
258
23.5k
      }
259
1.31M
    }
260
84.4k
  }
261
84.4k
}
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
7.89k
                                uint32_t* dst) {
267
7.89k
  int i;
268
16.3k
  for (i = 0; i < num_pixels; ++i) {
269
8.47k
    const uint32_t argb = src[i];
270
8.47k
    const uint32_t green = ((argb >> 8) & 0xff);
271
8.47k
    uint32_t red_blue = (argb & 0x00ff00ffu);
272
8.47k
    red_blue += (green << 16) | green;
273
8.47k
    red_blue &= 0x00ff00ffu;
274
8.47k
    dst[i] = (argb & 0xff00ff00u) | red_blue;
275
8.47k
  }
276
7.89k
}
277
278
219k
static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) {
279
219k
  return ((int)color_pred * color) >> 5;
280
219k
}
281
282
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
283
1.04M
                                               VP8LMultipliers* const m) {
284
1.04M
  m->green_to_red = (color_code >> 0) & 0xff;
285
1.04M
  m->green_to_blue = (color_code >> 8) & 0xff;
286
1.04M
  m->red_to_blue = (color_code >> 16) & 0xff;
287
1.04M
}
288
289
void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
290
                                 const uint32_t* src, int num_pixels,
291
38.4k
                                 uint32_t* dst) {
292
38.4k
  int i;
293
111k
  for (i = 0; i < num_pixels; ++i) {
294
73.1k
    const uint32_t argb = src[i];
295
73.1k
    const int8_t green = (int8_t)(argb >> 8);
296
73.1k
    const uint32_t red = argb >> 16;
297
73.1k
    int new_red = red & 0xff;
298
73.1k
    int new_blue = argb & 0xff;
299
73.1k
    new_red += ColorTransformDelta((int8_t)m->green_to_red, green);
300
73.1k
    new_red &= 0xff;
301
73.1k
    new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green);
302
73.1k
    new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red);
303
73.1k
    new_blue &= 0xff;
304
73.1k
    dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
305
73.1k
  }
306
38.4k
}
307
308
// Color space inverse transform.
309
static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform,
310
                                         int y_start, int y_end,
311
6.97k
                                         const uint32_t* src, uint32_t* dst) {
312
6.97k
  const int width = transform->xsize;
313
6.97k
  const int tile_width = 1 << transform->bits;
314
6.97k
  const int mask = tile_width - 1;
315
6.97k
  const int safe_width = width & ~mask;
316
6.97k
  const int remaining_width = width - safe_width;
317
6.97k
  const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
318
6.97k
  int y = y_start;
319
6.97k
  const uint32_t* pred_row =
320
6.97k
      transform->data + (y >> transform->bits) * tiles_per_row;
321
322
66.6k
  while (y < y_end) {
323
59.7k
    const uint32_t* pred = pred_row;
324
59.7k
    VP8LMultipliers m = {0, 0, 0};
325
59.7k
    const uint32_t* const src_safe_end = src + safe_width;
326
59.7k
    const uint32_t* const src_end = src + width;
327
1.06M
    while (src < src_safe_end) {
328
1.00M
      ColorCodeToMultipliers(*pred++, &m);
329
1.00M
      VP8LTransformColorInverse(&m, src, tile_width, dst);
330
1.00M
      src += tile_width;
331
1.00M
      dst += tile_width;
332
1.00M
    }
333
59.7k
    if (src < src_end) {  // Left-overs using C-version.
334
41.4k
      ColorCodeToMultipliers(*pred++, &m);
335
41.4k
      VP8LTransformColorInverse(&m, src, remaining_width, dst);
336
41.4k
      src += remaining_width;
337
41.4k
      dst += remaining_width;
338
41.4k
    }
339
59.7k
    ++y;
340
59.7k
    if ((y & mask) == 0) pred_row += tiles_per_row;
341
59.7k
  }
342
6.97k
}
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
30.7k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
30.7k
  int y;                                                                       \
352
109k
  for (y = y_start; y < y_end; ++y) {                                          \
353
78.7k
    int x;                                                                     \
354
8.79M
    for (x = 0; x < width; ++x) {                                              \
355
8.71M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
8.71M
    }                                                                          \
357
78.7k
  }                                                                            \
358
30.7k
}                                                                              \
lossless.c:MapARGB_C
Line
Count
Source
350
29.3k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
29.3k
  int y;                                                                       \
352
89.8k
  for (y = y_start; y < y_end; ++y) {                                          \
353
60.4k
    int x;                                                                     \
354
8.52M
    for (x = 0; x < width; ++x) {                                              \
355
8.46M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
8.46M
    }                                                                          \
357
60.4k
  }                                                                            \
358
29.3k
}                                                                              \
lossless.c:MapAlpha_C
Line
Count
Source
350
1.42k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
1.42k
  int y;                                                                       \
352
19.7k
  for (y = y_start; y < y_end; ++y) {                                          \
353
18.2k
    int x;                                                                     \
354
276k
    for (x = 0; x < width; ++x) {                                              \
355
258k
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
258k
    }                                                                          \
357
18.2k
  }                                                                            \
358
1.42k
}                                                                              \
359
STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform,               \
360
                           int y_start, int y_end, const TYPE* src,            \
361
35.4k
                           TYPE* dst) {                                        \
362
35.4k
  int y;                                                                       \
363
35.4k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
35.4k
  const int width = transform->xsize;                                          \
365
35.4k
  const uint32_t* const color_map = transform->data;                           \
366
35.4k
  if (bits_per_pixel < 8) {                                                    \
367
4.67k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
4.67k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
70.6k
    for (y = y_start; y < y_end; ++y) {                                        \
370
66.0k
      int x;                                                                   \
371
4.24M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
4.18M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
4.18M
        if (bits_per_pixel == 1) {                                             \
374
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
138k
          packed >>= 1;                                                        \
376
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
138k
          packed >>= 1;                                                        \
378
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
138k
          packed >>= 1;                                                        \
380
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
138k
          packed >>= 1;                                                        \
382
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
138k
          packed >>= 1;                                                        \
384
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
138k
          packed >>= 1;                                                        \
386
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
138k
          packed >>= 1;                                                        \
388
138k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
4.04M
        } else if (bits_per_pixel == 2) {                                      \
390
1.09M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
1.09M
          packed >>= 2;                                                        \
392
1.09M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
1.09M
          packed >>= 2;                                                        \
394
1.09M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
1.09M
          packed >>= 2;                                                        \
396
1.09M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
2.95M
        } else {                                                               \
398
2.95M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
2.95M
          packed >>= 4;                                                        \
400
2.95M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
2.95M
        }                                                                      \
402
4.18M
      }                                                                        \
403
66.0k
      if (x < width) {                                                         \
404
45.3k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
109k
        for (; x < width; ++x) {                                               \
406
64.3k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
64.3k
          packed >>= bits_per_pixel;                                           \
408
64.3k
        }                                                                      \
409
45.3k
      }                                                                        \
410
66.0k
    }                                                                          \
411
30.7k
  } else {                                                                     \
412
30.7k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
30.7k
  }                                                                            \
414
35.4k
}
VP8LColorIndexInverseTransformAlpha
Line
Count
Source
361
4.35k
                           TYPE* dst) {                                        \
362
4.35k
  int y;                                                                       \
363
4.35k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
4.35k
  const int width = transform->xsize;                                          \
365
4.35k
  const uint32_t* const color_map = transform->data;                           \
366
4.35k
  if (bits_per_pixel < 8) {                                                    \
367
2.92k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
2.92k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
44.6k
    for (y = y_start; y < y_end; ++y) {                                        \
370
41.7k
      int x;                                                                   \
371
923k
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
882k
        uint32_t packed = GET_INDEX(*src++);                                   \
373
882k
        if (bits_per_pixel == 1) {                                             \
374
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
1.23k
          packed >>= 1;                                                        \
376
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
1.23k
          packed >>= 1;                                                        \
378
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
1.23k
          packed >>= 1;                                                        \
380
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
1.23k
          packed >>= 1;                                                        \
382
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
1.23k
          packed >>= 1;                                                        \
384
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
1.23k
          packed >>= 1;                                                        \
386
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
1.23k
          packed >>= 1;                                                        \
388
1.23k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
880k
        } else if (bits_per_pixel == 2) {                                      \
390
589k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
589k
          packed >>= 2;                                                        \
392
589k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
589k
          packed >>= 2;                                                        \
394
589k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
589k
          packed >>= 2;                                                        \
396
589k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
589k
        } else {                                                               \
398
291k
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
291k
          packed >>= 4;                                                        \
400
291k
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
291k
        }                                                                      \
402
882k
      }                                                                        \
403
41.7k
      if (x < width) {                                                         \
404
33.7k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
76.9k
        for (; x < width; ++x) {                                               \
406
43.2k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
43.2k
          packed >>= bits_per_pixel;                                           \
408
43.2k
        }                                                                      \
409
33.7k
      }                                                                        \
410
41.7k
    }                                                                          \
411
2.92k
  } else {                                                                     \
412
1.42k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
1.42k
  }                                                                            \
414
4.35k
}
lossless.c:ColorIndexInverseTransform_C
Line
Count
Source
361
31.0k
                           TYPE* dst) {                                        \
362
31.0k
  int y;                                                                       \
363
31.0k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
31.0k
  const int width = transform->xsize;                                          \
365
31.0k
  const uint32_t* const color_map = transform->data;                           \
366
31.0k
  if (bits_per_pixel < 8) {                                                    \
367
1.75k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
1.75k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
26.0k
    for (y = y_start; y < y_end; ++y) {                                        \
370
24.2k
      int x;                                                                   \
371
3.32M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
3.30M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
3.30M
        if (bits_per_pixel == 1) {                                             \
374
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
137k
          packed >>= 1;                                                        \
376
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
137k
          packed >>= 1;                                                        \
378
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
137k
          packed >>= 1;                                                        \
380
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
137k
          packed >>= 1;                                                        \
382
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
137k
          packed >>= 1;                                                        \
384
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
137k
          packed >>= 1;                                                        \
386
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
137k
          packed >>= 1;                                                        \
388
137k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
3.16M
        } else if (bits_per_pixel == 2) {                                      \
390
501k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
501k
          packed >>= 2;                                                        \
392
501k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
501k
          packed >>= 2;                                                        \
394
501k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
501k
          packed >>= 2;                                                        \
396
501k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
2.66M
        } else {                                                               \
398
2.66M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
2.66M
          packed >>= 4;                                                        \
400
2.66M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
2.66M
        }                                                                      \
402
3.30M
      }                                                                        \
403
24.2k
      if (x < width) {                                                         \
404
11.5k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
32.6k
        for (; x < width; ++x) {                                               \
406
21.0k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
21.0k
          packed >>= bits_per_pixel;                                           \
408
21.0k
        }                                                                      \
409
11.5k
      }                                                                        \
410
24.2k
    }                                                                          \
411
29.3k
  } else {                                                                     \
412
29.3k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
29.3k
  }                                                                            \
414
31.0k
}
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
139k
                          uint32_t* const out) {
427
139k
  const int width = transform->xsize;
428
139k
  assert(row_start < row_end);
429
139k
  assert(row_end <= transform->ysize);
430
139k
  switch (transform->type) {
431
16.7k
    case SUBTRACT_GREEN_TRANSFORM:
432
16.7k
      VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out);
433
16.7k
      break;
434
84.4k
    case PREDICTOR_TRANSFORM:
435
84.4k
      PredictorInverseTransform_C(transform, row_start, row_end, in, out);
436
84.4k
      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
81.8k
        memcpy(out - width, out + (row_end - row_start - 1) * width,
440
81.8k
               width * sizeof(*out));
441
81.8k
      }
442
84.4k
      break;
443
6.97k
    case CROSS_COLOR_TRANSFORM:
444
6.97k
      ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out);
445
6.97k
      break;
446
31.0k
    case COLOR_INDEXING_TRANSFORM:
447
31.0k
      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
918
        const int out_stride = (row_end - row_start) * width;
454
918
        const int in_stride =
455
918
            (row_end - row_start) *
456
918
            VP8LSubSampleSize(transform->xsize, transform->bits);
457
918
        uint32_t* const src = out + out_stride - in_stride;
458
918
        memmove(src, out, in_stride * sizeof(*src));
459
918
        ColorIndexInverseTransform_C(transform, row_start, row_end, src, out);
460
30.1k
      } else {
461
30.1k
        ColorIndexInverseTransform_C(transform, row_start, row_end, in, out);
462
30.1k
      }
463
31.0k
      break;
464
139k
  }
465
139k
}
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
107k
                            uint8_t* WEBP_RESTRICT dst) {
480
107k
  const uint32_t* const src_end = src + num_pixels;
481
520k
  while (src < src_end) {
482
412k
    const uint32_t argb = *src++;
483
412k
    *dst++ = (argb >> 16) & 0xff;
484
412k
    *dst++ = (argb >> 8) & 0xff;
485
412k
    *dst++ = (argb >> 0) & 0xff;
486
412k
  }
487
107k
}
488
489
void VP8LConvertBGRAToRGBA_C(const uint32_t* WEBP_RESTRICT src, int num_pixels,
490
2.39M
                             uint8_t* WEBP_RESTRICT dst) {
491
2.39M
  const uint32_t* const src_end = src + num_pixels;
492
10.9M
  while (src < src_end) {
493
8.55M
    const uint32_t argb = *src++;
494
8.55M
    *dst++ = (argb >> 16) & 0xff;
495
8.55M
    *dst++ = (argb >> 8) & 0xff;
496
8.55M
    *dst++ = (argb >> 0) & 0xff;
497
8.55M
    *dst++ = (argb >> 24) & 0xff;
498
8.55M
  }
499
2.39M
}
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.56M
                         WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
562
1.56M
  switch (out_colorspace) {
563
153k
    case MODE_RGB:
564
153k
      VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
565
153k
      break;
566
1.41M
    case MODE_RGBA:
567
1.41M
      VP8LConvertBGRAToRGBA(in_data, num_pixels, rgba);
568
1.41M
      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.56M
  }
603
1.56M
}
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
627
VP8LMapARGBFunc VP8LMapColor32b;
628
VP8LMapAlphaFunc VP8LMapColor8b;
629
630
extern VP8CPUInfo VP8GetCPUInfo;
631
extern void VP8LDspInitSSE2(void);
632
extern void VP8LDspInitSSE41(void);
633
extern void VP8LDspInitAVX2(void);
634
extern void VP8LDspInitNEON(void);
635
extern void VP8LDspInitMIPSdspR2(void);
636
extern void VP8LDspInitMSA(void);
637
638
#define COPY_PREDICTOR_ARRAY(IN, OUT)                       \
639
21
  do {                                                      \
640
21
    (OUT)[0] = IN##0_C;                                     \
641
21
    (OUT)[1] = IN##1_C;                                     \
642
21
    (OUT)[2] = IN##2_C;                                     \
643
21
    (OUT)[3] = IN##3_C;                                     \
644
21
    (OUT)[4] = IN##4_C;                                     \
645
21
    (OUT)[5] = IN##5_C;                                     \
646
21
    (OUT)[6] = IN##6_C;                                     \
647
21
    (OUT)[7] = IN##7_C;                                     \
648
21
    (OUT)[8] = IN##8_C;                                     \
649
21
    (OUT)[9] = IN##9_C;                                     \
650
21
    (OUT)[10] = IN##10_C;                                   \
651
21
    (OUT)[11] = IN##11_C;                                   \
652
21
    (OUT)[12] = IN##12_C;                                   \
653
21
    (OUT)[13] = IN##13_C;                                   \
654
21
    (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \
655
21
    (OUT)[15] = IN##0_C;                                    \
656
21
  } while (0);
657
658
7
WEBP_DSP_INIT_FUNC(VP8LDspInit) {
659
7
  COPY_PREDICTOR_ARRAY(VP8LPredictor, VP8LPredictors)
660
7
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd)
661
7
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C)
662
663
7
#if !WEBP_NEON_OMIT_C_CODE
664
7
  VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
665
666
7
  VP8LTransformColorInverse = VP8LTransformColorInverse_C;
667
668
7
  VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
669
7
  VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
670
7
  VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
671
7
#endif
672
673
7
  VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
674
7
  VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
675
676
7
  VP8LMapColor32b = MapARGB_C;
677
7
  VP8LMapColor8b = MapAlpha_C;
678
679
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
680
7
  if (VP8GetCPUInfo != NULL) {
681
7
#if defined(WEBP_HAVE_SSE2)
682
7
    if (VP8GetCPUInfo(kSSE2)) {
683
7
      VP8LDspInitSSE2();
684
7
#if defined(WEBP_HAVE_SSE41)
685
7
      if (VP8GetCPUInfo(kSSE4_1)) {
686
7
        VP8LDspInitSSE41();
687
#if defined(WEBP_HAVE_AVX2)
688
        if (VP8GetCPUInfo(kAVX2)) {
689
          VP8LDspInitAVX2();
690
        }
691
#endif
692
7
      }
693
7
#endif
694
7
    }
695
7
#endif
696
#if defined(WEBP_USE_MIPS_DSP_R2)
697
    if (VP8GetCPUInfo(kMIPSdspR2)) {
698
      VP8LDspInitMIPSdspR2();
699
    }
700
#endif
701
#if defined(WEBP_USE_MSA)
702
    if (VP8GetCPUInfo(kMSA)) {
703
      VP8LDspInitMSA();
704
    }
705
#endif
706
7
  }
707
708
#if defined(WEBP_HAVE_NEON)
709
  if (WEBP_NEON_OMIT_C_CODE ||
710
      (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
711
    VP8LDspInitNEON();
712
  }
713
#endif
714
715
7
  assert(VP8LAddGreenToBlueAndRed != NULL);
716
7
  assert(VP8LTransformColorInverse != NULL);
717
7
  assert(VP8LConvertBGRAToRGBA != NULL);
718
7
  assert(VP8LConvertBGRAToRGB != NULL);
719
7
  assert(VP8LConvertBGRAToBGR != NULL);
720
7
  assert(VP8LConvertBGRAToRGBA4444 != NULL);
721
7
  assert(VP8LConvertBGRAToRGB565 != NULL);
722
7
  assert(VP8LMapColor32b != NULL);
723
  assert(VP8LMapColor8b != NULL);
724
7
}
725
#undef COPY_PREDICTOR_ARRAY
726
727
//------------------------------------------------------------------------------