OLD | NEW |
1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
9 // | 9 // |
10 // Alpha-plane decompression. | 10 // Alpha-plane decompression. |
11 // | 11 // |
12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
13 | 13 |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 #include "./alphai.h" |
15 #include "./vp8i.h" | 16 #include "./vp8i.h" |
16 #include "./vp8li.h" | 17 #include "./vp8li.h" |
17 #include "../utils/filters.h" | |
18 #include "../utils/quant_levels_dec.h" | 18 #include "../utils/quant_levels_dec.h" |
19 #include "../webp/format_constants.h" | 19 #include "../webp/format_constants.h" |
20 | 20 |
21 #if defined(__cplusplus) || defined(c_plusplus) | 21 //------------------------------------------------------------------------------ |
22 extern "C" { | 22 // ALPHDecoder object. |
23 #endif | 23 |
| 24 ALPHDecoder* ALPHNew(void) { |
| 25 ALPHDecoder* const dec = (ALPHDecoder*)calloc(1, sizeof(*dec)); |
| 26 return dec; |
| 27 } |
| 28 |
| 29 void ALPHDelete(ALPHDecoder* const dec) { |
| 30 if (dec != NULL) { |
| 31 VP8LDelete(dec->vp8l_dec_); |
| 32 dec->vp8l_dec_ = NULL; |
| 33 free(dec); |
| 34 } |
| 35 } |
24 | 36 |
25 //------------------------------------------------------------------------------ | 37 //------------------------------------------------------------------------------ |
26 // Decodes the compressed data 'data' of size 'data_size' into the 'output'. | 38 // Decoding. |
27 // The 'output' buffer should be pre-allocated and must be of the same | |
28 // dimension 'height'x'width', as that of the image. | |
29 // | |
30 // Returns 1 on successfully decoding the compressed alpha and | |
31 // 0 if either: | |
32 // error in bit-stream header (invalid compression mode or filter), or | |
33 // error returned by appropriate compression method. | |
34 | 39 |
35 static int DecodeAlpha(const uint8_t* data, size_t data_size, | 40 // Initialize alpha decoding by parsing the alpha header and decoding the image |
36 int width, int height, uint8_t* output) { | 41 // header for alpha data stored using lossless compression. |
37 WEBP_FILTER_TYPE filter; | 42 // Returns false in case of error in alpha header (data too short, invalid |
38 int pre_processing; | 43 // compression method or filter, error in lossless header data etc). |
39 int rsrv; | 44 static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data, |
| 45 size_t data_size, int width, int height, uint8_t* output) { |
40 int ok = 0; | 46 int ok = 0; |
41 int method; | |
42 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN; | 47 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN; |
43 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN; | 48 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN; |
| 49 int rsrv; |
44 | 50 |
45 assert(width > 0 && height > 0); | 51 assert(width > 0 && height > 0); |
46 assert(data != NULL && output != NULL); | 52 assert(data != NULL && output != NULL); |
47 | 53 |
| 54 dec->width_ = width; |
| 55 dec->height_ = height; |
| 56 |
48 if (data_size <= ALPHA_HEADER_LEN) { | 57 if (data_size <= ALPHA_HEADER_LEN) { |
49 return 0; | 58 return 0; |
50 } | 59 } |
51 | 60 |
52 method = (data[0] >> 0) & 0x03; | 61 dec->method_ = (data[0] >> 0) & 0x03; |
53 filter = (data[0] >> 2) & 0x03; | 62 dec->filter_ = (data[0] >> 2) & 0x03; |
54 pre_processing = (data[0] >> 4) & 0x03; | 63 dec->pre_processing_ = (data[0] >> 4) & 0x03; |
55 rsrv = (data[0] >> 6) & 0x03; | 64 rsrv = (data[0] >> 6) & 0x03; |
56 if (method < ALPHA_NO_COMPRESSION || | 65 if (dec->method_ < ALPHA_NO_COMPRESSION || |
57 method > ALPHA_LOSSLESS_COMPRESSION || | 66 dec->method_ > ALPHA_LOSSLESS_COMPRESSION || |
58 filter >= WEBP_FILTER_LAST || | 67 dec->filter_ >= WEBP_FILTER_LAST || |
59 pre_processing > ALPHA_PREPROCESSED_LEVELS || | 68 dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS || |
60 rsrv != 0) { | 69 rsrv != 0) { |
61 return 0; | 70 return 0; |
62 } | 71 } |
63 | 72 |
64 if (method == ALPHA_NO_COMPRESSION) { | 73 if (dec->method_ == ALPHA_NO_COMPRESSION) { |
65 const size_t alpha_decoded_size = height * width; | 74 const size_t alpha_decoded_size = dec->width_ * dec->height_; |
66 ok = (alpha_data_size >= alpha_decoded_size); | 75 ok = (alpha_data_size >= alpha_decoded_size); |
67 if (ok) memcpy(output, alpha_data, alpha_decoded_size); | |
68 } else { | 76 } else { |
69 ok = VP8LDecodeAlphaImageStream(width, height, alpha_data, alpha_data_size, | 77 assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION); |
70 output); | 78 ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size, output); |
71 } | 79 } |
| 80 return ok; |
| 81 } |
72 | 82 |
73 if (ok) { | 83 // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha |
74 WebPUnfilterFunc unfilter_func = WebPUnfilters[filter]; | 84 // starting from row number 'row'. It assumes that rows up to (row - 1) have |
75 if (unfilter_func != NULL) { | 85 // already been decoded. |
76 // TODO(vikas): Implement on-the-fly decoding & filter mechanism to decode | 86 // Returns false in case of bitstream error. |
77 // and apply filter per image-row. | 87 static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) { |
78 unfilter_func(width, height, width, output); | 88 ALPHDecoder* const alph_dec = dec->alph_dec_; |
79 } | 89 const int width = alph_dec->width_; |
80 if (pre_processing == ALPHA_PREPROCESSED_LEVELS) { | 90 const int height = alph_dec->height_; |
81 ok = DequantizeLevels(output, width, height); | 91 WebPUnfilterFunc unfilter_func = WebPUnfilters[alph_dec->filter_]; |
| 92 uint8_t* const output = dec->alpha_plane_; |
| 93 if (alph_dec->method_ == ALPHA_NO_COMPRESSION) { |
| 94 const size_t offset = row * width; |
| 95 const size_t num_pixels = num_rows * width; |
| 96 assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN + offset + num_pixels); |
| 97 memcpy(dec->alpha_plane_ + offset, |
| 98 dec->alpha_data_ + ALPHA_HEADER_LEN + offset, num_pixels); |
| 99 } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION |
| 100 assert(alph_dec->vp8l_dec_ != NULL); |
| 101 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) { |
| 102 return 0; |
82 } | 103 } |
83 } | 104 } |
84 | 105 |
85 return ok; | 106 if (unfilter_func != NULL) { |
| 107 unfilter_func(width, height, width, row, num_rows, output); |
| 108 } |
| 109 |
| 110 if (alph_dec->pre_processing_ == ALPHA_PREPROCESSED_LEVELS) { |
| 111 if (!DequantizeLevels(output, width, height, row, num_rows)) { |
| 112 return 0; |
| 113 } |
| 114 } |
| 115 |
| 116 if (row + num_rows == dec->pic_hdr_.height_) { |
| 117 dec->is_alpha_decoded_ = 1; |
| 118 } |
| 119 return 1; |
86 } | 120 } |
87 | 121 |
88 //------------------------------------------------------------------------------ | 122 //------------------------------------------------------------------------------ |
| 123 // Main entry point. |
89 | 124 |
90 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, | 125 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, |
91 int row, int num_rows) { | 126 int row, int num_rows) { |
92 const int width = dec->pic_hdr_.width_; | 127 const int width = dec->pic_hdr_.width_; |
93 const int height = dec->pic_hdr_.height_; | 128 const int height = dec->pic_hdr_.height_; |
94 | 129 |
95 if (row < 0 || num_rows < 0 || row + num_rows > height) { | 130 if (row < 0 || num_rows <= 0 || row + num_rows > height) { |
96 return NULL; // sanity check. | 131 return NULL; // sanity check. |
97 } | 132 } |
98 | 133 |
99 if (row == 0) { | 134 if (row == 0) { |
100 // Decode everything during the first call. | 135 // Initialize decoding. |
101 assert(!dec->is_alpha_decoded_); | 136 assert(dec->alpha_plane_ != NULL); |
102 if (!DecodeAlpha(dec->alpha_data_, (size_t)dec->alpha_data_size_, | 137 dec->alph_dec_ = ALPHNew(); |
103 width, height, dec->alpha_plane_)) { | 138 if (dec->alph_dec_ == NULL) return NULL; |
104 return NULL; // Error. | 139 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_, |
| 140 width, height, dec->alpha_plane_)) { |
| 141 ALPHDelete(dec->alph_dec_); |
| 142 dec->alph_dec_ = NULL; |
| 143 return NULL; |
105 } | 144 } |
106 dec->is_alpha_decoded_ = 1; | 145 } |
| 146 |
| 147 if (!dec->is_alpha_decoded_) { |
| 148 int ok = 0; |
| 149 assert(dec->alph_dec_ != NULL); |
| 150 ok = ALPHDecode(dec, row, num_rows); |
| 151 if (!ok || dec->is_alpha_decoded_) { |
| 152 ALPHDelete(dec->alph_dec_); |
| 153 dec->alph_dec_ = NULL; |
| 154 } |
| 155 if (!ok) return NULL; // Error. |
107 } | 156 } |
108 | 157 |
109 // Return a pointer to the current decoded row. | 158 // Return a pointer to the current decoded row. |
110 return dec->alpha_plane_ + row * width; | 159 return dec->alpha_plane_ + row * width; |
111 } | 160 } |
112 | 161 |
113 #if defined(__cplusplus) || defined(c_plusplus) | |
114 } // extern "C" | |
115 #endif | |
OLD | NEW |