Merge pull request #1155 from bitwangyaoyao:master_fix
[profile/ivi/opencv.git] / 3rdparty / libwebp / dec / alpha.c
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 "./vp8i.h"
16 #include "./vp8li.h"
17 #include "../utils/filters.h"
18 #include "../utils/quant_levels_dec.h"
19 #include "../webp/format_constants.h"
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 //------------------------------------------------------------------------------
26 // Decodes the compressed data 'data' of size 'data_size' into the 'output'.
27 // The 'output' buffer should be pre-allocated and must be of the same
28 // dimension 'height'x'width', as that of the image.
29 //
30 // Returns 1 on successfully decoding the compressed alpha and
31 //         0 if either:
32 //           error in bit-stream header (invalid compression mode or filter), or
33 //           error returned by appropriate compression method.
34
35 static int DecodeAlpha(const uint8_t* data, size_t data_size,
36                        int width, int height, uint8_t* output) {
37   WEBP_FILTER_TYPE filter;
38   int pre_processing;
39   int rsrv;
40   int ok = 0;
41   int method;
42   const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN;
43   const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN;
44
45   assert(width > 0 && height > 0);
46   assert(data != NULL && output != NULL);
47
48   if (data_size <= ALPHA_HEADER_LEN) {
49     return 0;
50   }
51
52   method = (data[0] >> 0) & 0x03;
53   filter = (data[0] >> 2) & 0x03;
54   pre_processing = (data[0] >> 4) & 0x03;
55   rsrv = (data[0] >> 6) & 0x03;
56   if (method < ALPHA_NO_COMPRESSION ||
57       method > ALPHA_LOSSLESS_COMPRESSION ||
58       filter >= WEBP_FILTER_LAST ||
59       pre_processing > ALPHA_PREPROCESSED_LEVELS ||
60       rsrv != 0) {
61     return 0;
62   }
63
64   if (method == ALPHA_NO_COMPRESSION) {
65     const size_t alpha_decoded_size = height * width;
66     ok = (alpha_data_size >= alpha_decoded_size);
67     if (ok) memcpy(output, alpha_data, alpha_decoded_size);
68   } else {
69     ok = VP8LDecodeAlphaImageStream(width, height, alpha_data, alpha_data_size,
70                                     output);
71   }
72
73   if (ok) {
74     WebPUnfilterFunc unfilter_func = WebPUnfilters[filter];
75     if (unfilter_func != NULL) {
76       // TODO(vikas): Implement on-the-fly decoding & filter mechanism to decode
77       // and apply filter per image-row.
78       unfilter_func(width, height, width, output);
79     }
80     if (pre_processing == ALPHA_PREPROCESSED_LEVELS) {
81       ok = DequantizeLevels(output, width, height);
82     }
83   }
84
85   return ok;
86 }
87
88 //------------------------------------------------------------------------------
89
90 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
91                                       int row, int num_rows) {
92   const int width = dec->pic_hdr_.width_;
93   const int height = dec->pic_hdr_.height_;
94
95   if (row < 0 || num_rows < 0 || row + num_rows > height) {
96     return NULL;    // sanity check.
97   }
98
99   if (row == 0) {
100     // Decode everything during the first call.
101     assert(!dec->is_alpha_decoded_);
102     if (!DecodeAlpha(dec->alpha_data_, (size_t)dec->alpha_data_size_,
103                      width, height, dec->alpha_plane_)) {
104       return NULL;  // Error.
105     }
106     dec->is_alpha_decoded_ = 1;
107   }
108
109   // Return a pointer to the current decoded row.
110   return dec->alpha_plane_ + row * width;
111 }
112
113 #if defined(__cplusplus) || defined(c_plusplus)
114 }    // extern "C"
115 #endif