Coverage Report

Created: 2026-06-07 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
19.7M
static WEBP_INLINE uint32_t Average2(uint32_t a0, uint32_t a1) {
36
19.7M
  return (((a0 ^ a1) & 0xfefefefeu) >> 1) + (a0 & a1);
37
19.7M
}
38
39
1.97M
static WEBP_INLINE uint32_t Average3(uint32_t a0, uint32_t a1, uint32_t a2) {
40
1.97M
  return Average2(Average2(a0, a2), a1);
41
1.97M
}
42
43
static WEBP_INLINE uint32_t Average4(uint32_t a0, uint32_t a1, uint32_t a2,
44
2.11M
                                     uint32_t a3) {
45
2.11M
  return Average2(Average2(a0, a1), Average2(a2, a3));
46
2.11M
}
47
48
17.3M
static WEBP_INLINE uint32_t Clip255(uint32_t a) {
49
17.3M
  if (a < 256) {
50
16.6M
    return a;
51
16.6M
  }
52
  // return 0, when a is a negative integer.
53
  // return 255, when a is positive.
54
643k
  return ~a >> 24;
55
17.3M
}
56
57
10.0M
static WEBP_INLINE int AddSubtractComponentFull(int a, int b, int c) {
58
10.0M
  return Clip255((uint32_t)(a + b - c));
59
10.0M
}
60
61
static WEBP_INLINE uint32_t ClampedAddSubtractFull(uint32_t c0, uint32_t c1,
62
2.50M
                                                   uint32_t c2) {
63
2.50M
  const int a = AddSubtractComponentFull(c0 >> 24, c1 >> 24, c2 >> 24);
64
2.50M
  const int r = AddSubtractComponentFull((c0 >> 16) & 0xff, (c1 >> 16) & 0xff,
65
2.50M
                                         (c2 >> 16) & 0xff);
66
2.50M
  const int g = AddSubtractComponentFull((c0 >> 8) & 0xff, (c1 >> 8) & 0xff,
67
2.50M
                                         (c2 >> 8) & 0xff);
68
2.50M
  const int b = AddSubtractComponentFull(c0 & 0xff, c1 & 0xff, c2 & 0xff);
69
2.50M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
70
2.50M
}
71
72
7.33M
static WEBP_INLINE int AddSubtractComponentHalf(int a, int b) {
73
7.33M
  return Clip255((uint32_t)(a + (a - b) / 2));
74
7.33M
}
75
76
static WEBP_INLINE uint32_t ClampedAddSubtractHalf(uint32_t c0, uint32_t c1,
77
1.83M
                                                   uint32_t c2) {
78
1.83M
  const uint32_t ave = Average2(c0, c1);
79
1.83M
  const int a = AddSubtractComponentHalf(ave >> 24, c2 >> 24);
80
1.83M
  const int r = AddSubtractComponentHalf((ave >> 16) & 0xff, (c2 >> 16) & 0xff);
81
1.83M
  const int g = AddSubtractComponentHalf((ave >> 8) & 0xff, (c2 >> 8) & 0xff);
82
1.83M
  const int b = AddSubtractComponentHalf((ave >> 0) & 0xff, (c2 >> 0) & 0xff);
83
1.83M
  return ((uint32_t)a << 24) | (r << 16) | (g << 8) | b;
84
1.83M
}
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.39M
static LOCAL_INLINE int Sub3(int a, int b, int c) {
95
8.39M
  const int pb = b - c;
96
8.39M
  const int pa = a - c;
97
8.39M
  return abs(pb) - abs(pa);
98
8.39M
}
99
100
#undef LOCAL_INLINE
101
102
2.09M
static WEBP_INLINE uint32_t Select(uint32_t a, uint32_t b, uint32_t c) {
103
2.09M
  const int pa_minus_pb =
104
2.09M
      Sub3((a >> 24), (b >> 24), (c >> 24)) +
105
2.09M
      Sub3((a >> 16) & 0xff, (b >> 16) & 0xff, (c >> 16) & 0xff) +
106
2.09M
      Sub3((a >> 8) & 0xff, (b >> 8) & 0xff, (c >> 8) & 0xff) +
107
2.09M
      Sub3((a) & 0xff, (b) & 0xff, (c) & 0xff);
108
2.09M
  return (pa_minus_pb <= 0) ? a : b;
109
2.09M
}
110
111
//------------------------------------------------------------------------------
112
// Predictors
113
114
static uint32_t VP8LPredictor0_C(const uint32_t* const left,
115
191M
                                 const uint32_t* const top) {
116
191M
  (void)top;
117
191M
  (void)left;
118
191M
  return ARGB_BLACK;
119
191M
}
120
static uint32_t VP8LPredictor1_C(const uint32_t* const left,
121
210M
                                 const uint32_t* const top) {
122
210M
  (void)top;
123
210M
  return *left;
124
210M
}
125
uint32_t VP8LPredictor2_C(const uint32_t* const left,
126
168M
                          const uint32_t* const top) {
127
168M
  (void)left;
128
168M
  return top[0];
129
168M
}
130
uint32_t VP8LPredictor3_C(const uint32_t* const left,
131
160M
                          const uint32_t* const top) {
132
160M
  (void)left;
133
160M
  return top[1];
134
160M
}
135
uint32_t VP8LPredictor4_C(const uint32_t* const left,
136
160M
                          const uint32_t* const top) {
137
160M
  (void)left;
138
160M
  return top[-1];
139
160M
}
140
uint32_t VP8LPredictor5_C(const uint32_t* const left,
141
1.97M
                          const uint32_t* const top) {
142
1.97M
  const uint32_t pred = Average3(*left, top[0], top[1]);
143
1.97M
  return pred;
144
1.97M
}
145
uint32_t VP8LPredictor6_C(const uint32_t* const left,
146
1.85M
                          const uint32_t* const top) {
147
1.85M
  const uint32_t pred = Average2(*left, top[-1]);
148
1.85M
  return pred;
149
1.85M
}
150
uint32_t VP8LPredictor7_C(const uint32_t* const left,
151
1.82M
                          const uint32_t* const top) {
152
1.82M
  const uint32_t pred = Average2(*left, top[0]);
153
1.82M
  return pred;
154
1.82M
}
155
uint32_t VP8LPredictor8_C(const uint32_t* const left,
156
1.89M
                          const uint32_t* const top) {
157
1.89M
  const uint32_t pred = Average2(top[-1], top[0]);
158
1.89M
  (void)left;
159
1.89M
  return pred;
160
1.89M
}
161
uint32_t VP8LPredictor9_C(const uint32_t* const left,
162
2.00M
                          const uint32_t* const top) {
163
2.00M
  const uint32_t pred = Average2(top[0], top[1]);
164
2.00M
  (void)left;
165
2.00M
  return pred;
166
2.00M
}
167
uint32_t VP8LPredictor10_C(const uint32_t* const left,
168
2.11M
                           const uint32_t* const top) {
169
2.11M
  const uint32_t pred = Average4(*left, top[-1], top[0], top[1]);
170
2.11M
  return pred;
171
2.11M
}
172
uint32_t VP8LPredictor11_C(const uint32_t* const left,
173
2.09M
                           const uint32_t* const top) {
174
2.09M
  const uint32_t pred = Select(top[0], *left, top[-1]);
175
2.09M
  return pred;
176
2.09M
}
177
uint32_t VP8LPredictor12_C(const uint32_t* const left,
178
2.50M
                           const uint32_t* const top) {
179
2.50M
  const uint32_t pred = ClampedAddSubtractFull(*left, top[0], top[-1]);
180
2.50M
  return pred;
181
2.50M
}
182
uint32_t VP8LPredictor13_C(const uint32_t* const left,
183
1.83M
                           const uint32_t* const top) {
184
1.83M
  const uint32_t pred = ClampedAddSubtractHalf(*left, top[0], top[-1]);
185
1.83M
  return pred;
186
1.83M
}
187
188
static void PredictorAdd0_C(const uint32_t* in, const uint32_t* upper,
189
377k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
190
377k
  int x;
191
377k
  (void)upper;
192
1.32M
  for (x = 0; x < num_pixels; ++x) out[x] = VP8LAddPixels(in[x], ARGB_BLACK);
193
377k
}
194
static void PredictorAdd1_C(const uint32_t* in, const uint32_t* upper,
195
384k
                            int num_pixels, uint32_t* WEBP_RESTRICT out) {
196
384k
  int i;
197
384k
  uint32_t left = out[-1];
198
384k
  (void)upper;
199
2.24M
  for (i = 0; i < num_pixels; ++i) {
200
1.86M
    out[i] = left = VP8LAddPixels(in[i], left);
201
1.86M
  }
202
384k
}
203
1.04M
GENERATE_PREDICTOR_ADD(VP8LPredictor2_C, PredictorAdd2_C)
204
14.7k
GENERATE_PREDICTOR_ADD(VP8LPredictor3_C, PredictorAdd3_C)
205
23.1k
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
25.1k
GENERATE_PREDICTOR_ADD(VP8LPredictor8_C, PredictorAdd8_C)
210
53.5k
GENERATE_PREDICTOR_ADD(VP8LPredictor9_C, PredictorAdd9_C)
211
81.9k
GENERATE_PREDICTOR_ADD(VP8LPredictor10_C, PredictorAdd10_C)
212
78.1k
GENERATE_PREDICTOR_ADD(VP8LPredictor11_C, PredictorAdd11_C)
213
226k
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
65.1k
                                        const uint32_t* in, uint32_t* out) {
222
65.1k
  const int width = transform->xsize;
223
65.1k
  if (y_start == 0) {  // First Row follows the L (mode=1) mode.
224
1.57k
    PredictorAdd0_C(in, NULL, 1, out);
225
1.57k
    PredictorAdd1_C(in + 1, NULL, width - 1, out + 1);
226
1.57k
    in += width;
227
1.57k
    out += width;
228
1.57k
    ++y_start;
229
1.57k
  }
230
231
65.1k
  {
232
65.1k
    int y = y_start;
233
65.1k
    const int tile_width = 1 << transform->bits;
234
65.1k
    const int mask = tile_width - 1;
235
65.1k
    const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
236
65.1k
    const uint32_t* pred_mode_base =
237
65.1k
        transform->data + (y >> transform->bits) * tiles_per_row;
238
239
1.08M
    while (y < y_end) {
240
1.02M
      const uint32_t* pred_mode_src = pred_mode_base;
241
1.02M
      int x = 1;
242
      // First pixel follows the T (mode=2) mode.
243
1.02M
      PredictorAdd2_C(in, out - width, 1, out);
244
      // .. the rest:
245
51.1M
      while (x < width) {
246
50.1M
        const VP8LPredictorAddSubFunc pred_func =
247
50.1M
            VP8LPredictorsAdd[((*pred_mode_src++) >> 8) & 0xf];
248
50.1M
        int x_end = (x & ~mask) + tile_width;
249
50.1M
        if (x_end > width) x_end = width;
250
50.1M
        pred_func(in + x, out + x - width, x_end - x, out + x);
251
50.1M
        x = x_end;
252
50.1M
      }
253
1.02M
      in += width;
254
1.02M
      out += width;
255
1.02M
      ++y;
256
1.02M
      if ((y & mask) == 0) {  // Use the same mask, since tiles are squares.
257
40.6k
        pred_mode_base += tiles_per_row;
258
40.6k
      }
259
1.02M
    }
260
65.1k
  }
261
65.1k
}
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
258
                                uint32_t* dst) {
267
258
  int i;
268
778
  for (i = 0; i < num_pixels; ++i) {
269
520
    const uint32_t argb = src[i];
270
520
    const uint32_t green = ((argb >> 8) & 0xff);
271
520
    uint32_t red_blue = (argb & 0x00ff00ffu);
272
520
    red_blue += (green << 16) | green;
273
520
    red_blue &= 0x00ff00ffu;
274
520
    dst[i] = (argb & 0xff00ff00u) | red_blue;
275
520
  }
276
258
}
277
278
89.2k
static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) {
279
89.2k
  return ((int)color_pred * color) >> 5;
280
89.2k
}
281
282
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
283
1.17M
                                               VP8LMultipliers* const m) {
284
1.17M
  m->green_to_red = (color_code >> 0) & 0xff;
285
1.17M
  m->green_to_blue = (color_code >> 8) & 0xff;
286
1.17M
  m->red_to_blue = (color_code >> 16) & 0xff;
287
1.17M
}
288
289
void VP8LTransformColorInverse_C(const VP8LMultipliers* const m,
290
                                 const uint32_t* src, int num_pixels,
291
13.1k
                                 uint32_t* dst) {
292
13.1k
  int i;
293
42.8k
  for (i = 0; i < num_pixels; ++i) {
294
29.7k
    const uint32_t argb = src[i];
295
29.7k
    const int8_t green = (int8_t)(argb >> 8);
296
29.7k
    const uint32_t red = argb >> 16;
297
29.7k
    int new_red = red & 0xff;
298
29.7k
    int new_blue = argb & 0xff;
299
29.7k
    new_red += ColorTransformDelta((int8_t)m->green_to_red, green);
300
29.7k
    new_red &= 0xff;
301
29.7k
    new_blue += ColorTransformDelta((int8_t)m->green_to_blue, green);
302
29.7k
    new_blue += ColorTransformDelta((int8_t)m->red_to_blue, (int8_t)new_red);
303
29.7k
    new_blue &= 0xff;
304
29.7k
    dst[i] = (argb & 0xff00ff00u) | (new_red << 16) | (new_blue);
305
29.7k
  }
306
13.1k
}
307
308
// Color space inverse transform.
309
static void ColorSpaceInverseTransform_C(const VP8LTransform* const transform,
310
                                         int y_start, int y_end,
311
899
                                         const uint32_t* src, uint32_t* dst) {
312
899
  const int width = transform->xsize;
313
899
  const int tile_width = 1 << transform->bits;
314
899
  const int mask = tile_width - 1;
315
899
  const int safe_width = width & ~mask;
316
899
  const int remaining_width = width - safe_width;
317
899
  const int tiles_per_row = VP8LSubSampleSize(width, transform->bits);
318
899
  int y = y_start;
319
899
  const uint32_t* pred_row =
320
899
      transform->data + (y >> transform->bits) * tiles_per_row;
321
322
14.7k
  while (y < y_end) {
323
13.8k
    const uint32_t* pred = pred_row;
324
13.8k
    VP8LMultipliers m = {0, 0, 0};
325
13.8k
    const uint32_t* const src_safe_end = src + safe_width;
326
13.8k
    const uint32_t* const src_end = src + width;
327
1.17M
    while (src < src_safe_end) {
328
1.15M
      ColorCodeToMultipliers(*pred++, &m);
329
1.15M
      VP8LTransformColorInverse(&m, src, tile_width, dst);
330
1.15M
      src += tile_width;
331
1.15M
      dst += tile_width;
332
1.15M
    }
333
13.8k
    if (src < src_end) {  // Left-overs using C-version.
334
13.6k
      ColorCodeToMultipliers(*pred++, &m);
335
13.6k
      VP8LTransformColorInverse(&m, src, remaining_width, dst);
336
13.6k
      src += remaining_width;
337
13.6k
      dst += remaining_width;
338
13.6k
    }
339
13.8k
    ++y;
340
13.8k
    if ((y & mask) == 0) pred_row += tiles_per_row;
341
13.8k
  }
342
899
}
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.05k
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
1.05k
  int y;                                                                       \
