1// Copyright 2011 Google Inc.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// Alpha-plane decompression.
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#include <stdlib.h>
13#include "vp8i.h"
14
15#ifdef WEBP_EXPERIMENTAL_FEATURES
16
17#include "zlib.h"
18
19#if defined(__cplusplus) || defined(c_plusplus)
20extern "C" {
21#endif
22
23//-----------------------------------------------------------------------------
24
25const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
26                                      int row, int num_rows) {
27  uint8_t* output = dec->alpha_plane_;
28  const int stride = dec->pic_hdr_.width_;
29  if (row < 0 || row + num_rows > dec->pic_hdr_.height_) {
30    return NULL;    // sanity check
31  }
32  if (row == 0) {
33    // TODO(skal): for now, we just decompress everything during the first call.
34    // Later, we'll decode progressively, but we need to store the
35    // z_stream state.
36    const uint8_t* data = dec->alpha_data_;
37    size_t data_size = dec->alpha_data_size_;
38    const size_t output_size = stride * dec->pic_hdr_.height_;
39    int ret = Z_OK;
40    z_stream strm;
41
42    memset(&strm, 0, sizeof(strm));
43    if (inflateInit(&strm) != Z_OK) {
44      return 0;
45    }
46    strm.avail_in = data_size;
47    strm.next_in = (unsigned char*)data;
48    do {
49      strm.avail_out = output_size;
50      strm.next_out = output;
51      ret = inflate(&strm, Z_NO_FLUSH);
52      if (ret == Z_NEED_DICT || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) {
53        break;
54      }
55    } while (strm.avail_out == 0);
56
57    inflateEnd(&strm);
58    if (ret != Z_STREAM_END) {
59      return NULL;    // error
60    }
61  }
62  return output + row * stride;
63}
64
65#if defined(__cplusplus) || defined(c_plusplus)
66}    // extern "C"
67#endif
68
69#endif    // WEBP_EXPERIMENTAL_FEATURES
70