Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dec/alpha_dec.c
Line
Count
Source
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 decompression.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stdlib.h>
16
17
#include "src/dec/alphai_dec.h"
18
#include "src/dec/vp8_dec.h"
19
#include "src/dec/vp8i_dec.h"
20
#include "src/dec/vp8li_dec.h"
21
#include "src/dec/webpi_dec.h"
22
#include "src/dsp/dsp.h"
23
#include "src/utils/quant_levels_dec_utils.h"
24
#include "src/utils/utils.h"
25
#include "src/webp/decode.h"
26
#include "src/webp/format_constants.h"
27
#include "src/webp/types.h"
28
29
WEBP_ASSUME_UNSAFE_INDEXABLE_ABI
30
31
//------------------------------------------------------------------------------
32
// ALPHDecoder object.
33
34
// Allocates a new alpha decoder instance.
35
557
WEBP_NODISCARD static ALPHDecoder* ALPHNew(void) {
36
557
  ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
37
557
  return dec;
38
557
}
39
40
// Clears and deallocates an alpha decoder instance.
41
192k
static void ALPHDelete(ALPHDecoder* const dec) {
42
192k
  if (dec != NULL) {
43
557
    VP8LDelete(dec->vp8l_dec);
44
557
    dec->vp8l_dec = NULL;
45
557
    WebPSafeFree(dec);
46
557
  }
47
192k
}
48
49
//------------------------------------------------------------------------------
50
// Decoding.
51
52
// Initialize alpha decoding by parsing the alpha header and decoding the image
53
// header for alpha data stored using lossless compression.
54
// Returns false in case of error in alpha header (data too short, invalid
55
// compression method or filter, error in lossless header data etc).
56
WEBP_NODISCARD static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,
57
                                   size_t data_size, const VP8Io* const src_io,
58
557
                                   uint8_t* output) {
59
557
  int ok = 0;
60
557
  const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
61
557
  int rsrv;
62
557
  VP8Io* const io = &dec->io;
63
64
557
  assert(data != NULL && output != NULL && src_io != NULL);
65
66
557
  VP8FiltersInit();
67
557
  dec->output = output;
68
557
  dec->width = src_io->width;
69
557
  dec->height = src_io->height;
70
557
  assert(dec->width > 0 && dec->height > 0);
71
72
557
  if (data_size <= ALPHA_HEADER_LEN) {
73
1
    return 0;
74
1
  }
75
76
556
  dec->method = (data[0] >> 0) & 0x03;
77
556
  dec->filter = (WEBP_FILTER_TYPE)((data[0] >> 2) & 0x03);
78
556
  dec->pre_processing = (data[0] >> 4) & 0x03;
79
556
  rsrv = (data[0] >> 6) & 0x03;
80
556
  if (dec->method < ALPHA_NO_COMPRESSION ||
81
556
      dec->method > ALPHA_LOSSLESS_COMPRESSION ||
82
554
      dec->filter >= WEBP_FILTER_LAST ||
83
554
      dec->pre_processing > ALPHA_PREPROCESSED_LEVELS || rsrv != 0) {
84
4
    return 0;
85
4
  }
86
87
  // Copy the necessary parameters from src_io to io
88
552
  if (!VP8InitIo(io)) {
89
0
    return 0;
90
0
  }
91
552
  WebPInitCustomIo(NULL, io);
92
552
  io->opaque = dec;
93
552
  io->width = src_io->width;
94
552
  io->height = src_io->height;
95
96
552
  io->use_cropping = src_io->use_cropping;
97
552
  io->crop_left = src_io->crop_left;
98
552
  io->crop_right = src_io->crop_right;
99
552
  io->crop_top = src_io->crop_top;
100
552
  io->crop_bottom = src_io->crop_bottom;
101
  // No need to copy the scaling parameters.
102
103
552
  {
104
552
    const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
105
552
    if (dec->method == ALPHA_NO_COMPRESSION) {
106
54
      const size_t alpha_decoded_size = dec->width * dec->height;
107
54
      ok = (alpha_data_size >= alpha_decoded_size);
108
498
    } else {
109
498
      assert(dec->method == ALPHA_LOSSLESS_COMPRESSION);
110
498
      {
111
498
        const uint8_t* WEBP_BIDI_INDEXABLE const bounded_alpha_data =
112
498
            WEBP_UNSAFE_FORGE_BIDI_INDEXABLE(const uint8_t*, alpha_data,
113
498
                                             alpha_data_size);
114
498
        ok = VP8LDecodeAlphaHeader(dec, bounded_alpha_data, alpha_data_size);
115
498
      }
116
498
    }
117
552
  }
118
119
552
  return ok;
120
552
}
121
122
// Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha
123
// starting from row number 'row'. It assumes that rows up to (row - 1) have
124
// already been decoded.
125
// Returns false in case of bitstream error.
126
WEBP_NODISCARD static int ALPHDecode(VP8Decoder* const dec, int row,
127
3.86k
                                     int num_rows) {
128
3.86k
  ALPHDecoder* const alph_dec = dec->alph_dec;
129
3.86k
  const int width = alph_dec->width;
130
3.86k
  const int height = alph_dec->io.crop_bottom;
131
3.86k
  if (alph_dec->method == ALPHA_NO_COMPRESSION) {
132
128
    int y;
133
128
    const uint8_t* prev_line = dec->alpha_prev_line;
134
128
    const uint8_t* deltas = dec->alpha_data + ALPHA_HEADER_LEN + row * width;
135
128
    uint8_t* dst = dec->alpha_plane + row * width;
136
128
    assert(deltas <= &dec->alpha_data[dec->alpha_data_size]);
137
128
    assert(WebPUnfilters[alph_dec->filter] != NULL);
138
1.54k
    for (y = 0; y < num_rows; ++y) {
139
1.42k
      WebPUnfilters[alph_dec->filter](prev_line, deltas, dst, width);
140
1.42k
      prev_line = dst;
141
1.42k
      dst += width;
142
1.42k
      deltas += width;
143
1.42k
    }
144
128
    dec->alpha_prev_line = prev_line;
145
3.73k
  } else {  // alph_dec->method == ALPHA_LOSSLESS_COMPRESSION
146
3.73k
    assert(alph_dec->vp8l_dec != NULL);
147
3.73k
    if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {
148
113
      return 0;
149
113
    }
150
3.73k
  }
151
152
3.74k
  if (row + num_rows >= height) {
153
212
    dec->is_alpha_decoded = 1;
154
212
  }
155
3.74k
  return 1;
156
3.86k
}
157
158
WEBP_NODISCARD static int AllocateAlphaPlane(VP8Decoder* const dec,
159
557
                                             const VP8Io* const io) {
160
557
  const int stride = io->width;
161
557
  const int height = io->crop_bottom;
162
557
  const uint64_t alpha_size = (uint64_t)stride * height;
163
557
  assert(dec->alpha_plane_mem == NULL);
164
557
  dec->alpha_plane_mem =
165
557
      (uint8_t*)WebPSafeMalloc(alpha_size, sizeof(*dec->alpha_plane));
166
557
  if (dec->alpha_plane_mem == NULL) {
167
0
    return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
168
0
                       "Alpha decoder initialization failed.");
169
0
  }