352
15.4k
  for (y = y_start; y < y_end; ++y) {                                          \
353
14.4k
    int x;                                                                     \
354
2.13M
    for (x = 0; x < width; ++x) {                                              \
355
2.11M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
2.11M
    }                                                                          \
357
14.4k
  }                                                                            \
358
1.05k
}                                                                              \
lossless.c:MapARGB_C
Line
Count
Source
350
809
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
809
  int y;                                                                       \
352
12.1k
  for (y = y_start; y < y_end; ++y) {                                          \
353
11.3k
    int x;                                                                     \
354
1.97M
    for (x = 0; x < width; ++x) {                                              \
355
1.96M
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
1.96M
    }                                                                          \
357
11.3k
  }                                                                            \
358
809
}                                                                              \
lossless.c:MapAlpha_C
Line
Count
Source
350
247
                   TYPE* dst, int y_start, int y_end, int width) {             \
351
247
  int y;                                                                       \
352
3.33k
  for (y = y_start; y < y_end; ++y) {                                          \
353
3.08k
    int x;                                                                     \
354
159k
    for (x = 0; x < width; ++x) {                                              \
355
156k
      *dst++ = GET_VALUE(color_map[GET_INDEX(*src++)]);                        \
356
156k
    }                                                                          \
357
3.08k
  }                                                                            \
358
247
}                                                                              \
359
STATIC_DECL void FUNC_NAME(const VP8LTransform* const transform,               \
360
                           int y_start, int y_end, const TYPE* src,            \
361
3.96k
                           TYPE* dst) {                                        \
362
3.96k
  int y;                                                                       \
363
3.96k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
3.96k
  const int width = transform->xsize;                                          \
365
3.96k
  const uint32_t* const color_map = transform->data;                           \
366
3.96k
  if (bits_per_pixel < 8) {                                                    \
367
2.90k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
2.90k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
47.1k
    for (y = y_start; y < y_end; ++y) {                                        \
370
44.2k
      int x;                                                                   \
371
8.78M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
8.74M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
8.74M
        if (bits_per_pixel == 1) {                                             \
374
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
249k
          packed >>= 1;                                                        \
376
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
249k
          packed >>= 1;                                                        \
378
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
249k
          packed >>= 1;                                                        \
380
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
249k
          packed >>= 1;                                                        \
382
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
249k
          packed >>= 1;                                                        \
384
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
249k
          packed >>= 1;                                                        \
386
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
249k
          packed >>= 1;                                                        \
388
249k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
8.49M
        } else if (bits_per_pixel == 2) {                                      \
390
6.86M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
6.86M
          packed >>= 2;                                                        \
392
6.86M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
6.86M
          packed >>= 2;                                                        \
394
6.86M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
6.86M
          packed >>= 2;                                                        \
396
6.86M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
6.86M
        } else {                                                               \
398
1.63M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
1.63M
          packed >>= 4;                                                        \
400
1.63M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
1.63M
        }                                                                      \
402
8.74M
      }                                                                        \
403
44.2k
      if (x < width) {                                                         \
404
33.8k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
90.2k
        for (; x < width; ++x) {                                               \
406
56.3k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
56.3k
          packed >>= bits_per_pixel;                                           \
408
56.3k
        }                                                                      \
409
33.8k
      }                                                                        \
410
44.2k
    }                                                                          \
411
2.90k
  } else {                                                                     \
412
1.05k
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
1.05k
  }                                                                            \
