alpha_dec.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 (pascal.massimino@gmail.com)
  13. #include <stdlib.h>
  14. #include "./alphai_dec.h"
  15. #include "./vp8i_dec.h"
  16. #include "./vp8li_dec.h"
  17. #include "../dsp/dsp.h"
  18. #include "../utils/quant_levels_dec_utils.h"
  19. #include "../utils/utils.h"
  20. #include "../webp/format_constants.h"
  21. //------------------------------------------------------------------------------
  22. // ALPHDecoder object.
  23. // Allocates a new alpha decoder instance.
  24. static ALPHDecoder* ALPHNew(void) {
  25. ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
  26. return dec;
  27. }
  28. // Clears and deallocates an alpha decoder instance.
  29. static void ALPHDelete(ALPHDecoder* const dec) {
  30. if (dec != NULL) {
  31. VP8LDelete(dec->vp8l_dec_);
  32. dec->vp8l_dec_ = NULL;
  33. WebPSafeFree(dec);
  34. }
  35. }
  36. //------------------------------------------------------------------------------
  37. // Decoding.
  38. // Initialize alpha decoding by parsing the alpha header and decoding the image
  39. // header for alpha data stored using lossless compression.
  40. // Returns false in case of error in alpha header (data too short, invalid
  41. // compression method or filter, error in lossless header data etc).
  42. static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,
  43. size_t data_size, const VP8Io* const src_io,
  44. uint8_t* output) {
  45. int ok = 0;
  46. const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
  47. const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
  48. int rsrv;
  49. VP8Io* const io = &dec->io_;
  50. assert(data != NULL && output != NULL && src_io != NULL);
  51. VP8FiltersInit();
  52. dec->output_ = output;
  53. dec->width_ = src_io->width;
  54. dec->height_ = src_io->height;
  55. assert(dec->width_ > 0 && dec->height_ > 0);
  56. if (data_size <= ALPHA_HEADER_LEN) {
  57. return 0;
  58. }
  59. dec->method_ = (data[0] >> 0) & 0x03;
  60. dec->filter_ = (WEBP_FILTER_TYPE)((data[0] >> 2) & 0x03);
  61. dec->pre_processing_ = (data[0] >> 4) & 0x03;
  62. rsrv = (data[0] >> 6) & 0x03;
  63. if (dec->method_ < ALPHA_NO_COMPRESSION ||
  64. dec->method_ > ALPHA_LOSSLESS_COMPRESSION ||
  65. dec->filter_ >= WEBP_FILTER_LAST ||
  66. dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS ||
  67. rsrv != 0) {
  68. return 0;
  69. }
  70. // Copy the necessary parameters from src_io to io
  71. VP8InitIo(io);
  72. WebPInitCustomIo(NULL, io);
  73. io->opaque = dec;
  74. io->width = src_io->width;
  75. io->height = src_io->height;
  76. io->use_cropping = src_io->use_cropping;
  77. io->crop_left = src_io->crop_left;
  78. io->crop_right = src_io->crop_right;
  79. io->crop_top = src_io->crop_top;
  80. io->crop_bottom = src_io->crop_bottom;
  81. // No need to copy the scaling parameters.
  82. if (dec->method_ == ALPHA_NO_COMPRESSION) {
  83. const size_t alpha_decoded_size = dec->width_ * dec->height_;
  84. ok = (alpha_data_size >= alpha_decoded_size);
  85. } else {
  86. assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION);
  87. ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size);
  88. }
  89. return ok;
  90. }
  91. // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha
  92. // starting from row number 'row'. It assumes that rows up to (row - 1) have
  93. // already been decoded.
  94. // Returns false in case of bitstream error.
  95. static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) {
  96. ALPHDecoder* const alph_dec = dec->alph_dec_;
  97. const int width = alph_dec->width_;
  98. const int height = alph_dec->io_.crop_bottom;
  99. if (alph_dec->method_ == ALPHA_NO_COMPRESSION) {
  100. int y;
  101. const uint8_t* prev_line = dec->alpha_prev_line_;
  102. const uint8_t* deltas = dec->alpha_data_ + ALPHA_HEADER_LEN + row * width;
  103. uint8_t* dst = dec->alpha_plane_ + row * width;
  104. assert(deltas <= &dec->alpha_data_[dec->alpha_data_size_]);
  105. if (alph_dec->filter_ != WEBP_FILTER_NONE) {
  106. assert(WebPUnfilters[alph_dec->filter_] != NULL);
  107. for (y = 0; y < num_rows; ++y) {
  108. WebPUnfilters[alph_dec->filter_](prev_line, deltas, dst, width);
  109. prev_line = dst;
  110. dst += width;
  111. deltas += width;
  112. }
  113. } else {
  114. for (y = 0; y < num_rows; ++y) {
  115. memcpy(dst, deltas, width * sizeof(*dst));
  116. prev_line = dst;
  117. dst += width;
  118. deltas += width;
  119. }
  120. }
  121. dec->alpha_prev_line_ = prev_line;
  122. } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION
  123. assert(alph_dec->vp8l_dec_ != NULL);
  124. if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {
  125. return 0;
  126. }
  127. }
  128. if (row + num_rows >= height) {
  129. dec->is_alpha_decoded_ = 1;
  130. }
  131. return 1;
  132. }
  133. static int AllocateAlphaPlane(VP8Decoder* const dec, const VP8Io* const io) {
  134. const int stride = io->width;
  135. const int height = io->crop_bottom;
  136. const uint64_t alpha_size = (uint64_t)stride * height;
  137. assert(dec->alpha_plane_mem_ == NULL);
  138. dec->alpha_plane_mem_ =
  139. (uint8_t*)WebPSafeMalloc(alpha_size, sizeof(*dec->alpha_plane_));
  140. if (dec->alpha_plane_mem_ == NULL) {
  141. return 0;
  142. }
  143. dec->alpha_plane_ = dec->alpha_plane_mem_;
  144. dec->alpha_prev_line_ = NULL;
  145. return 1;
  146. }
  147. void WebPDeallocateAlphaMemory(VP8Decoder* const dec) {
  148. assert(dec != NULL);
  149. WebPSafeFree(dec->alpha_plane_mem_);
  150. dec->alpha_plane_mem_ = NULL;
  151. dec->alpha_plane_ = NULL;
  152. ALPHDelete(dec->alph_dec_);
  153. dec->alph_dec_ = NULL;
  154. }
  155. //------------------------------------------------------------------------------
  156. // Main entry point.
  157. const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
  158. const VP8Io* const io,
  159. int row, int num_rows) {
  160. const int width = io->width;
  161. const int height = io->crop_bottom;
  162. assert(dec != NULL && io != NULL);
  163. if (row < 0 || num_rows <= 0 || row + num_rows > height) {
  164. return NULL;
  165. }
  166. if (!dec->is_alpha_decoded_) {
  167. if (dec->alph_dec_ == NULL) { // Initialize decoder.
  168. dec->alph_dec_ = ALPHNew();
  169. if (dec->alph_dec_ == NULL) return NULL;
  170. if (!AllocateAlphaPlane(dec, io)) goto Error;
  171. if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_,
  172. io, dec->alpha_plane_)) {
  173. goto Error;
  174. }
  175. // if we allowed use of alpha dithering, check whether it's needed at all
  176. if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {
  177. dec->alpha_dithering_ = 0; // disable dithering
  178. } else {
  179. num_rows = height - row; // decode everything in one pass
  180. }
  181. }
  182. assert(dec->alph_dec_ != NULL);
  183. assert(row + num_rows <= height);
  184. if (!ALPHDecode(dec, row, num_rows)) goto Error;
  185. if (dec->is_alpha_decoded_) { // finished?
  186. ALPHDelete(dec->alph_dec_);
  187. dec->alph_dec_ = NULL;
  188. if (dec->alpha_dithering_ > 0) {
  189. uint8_t* const alpha = dec->alpha_plane_ + io->crop_top * width
  190. + io->crop_left;
  191. if (!WebPDequantizeLevels(alpha,
  192. io->crop_right - io->crop_left,
  193. io->crop_bottom - io->crop_top,
  194. width, dec->alpha_dithering_)) {
  195. goto Error;
  196. }
  197. }
  198. }
  199. }
  200. // Return a pointer to the current decoded row.
  201. return dec->alpha_plane_ + row * width;
  202. Error:
  203. WebPDeallocateAlphaMemory(dec);
  204. return NULL;
  205. }