1 // Copyright 2011 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Alpha-plane decompression.
12 // Author: Skal (pascal.massimino@gmail.com)
17 #include "../utils/filters.h"
18 #include "../utils/quant_levels_dec.h"
19 #include "../webp/format_constants.h"
21 #if defined(__cplusplus) || defined(c_plusplus)
25 //------------------------------------------------------------------------------
26 // Decodes the compressed data 'data' of size 'data_size' into the 'output'.
27 // The 'output' buffer should be pre-allocated and must be of the same
28 // dimension 'height'x'width', as that of the image.
30 // Returns 1 on successfully decoding the compressed alpha and
32 // error in bit-stream header (invalid compression mode or filter), or
33 // error returned by appropriate compression method.
35 static int DecodeAlpha(const uint8_t* data, size_t data_size,
36 int width, int height, uint8_t* output) {
37 WEBP_FILTER_TYPE filter;
42 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
43 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
45 assert(width > 0 && height > 0);
46 assert(data != NULL && output != NULL);
48 if (data_size <= ALPHA_HEADER_LEN) {
52 method = (data[0] >> 0) & 0x03;
53 filter = (data[0] >> 2) & 0x03;
54 pre_processing = (data[0] >> 4) & 0x03;
55 rsrv = (data[0] >> 6) & 0x03;
56 if (method < ALPHA_NO_COMPRESSION ||
57 method > ALPHA_LOSSLESS_COMPRESSION ||
58 filter >= WEBP_FILTER_LAST ||
59 pre_processing > ALPHA_PREPROCESSED_LEVELS ||
64 if (method == ALPHA_NO_COMPRESSION) {
65 const size_t alpha_decoded_size = height * width;
66 ok = (alpha_data_size >= alpha_decoded_size);
67 if (ok) memcpy(output, alpha_data, alpha_decoded_size);
69 ok = VP8LDecodeAlphaImageStream(width, height, alpha_data, alpha_data_size,
74 WebPUnfilterFunc unfilter_func = WebPUnfilters[filter];
75 if (unfilter_func != NULL) {
76 // TODO(vikas): Implement on-the-fly decoding & filter mechanism to decode
77 // and apply filter per image-row.
78 unfilter_func(width, height, width, output);
80 if (pre_processing == ALPHA_PREPROCESSED_LEVELS) {
81 ok = DequantizeLevels(output, width, height);
88 //------------------------------------------------------------------------------
90 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
91 int row, int num_rows) {
92 const int width = dec->pic_hdr_.width_;
93 const int height = dec->pic_hdr_.height_;
95 if (row < 0 || num_rows < 0 || row + num_rows > height) {
96 return NULL; // sanity check.
100 // Decode everything during the first call.
101 assert(!dec->is_alpha_decoded_);
102 if (!DecodeAlpha(dec->alpha_data_, (size_t)dec->alpha_data_size_,
103 width, height, dec->alpha_plane_)) {
104 return NULL; // Error.
106 dec->is_alpha_decoded_ = 1;
109 // Return a pointer to the current decoded row.
110 return dec->alpha_plane_ + row * width;
113 #if defined(__cplusplus) || defined(c_plusplus)