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 | ␊ |
23 | #include "libsa.h"␊ |
24 | ␊ |
25 | static void PreviewDecompress16(uint32_t * compressBuffer, ␊ |
26 | ␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
27 | ␉␉␉uint16_t * output);␊ |
28 | ␊ |
29 | static void PreviewDecompress32(uint32_t * compressBuffer, ␊ |
30 | uint32_t width, uint32_t height, uint32_t row, ␊ |
31 | uint32_t * output);␊ |
32 | ␊ |
33 | static void PreviewDecompress16(uint32_t * compressBuffer, ␊ |
34 | ␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
35 | ␉␉␉uint16_t * output)␊ |
36 | {␊ |
37 | ␉int i, j;␊ |
38 | ␉uint32_t * input;␊ |
39 | ␊ |
40 | ␉uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
41 | ␉uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
42 | ␉uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
43 | ␉uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
44 | ␊ |
45 | ␉if (!sc0 || !sc1 || !sc2 || !sc3) {␊ |
46 | ␉␉return;␊ |
47 | ␉}␊ |
48 | ␊ |
49 | ␉uint32_t sr0, sr1, sr2, sr3;␊ |
50 | ␊ |
51 | ␉bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
52 | ␉bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
53 | ␉bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
54 | ␉bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
55 | ␊ |
56 | ␉uint32_t tmp1, tmp2, out;␊ |
57 | ␉for (j = 0; j < (height + 2); j++) {␊ |
58 | ␉␉input = compressBuffer;␊ |
59 | ␊ |
60 | ␉␉if (j < height)␊ |
61 | ␉␉{␊ |
62 | ␉␉␉input += j;␊ |
63 | ␉␉}␊ |
64 | ␉␉else␊ |
65 | ␉␉{␊ |
66 | ␉␉␉input += height - 1;␊ |
67 | ␉␉}␊ |
68 | ␊ |
69 | ␉␉input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
70 | ␊ |
71 | ␉␉uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
72 | ␉␉sr0 = sr1 = sr2 = sr3 = 0;␊ |
73 | ␊ |
74 | ␉␉for (i = 0; i < (width + 2); i++)␊ |
75 | ␉␉{␊ |
76 | ␉␉␉if (i < width)␊ |
77 | ␉␉␉{␊ |
78 | ␉␉␉␉if (!count)␊ |
79 | ␉␉␉␉{␊ |
80 | ␉␉␉␉␉count = *input++;␊ |
81 | ␉␉␉␉␉repeat = (count & 0xff000000);␊ |
82 | ␉␉␉␉␉count ^= repeat;␊ |
83 | ␉␉␉␉␉fetch = true;␊ |
84 | ␉␉␉␉}␊ |
85 | ␉␉␉␉else␊ |
86 | ␉␉␉␉{␊ |
87 | ␉␉␉␉␉fetch = (0 == repeat);␊ |
88 | ␉␉␉␉}␊ |
89 | ␊ |
90 | ␉␉␉␉count--;␊ |
91 | ␊ |
92 | ␉␉␉␉if (fetch)␊ |
93 | ␉␉␉␉{␊ |
94 | ␉␉␉␉␉data = *((uint16_t *)input);␊ |
95 | ␉␉␉␉␉(*((uint16_t *)input))++;␊ |
96 | ␊ |
97 | ␉␉␉␉␉// grayscale␊ |
98 | ␉␉␉␉␉// srgb 13933, 46871, 4732␊ |
99 | ␉␉␉␉␉// ntsc 19595, 38470, 7471␊ |
100 | ␉␉␉␉␉data = 13933 * (0x1f & (data >> 10))␊ |
101 | ␉␉␉␉␉␉+ 46871 * (0x1f & (data >> 5))␊ |
102 | ␉␉␉␉␉␉+ 4732 * (0x1f & data);␊ |
103 | ␉␉␉␉␉data >>= 13;␊ |
104 | ␊ |
105 | ␉␉␉␉␉// 70% white, 30 % black␊ |
106 | ␉␉␉␉␉data *= 19661;␊ |
107 | ␉␉␉␉␉data += (103 << 16);␊ |
108 | ␉␉␉␉␉data >>= 16;␊ |
109 | ␉␉␉␉}␊ |
110 | ␉␉␉}␊ |
111 | ␊ |
112 | ␉␉␉// gauss blur␊ |
113 | ␉␉␉tmp2 = sr0 + data;␊ |
114 | ␉␉␉sr0 = data;␊ |
115 | ␉␉␉tmp1 = sr1 + tmp2;␊ |
116 | ␉␉␉sr1 = tmp2;␊ |
117 | ␉␉␉tmp2 = sr2 + tmp1;␊ |
118 | ␉␉␉sr2 = tmp1;␊ |
119 | ␉␉␉tmp1 = sr3 + tmp2;␊ |
120 | ␉␉␉sr3 = tmp2;␊ |
121 | ␊ |
122 | ␉␉␉tmp2 = sc0[i] + tmp1;␊ |
123 | ␉␉␉sc0[i] = tmp1;␊ |
124 | ␉␉␉tmp1 = sc1[i] + tmp2;␊ |
125 | ␉␉␉sc1[i] = tmp2;␊ |
126 | ␉␉␉tmp2 = sc2[i] + tmp1;␊ |
127 | ␉␉␉sc2[i] = tmp1;␊ |
128 | ␉␉␉out = (128 + sc3[i] + tmp2) >> 11;␊ |
129 | ␉␉␉sc3[i] = tmp2;␊ |
130 | ␊ |
131 | ␉␉␉out &= 0x1f;␊ |
132 | ␉␉␉if ((i > 1) && (j > 1))␊ |
133 | ␉␉␉{␊ |
134 | ␉␉␉␉output[i-2] = out | (out << 5) | (out << 10);␊ |
135 | ␉␉␉}␊ |
136 | ␉␉}␊ |
137 | ␊ |
138 | ␉␉if (j > 1)␊ |
139 | ␉␉{␊ |
140 | ␉␉␉output += row;␊ |
141 | ␉␉}␊ |
142 | ␉}␊ |
143 | ␉free(sc3);␊ |
144 | ␉free(sc2);␊ |
145 | ␉free(sc1);␊ |
146 | ␉free(sc0);␊ |
147 | }␊ |
148 | ␊ |
149 | static void PreviewDecompress32(uint32_t * compressBuffer, ␊ |
150 | ␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
151 | ␉␉␉uint32_t * output)␊ |
152 | {␊ |
153 | ␉int i, j;␊ |
154 | ␉uint32_t * input;␊ |
155 | ␊ |
156 | ␉uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
157 | ␉uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
158 | ␉uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
159 | ␉uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
160 | ␊ |
161 | ␉if (!sc0 || !sc1 || !sc2 || !sc3)␊ |
162 | ␉{␊ |
163 | ␉␉return;␊ |
164 | ␉}␊ |
165 | ␊ |
166 | ␉uint32_t sr0, sr1, sr2, sr3;␊ |
167 | ␊ |
168 | ␉bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
169 | ␉bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
170 | ␉bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
171 | ␉bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
172 | ␊ |
173 | ␉uint32_t tmp1, tmp2, out;␊ |
174 | ␉for (j = 0; j < (height + 2); j++)␊ |
175 | ␉{␊ |
176 | ␉␉input = compressBuffer;␊ |
177 | ␉␉if (j < height)␊ |
178 | ␉␉{␊ |
179 | ␉␉␉input += j;␊ |
180 | ␉␉}␊ |
181 | ␉␉else␊ |
182 | ␉␉{␊ |
183 | ␉␉␉input += height - 1;␊ |
184 | ␉␉}␊ |
185 | ␉␉input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
186 | ␊ |
187 | ␉␉uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
188 | ␉␉sr0 = sr1 = sr2 = sr3 = 0;␊ |
189 | ␊ |
190 | ␉␉for (i = 0; i < (width + 2); i++)␊ |
191 | ␉␉{␊ |
192 | ␉␉␉if (i < width)␊ |
193 | ␉␉␉{␊ |
194 | ␉␉␉␉if (!count)␊ |
195 | ␉␉␉␉{␊ |
196 | ␉␉␉␉␉count = *input++;␊ |
197 | ␉␉␉␉␉repeat = (count & 0xff000000);␊ |
198 | ␉␉␉␉␉count ^= repeat;␊ |
199 | ␉␉␉␉␉fetch = true;␊ |
200 | ␉␉␉␉}␊ |
201 | ␉␉␉␉else␊ |
202 | ␉␉␉␉{␊ |
203 | ␉␉␉␉␉fetch = (0 == repeat);␊ |
204 | ␉␉␉␉}␊ |
205 | ␊ |
206 | ␉␉␉␉count--;␊ |
207 | ␊ |
208 | ␉␉␉␉if (fetch)␊ |
209 | ␉␉␉␉{␊ |
210 | ␉␉␉␉␉data = *input++;␊ |
211 | ␊ |
212 | ␉␉␉␉␉// grayscale␊ |
213 | ␉␉␉␉␉// srgb 13933, 46871, 4732␊ |
214 | ␉␉␉␉␉// ntsc 19595, 38470, 7471␊ |
215 | ␉␉␉␉␉data = 13933 * (0xff & (data >> 24))␊ |
216 | ␉␉␉␉␉␉+ 46871 * (0xff & (data >> 16))␊ |
217 | ␉␉␉␉␉␉+ 4732 * (0xff & data);␊ |
218 | ␉␉␉␉␉data >>= 16;␊ |
219 | ␊ |
220 | ␉␉␉␉␉// 70% white, 30 % black␊ |
221 | ␉␉␉␉␉data *= 19661;␊ |
222 | ␉␉␉␉␉data += (103 << 16);␊ |
223 | ␉␉␉␉␉data >>= 16;␊ |
224 | ␉␉␉␉}␊ |
225 | ␉␉␉}␊ |
226 | ␊ |
227 | ␉␉␉// gauss blur␊ |
228 | ␉␉␉tmp2 = sr0 + data;␊ |
229 | ␉␉␉sr0 = data;␊ |
230 | ␉␉␉tmp1 = sr1 + tmp2;␊ |
231 | ␉␉␉sr1 = tmp2;␊ |
232 | ␉␉␉tmp2 = sr2 + tmp1;␊ |
233 | ␉␉␉sr2 = tmp1;␊ |
234 | ␉␉␉tmp1 = sr3 + tmp2;␊ |
235 | ␉␉␉sr3 = tmp2;␊ |
236 | ␊ |
237 | ␉␉␉tmp2 = sc0[i] + tmp1;␊ |
238 | ␉␉␉sc0[i] = tmp1;␊ |
239 | ␉␉␉tmp1 = sc1[i] + tmp2;␊ |
240 | ␉␉␉sc1[i] = tmp2;␊ |
241 | ␉␉␉tmp2 = sc2[i] + tmp1;␊ |
242 | ␉␉␉sc2[i] = tmp1;␊ |
243 | ␉␉␉out = (128 + sc3[i] + tmp2) >> 8;␊ |
244 | ␉␉␉sc3[i] = tmp2;␊ |
245 | ␊ |
246 | ␉␉␉out &= 0xff;␊ |
247 | ␉␉␉if ((i > 1) && (j > 1))␊ |
248 | ␉␉␉{␊ |
249 | ␉␉␉␉output[i-2] = out | (out << 8) | (out << 16);␊ |
250 | ␉␉␉}␊ |
251 | ␉␉}␊ |
252 | ␊ |
253 | ␉␉if (j > 1)␊ |
254 | ␉␉{␊ |
255 | ␉␉␉output += row;␊ |
256 | ␉␉}␊ |
257 | ␉}␊ |
258 | ␊ |
259 | ␉free(sc3);␊ |
260 | ␉free(sc2);␊ |
261 | ␉free(sc1);␊ |
262 | ␉free(sc0);␊ |
263 | }␊ |
264 | ␊ |
265 | void *␊ |
266 | DecompressData(void *srcbase, int *dw, int *dh, int *bitsPerPixel)␊ |
267 | {␊ |
268 | ␉uint32_t * src = (uint32_t *) srcbase, size;␊ |
269 | ␉void * ret;␊ |
270 | ␊ |
271 | ␉*bitsPerPixel = 8 * ((int) src[0]);␊ |
272 | ␉*dw = (int) src[1];␊ |
273 | ␉*dh = (int) src[2];␊ |
274 | ␉␊ |
275 | ␉size = (*dw * *dh * *bitsPerPixel)/ 8;␊ |
276 | ␉if (!size)␊ |
277 | ␉{␊ |
278 | ␉␉return 0;␊ |
279 | ␉}␊ |
280 | ␊ |
281 | ␉ret = malloc (size);␊ |
282 | ␊ |
283 | ␉if (!ret)␊ |
284 | ␉{␊ |
285 | ␉␉return 0;␊ |
286 | ␉}␊ |
287 | ␉bzero(ret, size);␊ |
288 | ␊ |
289 | ␉switch(*bitsPerPixel)␊ |
290 | ␉{␊ |
291 | ␉␉case 32:␊ |
292 | ␉␉␉PreviewDecompress32((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
293 | ␉␉␉break;␊ |
294 | ␉␉case 16:␊ |
295 | ␉␉␉PreviewDecompress16((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
296 | ␉␉␉break;␊ |
297 | ␉␉default:␊ |
298 | ␉␉␉free(ret);␊ |
299 | ␉␉␉ret = 0;␊ |
300 | ␉␉␉break;␊ |
301 | ␉}␊ |
302 | ␉return ret;␊ |
303 | }␊ |
304 | |