| 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 "./alphai.h" |
| 16 #include "./vp8i.h" | 16 #include "./vp8i.h" |
| 17 #include "./vp8li.h" | 17 #include "./vp8li.h" |
| 18 #include "../utils/quant_levels_dec.h" | 18 #include "../utils/quant_levels_dec.h" |
| 19 #include "../utils/utils.h" |
| 19 #include "../webp/format_constants.h" | 20 #include "../webp/format_constants.h" |
| 20 | 21 |
| 21 //------------------------------------------------------------------------------ | 22 //------------------------------------------------------------------------------ |
| 22 // ALPHDecoder object. | 23 // ALPHDecoder object. |
| 23 | 24 |
| 24 ALPHDecoder* ALPHNew(void) { | 25 ALPHDecoder* ALPHNew(void) { |
| 25 ALPHDecoder* const dec = (ALPHDecoder*)calloc(1, sizeof(*dec)); | 26 ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); |
| 26 return dec; | 27 return dec; |
| 27 } | 28 } |
| 28 | 29 |
| 29 void ALPHDelete(ALPHDecoder* const dec) { | 30 void ALPHDelete(ALPHDecoder* const dec) { |
| 30 if (dec != NULL) { | 31 if (dec != NULL) { |
| 31 VP8LDelete(dec->vp8l_dec_); | 32 VP8LDelete(dec->vp8l_dec_); |
| 32 dec->vp8l_dec_ = NULL; | 33 dec->vp8l_dec_ = NULL; |
| 33 free(dec); | 34 WebPSafeFree(dec); |
| 34 } | 35 } |
| 35 } | 36 } |
| 36 | 37 |
| 37 //------------------------------------------------------------------------------ | 38 //------------------------------------------------------------------------------ |
| 38 // Decoding. | 39 // Decoding. |
| 39 | 40 |
| 40 // Initialize alpha decoding by parsing the alpha header and decoding the image | 41 // Initialize alpha decoding by parsing the alpha header and decoding the image |
| 41 // header for alpha data stored using lossless compression. | 42 // header for alpha data stored using lossless compression. |
| 42 // Returns false in case of error in alpha header (data too short, invalid | 43 // Returns false in case of error in alpha header (data too short, invalid |
| 43 // compression method or filter, error in lossless header data etc). | 44 // compression method or filter, error in lossless header data etc). |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 assert(alph_dec->vp8l_dec_ != NULL); | 101 assert(alph_dec->vp8l_dec_ != NULL); |
| 101 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) { | 102 if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) { |
| 102 return 0; | 103 return 0; |
| 103 } | 104 } |
| 104 } | 105 } |
| 105 | 106 |
| 106 if (unfilter_func != NULL) { | 107 if (unfilter_func != NULL) { |
| 107 unfilter_func(width, height, width, row, num_rows, output); | 108 unfilter_func(width, height, width, row, num_rows, output); |
| 108 } | 109 } |
| 109 | 110 |
| 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_) { | 111 if (row + num_rows == dec->pic_hdr_.height_) { |
| 117 dec->is_alpha_decoded_ = 1; | 112 dec->is_alpha_decoded_ = 1; |
| 118 } | 113 } |
| 119 return 1; | 114 return 1; |
| 120 } | 115 } |
| 121 | 116 |
| 122 //------------------------------------------------------------------------------ | 117 //------------------------------------------------------------------------------ |
| 123 // Main entry point. | 118 // Main entry point. |
| 124 | 119 |
| 125 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, | 120 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, |
| 126 int row, int num_rows) { | 121 int row, int num_rows) { |
| 127 const int width = dec->pic_hdr_.width_; | 122 const int width = dec->pic_hdr_.width_; |
| 128 const int height = dec->pic_hdr_.height_; | 123 const int height = dec->pic_hdr_.height_; |
| 129 | 124 |
| 130 if (row < 0 || num_rows <= 0 || row + num_rows > height) { | 125 if (row < 0 || num_rows <= 0 || row + num_rows > height) { |
| 131 return NULL; // sanity check. | 126 return NULL; // sanity check. |
| 132 } | 127 } |
| 133 | 128 |
| 134 if (row == 0) { | 129 if (row == 0) { |
| 135 // Initialize decoding. | 130 // Initialize decoding. |
| 136 assert(dec->alpha_plane_ != NULL); | 131 assert(dec->alpha_plane_ != NULL); |
| 137 dec->alph_dec_ = ALPHNew(); | 132 dec->alph_dec_ = ALPHNew(); |
| 138 if (dec->alph_dec_ == NULL) return NULL; | 133 if (dec->alph_dec_ == NULL) return NULL; |
| 139 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_, | 134 if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_, |
| 140 width, height, dec->alpha_plane_)) { | 135 width, height, dec->alpha_plane_)) { |
| 141 ALPHDelete(dec->alph_dec_); | 136 ALPHDelete(dec->alph_dec_); |
| 142 dec->alph_dec_ = NULL; | 137 dec->alph_dec_ = NULL; |
| 143 return NULL; | 138 return NULL; |
| 144 } | 139 } |
| 140 // if we allowed use of alpha dithering, check whether it's needed at all |
| 141 if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) { |
| 142 dec->alpha_dithering_ = 0; // disable dithering |
| 143 } else { |
| 144 num_rows = height; // decode everything in one pass |
| 145 } |
| 145 } | 146 } |
| 146 | 147 |
| 147 if (!dec->is_alpha_decoded_) { | 148 if (!dec->is_alpha_decoded_) { |
| 148 int ok = 0; | 149 int ok = 0; |
| 149 assert(dec->alph_dec_ != NULL); | 150 assert(dec->alph_dec_ != NULL); |
| 150 ok = ALPHDecode(dec, row, num_rows); | 151 ok = ALPHDecode(dec, row, num_rows); |
| 152 if (ok && dec->alpha_dithering_ > 0) { |
| 153 ok = WebPDequantizeLevels(dec->alpha_plane_, width, height, |
| 154 dec->alpha_dithering_); |
| 155 } |
| 151 if (!ok || dec->is_alpha_decoded_) { | 156 if (!ok || dec->is_alpha_decoded_) { |
| 152 ALPHDelete(dec->alph_dec_); | 157 ALPHDelete(dec->alph_dec_); |
| 153 dec->alph_dec_ = NULL; | 158 dec->alph_dec_ = NULL; |
| 154 } | 159 } |
| 155 if (!ok) return NULL; // Error. | 160 if (!ok) return NULL; // Error. |
| 156 } | 161 } |
| 157 | 162 |
| 158 // Return a pointer to the current decoded row. | 163 // Return a pointer to the current decoded row. |
| 159 return dec->alpha_plane_ + row * width; | 164 return dec->alpha_plane_ + row * width; |
| 160 } | 165 } |
| 161 | |
| OLD | NEW |