Mercurial > hg > dmlib
annotate src/dmzlib.c @ 964:76ac0d5c89b3
Oops, bugfix.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 27 Feb 2015 05:21:29 +0200 |
parents | 1832ac20edb2 |
children | df8d2ad98f7d |
rev | line source |
---|---|
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * DMLib |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * -- ZLib implementation |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 * Public domain zlib decode v0.2 by Sean Barrett 2006-11-18 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * Modified and reformatted for DMLib by Matti 'ccr' Hamalainen |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 */ |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "dmzlib.h" |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #define DM_ZLIB_TMPBUF_SIZE (16 * 1024) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
11 #define DMSTBI_ASSERT(x) // dummy |
951
1723ebe6771c
Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents:
949
diff
changeset
|
12 |
1723ebe6771c
Add kludge stbi__err() implementation temporarily until the error handling
Matti Hamalainen <ccr@tnsp.org>
parents:
949
diff
changeset
|
13 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 // @TODO: should statically initialize these for optimal thread safety |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
15 static Uint8 dm_zdefault_length[288], dm_zdefault_distance[32]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 void dmZLibInit() |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 int i; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 // use <= to match clearly with spec |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 for (i = 0; i <= 143; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
24 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 for (; i <= 255; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
27 dm_zdefault_length[i] = 9; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 for (; i <= 279; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
30 dm_zdefault_length[i] = 7; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 for (; i <= 287; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
33 dm_zdefault_length[i] = 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 for (i = 0; i <= 31; i++) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
36 dm_zdefault_distance[i] = 5; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 static inline int stbi__bit_reverse_16(int n) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 return n; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 static inline int stbi__bit_reverse_n(int v, int bits) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
52 DMSTBI_ASSERT(bits <= 16); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 // to bit reverse n bits, reverse 16 and shift |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 // e.g. 11 bits, bit reverse and shift away 5 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 return stbi__bit_reverse_16(v) >> (16 - bits); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
59 static int dmZLibBuildHuffmanTables(DMZHuffmanContext * ctx, const Uint8 * sizelist, const int num) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 int i, k = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 int code, next_code[16], sizes[17]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 // DEFLATE spec for generating codes |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 memset(sizes, 0, sizeof(sizes)); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
66 memset(ctx->fast, 0, sizeof(ctx->fast)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 for (i = 0; i < num; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 sizes[sizelist[i]]++; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 sizes[0] = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 for (i = 1; i < 16; i++) |
954 | 73 { |
74 if (sizes[i] > (1 << i)) | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
75 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
76 return dmError(DMERR_INTERNAL, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
77 "Sizes assert failed while building Huffman codes.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
78 } |
954 | 79 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 code = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 for (i = 1; i < 16; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 next_code[i] = code; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
85 ctx->firstCode[i] = (Uint16) code; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
86 ctx->firstSymbol[i] = (Uint16) k; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 code = (code + sizes[i]); |
954 | 88 |
89 if (sizes[i] && code - 1 >= (1 << i)) | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
90 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
91 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
92 "Bad Huffman code lengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
93 } |
954 | 94 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
95 ctx->maxCode[i] = code << (16 - i); // preshift for inner loop |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 code <<= 1; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 k += sizes[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
100 ctx->maxCode[16] = 0x10000; // sentinel |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 for (i = 0; i < num; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 int s = sizelist[i]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 if (s) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
106 int c = next_code[s] - ctx->firstCode[s] + ctx->firstSymbol[s]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 Uint16 fastv = (Uint16) ((s << 9) | i); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
108 ctx->size[c] = (Uint8) s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
109 ctx->value[c] = (Uint16) i; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
110 if (s <= STBI__ZFAST_BITS) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 int k = stbi__bit_reverse_n(next_code[s], s); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 while (k < STBI__ZFAST_SIZE) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
114 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
115 ctx->fast[k] = fastv; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 k += (1 << s); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 ++next_code[s]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 } |
964 | 122 |
123 return DMERR_OK; | |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
126 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
127 static inline Uint8 dmZGet8(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
129 if (ctx->zbuffer >= ctx->zbufferEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
131 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
132 return *ctx->zbuffer++; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
136 static void stbi__fill_bits(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
140 DMSTBI_ASSERT(ctx->codeBuffer < (1U << ctx->numBits)); |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
141 ctx->codeBuffer |= dmZGet8(ctx) << ctx->numBits; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
142 ctx->numBits += 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
143 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
144 while (ctx->numBits <= 24); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
148 static inline unsigned int dmZReceive(DMZLibContext * ctx, int n) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
150 unsigned int val; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
151 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
152 if (ctx->numBits < n) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
153 stbi__fill_bits(ctx); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
154 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
155 val = ctx->codeBuffer & ((1 << n) - 1); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
156 ctx->codeBuffer >>= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
157 ctx->numBits -= n; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
158 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
159 return val; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
163 static int stbi__zhuffman_decode_slowpath(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 int b, s, k; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
166 *val = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 // not resolved by fast table, so compute it the slow way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 // use jpeg approach, which requires MSbits at top |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
170 k = stbi__bit_reverse_16(ctx->codeBuffer); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 for (s = STBI__ZFAST_BITS + 1; ; s++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
173 if (k < huff->maxCode[s]) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
174 break; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 if (s == 16) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
178 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
179 return dmError(DMERR_DATA_ERROR, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
180 "Bad Huffman code.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
181 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 // code size is s, so: |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
184 b = (k >> (16 - s)) - huff->firstCode[s] + huff->firstSymbol[s]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
185 DMSTBI_ASSERT(huff->size[b] == s); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
186 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
187 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
188 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
189 *val = huff->value[b]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
190 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
191 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
193 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
195 static inline int stbi__zhuffman_decode(DMZLibContext * ctx, DMZHuffmanContext * huff, int *val) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
196 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 int b; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
198 if (ctx->numBits < 16) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
199 stbi__fill_bits(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
201 b = huff->fast[ctx->codeBuffer & STBI__ZFAST_MASK]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 if (b) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 int s = b >> 9; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
205 ctx->codeBuffer >>= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
206 ctx->numBits -= s; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
207 *val = b & 511; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
208 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
210 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
211 return stbi__zhuffman_decode_slowpath(ctx, huff, val); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
214 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
215 static int stbi__zexpand(DMZLibContext * ctx, Uint8 *zout, size_t n) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
217 Uint8 *newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
218 size_t cur, limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
219 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
220 ctx->zout = zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
221 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
222 if (!ctx->expandable) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
223 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
224 return dmError(DMERR_BOUNDS, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
225 "Output buffer limit hit, and is not expandable.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
226 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
228 cur = ctx->zout - ctx->zoutStart; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
229 limit = ctx->zoutEnd - ctx->zoutStart; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 while (cur + n > limit) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 limit *= 2; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
233 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
234 if ((newBuf = (Uint8 *) dmRealloc(ctx->zoutStart, limit)) == NULL) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
235 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
236 return dmError(DMERR_MALLOC, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
237 "Could not reallocate buffer.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
238 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
240 ctx->zoutStart = newBuf; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
241 ctx->zout = newBuf + cur; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
242 ctx->zoutEnd = newBuf + limit; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
243 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
244 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
245 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
246 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
247 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
248 static const int stbi__zlength_base[31] = |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
249 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
250 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
251 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
252 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 static const int stbi__zlength_extra[31] = |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 5, 5, 5, 5, 0, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 static const int stbi__zdist_base[32] = |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
265 16385, 24577, 0, 0 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
267 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 static const int stbi__zdist_extra[32] = |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 10, 11, 11, 12, 12, 13, 13 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
275 static int dmZLibParseHuffmanBlock(DMZLibContext * a) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
276 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
277 Uint8 *zout = a->zout; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
278 for (;;) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
279 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
280 int z, ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
281 if ((ret = stbi__zhuffman_decode(a, &a->zlength, &z)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
282 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
283 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
284 if (z < 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
285 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
286 if (zout >= a->zoutEnd) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
287 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
288 if ((ret = stbi__zexpand(a, zout, 1)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
289 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
291 zout = a->zout; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
293 *zout++ = (Uint8) z; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 Uint8 *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 int len, dist; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 if (z == 256) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 a->zout = zout; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
302 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
304 z -= 257; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 len = stbi__zlength_base[z]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 if (stbi__zlength_extra[z]) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
308 len += dmZReceive(a, stbi__zlength_extra[z]); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
310 if ((ret = stbi__zhuffman_decode(a, &a->zdistance, &z)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
311 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
312 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
313 dist = stbi__zdist_base[z]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
314 if (stbi__zdist_extra[z]) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
315 dist += dmZReceive(a, stbi__zdist_extra[z]); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
316 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
317 if (zout - a->zoutStart < dist) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
318 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
319 return dmError(DMERR_DATA_ERROR, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
320 "Bad Huffman block distance.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
321 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
322 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
323 if (zout + len > a->zoutEnd) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
324 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
325 if (!stbi__zexpand(a, zout, len)) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
326 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
327 zout = a->zout; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
328 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
329 p = (Uint8 *) (zout - dist); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
330 if (dist == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
331 { // run of one byte; common in images. |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
332 Uint8 v = *p; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
333 do { *zout++ = v; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
334 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
335 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
336 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
337 do { *zout++ = *p++; } while (--len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
338 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
339 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
340 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
341 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
342 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
343 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
344 static const Uint8 length_dezigzag[19] = |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
345 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
346 16, 17, 18, 0, 8, 7, 9, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
347 6 , 10, 5 , 11, 4, 12, 3, |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
348 13, 2 , 14, 1 , 15 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
349 }; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
350 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
351 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
352 static int dmZLibComputeHuffmanCodes(DMZLibContext * a) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
353 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
354 DMZHuffmanContext z_codelength; |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
355 Uint8 codeLengths[286 + 32 + 137]; //padding for maximum single op |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
356 Uint8 codeLengthSizes[19]; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
357 int i, n, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
358 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
359 int hlit = dmZReceive(a, 5) + 257; |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
360 int hdist = dmZReceive(a, 5) + 1; |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
361 int hclen = dmZReceive(a, 4) + 4; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
362 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
363 memset(codeLengthSizes, 0, sizeof(codeLengthSizes)); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
365 for (i = 0; i < hclen; i++) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
366 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
367 int s = dmZReceive(a, 3); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
368 codeLengthSizes[length_dezigzag[i]] = (Uint8) s; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
369 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
370 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
371 if ((ret = dmZLibBuildHuffmanTables(&z_codelength, codeLengthSizes, 19)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
372 return ret; |
955 | 373 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
374 n = 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
375 while (n < hlit + hdist) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
376 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
377 int c; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
378 if ((ret = stbi__zhuffman_decode(a, &z_codelength, &c)) != DMERR_OK) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
379 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
380 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
381 DMSTBI_ASSERT(c >= 0 && c < 19); |
955 | 382 |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
383 if (c < 16) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
384 codeLengths[n++] = (Uint8) c; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
385 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
386 if (c == 16) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
387 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
388 c = dmZReceive(a, 2) + 3; |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
389 memset(codeLengths + n, codeLengths[n - 1], c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
390 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
391 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
392 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
393 if (c == 17) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
394 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
395 c = dmZReceive(a, 3) + 3; |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
396 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
397 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
398 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
399 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
400 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
401 c = dmZReceive(a, 7) + 11; |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
402 memset(codeLengths + n, 0, c); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
403 n += c; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
404 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
405 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
406 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
407 if (n != hlit + hdist) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
408 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
409 return dmError(DMERR_DATA_ERROR, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
410 "Bad huffman codelengths.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
411 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
412 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
413 if ((ret = dmZLibBuildHuffmanTables(&a->zlength, codeLengths, hlit)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
414 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
415 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
416 if ((ret = dmZLibBuildHuffmanTables(&a->zdistance, codeLengths + hlit, hdist)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
417 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
418 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
419 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
420 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
421 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
422 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
423 static int dmZLibParseUncompresedBlock(DMZLibContext * a) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
424 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
425 Uint8 header[4]; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
426 int len, nlen, k; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
427 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
428 if (a->numBits & 7) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
429 dmZReceive(a, a->numBits & 7); // discard |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
430 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
431 // drain the bit-packed data into header |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
432 k = 0; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
433 while (a->numBits > 0) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
434 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
435 header[k++] = (Uint8) (a->codeBuffer & 255); // suppress MSVC run-time check |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
436 a->codeBuffer >>= 8; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
437 a->numBits -= 8; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
438 } |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
439 DMSTBI_ASSERT(a->numBits == 0); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
440 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
441 // now fill header the normal way |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
442 while (k < 4) |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
443 header[k++] = dmZGet8(a); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
444 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
445 len = (header[1] << 8) | header[0]; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
446 nlen = (header[3] << 8) | header[2]; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
447 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
448 if (nlen != (len ^ 0xffff)) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
449 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
450 return dmError(DMERR_DATA_ERROR, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
451 "Compressed data corrupt.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
452 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
453 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
454 if (a->zbuffer + len > a->zbufferEnd) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
455 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
456 return dmError(DMERR_BOUNDS, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
457 "Read past buffer, probably corrupt compressed data.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
458 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
459 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
460 if (a->zout + len > a->zoutEnd && !stbi__zexpand(a, a->zout, len)) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
461 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
462 return dmError(DMERR_DATA_ERROR, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
463 "XXXX TODO"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
464 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
465 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
466 memcpy(a->zout, a->zbuffer, len); |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
467 a->zbuffer += len; |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
468 a->zout += len; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
469 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
470 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
471 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
472 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
473 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
474 int dmZLibParseHeader(DMZLibContext * ctx, BOOL checkPNG) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
475 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
476 int cmf = dmZGet8(ctx); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
477 int flags = dmZGet8(ctx); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
478 int cm = cmf & 15; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
479 // int cinfo = cmf >> 4; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
480 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
481 if ((cmf * 256 + flags) % 31 != 0) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
482 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
483 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
484 "Bad zlib header."); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
485 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
486 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
487 if (checkPNG && (flags & 32)) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
488 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
489 // preset dictionary not allowed in png |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
490 return dmError(DMERR_NOT_SUPPORTED, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
491 "Preset dictionary not allowed in PNG.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
492 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
493 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
494 if (checkPNG && cm != 8) |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
495 { |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
496 return dmError(DMERR_INVALID_DATA, |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
497 "Bad or unsupported compression type.\n"); |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
498 } |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
499 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
500 // window = 1 << (8 + cinfo)... but who cares, we fully buffer output |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
501 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
502 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
503 |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
504 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
505 int dmZLibDecode(DMZLibContext * ctx) |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
506 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
507 int final, type, ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
508 |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
509 ctx->numBits = 0; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
510 ctx->codeBuffer = 0; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
511 do |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
512 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
513 final = dmZReceive(ctx, 1); |
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
514 type = dmZReceive(ctx, 2); |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
515 if (type == 0) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
516 { |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
517 if ((ret = dmZLibParseUncompresedBlock(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
518 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
519 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
520 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
521 if (type == 3) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
522 return 0; |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
523 else |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
524 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
525 if (type == 1) |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
526 { |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
527 // use fixed code lengths |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
528 if ((ret = dmZLibBuildHuffmanTables(&ctx->zlength, dm_zdefault_length, 288)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
529 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
530 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
531 if ((ret = dmZLibBuildHuffmanTables(&ctx->zdistance, dm_zdefault_distance, 32)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
532 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
533 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
534 else |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
535 if ((ret = dmZLibComputeHuffmanCodes(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
536 return ret; |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
537 |
960
1832ac20edb2
Clean up dmzlib and use it in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
538 if ((ret = dmZLibParseHuffmanBlock(ctx)) != DMERR_OK) |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
539 return ret; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
540 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
541 } |
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
542 while (!final); |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
543 |
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
955
diff
changeset
|
544 return DMERR_OK; |
949
6ed9465f3913
Initial import of separated zlib decoding routines lifted from
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
545 } |