alpha.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.h"
  15. #include "./vp8i.h"
  16. #include "./vp8li.h"
  17. #include "../utils/quant_levels_dec.h"
  18. #include "../utils/utils.h"
  19. #include "../webp/format_constants.h"
  20. //------------------------------------------------------------------------------
  21. // ALPHDecoder object.
  22. ALPHDecoder* ALPHNew(void) {
  23. ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
  24. return dec;
  25. }
  26. void ALPHDelete(ALPHDecoder* const dec) {
  27. if (dec != NULL) {
  28. VP8LDelete(dec->vp8l_dec_);
  29. dec->vp8l_dec_ = NULL;
  30. WebPSafeFree(dec);
  31. }
  32. }
  33. //------------------------------------------------------------------------------
  34. // Decoding.
  35. // Initialize alpha decoding by parsing the alpha header and decoding the image
  36. // header for alpha data stored using lossless compression.
  37. // Returns false in case of error in alpha header (data too short, invalid
  38. // compression method or filter, error in lossless header data etc).
  39. static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,
  40. size_t data_size, int width, int height, uint8_t* output) {
  41. int ok = 0;
  42. const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
  43. const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
  44. int rsrv;
  45. assert(width > 0 && height > 0);
  46. assert(data != NULL && output != NULL);
  47. dec->width_ = width;
  48. dec->height_ = height;
  49. if (data_size <= ALPHA_HEADER_LEN) {
  50. return 0;
  51. }
  52. dec->method_ = (data[0] >> 0) & 0x03;
  53. dec->filter_ = (data[0] >> 2) & 0x03;
  54. dec->pre_processing_ = (data[0] >> 4) & 0x03;
  55. rsrv = (data[0] >> 6) & 0x03;
  56. if (dec->method_ < ALPHA_NO_COMPRESSION ||
  57. dec->method_ > ALPHA_LOSSLESS_COMPRESSION ||
  58. dec->filter_ >= WEBP_FILTER_LAST ||
  59. dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS ||
  60. rsrv != 0) {
  61. return 0;
  62. }
  63. if (dec->method_ == ALPHA_NO_COMPRESSION) {
  64. const size_t alpha_decoded_size = dec->width_ * dec->height_;
  65. ok = (alpha_data_size >= alpha_decoded_size);
  66. } else {
  67. assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION);
  68. ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size, output);
  69. }
  70. return ok;
  71. }
  72. // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha
  73. // starting from row number 'row'. It assumes that rows up to (row - 1) have
  74. // already been decoded.
  75. // Returns false in case of bitstream error.
  76. static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) {
  77. ALPHDecoder* const alph_dec = dec->alph_dec_;
  78. const int width = alph_dec->width_;
  79. const int height = alph_dec->height_;
  80. WebPUnfilterFunc unfilter_func = WebPUnfilters[alph_dec->filter_];
  81. uint8_t* const output = dec->alpha_plane_;
  82. if (alph_dec->method_ == ALPHA_NO_COMPRESSION) {
  83. const size_t offset = row * width;
  84. const size_t num_pixels = num_rows * width;
  85. assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN + offset + num_pixels);
  86. memcpy(dec->alpha_plane_ + offset,
  87. dec->alpha_data_ + ALPHA_HEADER_LEN + offset, num_pixels);
  88. } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION
  89. assert(alph_dec->vp8l_dec_ != NULL);
  90. if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {
  91. return 0;
  92. }
  93. }
  94. if (unfilter_func != NULL) {
  95. unfilter_func(width, height, width, row, num_rows, output);
  96. }
  97. if (row + num_rows == dec->pic_hdr_.height_) {
  98. dec->is_alpha_decoded_ = 1;
  99. }
  100. return 1;
  101. }
  102. //------------------------------------------------------------------------------
  103. // Main entry point.
  104. const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
  105. int row, int num_rows) {
  106. const int width = dec->pic_hdr_.width_;
  107. const int height = dec->pic_hdr_.height_;
  108. if (row < 0 || num_rows <= 0 || row + num_rows > height) {
  109. return NULL; // sanity check.
  110. }
  111. if (row == 0) {
  112. // Initialize decoding.
  113. assert(dec->alpha_plane_ != NULL);
  114. dec->alph_dec_ = ALPHNew();
  115. if (dec->alph_dec_ == NULL) return NULL;
  116. if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_,
  117. width, height, dec->alpha_plane_)) {
  118. ALPHDelete(dec->alph_dec_);
  119. dec->alph_dec_ = NULL;
  120. return NULL;
  121. }
  122. // if we allowed use of alpha dithering, check whether it's needed at all
  123. if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {
  124. dec->alpha_dithering_ = 0; // disable dithering
  125. } else {
  126. num_rows = height; // decode everything in one pass
  127. }
  128. }
  129. if (!dec->is_alpha_decoded_) {
  130. int ok = 0;
  131. assert(dec->alph_dec_ != NULL);
  132. ok = ALPHDecode(dec, row, num_rows);
  133. if (ok && dec->alpha_dithering_ > 0) {
  134. ok = WebPDequantizeLevels(dec->alpha_plane_, width, height,
  135. dec->alpha_dithering_);
  136. }
  137. if (!ok || dec->is_alpha_decoded_) {
  138. ALPHDelete(dec->alph_dec_);
  139. dec->alph_dec_ = NULL;
  140. }
  141. if (!ok) return NULL; // Error.
  142. }
  143. // Return a pointer to the current decoded row.
  144. return dec->alpha_plane_ + row * width;
  145. }