414
3.96k
}
VP8LColorIndexInverseTransformAlpha
Line
Count
Source
361
912
                           TYPE* dst) {                                        \
362
912
  int y;                                                                       \
363
912
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
912
  const int width = transform->xsize;                                          \
365
912
  const uint32_t* const color_map = transform->data;                           \
366
912
  if (bits_per_pixel < 8) {                                                    \
367
665
    const int pixels_per_byte = 1 << transform->bits;                          \
368
665
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
10.4k
    for (y = y_start; y < y_end; ++y) {                                        \
370
9.80k
      int x;                                                                   \
371
172k
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
162k
        uint32_t packed = GET_INDEX(*src++);                                   \
373
162k
        if (bits_per_pixel == 1) {                                             \
374
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
8.16k
          packed >>= 1;                                                        \
376
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
8.16k
          packed >>= 1;                                                        \
378
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
8.16k
          packed >>= 1;                                                        \
380
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
8.16k
          packed >>= 1;                                                        \
382
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
8.16k
          packed >>= 1;                                                        \
384
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
8.16k
          packed >>= 1;                                                        \
386
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
8.16k
          packed >>= 1;                                                        \
388
8.16k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
154k
        } else if (bits_per_pixel == 2) {                                      \
390
154k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
154k
          packed >>= 2;                                                        \
392
154k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
154k
          packed >>= 2;                                                        \
394
154k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
154k
          packed >>= 2;                                                        \
396
154k
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
154k
        } else {                                                               \
398
0
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
0
          packed >>= 4;                                                        \
400
0
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
0
        }                                                                      \
402
162k
      }                                                                        \
403
9.80k
      if (x < width) {                                                         \
404
8.79k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
23.1k
        for (; x < width; ++x) {                                               \
406
14.3k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
14.3k
          packed >>= bits_per_pixel;                                           \
408
14.3k
        }                                                                      \
409
8.79k
      }                                                                        \
410
9.80k
    }                                                                          \
411
665
  } else {                                                                     \
412
247
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
247
  }                                                                            \
