opencv/3rdparty/libwebp/dec/alpha.c

116 lines
3.7 KiB
C
Raw Normal View History

2013-03-05 00:57:25 +08:00
// Copyright 2011 Google Inc. All Rights Reserved.
//
2013-06-30 02:01:02 +08:00
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
2013-03-05 00:57:25 +08:00
// -----------------------------------------------------------------------------
//
// Alpha-plane decompression.
//
// Author: Skal (pascal.massimino@gmail.com)
#include <stdlib.h>
#include "./vp8i.h"
#include "./vp8li.h"
#include "../utils/filters.h"
2013-04-02 19:22:10 +08:00
#include "../utils/quant_levels_dec.h"
2013-03-05 00:57:25 +08:00
#include "../webp/format_constants.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
//------------------------------------------------------------------------------
// Decodes the compressed data 'data' of size 'data_size' into the 'output'.
// The 'output' buffer should be pre-allocated and must be of the same
2013-06-30 02:01:02 +08:00
// dimension 'height'x'width', as that of the image.
2013-03-05 00:57:25 +08:00
//
// Returns 1 on successfully decoding the compressed alpha and
// 0 if either:
// error in bit-stream header (invalid compression mode or filter), or
// error returned by appropriate compression method.
static int DecodeAlpha(const uint8_t* data, size_t data_size,
2013-06-30 02:01:02 +08:00
int width, int height, uint8_t* output) {
2013-03-05 00:57:25 +08:00
WEBP_FILTER_TYPE filter;
int pre_processing;
int rsrv;
int ok = 0;
int method;
2013-06-30 02:01:02 +08:00
const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
2013-03-05 00:57:25 +08:00
2013-06-30 02:01:02 +08:00
assert(width > 0 && height > 0);
2013-03-05 00:57:25 +08:00
assert(data != NULL && output != NULL);
if (data_size <= ALPHA_HEADER_LEN) {
return 0;
}
method = (data[0] >> 0) & 0x03;
filter = (data[0] >> 2) & 0x03;
pre_processing = (data[0] >> 4) & 0x03;
rsrv = (data[0] >> 6) & 0x03;
if (method < ALPHA_NO_COMPRESSION ||
method > ALPHA_LOSSLESS_COMPRESSION ||
filter >= WEBP_FILTER_LAST ||
pre_processing > ALPHA_PREPROCESSED_LEVELS ||
rsrv != 0) {
return 0;
}
if (method == ALPHA_NO_COMPRESSION) {
2013-06-30 02:01:02 +08:00
const size_t alpha_decoded_size = height * width;
ok = (alpha_data_size >= alpha_decoded_size);
if (ok) memcpy(output, alpha_data, alpha_decoded_size);
2013-03-05 00:57:25 +08:00
} else {
2013-06-30 02:01:02 +08:00
ok = VP8LDecodeAlphaImageStream(width, height, alpha_data, alpha_data_size,
output);
2013-03-05 00:57:25 +08:00
}
if (ok) {
2013-04-02 19:22:10 +08:00
WebPUnfilterFunc unfilter_func = WebPUnfilters[filter];
2013-03-05 00:57:25 +08:00
if (unfilter_func != NULL) {
// TODO(vikas): Implement on-the-fly decoding & filter mechanism to decode
// and apply filter per image-row.
2013-06-30 02:01:02 +08:00
unfilter_func(width, height, width, output);
2013-03-05 00:57:25 +08:00
}
if (pre_processing == ALPHA_PREPROCESSED_LEVELS) {
2013-06-30 02:01:02 +08:00
ok = DequantizeLevels(output, width, height);
2013-03-05 00:57:25 +08:00
}
}
return ok;
}
//------------------------------------------------------------------------------
const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
int row, int num_rows) {
2013-06-30 02:01:02 +08:00
const int width = dec->pic_hdr_.width_;
const int height = dec->pic_hdr_.height_;
2013-03-05 00:57:25 +08:00
2013-06-30 02:01:02 +08:00
if (row < 0 || num_rows < 0 || row + num_rows > height) {
2013-03-05 00:57:25 +08:00
return NULL; // sanity check.
}
if (row == 0) {
// Decode everything during the first call.
2013-06-30 02:01:02 +08:00
assert(!dec->is_alpha_decoded_);
2013-03-05 00:57:25 +08:00
if (!DecodeAlpha(dec->alpha_data_, (size_t)dec->alpha_data_size_,
2013-06-30 02:01:02 +08:00
width, height, dec->alpha_plane_)) {
2013-03-05 00:57:25 +08:00
return NULL; // Error.
}
2013-06-30 02:01:02 +08:00
dec->is_alpha_decoded_ = 1;
2013-03-05 00:57:25 +08:00
}
// Return a pointer to the current decoded row.
2013-06-30 02:01:02 +08:00
return dec->alpha_plane_ + row * width;
2013-03-05 00:57:25 +08:00
}
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif