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 ([email protected])
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.
ALPHNew(void)29 WEBP_NODISCARD static ALPHDecoder* ALPHNew(void) {
30 ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
31 return dec;
32 }
33
34 // Clears and deallocates an alpha decoder instance.
ALPHDelete(ALPHDecoder * const dec)35 static void ALPHDelete(ALPHDecoder* const dec) {
36 if (dec != NULL) {
37 VP8LDelete(dec->vp8l_dec_);
38 dec->vp8l_dec_ = NULL;
39 WebPSafeFree(dec);
40 }
41 }
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).
ALPHInit(ALPHDecoder * const dec,const uint8_t * data,size_t data_size,const VP8Io * const src_io,uint8_t * output)50 WEBP_NODISCARD static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,
51 size_t data_size, const VP8Io* const src_io,
52 uint8_t* output) {
53 int ok = 0;
54 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
55 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
56 int rsrv;
57 VP8Io* const io = &dec->io_;
58
59 assert(data != NULL && output != NULL && src_io != NULL);
60
61 VP8FiltersInit();
62 dec->output_ = output;
63 dec->width_ = src_io->width;
64 dec->height_ = src_io->height;
65 assert(dec->width_ > 0 && dec->height_ > 0);
66
67 if (data_size <= ALPHA_HEADER_LEN) {
68 return 0;
69 }
70
71 dec->method_ = (data[0] >> 0) & 0x03;
72 dec->filter_ = (WEBP_FILTER_TYPE)((data[0] >> 2) & 0x03);
73 dec->pre_processing_ = (data[0] >> 4) & 0x03;
74 rsrv = (data[0] >> 6) & 0x03;
75 if (dec->method_ < ALPHA_NO_COMPRESSION ||
76 dec->method_ > ALPHA_LOSSLESS_COMPRESSION ||
77 dec->filter_ >= WEBP_FILTER_LAST ||
78 dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS ||
79 rsrv != 0) {
80 return 0;
81 }
82
83 // Copy the necessary parameters from src_io to io
84 if (!VP8InitIo(io)) {
85 return 0;
86 }
87 WebPInitCustomIo(NULL, io);
88 io->opaque = dec;
89 io->width = src_io->width;
90 io->height = src_io->height;
91
92 io->use_cropping = src_io->use_cropping;
93 io->crop_left = src_io->crop_left;
94 io->crop_right = src_io->crop_right;
95 io->crop_top = src_io->crop_top;
96 io->crop_bottom = src_io->crop_bottom;
97 // No need to copy the scaling parameters.
98
99 if (dec->method_ == ALPHA_NO_COMPRESSION) {
100 const size_t alpha_decoded_size = dec->width_ * dec->height_;
101 ok = (alpha_data_size >= alpha_decoded_size);
102 } else {
103 assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION);
104 ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size);
105 }
106
107 return ok;
108 }
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.
ALPHDecode(VP8Decoder * const dec,int row,int num_rows)114 WEBP_NODISCARD static int ALPHDecode(VP8Decoder* const dec, int row,
115 int num_rows) {
116 ALPHDecoder* const alph_dec = dec->alph_dec_;
117 const int width = alph_dec->width_;
118 const int height = alph_dec->io_.crop_bottom;
119 if (alph_dec->method_ == ALPHA_NO_COMPRESSION) {
120 int y;
121 const uint8_t* prev_line = dec->alpha_prev_line_;
122 const uint8_t* deltas = dec->alpha_data_ + ALPHA_HEADER_LEN + row * width;
123 uint8_t* dst = dec->alpha_plane_ + row * width;
124 assert(deltas <= &dec->alpha_data_[dec->alpha_data_size_]);
125 assert(WebPUnfilters[alph_dec->filter_] != NULL);
126 for (y = 0; y < num_rows; ++y) {
127 WebPUnfilters[alph_dec->filter_](prev_line, deltas, dst, width);
128 prev_line = dst;
129 dst += width;
130 deltas += width;
131 }
132 dec->alpha_prev_line_ = prev_line;
133 } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION
134 assert(alph_dec->vp8l_dec_ != NULL);
135 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {
136 return 0;
137 }
138 }
139
140 if (row + num_rows >= height) {
141 dec->is_alpha_decoded_ = 1;
142 }
143 return 1;
144 }
145
AllocateAlphaPlane(VP8Decoder * const dec,const VP8Io * const io)146 WEBP_NODISCARD static int AllocateAlphaPlane(VP8Decoder* const dec,
147 const VP8Io* const io) {
148 const int stride = io->width;
149 const int height = io->crop_bottom;
150 const uint64_t alpha_size = (uint64_t)stride * height;
151 assert(dec->alpha_plane_mem_ == NULL);
152 dec->alpha_plane_mem_ =
153 (uint8_t*)WebPSafeMalloc(alpha_size, sizeof(*dec->alpha_plane_));
154 if (dec->alpha_plane_mem_ == NULL) {
155 return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
156 "Alpha decoder initialization failed.");
157 }
158 dec->alpha_plane_ = dec->alpha_plane_mem_;
159 dec->alpha_prev_line_ = NULL;
160 return 1;
161 }
162
WebPDeallocateAlphaMemory(VP8Decoder * const dec)163 void WebPDeallocateAlphaMemory(VP8Decoder* const dec) {
164 assert(dec != NULL);
165 WebPSafeFree(dec->alpha_plane_mem_);
166 dec->alpha_plane_mem_ = NULL;
167 dec->alpha_plane_ = NULL;
168 ALPHDelete(dec->alph_dec_);
169 dec->alph_dec_ = NULL;
170 }
171
172 //------------------------------------------------------------------------------
173 // Main entry point.
174
VP8DecompressAlphaRows(VP8Decoder * const dec,const VP8Io * const io,int row,int num_rows)175 WEBP_NODISCARD const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
176 const VP8Io* const io,
177 int row, int num_rows) {
178 const int width = io->width;
179 const int height = io->crop_bottom;
180
181 assert(dec != NULL && io != NULL);
182
183 if (row < 0 || num_rows <= 0 || row + num_rows > height) {
184 return NULL;
185 }
186
187 if (!dec->is_alpha_decoded_) {
188 if (dec->alph_dec_ == NULL) { // Initialize decoder.
189 dec->alph_dec_ = ALPHNew();
190 if (dec->alph_dec_ == NULL) {
191 VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
192 "Alpha decoder initialization failed.");
193 return NULL;
194 }
195 if (!AllocateAlphaPlane(dec, io)) goto Error;
196 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_,
197 io, dec->alpha_plane_)) {
198 VP8LDecoder* const vp8l_dec = dec->alph_dec_->vp8l_dec_;
199 VP8SetError(dec,
200 (vp8l_dec == NULL) ? VP8_STATUS_OUT_OF_MEMORY
201 : vp8l_dec->status_,
202 "Alpha decoder initialization failed.");
203 goto Error;
204 }
205 // if we allowed use of alpha dithering, check whether it's needed at all
206 if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {
207 dec->alpha_dithering_ = 0; // disable dithering
208 } else {
209 num_rows = height - row; // decode everything in one pass
210 }
211 }
212
213 assert(dec->alph_dec_ != NULL);
214 assert(row + num_rows <= height);
215 if (!ALPHDecode(dec, row, num_rows)) goto Error;
216
217 if (dec->is_alpha_decoded_) { // finished?
218 ALPHDelete(dec->alph_dec_);
219 dec->alph_dec_ = NULL;
220 if (dec->alpha_dithering_ > 0) {
221 uint8_t* const alpha = dec->alpha_plane_ + io->crop_top * width
222 + io->crop_left;
223 if (!WebPDequantizeLevels(alpha,
224 io->crop_right - io->crop_left,
225 io->crop_bottom - io->crop_top,
226 width, dec->alpha_dithering_)) {
227 goto Error;
228 }
229 }
230 }
231 }
232
233 // Return a pointer to the current decoded row.
234 return dec->alpha_plane_ + row * width;
235
236 Error:
237 WebPDeallocateAlphaMemory(dec);
238 return NULL;
239 }
240