414
912
}
lossless.c:ColorIndexInverseTransform_C
Line
Count
Source
361
3.04k
                           TYPE* dst) {                                        \
362
3.04k
  int y;                                                                       \
363
3.04k
  const int bits_per_pixel = 8 >> transform->bits;                             \
364
3.04k
  const int width = transform->xsize;                                          \
365
3.04k
  const uint32_t* const color_map = transform->data;                           \
366
3.04k
  if (bits_per_pixel < 8) {                                                    \
367
2.23k
    const int pixels_per_byte = 1 << transform->bits;                          \
368
2.23k
    const uint32_t bit_mask = (1 << bits_per_pixel) - 1;                       \
369
36.6k
    for (y = y_start; y < y_end; ++y) {                                        \
370
34.4k
      int x;                                                                   \
371
8.61M
      for (x = 0; x + pixels_per_byte <= width; x += pixels_per_byte) {        \
372
8.58M
        uint32_t packed = GET_INDEX(*src++);                                   \
373
8.58M
        if (bits_per_pixel == 1) {                                             \
374
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
375
240k
          packed >>= 1;                                                        \
376
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
377
240k
          packed >>= 1;                                                        \
378
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
379
240k
          packed >>= 1;                                                        \
380
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
381
240k
          packed >>= 1;                                                        \
382
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
383
240k
          packed >>= 1;                                                        \
384
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
385
240k
          packed >>= 1;                                                        \
386
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
387
240k
          packed >>= 1;                                                        \
388
240k
          *dst++ = GET_VALUE(color_map[packed & 1]);                           \
389
8.34M
        } else if (bits_per_pixel == 2) {                                      \
390
6.70M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
391
6.70M
          packed >>= 2;                                                        \
392
6.70M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
393
6.70M
          packed >>= 2;                                                        \
394
6.70M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
395
6.70M
          packed >>= 2;                                                        \
396
6.70M
          *dst++ = GET_VALUE(color_map[packed & 3]);                           \
397
6.70M
        } else {                                                               \
398
1.63M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
399
1.63M
          packed >>= 4;                                                        \
400
1.63M
          *dst++ = GET_VALUE(color_map[packed & 15]);                          \
401
1.63M
        }                                                                      \
402
8.58M
      }                                                                        \
403
34.4k
      if (x < width) {                                                         \
404
25.0k
        uint32_t packed = GET_INDEX(*src++);                                   \
405
67.0k
        for (; x < width; ++x) {                                               \
406
42.0k
          *dst++ = GET_VALUE(color_map[packed & bit_mask]);                    \
407
42.0k
          packed >>= bits_per_pixel;                                           \
408
42.0k
        }                                                                      \
409
25.0k
      }                                                                        \
410
34.4k
    }                                                                          \
411
2.23k
  } else {                                                                     \
412
809
    VP8LMapColor##BIT_SUFFIX(src, color_map, dst, y_start, y_end, width);      \
413
809
  }                                                                            \
414
3.04k
}
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
75.5k
                          uint32_t* const out) {
427
75.5k
  const int width = transform->xsize;
428
75.5k
  assert(row_start < row_end);
429
75.5k
  assert(row_end <= transform->ysize);
430
75.5k
  switch (transform->type) {
431
6.42k
    case SUBTRACT_GREEN_TRANSFORM:
432
6.42k
      VP8LAddGreenToBlueAndRed(in, (row_end - row_start) * width, out);
433
6.42k
      break;
434
65.1k
    case PREDICTOR_TRANSFORM:
435
65.1k
      PredictorInverseTransform_C(transform, row_start, row_end, in, out);
436
65.1k
      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
63.6k
        memcpy(out - width, out + (row_end - row_start - 1) * width,
440
63.6k
               width * sizeof(*out));
441
63.6k
      }
442
65.1k
      break;
443
899
    case CROSS_COLOR_TRANSFORM:
444
899
      ColorSpaceInverseTransform_C(transform, row_start, row_end, in, out);
445
899
      break;
446
3.04k
    case COLOR_INDEXING_TRANSFORM:
447
3.04k
      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.55k
        const int out_stride = (row_end - row_start) * width;
454
1.55k
        const int in_stride =
455
1.55k
            (row_end - row_start) *
456
1.55k
            VP8LSubSampleSize(transform->xsize, transform->bits);
457
1.55k
        uint32_t* const src = out + out_stride - in_stride;
458
1.55k
        memmove(src, out, in_stride * sizeof(*src));
459
1.55k
        ColorIndexInverseTransform_C(transform, row_start, row_end, src, out);
460
1.55k
      } else {
461
1.48k
        ColorIndexInverseTransform_C(transform, row_start, row_end, in, out);
462
1.48k
      }
463
3.04k
      break;
464
75.5k
  }
465
75.5k
}
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.23M
                             uint8_t* WEBP_RESTRICT dst) {
491
1.23M
  const uint32_t* const src_end = src + num_pixels;
492
5.80M
  while (src < src_end) {
493
4.56M
    const uint32_t argb = *src++;
494
4.56M
    *dst++ = (argb >> 16) & 0xff;
495
4.56M
    *dst++ = (argb >> 8) & 0xff;
496
4.56M
    *dst++ = (argb >> 0) & 0xff;
497
4.56M
    *dst++ = (argb >> 24) & 0xff;
498
4.56M
  }
499
1.23M
}
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.41M
                         WEBP_CSP_MODE out_colorspace, uint8_t* const rgba) {
562
1.41M
  switch (out_colorspace) {
563
0
    case MODE_RGB:
564
0
      VP8LConvertBGRAToRGB(in_data, num_pixels, rgba);
565
0
      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.41M
  }
603
1.41M
}
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
3
  do {                                                      \
640
3
    (OUT)[0] = IN##0_C;                                     \
641
3
    (OUT)[1] = IN##1_C;                                     \
642
3
    (OUT)[2] = IN##2_C;                                     \
643
3
    (OUT)[3] = IN##3_C;                                     \
644
3
    (OUT)[4] = IN##4_C;                                     \
645
3
    (OUT)[5] = IN##5_C;                                     \
646
3
    (OUT)[6] = IN##6_C;                                     \
647
3
    (OUT)[7] = IN##7_C;                                     \
648
3
    (OUT)[8] = IN##8_C;                                     \
649
3
    (OUT)[9] = IN##9_C;                                     \
650
3
    (OUT)[10] = IN##10_C;                                   \
651
3
    (OUT)[11] = IN##11_C;                                   \
652
3
    (OUT)[12] = IN##12_C;                                   \
653
3
    (OUT)[13] = IN##13_C;                                   \
654
3
    (OUT)[14] = IN##0_C; /* <- padding security sentinels*/ \
655
3
    (OUT)[15] = IN##0_C;                                    \
656
3
  } while (0);
