Path: blob/master/3rdparty/libwebp/src/dec/alpha_dec.c
16358 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Alpha-plane decompression.10//11// Author: Skal ([email protected])1213#include <stdlib.h>14#include "src/dec/alphai_dec.h"15#include "src/dec/vp8i_dec.h"16#include "src/dec/vp8li_dec.h"17#include "src/dsp/dsp.h"18#include "src/utils/quant_levels_dec_utils.h"19#include "src/utils/utils.h"20#include "src/webp/format_constants.h"2122//------------------------------------------------------------------------------23// ALPHDecoder object.2425// Allocates a new alpha decoder instance.26static ALPHDecoder* ALPHNew(void) {27ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec));28return dec;29}3031// Clears and deallocates an alpha decoder instance.32static void ALPHDelete(ALPHDecoder* const dec) {33if (dec != NULL) {34VP8LDelete(dec->vp8l_dec_);35dec->vp8l_dec_ = NULL;36WebPSafeFree(dec);37}38}3940//------------------------------------------------------------------------------41// Decoding.4243// Initialize alpha decoding by parsing the alpha header and decoding the image44// header for alpha data stored using lossless compression.45// Returns false in case of error in alpha header (data too short, invalid46// compression method or filter, error in lossless header data etc).47static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data,48size_t data_size, const VP8Io* const src_io,49uint8_t* output) {50int ok = 0;51const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;52const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;53int rsrv;54VP8Io* const io = &dec->io_;5556assert(data != NULL && output != NULL && src_io != NULL);5758VP8FiltersInit();59dec->output_ = output;60dec->width_ = src_io->width;61dec->height_ = src_io->height;62assert(dec->width_ > 0 && dec->height_ > 0);6364if (data_size <= ALPHA_HEADER_LEN) {65return 0;66}6768dec->method_ = (data[0] >> 0) & 0x03;69dec->filter_ = (WEBP_FILTER_TYPE)((data[0] >> 2) & 0x03);70dec->pre_processing_ = (data[0] >> 4) & 0x03;71rsrv = (data[0] >> 6) & 0x03;72if (dec->method_ < ALPHA_NO_COMPRESSION ||73dec->method_ > ALPHA_LOSSLESS_COMPRESSION ||74dec->filter_ >= WEBP_FILTER_LAST ||75dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS ||76rsrv != 0) {77return 0;78}7980// Copy the necessary parameters from src_io to io81VP8InitIo(io);82WebPInitCustomIo(NULL, io);83io->opaque = dec;84io->width = src_io->width;85io->height = src_io->height;8687io->use_cropping = src_io->use_cropping;88io->crop_left = src_io->crop_left;89io->crop_right = src_io->crop_right;90io->crop_top = src_io->crop_top;91io->crop_bottom = src_io->crop_bottom;92// No need to copy the scaling parameters.9394if (dec->method_ == ALPHA_NO_COMPRESSION) {95const size_t alpha_decoded_size = dec->width_ * dec->height_;96ok = (alpha_data_size >= alpha_decoded_size);97} else {98assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION);99ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size);100}101102return ok;103}104105// Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha106// starting from row number 'row'. It assumes that rows up to (row - 1) have107// already been decoded.108// Returns false in case of bitstream error.109static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) {110ALPHDecoder* const alph_dec = dec->alph_dec_;111const int width = alph_dec->width_;112const int height = alph_dec->io_.crop_bottom;113if (alph_dec->method_ == ALPHA_NO_COMPRESSION) {114int y;115const uint8_t* prev_line = dec->alpha_prev_line_;116const uint8_t* deltas = dec->alpha_data_ + ALPHA_HEADER_LEN + row * width;117uint8_t* dst = dec->alpha_plane_ + row * width;118assert(deltas <= &dec->alpha_data_[dec->alpha_data_size_]);119if (alph_dec->filter_ != WEBP_FILTER_NONE) {120assert(WebPUnfilters[alph_dec->filter_] != NULL);121for (y = 0; y < num_rows; ++y) {122WebPUnfilters[alph_dec->filter_](prev_line, deltas, dst, width);123prev_line = dst;124dst += width;125deltas += width;126}127} else {128for (y = 0; y < num_rows; ++y) {129memcpy(dst, deltas, width * sizeof(*dst));130prev_line = dst;131dst += width;132deltas += width;133}134}135dec->alpha_prev_line_ = prev_line;136} else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION137assert(alph_dec->vp8l_dec_ != NULL);138if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) {139return 0;140}141}142143if (row + num_rows >= height) {144dec->is_alpha_decoded_ = 1;145}146return 1;147}148149static int AllocateAlphaPlane(VP8Decoder* const dec, const VP8Io* const io) {150const int stride = io->width;151const int height = io->crop_bottom;152const uint64_t alpha_size = (uint64_t)stride * height;153assert(dec->alpha_plane_mem_ == NULL);154dec->alpha_plane_mem_ =155(uint8_t*)WebPSafeMalloc(alpha_size, sizeof(*dec->alpha_plane_));156if (dec->alpha_plane_mem_ == NULL) {157return 0;158}159dec->alpha_plane_ = dec->alpha_plane_mem_;160dec->alpha_prev_line_ = NULL;161return 1;162}163164void WebPDeallocateAlphaMemory(VP8Decoder* const dec) {165assert(dec != NULL);166WebPSafeFree(dec->alpha_plane_mem_);167dec->alpha_plane_mem_ = NULL;168dec->alpha_plane_ = NULL;169ALPHDelete(dec->alph_dec_);170dec->alph_dec_ = NULL;171}172173//------------------------------------------------------------------------------174// Main entry point.175176const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,177const VP8Io* const io,178int row, int num_rows) {179const int width = io->width;180const int height = io->crop_bottom;181182assert(dec != NULL && io != NULL);183184if (row < 0 || num_rows <= 0 || row + num_rows > height) {185return NULL; // sanity check.186}187188if (!dec->is_alpha_decoded_) {189if (dec->alph_dec_ == NULL) { // Initialize decoder.190dec->alph_dec_ = ALPHNew();191if (dec->alph_dec_ == NULL) return NULL;192if (!AllocateAlphaPlane(dec, io)) goto Error;193if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_,194io, dec->alpha_plane_)) {195goto Error;196}197// if we allowed use of alpha dithering, check whether it's needed at all198if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) {199dec->alpha_dithering_ = 0; // disable dithering200} else {201num_rows = height - row; // decode everything in one pass202}203}204205assert(dec->alph_dec_ != NULL);206assert(row + num_rows <= height);207if (!ALPHDecode(dec, row, num_rows)) goto Error;208209if (dec->is_alpha_decoded_) { // finished?210ALPHDelete(dec->alph_dec_);211dec->alph_dec_ = NULL;212if (dec->alpha_dithering_ > 0) {213uint8_t* const alpha = dec->alpha_plane_ + io->crop_top * width214+ io->crop_left;215if (!WebPDequantizeLevels(alpha,216io->crop_right - io->crop_left,217io->crop_bottom - io->crop_top,218width, dec->alpha_dithering_)) {219goto Error;220}221}222}223}224225// Return a pointer to the current decoded row.226return dec->alpha_plane_ + row * width;227228Error:229WebPDeallocateAlphaMemory(dec);230return NULL;231}232233234