Coverage Report

Created: 2025-06-16 07:00

/src/libwebp/src/enc/alpha_enc.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2011 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
// Alpha-plane compression.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "src/dsp/dsp.h"
19
#include "src/webp/types.h"
20
#include "src/enc/vp8i_enc.h"
21
#include "src/utils/bit_writer_utils.h"
22
#include "src/utils/filters_utils.h"
23
#include "src/utils/quant_levels_utils.h"
24
#include "src/utils/thread_utils.h"
25
#include "src/utils/utils.h"
26
#include "src/webp/encode.h"
27
#include "src/webp/format_constants.h"
28
29
// -----------------------------------------------------------------------------
30
// Encodes the given alpha data via specified compression method 'method'.
31
// The pre-processing (quantization) is performed if 'quality' is less than 100.
32
// For such cases, the encoding is lossy. The valid range is [0, 100] for
33
// 'quality' and [0, 1] for 'method':
34
//   'method = 0' - No compression;
35
//   'method = 1' - Use lossless coder on the alpha plane only
36
// 'filter' values [0, 4] correspond to prediction modes none, horizontal,
37
// vertical & gradient filters. The prediction mode 4 will try all the
38
// prediction modes 0 to 3 and pick the best one.
39
// 'effort_level': specifies how much effort must be spent to try and reduce
40
//  the compressed output size. In range 0 (quick) to 6 (slow).
41
//
42
// 'output' corresponds to the buffer containing compressed alpha data.
43
//          This buffer is allocated by this method and caller should call
44
//          WebPSafeFree(*output) when done.
45
// 'output_size' corresponds to size of this compressed alpha buffer.
46
//
47
// Returns 1 on successfully encoding the alpha and
48
//         0 if either:
49
//           invalid quality or method, or
50
//           memory allocation for the compressed data fails.
51
52
#include "src/enc/vp8li_enc.h"
53
54
static int EncodeLossless(const uint8_t* const data, int width, int height,
55
                          int effort_level,  // in [0..6] range
56
                          int use_quality_100, VP8LBitWriter* const bw,
57
1.98k
                          WebPAuxStats* const stats) {
58
1.98k
  int ok = 0;
59
1.98k
  WebPConfig config;
60
1.98k
  WebPPicture picture;
61
62
1.98k
  if (!WebPPictureInit(&picture)) return 0;
63
1.98k
  picture.width = width;
64
1.98k
  picture.height = height;
65
1.98k
  picture.use_argb = 1;
66
1.98k
  picture.stats = stats;
67
1.98k
  if (!WebPPictureAlloc(&picture)) return 0;
68
69
  // Transfer the alpha values to the green channel.
70
1.98k
  WebPDispatchAlphaToGreen(data, width, picture.width, picture.height,
71
1.98k
                           picture.argb, picture.argb_stride);
72
73
1.98k
  if (!WebPConfigInit(&config)) return 0;
74
1.98k
  config.lossless = 1;
75
  // Enable exact, or it would alter RGB values of transparent alpha, which is
76
  // normally OK but not here since we are not encoding the input image but  an
77
  // internal encoding-related image containing necessary exact information in
78
  // RGB channels.
79
1.98k
  config.exact = 1;
80
1.98k
  config.method = effort_level;  // impact is very small
81
  // Set a low default quality for encoding alpha. Ensure that Alpha quality at
82
  // lower methods (3 and below) is less than the threshold for triggering
83
  // costly 'BackwardReferencesTraceBackwards'.
84
  // If the alpha quality is set to 100 and the method to 6, allow for a high
85
  // lossless quality to trigger the cruncher.
86
1.98k
  config.quality =
87
1.98k
      (use_quality_100 && effort_level == 6) ? 100 : 8.f * effort_level;
88
1.98k
  assert(config.quality >= 0 && config.quality <= 100.f);
89
90
1.98k
  ok = VP8LEncodeStream(&config, &picture, bw);
91
1.98k
  WebPPictureFree(&picture);
92
1.98k
  ok = ok && !bw->error;
93
1.98k
  if (!ok) {
94
0
    VP8LBitWriterWipeOut(bw);
95
0
    return 0;
96
0
  }
97
1.98k
  return 1;
98
1.98k
}
99
100
// -----------------------------------------------------------------------------
101
102
// Small struct to hold the result of a filter mode compression attempt.
103
typedef struct {
104
  size_t score;
105
  VP8BitWriter bw;
106
  WebPAuxStats stats;
107
} FilterTrial;
108
109
// This function always returns an initialized 'bw' object, even upon error.
110
static int EncodeAlphaInternal(const uint8_t* const data, int width, int height,
111
                               int method, int filter, int reduce_levels,
112
                               int effort_level,  // in [0..6] range
113
                               uint8_t* const tmp_alpha,
114
1.98k
                               FilterTrial* result) {
115
1.98k
  int ok = 0;
116
1.98k
  const uint8_t* alpha_src;
117
1.98k
  WebPFilterFunc filter_func;
118
1.98k
  uint8_t header;
119
1.98k
  const size_t data_size = width * height;
120
1.98k
  const uint8_t* output = NULL;
121
1.98k
  size_t output_size = 0;
122
1.98k
  VP8LBitWriter tmp_bw;
123
124
1.98k
  assert((uint64_t)data_size == (uint64_t)width * height);  // as per spec
125
1.98k
  assert(filter >= 0 && filter < WEBP_FILTER_LAST);
126
1.98k
  assert(method >= ALPHA_NO_COMPRESSION);
127
1.98k
  assert(method <= ALPHA_LOSSLESS_COMPRESSION);
128
1.98k
  assert(sizeof(header) == ALPHA_HEADER_LEN);
129
130
1.98k
  filter_func = WebPFilters[filter];
131
1.98k
  if (filter_func != NULL) {
132
650
    filter_func(data, width, height, width, tmp_alpha);
133
650
    alpha_src = tmp_alpha;
134
1.33k
  }  else {
135
1.33k
    alpha_src = data;
136
1.33k
  }
137
138
1.98k
  if (method != ALPHA_NO_COMPRESSION) {
139
1.98k
    ok = VP8LBitWriterInit(&tmp_bw, data_size >> 3);
140
1.98k
    ok = ok && EncodeLossless(alpha_src, width, height, effort_level,
141
1.98k
                              !reduce_levels, &tmp_bw, &result->stats);
142
1.98k
    if (ok) {
143
1.98k
      output = VP8LBitWriterFinish(&tmp_bw);
144
1.98k
      if (tmp_bw.error) {
145
0
        VP8LBitWriterWipeOut(&tmp_bw);
146
0
        memset(&result->bw, 0, sizeof(result->bw));
147
0
        return 0;
148
0
      }
149
1.98k
      output_size = VP8LBitWriterNumBytes(&tmp_bw);
150
1.98k
      if (output_size > data_size) {
151
        // compressed size is larger than source! Revert to uncompressed mode.
152
104
        method = ALPHA_NO_COMPRESSION;
153
104
        VP8LBitWriterWipeOut(&tmp_bw);
154
104
      }
155
1.98k
    } else {
156
0
      VP8LBitWriterWipeOut(&tmp_bw);
157
0
      memset(&result->bw, 0, sizeof(result->bw));
158
0
      return 0;
159
0
    }
160
1.98k
  }
161
162
1.98k
  if (method == ALPHA_NO_COMPRESSION) {
163
104
    output = alpha_src;
164
104
    output_size = data_size;
165
104
    ok = 1;
166
104
  }
167
168
  // Emit final result.
169
1.98k
  header = method | (filter << 2);
170
1.98k
  if (reduce_levels) header |= ALPHA_PREPROCESSED_LEVELS << 4;
171
172
1.98k
  if (!VP8BitWriterInit(&result->bw, ALPHA_HEADER_LEN + output_size)) ok = 0;
173
1.98k
  ok = ok && VP8BitWriterAppend(&result->bw, &header, ALPHA_HEADER_LEN);
174
1.98k
  ok = ok && VP8BitWriterAppend(&result->bw, output, output_size);
175
176
1.98k
  if (method != ALPHA_NO_COMPRESSION) {
177
1.88k
    VP8LBitWriterWipeOut(&tmp_bw);
178
1.88k
  }
179
1.98k
  ok = ok && !result->bw.error;
180
1.98k
  result->score = VP8BitWriterSize(&result->bw);
181
1.98k
  return ok;
182
1.98k
}
183
184
// -----------------------------------------------------------------------------
185
186
static int GetNumColors(const uint8_t* data, int width, int height,
187
1.33k
                        int stride) {
188
1.33k
  int j;
189
1.33k
  int colors = 0;
190
1.33k
  uint8_t color[256] = { 0 };
191
192
913k
  for (j = 0; j < height; ++j) {
193
912k
    int i;
194
912k
    const uint8_t* const p = data + j * stride;
195
790M
    for (i = 0; i < width; ++i) {
196
790M
      color[p[i]] = 1;
197
790M
    }
198
912k
  }
199
343k
  for (j = 0; j < 256; ++j) {
200
342k
    if (color[j] > 0) ++colors;
201
342k
  }
202
1.33k
  return colors;
203
1.33k
}
204
205
2.67k
#define FILTER_TRY_NONE (1 << WEBP_FILTER_NONE)
206
0
#define FILTER_TRY_ALL ((1 << WEBP_FILTER_LAST) - 1)
207
208
// Given the input 'filter' option, return an OR'd bit-set of filters to try.
209
static uint32_t GetFilterMap(const uint8_t* alpha, int width, int height,
210
1.33k
                             int filter, int effort_level) {
211
1.33k
  uint32_t bit_map = 0U;
212
1.33k
  if (filter == WEBP_FILTER_FAST) {
213
    // Quick estimate of the best candidate.
214
1.33k
    int try_filter_none = (effort_level > 3);
215
1.33k
    const int kMinColorsForFilterNone = 16;
216
1.33k
    const int kMaxColorsForFilterNone = 192;
217
1.33k
    const int num_colors = GetNumColors(alpha, width, height, width);
218
    // For low number of colors, NONE yields better compression.
219
1.33k
    filter = (num_colors <= kMinColorsForFilterNone)
220
1.33k
        ? WEBP_FILTER_NONE
221
1.33k
        : WebPEstimateBestFilter(alpha, width, height, width);
222
1.33k
    bit_map |= 1 << filter;
223
    // For large number of colors, try FILTER_NONE in addition to the best
224
    // filter as well.
225
1.33k
    if (try_filter_none || num_colors > kMaxColorsForFilterNone) {
226
1.33k
      bit_map |= FILTER_TRY_NONE;
227
1.33k
    }
228
1.33k
  } else if (filter == WEBP_FILTER_NONE) {
229
0
    bit_map = FILTER_TRY_NONE;
230
0
  } else {  // WEBP_FILTER_BEST -> try all
231
0
    bit_map = FILTER_TRY_ALL;
232
0
  }
233
1.33k
  return bit_map;
234
1.33k
}
235
236
1.33k
static void InitFilterTrial(FilterTrial* const score) {
237
1.33k
  score->score = (size_t)~0U;
238
1.33k
  VP8BitWriterInit(&score->bw, 0);
239
1.33k
}
240
241
static int ApplyFiltersAndEncode(const uint8_t* alpha, int width, int height,
242
                                 size_t data_size, int method, int filter,
243
                                 int reduce_levels, int effort_level,
244
                                 uint8_t** const output,
245
                                 size_t* const output_size,
246
1.33k
                                 WebPAuxStats* const stats) {
247
1.33k
  int ok = 1;
248
1.33k
  FilterTrial best;
249
1.33k
  uint32_t try_map =
250
1.33k
      GetFilterMap(alpha, width, height, filter, effort_level);
251
1.33k
  InitFilterTrial(&best);
252
253
1.33k
  if (try_map != FILTER_TRY_NONE) {
254
650
    uint8_t* filtered_alpha =  (uint8_t*)WebPSafeMalloc(1ULL, data_size);
255
650
    if (filtered_alpha == NULL) return 0;
256
257
2.43k
    for (filter = WEBP_FILTER_NONE; ok && try_map; ++filter, try_map >>= 1) {
258
1.78k
      if (try_map & 1) {
259
1.30k
        FilterTrial trial;
260
1.30k
        ok = EncodeAlphaInternal(alpha, width, height, method, filter,
261
1.30k
                                 reduce_levels, effort_level, filtered_alpha,
262
1.30k
                                 &trial);
263
1.30k
        if (ok && trial.score < best.score) {
264
937
          VP8BitWriterWipeOut(&best.bw);
265
937
          best = trial;
266
937
        } else {
267
363
          VP8BitWriterWipeOut(&trial.bw);
268
363
        }
269
1.30k
      }
270
1.78k
    }
271
650
    WebPSafeFree(filtered_alpha);
272
687
  } else {
273
687
    ok = EncodeAlphaInternal(alpha, width, height, method, WEBP_FILTER_NONE,
274
687
                             reduce_levels, effort_level, NULL, &best);
275
687
  }
276
1.33k
  if (ok) {
277
1.33k
#if !defined(WEBP_DISABLE_STATS)
278
1.33k
    if (stats != NULL) {
279
0
      stats->lossless_features = best.stats.lossless_features;
280
0
      stats->histogram_bits = best.stats.histogram_bits;
281
0
      stats->transform_bits = best.stats.transform_bits;
282
0
      stats->cross_color_transform_bits = best.stats.cross_color_transform_bits;
283
0
      stats->cache_bits = best.stats.cache_bits;
284
0
      stats->palette_size = best.stats.palette_size;
285
0
      stats->lossless_size = best.stats.lossless_size;
286
0
      stats->lossless_hdr_size = best.stats.lossless_hdr_size;
287
0
      stats->lossless_data_size = best.stats.lossless_data_size;
288
0
    }
289
#else
290
    (void)stats;
291
#endif
292
1.33k
    *output_size = VP8BitWriterSize(&best.bw);
293
1.33k
    *output = VP8BitWriterBuf(&best.bw);
294
1.33k
  } else {
295
0
    VP8BitWriterWipeOut(&best.bw);
296
0
  }
297
1.33k
  return ok;
298
1.33k
}
299
300
static int EncodeAlpha(VP8Encoder* const enc,
301
                       int quality, int method, int filter,
302
                       int effort_level,
303
1.33k
                       uint8_t** const output, size_t* const output_size) {
304
1.33k
  const WebPPicture* const pic = enc->pic;
305
1.33k
  const int width = pic->width;
306
1.33k
  const int height = pic->height;
307
308
1.33k
  uint8_t* quant_alpha = NULL;
309
1.33k
  const size_t data_size = width * height;
310
1.33k
  uint64_t sse = 0;
311
1.33k
  int ok = 1;
312
1.33k
  const int reduce_levels = (quality < 100);
313
314
  // quick correctness checks
315
1.33k
  assert((uint64_t)data_size == (uint64_t)width * height);  // as per spec
316
1.33k
  assert(enc != NULL && pic != NULL && pic->a != NULL);
317
1.33k
  assert(output != NULL && output_size != NULL);
318
1.33k
  assert(width > 0 && height > 0);
319
1.33k
  assert(pic->a_stride >= width);
320
1.33k
  assert(filter >= WEBP_FILTER_NONE && filter <= WEBP_FILTER_FAST);
321
322
1.33k
  if (quality < 0 || quality > 100) {
323
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
324
0
  }
325
326
1.33k
  if (method < ALPHA_NO_COMPRESSION || method > ALPHA_LOSSLESS_COMPRESSION) {
327
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
328
0
  }
329
330
1.33k
  if (method == ALPHA_NO_COMPRESSION) {
331
    // Don't filter, as filtering will make no impact on compressed size.
332
0
    filter = WEBP_FILTER_NONE;
333
0
  }
334
335
1.33k
  quant_alpha = (uint8_t*)WebPSafeMalloc(1ULL, data_size);
336
1.33k
  if (quant_alpha == NULL) {
337
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
338
0
  }
339
340
  // Extract alpha data (width x height) from raw_data (stride x height).
341
1.33k
  WebPCopyPlane(pic->a, pic->a_stride, quant_alpha, width, width, height);
342
343
1.33k
  if (reduce_levels) {  // No Quantization required for 'quality = 100'.
344
    // 16 alpha levels gives quite a low MSE w.r.t original alpha plane hence
345
    // mapped to moderate quality 70. Hence Quality:[0, 70] -> Levels:[2, 16]
346
    // and Quality:]70, 100] -> Levels:]16, 256].
347
0
    const int alpha_levels = (quality <= 70) ? (2 + quality / 5)
348
0
                                             : (16 + (quality - 70) * 8);
349
0
    ok = QuantizeLevels(quant_alpha, width, height, alpha_levels, &sse);
350
0
  }
351
352
1.33k
  if (ok) {
353
1.33k
    VP8FiltersInit();
354
1.33k
    ok = ApplyFiltersAndEncode(quant_alpha, width, height, data_size, method,
355
1.33k
                               filter, reduce_levels, effort_level, output,
356
1.33k
                               output_size, pic->stats);
357
1.33k
    if (!ok) {
358
0
      WebPEncodingSetError(pic, VP8_ENC_ERROR_OUT_OF_MEMORY);  // imprecise
359
0
    }
360
1.33k
#if !defined(WEBP_DISABLE_STATS)
361
1.33k
    if (pic->stats != NULL) {  // need stats?
362
0
      pic->stats->coded_size += (int)(*output_size);
363
0
      enc->sse[3] = sse;
364
0
    }
365
1.33k
#endif
366
1.33k
  }
367
368
1.33k
  WebPSafeFree(quant_alpha);
369
1.33k
  return ok;
370
1.33k
}
371
372
//------------------------------------------------------------------------------
373
// Main calls
374
375
1.33k
static int CompressAlphaJob(void* arg1, void* unused) {
376
1.33k
  VP8Encoder* const enc = (VP8Encoder*)arg1;
377
1.33k
  const WebPConfig* config = enc->config;
378
1.33k
  uint8_t* alpha_data = NULL;
379
1.33k
  size_t alpha_size = 0;
380
1.33k
  const int effort_level = config->method;  // maps to [0..6]
381
1.33k
  const WEBP_FILTER_TYPE filter =
382
1.33k
      (config->alpha_filtering == 0) ? WEBP_FILTER_NONE :
383
1.33k
      (config->alpha_filtering == 1) ? WEBP_FILTER_FAST :
384
1.33k
                                       WEBP_FILTER_BEST;
385
1.33k
  if (!EncodeAlpha(enc, config->alpha_quality, config->alpha_compression,
386
1.33k
                   filter, effort_level, &alpha_data, &alpha_size)) {
387
0
    return 0;
388
0
  }
389
1.33k
  if (alpha_size != (uint32_t)alpha_size) {  // Soundness check.
390
0
    WebPSafeFree(alpha_data);
391
0
    return 0;
392
0
  }
393
1.33k
  enc->alpha_data_size = (uint32_t)alpha_size;
394
1.33k
  enc->alpha_data = alpha_data;
395
1.33k
  (void)unused;
396
1.33k
  return 1;
397
1.33k
}
398
399
1.82k
void VP8EncInitAlpha(VP8Encoder* const enc) {
400
1.82k
  WebPInitAlphaProcessing();
401
1.82k
  enc->has_alpha = WebPPictureHasTransparency(enc->pic);
402
1.82k
  enc->alpha_data = NULL;
403
1.82k
  enc->alpha_data_size = 0;
404
1.82k
  if (enc->thread_level > 0) {
405
0
    WebPWorker* const worker = &enc->alpha_worker;
406
0
    WebPGetWorkerInterface()->Init(worker);
407
0
    worker->data1 = enc;
408
0
    worker->data2 = NULL;
409
0
    worker->hook = CompressAlphaJob;
410
0
  }
411
1.82k
}
412
413
1.82k
int VP8EncStartAlpha(VP8Encoder* const enc) {
414
1.82k
  if (enc->has_alpha) {
415
1.33k
    if (enc->thread_level > 0) {
416
0
      WebPWorker* const worker = &enc->alpha_worker;
417
      // Makes sure worker is good to go.
418
0
      if (!WebPGetWorkerInterface()->Reset(worker)) {
419
0
        return WebPEncodingSetError(enc->pic, VP8_ENC_ERROR_OUT_OF_MEMORY);
420
0
      }
421
0
      WebPGetWorkerInterface()->Launch(worker);
422
0
      return 1;
423
1.33k
    } else {
424
1.33k
      return CompressAlphaJob(enc, NULL);   // just do the job right away
425
1.33k
    }
426
1.33k
  }
427
486
  return 1;
428
1.82k
}
429
430
1.82k
int VP8EncFinishAlpha(VP8Encoder* const enc) {
431
1.82k
  if (enc->has_alpha) {
432
1.33k
    if (enc->thread_level > 0) {
433
0
      WebPWorker* const worker = &enc->alpha_worker;
434
0
      if (!WebPGetWorkerInterface()->Sync(worker)) return 0;  // error
435
0
    }
436
1.33k
  }
437
1.82k
  return WebPReportProgress(enc->pic, enc->percent + 20, &enc->percent);
438
1.82k
}
439
440
1.82k
int VP8EncDeleteAlpha(VP8Encoder* const enc) {
441
1.82k
  int ok = 1;
442
1.82k
  if (enc->thread_level > 0) {
443
0
    WebPWorker* const worker = &enc->alpha_worker;
444
    // finish anything left in flight
445
0
    ok = WebPGetWorkerInterface()->Sync(worker);
446
    // still need to end the worker, even if !ok
447
0
    WebPGetWorkerInterface()->End(worker);
448
0
  }
449
1.82k
  WebPSafeFree(enc->alpha_data);
450
1.82k
  enc->alpha_data = NULL;
451
1.82k
  enc->alpha_data_size = 0;
452
1.82k
  enc->has_alpha = 0;
453
1.82k
  return ok;
454
1.82k
}