657
658
1
WEBP_DSP_INIT_FUNC(VP8LDspInit) {
659
1
  COPY_PREDICTOR_ARRAY(VP8LPredictor, VP8LPredictors)
660
1
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd)
661
1
  COPY_PREDICTOR_ARRAY(PredictorAdd, VP8LPredictorsAdd_C)
662
663
1
#if !WEBP_NEON_OMIT_C_CODE
664
1
  VP8LAddGreenToBlueAndRed = VP8LAddGreenToBlueAndRed_C;
665
666
1
  VP8LTransformColorInverse = VP8LTransformColorInverse_C;
667
668
1
  VP8LConvertBGRAToRGBA = VP8LConvertBGRAToRGBA_C;
669
1
  VP8LConvertBGRAToRGB = VP8LConvertBGRAToRGB_C;
670
1
  VP8LConvertBGRAToBGR = VP8LConvertBGRAToBGR_C;
671
1
#endif
672
673
1
  VP8LConvertBGRAToRGBA4444 = VP8LConvertBGRAToRGBA4444_C;
674
1
  VP8LConvertBGRAToRGB565 = VP8LConvertBGRAToRGB565_C;
675
676
1
  VP8LMapColor32b = MapARGB_C;
677
1
  VP8LMapColor8b = MapAlpha_C;
