Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: third_party/libwebp/dec/alpha.c

Issue 2149863002: libwebp: update to v0.5.1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libwebp/README.chromium ('k') | third_party/libwebp/dec/alphai.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "./alphai.h"
16 #include "./vp8i.h" 16 #include "./vp8i.h"
17 #include "./vp8li.h" 17 #include "./vp8li.h"
18 #include "../dsp/dsp.h" 18 #include "../dsp/dsp.h"
19 #include "../utils/quant_levels_dec.h" 19 #include "../utils/quant_levels_dec.h"
20 #include "../utils/utils.h" 20 #include "../utils/utils.h"
21 #include "../webp/format_constants.h" 21 #include "../webp/format_constants.h"
22 22
23 //------------------------------------------------------------------------------ 23 //------------------------------------------------------------------------------
24 // ALPHDecoder object. 24 // ALPHDecoder object.
25 25
26 ALPHDecoder* ALPHNew(void) { 26 // Allocates a new alpha decoder instance.
27 static ALPHDecoder* ALPHNew(void) {
27 ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); 28 ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
28 return dec; 29 return dec;
29 } 30 }
30 31
31 void ALPHDelete(ALPHDecoder* const dec) { 32 // Clears and deallocates an alpha decoder instance.
33 static void ALPHDelete(ALPHDecoder* const dec) {
32 if (dec != NULL) { 34 if (dec != NULL) {
33 VP8LDelete(dec->vp8l_dec_); 35 VP8LDelete(dec->vp8l_dec_);
34 dec->vp8l_dec_ = NULL; 36 dec->vp8l_dec_ = NULL;
35 WebPSafeFree(dec); 37 WebPSafeFree(dec);
36 } 38 }
37 } 39 }
38 40
39 //------------------------------------------------------------------------------ 41 //------------------------------------------------------------------------------
40 // Decoding. 42 // Decoding.
41 43
42 // Initialize alpha decoding by parsing the alpha header and decoding the image 44 // Initialize alpha decoding by parsing the alpha header and decoding the image
43 // header for alpha data stored using lossless compression. 45 // header for alpha data stored using lossless compression.
44 // Returns false in case of error in alpha header (data too short, invalid 46 // Returns false in case of error in alpha header (data too short, invalid
45 // compression method or filter, error in lossless header data etc). 47 // compression method or filter, error in lossless header data etc).
46 static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data, 48 static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,
47 size_t data_size, int width, int height, uint8_t* output) { 49 size_t data_size, const VP8Io* const src_io,
50 uint8_t* output) {
48 int ok = 0; 51 int ok = 0;
49 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN; 52 const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
50 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN; 53 const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
51 int rsrv; 54 int rsrv;
55 VP8Io* const io = &dec->io_;
52 56
53 assert(width > 0 && height > 0); 57 assert(data != NULL && output != NULL && src_io != NULL);
54 assert(data != NULL && output != NULL);
55 58
56 dec->width_ = width; 59 VP8FiltersInit();
57 dec->height_ = height; 60 dec->output_ = output;
61 dec->width_ = src_io->width;
62 dec->height_ = src_io->height;
63 assert(dec->width_ > 0 && dec->height_ > 0);
58 64
59 if (data_size <= ALPHA_HEADER_LEN) { 65 if (data_size <= ALPHA_HEADER_LEN) {
60 return 0; 66 return 0;
61 } 67 }
62 68
63 dec->method_ = (data[0] >> 0) & 0x03; 69 dec->method_ = (data[0] >> 0) & 0x03;
64 dec->filter_ = (data[0] >> 2) & 0x03; 70 dec->filter_ = (data[0] >> 2) & 0x03;
65 dec->pre_processing_ = (data[0] >> 4) & 0x03; 71 dec->pre_processing_ = (data[0] >> 4) & 0x03;
66 rsrv = (data[0] >> 6) & 0x03; 72 rsrv = (data[0] >> 6) & 0x03;
67 if (dec->method_ < ALPHA_NO_COMPRESSION || 73 if (dec->method_ < ALPHA_NO_COMPRESSION ||
68 dec->method_ > ALPHA_LOSSLESS_COMPRESSION || 74 dec->method_ > ALPHA_LOSSLESS_COMPRESSION ||
69 dec->filter_ >= WEBP_FILTER_LAST || 75 dec->filter_ >= WEBP_FILTER_LAST ||
70 dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS || 76 dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS ||
71 rsrv != 0) { 77 rsrv != 0) {
72 return 0; 78 return 0;
73 } 79 }
74 80
81 // Copy the necessary parameters from src_io to io
82 VP8InitIo(io);
83 WebPInitCustomIo(NULL, io);
84 io->opaque = dec;
85 io->width = src_io->width;
86 io->height = src_io->height;
87
88 io->use_cropping = src_io->use_cropping;
89 io->crop_left = src_io->crop_left;
90 io->crop_right = src_io->crop_right;
91 io->crop_top = src_io->crop_top;
92 io->crop_bottom = src_io->crop_bottom;
93 // No need to copy the scaling parameters.
94
75 if (dec->method_ == ALPHA_NO_COMPRESSION) { 95 if (dec->method_ == ALPHA_NO_COMPRESSION) {
76 const size_t alpha_decoded_size = dec->width_ * dec->height_; 96 const size_t alpha_decoded_size = dec->width_ * dec->height_;
77 ok = (alpha_data_size >= alpha_decoded_size); 97 ok = (alpha_data_size >= alpha_decoded_size);
78 } else { 98 } else {
79 assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION); 99 assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION);
80 ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size, output); 100 ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size);
81 } 101 }
82 VP8FiltersInit(); 102
83 return ok; 103 return ok;
84 } 104 }
85 105
86 // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha 106 // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha
87 // starting from row number 'row'. It assumes that rows up to (row - 1) have 107 // starting from row number 'row'. It assumes that rows up to (row - 1) have
88 // already been decoded. 108 // already been decoded.
89 // Returns false in case of bitstream error. 109 // Returns false in case of bitstream error.
90 static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) { 110 static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) {
91 ALPHDecoder* const alph_dec = dec->alph_dec_; 111 ALPHDecoder* const alph_dec = dec->alph_dec_;
92 const int width = alph_dec->width_; 112 const int width = alph_dec->width_;
93 const int height = alph_dec->height_; 113 const int height = alph_dec->io_.crop_bottom;
94 WebPUnfilterFunc unfilter_func = WebPUnfilters[alph_dec->filter_];
95 uint8_t* const output = dec->alpha_plane_;
96 if (alph_dec->method_ == ALPHA_NO_COMPRESSION) { 114 if (alph_dec->method_ == ALPHA_NO_COMPRESSION) {
97 const size_t offset = row * width; 115 int y;
98 const size_t num_pixels = num_rows * width; 116 const uint8_t* prev_line = dec->alpha_prev_line_;
99 assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN + offset + num_pixels); 117 const uint8_t* deltas = dec->alpha_data_ + ALPHA_HEADER_LEN + row * width;
100 memcpy(dec->alpha_plane_ + offset, 118 uint8_t* dst = dec->alpha_plane_ + row * width;
101 dec->alpha_data_ + ALPHA_HEADER_LEN + offset, num_pixels); 119 assert(deltas <= &dec->alpha_data_[dec->alpha_data_size_]);
120 if (alph_dec->filter_ != WEBP_FILTER_NONE) {
121 assert(WebPUnfilters[alph_dec->filter_] != NULL);
122 for (y = 0; y < num_rows; ++y) {
123 WebPUnfilters[alph_dec->filter_](prev_line, deltas, dst, width);
124 prev_line = dst;
125 dst += width;
126 deltas += width;
127 }
128 } else {
129 for (y = 0; y < num_rows; ++y) {
130 memcpy(dst, deltas, width * sizeof(*dst));
131 prev_line = dst;
132 dst += width;
133 deltas += width;
134 }
135 }
136 dec->alpha_prev_line_ = prev_line;
102 } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION 137 } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION
103 assert(alph_dec->vp8l_dec_ != NULL); 138 assert(alph_dec->vp8l_dec_ != NULL);
104 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) { 139 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {
105 return 0; 140 return 0;
106 } 141 }
107 } 142 }
108 143
109 if (unfilter_func != NULL) { 144 if (row + num_rows >= height) {
110 unfilter_func(width, height, width, row, num_rows, output);
111 }
112
113 if (row + num_rows == dec->pic_hdr_.height_) {
114 dec->is_alpha_decoded_ = 1; 145 dec->is_alpha_decoded_ = 1;
115 } 146 }
116 return 1; 147 return 1;
117 } 148 }
118 149
150 static int AllocateAlphaPlane(VP8Decoder* const dec, const VP8Io* const io) {
151 const int stride = io->width;
152 const int height = io->crop_bottom;
153 const uint64_t alpha_size = (uint64_t)stride * height;
154 assert(dec->alpha_plane_mem_ == NULL);
155 dec->alpha_plane_mem_ =
156 (uint8_t*)WebPSafeMalloc(alpha_size, sizeof(*dec->alpha_plane_));
157 if (dec->alpha_plane_mem_ == NULL) {
158 return 0;
159 }
160 dec->alpha_plane_ = dec->alpha_plane_mem_;
161 dec->alpha_prev_line_ = NULL;
162 return 1;
163 }
164
165 void WebPDeallocateAlphaMemory(VP8Decoder* const dec) {
166 assert(dec != NULL);
167 WebPSafeFree(dec->alpha_plane_mem_);
168 dec->alpha_plane_mem_ = NULL;
169 dec->alpha_plane_ = NULL;
170 ALPHDelete(dec->alph_dec_);
171 dec->alph_dec_ = NULL;
172 }
173
119 //------------------------------------------------------------------------------ 174 //------------------------------------------------------------------------------
120 // Main entry point. 175 // Main entry point.
121 176
122 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, 177 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
178 const VP8Io* const io,
123 int row, int num_rows) { 179 int row, int num_rows) {
124 const int width = dec->pic_hdr_.width_; 180 const int width = io->width;
125 const int height = dec->pic_hdr_.height_; 181 const int height = io->crop_bottom;
182
183 assert(dec != NULL && io != NULL);
126 184
127 if (row < 0 || num_rows <= 0 || row + num_rows > height) { 185 if (row < 0 || num_rows <= 0 || row + num_rows > height) {
128 return NULL; // sanity check. 186 return NULL; // sanity check.
129 } 187 }
130 188
131 if (row == 0) { 189 if (!dec->is_alpha_decoded_) {
132 // Initialize decoding. 190 if (dec->alph_dec_ == NULL) { // Initialize decoder.
133 assert(dec->alpha_plane_ != NULL); 191 dec->alph_dec_ = ALPHNew();
134 dec->alph_dec_ = ALPHNew(); 192 if (dec->alph_dec_ == NULL) return NULL;
135 if (dec->alph_dec_ == NULL) return NULL; 193 if (!AllocateAlphaPlane(dec, io)) goto Error;
136 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_, 194 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_,
137 width, height, dec->alpha_plane_)) { 195 io, dec->alpha_plane_)) {
196 goto Error;
197 }
198 // if we allowed use of alpha dithering, check whether it's needed at all
199 if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {
200 dec->alpha_dithering_ = 0; // disable dithering
201 } else {
202 num_rows = height - row; // decode everything in one pass
203 }
204 }
205
206 assert(dec->alph_dec_ != NULL);
207 assert(row + num_rows <= height);
208 if (!ALPHDecode(dec, row, num_rows)) goto Error;
209
210 if (dec->is_alpha_decoded_) { // finished?
138 ALPHDelete(dec->alph_dec_); 211 ALPHDelete(dec->alph_dec_);
139 dec->alph_dec_ = NULL; 212 dec->alph_dec_ = NULL;
140 return NULL; 213 if (dec->alpha_dithering_ > 0) {
214 uint8_t* const alpha = dec->alpha_plane_ + io->crop_top * width
215 + io->crop_left;
216 if (!WebPDequantizeLevels(alpha,
217 io->crop_right - io->crop_left,
218 io->crop_bottom - io->crop_top,
219 width, dec->alpha_dithering_)) {
220 goto Error;
221 }
222 }
141 } 223 }
142 // if we allowed use of alpha dithering, check whether it's needed at all
143 if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {
144 dec->alpha_dithering_ = 0; // disable dithering
145 } else {
146 num_rows = height; // decode everything in one pass
147 }
148 }
149
150 if (!dec->is_alpha_decoded_) {
151 int ok = 0;
152 assert(dec->alph_dec_ != NULL);
153 ok = ALPHDecode(dec, row, num_rows);
154 if (ok && dec->alpha_dithering_ > 0) {
155 ok = WebPDequantizeLevels(dec->alpha_plane_, width, height,
156 dec->alpha_dithering_);
157 }
158 if (!ok || dec->is_alpha_decoded_) {
159 ALPHDelete(dec->alph_dec_);
160 dec->alph_dec_ = NULL;
161 }
162 if (!ok) return NULL; // Error.
163 } 224 }
164 225
165 // Return a pointer to the current decoded row. 226 // Return a pointer to the current decoded row.
166 return dec->alpha_plane_ + row * width; 227 return dec->alpha_plane_ + row * width;
228
229 Error:
230 WebPDeallocateAlphaMemory(dec);
231 return NULL;
167 } 232 }
OLDNEW
« no previous file with comments | « third_party/libwebp/README.chromium ('k') | third_party/libwebp/dec/alphai.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698