Coverage Report

Created: 2024-07-27 06:27

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