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