| 1 |
/* |
|---|
| 2 |
* Small jpeg decoder library (Internal header) |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (c) 2006, Luc Saillard <luc@saillard.org> |
|---|
| 5 |
* All rights reserved. |
|---|
| 6 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 7 |
* modification, are permitted provided that the following conditions are met: |
|---|
| 8 |
* |
|---|
| 9 |
* - Redistributions of source code must retain the above copyright notice, |
|---|
| 10 |
* this list of conditions and the following disclaimer. |
|---|
| 11 |
* |
|---|
| 12 |
* - Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 13 |
* this list of conditions and the following disclaimer in the documentation |
|---|
| 14 |
* and/or other materials provided with the distribution. |
|---|
| 15 |
* |
|---|
| 16 |
* - Neither the name of the author nor the names of its contributors may be |
|---|
| 17 |
* used to endorse or promote products derived from this software without |
|---|
| 18 |
* specific prior written permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|---|
| 21 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|---|
| 24 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 25 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 26 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|---|
| 27 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|---|
| 28 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 29 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 30 |
* POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
* |
|---|
| 32 |
*/ |
|---|
| 33 |
|
|---|
| 34 |
/* |
|---|
| 35 |
* Ported to the D programming language by |
|---|
| 36 |
* Tomas Lindquist Olsen <tomas@famolsen.dk> |
|---|
| 37 |
*/ |
|---|
| 38 |
|
|---|
| 39 |
module tinyjpeg.internal; |
|---|
| 40 |
|
|---|
| 41 |
//#include <setjmp.h> |
|---|
| 42 |
|
|---|
| 43 |
version(Tango) |
|---|
| 44 |
import tango.stdc.stdint; |
|---|
| 45 |
else |
|---|
| 46 |
import std.stdint; |
|---|
| 47 |
|
|---|
| 48 |
// this enables a lot of error checking, set to 0 to disable |
|---|
| 49 |
enum { SANITY_CHECK = 1 } |
|---|
| 50 |
|
|---|
| 51 |
enum |
|---|
| 52 |
{ |
|---|
| 53 |
HUFFMAN_BITS_SIZE = 256, |
|---|
| 54 |
HUFFMAN_HASH_NBITS = 9, |
|---|
| 55 |
HUFFMAN_HASH_SIZE = (1U<<HUFFMAN_HASH_NBITS), |
|---|
| 56 |
HUFFMAN_HASH_MASK = (HUFFMAN_HASH_SIZE-1), |
|---|
| 57 |
|
|---|
| 58 |
HUFFMAN_TABLES = 4, |
|---|
| 59 |
COMPONENTS = 3, |
|---|
| 60 |
JPEG_MAX_WIDTH = 2048, |
|---|
| 61 |
JPEG_MAX_HEIGHT = 2048 |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
struct huffman_table |
|---|
| 65 |
{ |
|---|
| 66 |
/* Fast look up table, using HUFFMAN_HASH_NBITS bits we can have directly the symbol, |
|---|
| 67 |
* if the symbol is <0, then we need to look into the tree table */ |
|---|
| 68 |
short lookup[HUFFMAN_HASH_SIZE]; |
|---|
| 69 |
/* code size: give the number of bits of a symbol is encoded */ |
|---|
| 70 |
ubyte code_size[HUFFMAN_HASH_SIZE]; |
|---|
| 71 |
/* some place to store value that is not encoded in the lookup table |
|---|
| 72 |
* FIXME: Calculate if 256 value is enough to store all values |
|---|
| 73 |
*/ |
|---|
| 74 |
uint16_t slowtable[16-HUFFMAN_HASH_NBITS][256]; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
struct component |
|---|
| 78 |
{ |
|---|
| 79 |
uint Hfactor; |
|---|
| 80 |
uint Vfactor; |
|---|
| 81 |
float *Q_table; /* Pointer to the quantisation table to use */ |
|---|
| 82 |
huffman_table *AC_table; |
|---|
| 83 |
huffman_table *DC_table; |
|---|
| 84 |
short previous_DC; /* Previous DC coefficient */ |
|---|
| 85 |
short DCT[64]; /* DCT coef */ |
|---|
| 86 |
static if (SANITY_CHECK) |
|---|
| 87 |
{ |
|---|
| 88 |
uint cid; |
|---|
| 89 |
} |
|---|
| 90 |
}; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
typedef void (*decode_MCU_fct) (jdec_private *priv); |
|---|
| 94 |
typedef void (*convert_colorspace_fct) (jdec_private *priv); |
|---|
| 95 |
|
|---|
| 96 |
struct jdec_private |
|---|
| 97 |
{ |
|---|
| 98 |
/* Public variables */ |
|---|
| 99 |
uint8_t *components[COMPONENTS]; |
|---|
| 100 |
uint width, height; /* Size of the image */ |
|---|
| 101 |
uint flags; |
|---|
| 102 |
|
|---|
| 103 |
/* Private variables */ |
|---|
| 104 |
const ubyte* stream_begin, stream_end; |
|---|
| 105 |
uint stream_length; |
|---|
| 106 |
|
|---|
| 107 |
const ubyte *stream; /* Pointer to the current stream */ |
|---|
| 108 |
uint reservoir, nbits_in_reservoir; |
|---|
| 109 |
|
|---|
| 110 |
component component_infos[COMPONENTS]; |
|---|
| 111 |
float Q_tables[COMPONENTS][64]; /* quantization tables */ |
|---|
| 112 |
huffman_table HTDC[HUFFMAN_TABLES]; /* DC huffman tables */ |
|---|
| 113 |
huffman_table HTAC[HUFFMAN_TABLES]; /* AC huffman tables */ |
|---|
| 114 |
int default_huffman_table_initialized; |
|---|
| 115 |
int restart_interval; |
|---|
| 116 |
int restarts_to_go; /* MCUs left in this restart interval */ |
|---|
| 117 |
int last_rst_marker_seen; /* Rst marker is incremented each time */ |
|---|
| 118 |
|
|---|
| 119 |
/* Temp space used after the IDCT to store each components */ |
|---|
| 120 |
uint8_t Y[64*4]; |
|---|
| 121 |
uint8_t[64] Cr, Cb; |
|---|
| 122 |
|
|---|
| 123 |
//jmp_buf jump_state; |
|---|
| 124 |
|
|---|
| 125 |
/* Internal Pointer use for colorspace conversion, do not modify it !!! */ |
|---|
| 126 |
uint8_t *plane[COMPONENTS]; |
|---|
| 127 |
|
|---|
| 128 |
// error string |
|---|
| 129 |
char[] error_string; |
|---|
| 130 |
}; |
|---|