170
557
  dec->alpha_plane = dec->alpha_plane_mem;
171
557
  dec->alpha_prev_line = NULL;
172
557
  return 1;
173
557
}
174
175
192k
void WebPDeallocateAlphaMemory(VP8Decoder* const dec) {
176
192k
  assert(dec != NULL);
177
192k
  WebPSafeFree(dec->alpha_plane_mem);
178
192k
  dec->alpha_plane_mem = NULL;
179
192k
  dec->alpha_plane = NULL;
180
192k
  ALPHDelete(dec->alph_dec);
181
192k
  dec->alph_dec = NULL;
182
192k
}
183
184
//------------------------------------------------------------------------------
185
// Main entry point.
186
187
WEBP_NODISCARD const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
188
                                                     const VP8Io* const io,
189
4.86k
                                                     int row, int num_rows) {
190
4.86k
  const int width = io->width;
191
4.86k
  const int height = io->crop_bottom;
192
193
4.86k
  assert(dec != NULL && io != NULL);
194
195
4.86k
  if (row < 0 || num_rows <= 0 || row + num_rows > height) {
196
0
    return NULL;
197
0
  }
198
199
4.86k
  if (!dec->is_alpha_decoded) {
200
3.88k
    if (dec->alph_dec == NULL) {  // Initialize decoder.
201
557
      dec->alph_dec = ALPHNew();
202
557
      if (dec->alph_dec == NULL) {
203
0
        VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
204
0
                    "Alpha decoder initialization failed.");
205
0
        return NULL;
206
0
      }
207
557
      if (!AllocateAlphaPlane(dec, io)) goto Error;
208
557
      if (!ALPHInit(dec->alph_dec, dec->alpha_data, dec->alpha_data_size, io,
209
557
                    dec->alpha_plane)) {
210
21
        VP8LDecoder* const vp8l_dec = dec->alph_dec->vp8l_dec;
211
21
        VP8SetError(
212
21
            dec,
213
21
            (vp8l_dec == NULL) ? VP8_STATUS_OUT_OF_MEMORY : vp8l_dec->status,
214
21
            "Alpha decoder initialization failed.");
215
21
        goto Error;
216
21
      }
217
      // if we allowed use of alpha dithering, check whether it's needed at all
218
536
      if (dec->alph_dec->pre_processing != ALPHA_PREPROCESSED_LEVELS) {
219
383
        dec->alpha_dithering = 0;  // disable dithering
220
383
      } else {
221
153
        num_rows = height - row;  // decode everything in one pass
222
153
      }
223
536
    }
224
225
3.88k
    assert(dec->alph_dec != NULL);
226
3.86k
    assert(row + num_rows <= height);
227
3.86k
    if (!ALPHDecode(dec, row, num_rows)) goto Error;
228
229
3.74k
    if (dec->is_alpha_decoded) {  // finished?
230
212
      ALPHDelete(dec->alph_dec);
231
212
      dec->alph_dec = NULL;
232
212
      if (dec->alpha_dithering > 0) {
233
0
        uint8_t* const alpha =
234
0
            dec->alpha_plane + io->crop_top * width + io->crop_left;
235
0
        uint8_t* WEBP_BIDI_INDEXABLE const bounded_alpha =
236
0
            WEBP_UNSAFE_FORGE_BIDI_INDEXABLE(
237
0
                uint8_t*, alpha,
238
0
                (size_t)width*(io->crop_bottom - io->crop_top));
239
0
        if (!WebPDequantizeLevels(bounded_alpha, io->crop_right - io->crop_left,
240
0
                                  io->crop_bottom - io->crop_top, width,
241
0
                                  dec->alpha_dithering)) {
242
0
          goto Error;
243
0
        }
244
0
      }
245
212
    }
246
3.74k
  }
247
248
  // Return a pointer to the current decoded row.
249
4.72k
  return dec->alpha_plane + row * width;
250
251
134
Error:
252
134
  WebPDeallocateAlphaMemory(dec);
253
  return NULL;
254
4.86k
}