1 | /*␊ |
2 | * Copyright (c) 1995-2002 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * The contents of this file constitute Original Code as defined in and␊ |
7 | * are subject to the Apple Public Source License Version 1.1 (the␊ |
8 | * "License"). You may not use this file except in compliance with the␊ |
9 | * License. Please obtain a copy of the License at␊ |
10 | * http://www.apple.com/publicsource and read it before using this file.␊ |
11 | * ␊ |
12 | * This Original Code and all software distributed under the License are␊ |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
17 | * License for the specific language governing rights and limitations␊ |
18 | * under the License.␊ |
19 | * ␊ |
20 | * @APPLE_LICENSE_HEADER_END@␊ |
21 | */␊ |
22 | #include "bmdecompress.h"␊ |
23 | ␊ |
24 | typedef uint8_t UInt8;␊ |
25 | typedef uint16_t UInt16;␊ |
26 | typedef int8_t SInt8;␊ |
27 | typedef int16_t SInt16;␊ |
28 | static void ␊ |
29 | PreviewDecompress16(uint32_t * compressBuffer, ␊ |
30 | ␉␉␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
31 | ␉␉␉␉␉uint16_t * output);␊ |
32 | static void ␊ |
33 | PreviewDecompress32(uint32_t * compressBuffer, ␊ |
34 | ␉␉␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
35 | ␉␉␉␉␉uint32_t * output);␊ |
36 | ␊ |
37 | static void ␊ |
38 | PreviewDecompress16(uint32_t * compressBuffer, ␊ |
39 | uint32_t width, uint32_t height, uint32_t row, ␊ |
40 | uint16_t * output)␊ |
41 | {␊ |
42 | int i, j;␊ |
43 | uint32_t * input;␊ |
44 | ␊ |
45 | uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
46 | uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
47 | uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
48 | uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
49 | ␉␊ |
50 | ␉if (!sc0 || !sc1 || !sc2 || !sc3) return;␊ |
51 | ␉␊ |
52 | uint32_t sr0, sr1, sr2, sr3;␊ |
53 | ␊ |
54 | bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
55 | bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
56 | bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
57 | bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
58 | ␊ |
59 | uint32_t tmp1, tmp2, out;␊ |
60 | for (j = 0; j < (height + 2); j++)␊ |
61 | {␊ |
62 | input = compressBuffer;␊ |
63 | if (j < height)␊ |
64 | input += j;␊ |
65 | else␊ |
66 | input += height - 1;␊ |
67 | input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
68 | ␊ |
69 | uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
70 | sr0 = sr1 = sr2 = sr3 = 0;␊ |
71 | ␊ |
72 | for (i = 0; i < (width + 2); i++)␊ |
73 | {␊ |
74 | if (i < width)␊ |
75 | {␊ |
76 | if (!count)␊ |
77 | {␊ |
78 | count = *input++;␊ |
79 | repeat = (count & 0xff000000);␊ |
80 | count ^= repeat;␊ |
81 | fetch = true;␊ |
82 | }␊ |
83 | else␊ |
84 | fetch = (0 == repeat);␊ |
85 | ␊ |
86 | count--;␊ |
87 | ␊ |
88 | if (fetch)␊ |
89 | {␊ |
90 | ␉␉␉␉␉data = *((uint16_t *)input);␊ |
91 | (*((uint16_t *)input))++;␊ |
92 | ␊ |
93 | // grayscale␊ |
94 | // srgb 13933, 46871, 4732␊ |
95 | // ntsc 19595, 38470, 7471␊ |
96 | data = 13933 * (0x1f & (data >> 10))␊ |
97 | + 46871 * (0x1f & (data >> 5))␊ |
98 | + 4732 * (0x1f & data);␊ |
99 | data >>= 13;␊ |
100 | ␊ |
101 | // 70% white, 30 % black␊ |
102 | data *= 19661;␊ |
103 | data += (103 << 16);␊ |
104 | data >>= 16;␊ |
105 | }␊ |
106 | }␊ |
107 | ␊ |
108 | // gauss blur␊ |
109 | tmp2 = sr0 + data;␊ |
110 | sr0 = data;␊ |
111 | tmp1 = sr1 + tmp2;␊ |
112 | sr1 = tmp2;␊ |
113 | tmp2 = sr2 + tmp1;␊ |
114 | sr2 = tmp1;␊ |
115 | tmp1 = sr3 + tmp2;␊ |
116 | sr3 = tmp2;␊ |
117 | ␊ |
118 | tmp2 = sc0[i] + tmp1;␊ |
119 | sc0[i] = tmp1;␊ |
120 | tmp1 = sc1[i] + tmp2;␊ |
121 | sc1[i] = tmp2;␊ |
122 | tmp2 = sc2[i] + tmp1;␊ |
123 | sc2[i] = tmp1;␊ |
124 | out = (128 + sc3[i] + tmp2) >> 11;␊ |
125 | sc3[i] = tmp2;␊ |
126 | ␊ |
127 | out &= 0x1f;␊ |
128 | if ((i > 1) && (j > 1))␊ |
129 | output[i-2] = out | (out << 5) | (out << 10);␊ |
130 | }␊ |
131 | ␊ |
132 | if (j > 1)␊ |
133 | output += row;␊ |
134 | }␊ |
135 | free(sc3);␊ |
136 | free(sc2);␊ |
137 | free(sc1);␊ |
138 | free(sc0);␊ |
139 | }␊ |
140 | ␊ |
141 | static void ␊ |
142 | PreviewDecompress32(uint32_t * compressBuffer, ␊ |
143 | uint32_t width, uint32_t height, uint32_t row, ␊ |
144 | uint32_t * output)␊ |
145 | {␊ |
146 | int i, j;␊ |
147 | uint32_t * input;␊ |
148 | ␊ |
149 | uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
150 | uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
151 | uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
152 | uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
153 | ␉␊ |
154 | ␉if (!sc0 || !sc1 || !sc2 || !sc3) return;␊ |
155 | ␉␊ |
156 | ␉␊ |
157 | uint32_t sr0, sr1, sr2, sr3;␊ |
158 | ␊ |
159 | bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
160 | bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
161 | bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
162 | bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
163 | ␊ |
164 | uint32_t tmp1, tmp2, out;␊ |
165 | for (j = 0; j < (height + 2); j++)␊ |
166 | {␊ |
167 | input = compressBuffer;␊ |
168 | if (j < height)␊ |
169 | input += j;␊ |
170 | else␊ |
171 | input += height - 1;␊ |
172 | input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
173 | ␊ |
174 | uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
175 | sr0 = sr1 = sr2 = sr3 = 0;␊ |
176 | ␊ |
177 | for (i = 0; i < (width + 2); i++)␊ |
178 | {␊ |
179 | if (i < width)␊ |
180 | {␊ |
181 | if (!count)␊ |
182 | {␊ |
183 | count = *input++;␊ |
184 | repeat = (count & 0xff000000);␊ |
185 | count ^= repeat;␊ |
186 | fetch = true;␊ |
187 | }␊ |
188 | else␊ |
189 | fetch = (0 == repeat);␊ |
190 | ␊ |
191 | count--;␊ |
192 | ␊ |
193 | if (fetch)␊ |
194 | {␊ |
195 | data = *input++;␊ |
196 | ␊ |
197 | // grayscale␊ |
198 | // srgb 13933, 46871, 4732␊ |
199 | // ntsc 19595, 38470, 7471␊ |
200 | data = 13933 * (0xff & (data >> 24))␊ |
201 | + 46871 * (0xff & (data >> 16))␊ |
202 | + 4732 * (0xff & data);␊ |
203 | data >>= 16;␊ |
204 | ␊ |
205 | // 70% white, 30 % black␊ |
206 | data *= 19661;␊ |
207 | data += (103 << 16);␊ |
208 | data >>= 16;␊ |
209 | }␊ |
210 | }␊ |
211 | ␊ |
212 | // gauss blur␊ |
213 | tmp2 = sr0 + data;␊ |
214 | sr0 = data;␊ |
215 | tmp1 = sr1 + tmp2;␊ |
216 | sr1 = tmp2;␊ |
217 | tmp2 = sr2 + tmp1;␊ |
218 | sr2 = tmp1;␊ |
219 | tmp1 = sr3 + tmp2;␊ |
220 | sr3 = tmp2;␊ |
221 | ␊ |
222 | tmp2 = sc0[i] + tmp1;␊ |
223 | sc0[i] = tmp1;␊ |
224 | tmp1 = sc1[i] + tmp2;␊ |
225 | sc1[i] = tmp2;␊ |
226 | tmp2 = sc2[i] + tmp1;␊ |
227 | sc2[i] = tmp1;␊ |
228 | out = (128 + sc3[i] + tmp2) >> 8;␊ |
229 | sc3[i] = tmp2;␊ |
230 | ␊ |
231 | out &= 0xff;␊ |
232 | if ((i > 1) && (j > 1))␊ |
233 | output[i-2] = out | (out << 8) | (out << 16);␊ |
234 | }␊ |
235 | ␊ |
236 | if (j > 1)␊ |
237 | output += row;␊ |
238 | }␊ |
239 | ␊ |
240 | free(sc3);␊ |
241 | free(sc2);␊ |
242 | free(sc1);␊ |
243 | free(sc0);␊ |
244 | }␊ |
245 | ␊ |
246 | void *␊ |
247 | DecompressData(void *srcbase, int *dw, int *dh, int *bitsPerPixel)␊ |
248 | {␊ |
249 | uint32_t * src = (uint32_t *) srcbase, size;␊ |
250 | ␉void * ret;␊ |
251 | ␊ |
252 | *bitsPerPixel = 8 * ((int) src[0]);␊ |
253 | ␉*dw = (int) src[1];␊ |
254 | ␉*dh = (int) src[2];␊ |
255 | ␉␊ |
256 | ␉size = (*dw * *dh * *bitsPerPixel)/ 8;␊ |
257 | ␉if (!size) ␊ |
258 | ␉{␊ |
259 | ␉␉return 0;␊ |
260 | ␉}␊ |
261 | ␉␊ |
262 | ␉ret = malloc (size);␊ |
263 | ␊ |
264 | if (!ret) ␊ |
265 | {␊ |
266 | return 0;␊ |
267 | }␊ |
268 | ␉bzero(ret, size);␊ |
269 | ␉␊ |
270 | switch(*bitsPerPixel)␊ |
271 | {␊ |
272 | case 32:␊ |
273 | PreviewDecompress32((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
274 | break;␊ |
275 | case 16:␊ |
276 | PreviewDecompress16((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
277 | break;␊ |
278 | default:␊ |
279 | {␊ |
280 | free(ret);␊ |
281 | ret = 0; ␊ |
282 | break;␊ |
283 | }␊ |
284 | ␊ |
285 | }␊ |
286 | return ret;␊ |
287 | }␊ |
288 | |