678
679
  // If defined, use CPUInfo() to overwrite some pointers with faster versions.
680
1
  if (VP8GetCPUInfo != NULL) {
681
1
#if defined(WEBP_HAVE_SSE2)
682
1
    if (VP8GetCPUInfo(kSSE2)) {
683
1
      VP8LDspInitSSE2();
684
1
#if defined(WEBP_HAVE_SSE41)
685
1
      if (VP8GetCPUInfo(kSSE4_1)) {
686
1
        VP8LDspInitSSE41();
687
#if defined(WEBP_HAVE_AVX2)
688
        if (VP8GetCPUInfo(kAVX2)) {
689
          VP8LDspInitAVX2();
690
        }
691
#endif
692
1
      }
693
1
#endif
694
1
    }
695
1
#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
1
  }
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
1
  assert(VP8LAddGreenToBlueAndRed != NULL);
716
1
  assert(VP8LTransformColorInverse != NULL);
717
1
  assert(VP8LConvertBGRAToRGBA != NULL);
718
1
  assert(VP8LConvertBGRAToRGB != NULL);
719
1
  assert(VP8LConvertBGRAToBGR != NULL);
720
1
  assert(VP8LConvertBGRAToRGBA4444 != NULL);
721
1
  assert(VP8LConvertBGRAToRGB565 != NULL);
722
1
  assert(VP8LMapColor32b != NULL);
723
  assert(VP8LMapColor8b != NULL);
724
1
}
725
#undef COPY_PREDICTOR_ARRAY
726
727
//------------------------------------------------------------------------------