1// Copyright 2012 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// Lossless decoder: internal header.
11//
12// Author: Skal (pascal.massimino@gmail.com)
13//         Vikas Arora(vikaas.arora@gmail.com)
14
15#ifndef WEBP_DEC_VP8LI_H_
16#define WEBP_DEC_VP8LI_H_
17
18#include <string.h>     // for memcpy()
19#include "./webpi.h"
20#include "../utils/bit_reader.h"
21#include "../utils/color_cache.h"
22#include "../utils/huffman.h"
23#include "webp/format_constants.h"
24
25#if defined(__cplusplus) || defined(c_plusplus)
26extern "C" {
27#endif
28
29typedef enum {
30  READ_DATA = 0,
31  READ_HDR = 1,
32  READ_DIM = 2
33} VP8LDecodeState;
34
35typedef struct VP8LTransform VP8LTransform;
36struct VP8LTransform {
37  VP8LImageTransformType type_;   // transform type.
38  int                    bits_;   // subsampling bits defining transform window.
39  int                    xsize_;  // transform window X index.
40  int                    ysize_;  // transform window Y index.
41  uint32_t              *data_;   // transform data.
42};
43
44typedef struct {
45  HuffmanTree htrees_[HUFFMAN_CODES_PER_META_CODE];
46} HTreeGroup;
47
48typedef struct {
49  int             color_cache_size_;
50  VP8LColorCache  color_cache_;
51
52  int             huffman_mask_;
53  int             huffman_subsample_bits_;
54  int             huffman_xsize_;
55  uint32_t       *huffman_image_;
56  int             num_htree_groups_;
57  HTreeGroup     *htree_groups_;
58} VP8LMetadata;
59
60typedef struct {
61  VP8StatusCode    status_;
62  VP8LDecodeState  action_;
63  VP8LDecodeState  state_;
64  VP8Io           *io_;
65
66  const WebPDecBuffer *output_;    // shortcut to io->opaque->output
67
68  uint32_t        *pixels_;        // Internal data: either uint8_t* for alpha
69                                   // or uint32_t* for BGRA.
70  uint32_t        *argb_cache_;    // Scratch buffer for temporary BGRA storage.
71
72  VP8LBitReader    br_;
73
74  int              width_;
75  int              height_;
76  int              last_row_;      // last input row decoded so far.
77  int              last_out_row_;  // last row output so far.
78
79  VP8LMetadata     hdr_;
80
81  int              next_transform_;
82  VP8LTransform    transforms_[NUM_TRANSFORMS];
83  // or'd bitset storing the transforms types.
84  uint32_t         transforms_seen_;
85
86  uint8_t         *rescaler_memory;  // Working memory for rescaling work.
87  WebPRescaler    *rescaler;         // Common rescaler for all channels.
88} VP8LDecoder;
89
90//------------------------------------------------------------------------------
91// internal functions. Not public.
92
93// in vp8l.c
94
95// Decodes a raw image stream (without header) and store the alpha data
96// into *output, which must be of size width x height. Returns false in case
97// of error.
98int VP8LDecodeAlphaImageStream(int width, int height, const uint8_t* const data,
99                               size_t data_size, uint8_t* const output);
100
101// Allocates and initialize a new lossless decoder instance.
102VP8LDecoder* VP8LNew(void);
103
104// Decodes the image header. Returns false in case of error.
105int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io);
106
107// Decodes an image. It's required to decode the lossless header before calling
108// this function. Returns false in case of error, with updated dec->status_.
109int VP8LDecodeImage(VP8LDecoder* const dec);
110
111// Resets the decoder in its initial state, reclaiming memory.
112// Preserves the dec->status_ value.
113void VP8LClear(VP8LDecoder* const dec);
114
115// Clears and deallocate a lossless decoder instance.
116void VP8LDelete(VP8LDecoder* const dec);
117
118//------------------------------------------------------------------------------
119
120#if defined(__cplusplus) || defined(c_plusplus)
121}    // extern "C"
122#endif
123
124#endif  /* WEBP_DEC_VP8LI_H_ */
125