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 | ␉uint32_t sr0, sr1, sr2, sr3;␊ |
45 | ␊ |
46 | ␉bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
47 | ␉bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
48 | ␉bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
49 | ␉bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
50 | ␊ |
51 | ␉uint32_t tmp1, tmp2, out;␊ |
52 | ␉for (j = 0; j < (height + 2); j++)␊ |
53 | ␉{␊ |
54 | ␉␉input = compressBuffer;␊ |
55 | ␊ |
56 | ␉␉if (j < height)␊ |
57 | ␉␉{␊ |
58 | ␉␉␉input += j;␊ |
59 | ␉␉}␊ |
60 | ␉␉else␊ |
61 | ␉␉{␊ |
62 | ␉␉␉input += height - 1;␊ |
63 | ␉␉}␊ |
64 | ␊ |
65 | ␉␉input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
66 | ␊ |
67 | ␉␉uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
68 | ␉␉sr0 = sr1 = sr2 = sr3 = 0;␊ |
69 | ␊ |
70 | ␉␉for (i = 0; i < (width + 2); i++)␊ |
71 | ␉␉{␊ |
72 | ␉␉␉if (i < width)␊ |
73 | ␉␉␉{␊ |
74 | ␉␉␉␉if (!count)␊ |
75 | ␉␉␉␉{␊ |
76 | ␉␉␉␉␉count = *input++;␊ |
77 | ␉␉␉␉␉repeat = (count & 0xff000000);␊ |
78 | ␉␉␉␉␉count ^= repeat;␊ |
79 | ␉␉␉␉␉fetch = true;␊ |
80 | ␉␉␉␉}␊ |
81 | ␉␉␉␉else␊ |
82 | ␉␉␉␉{␊ |
83 | ␉␉␉␉␉fetch = (0 == repeat);␊ |
84 | ␉␉␉␉}␊ |
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 | ␉␉␉{␊ |
130 | ␉␉␉␉output[i-2] = out | (out << 5) | (out << 10);␊ |
131 | ␉␉␉}␊ |
132 | ␉␉}␊ |
133 | ␊ |
134 | ␉␉if (j > 1)␊ |
135 | ␉␉{␊ |
136 | ␉␉␉output += row;␊ |
137 | ␉␉}␊ |
138 | ␉}␊ |
139 | ␉free(sc3);␊ |
140 | ␉free(sc2);␊ |
141 | ␉free(sc1);␊ |
142 | ␉free(sc0);␊ |
143 | }␊ |
144 | ␊ |
145 | static void PreviewDecompress32(uint32_t * compressBuffer, ␊ |
146 | ␉␉␉uint32_t width, uint32_t height, uint32_t row, ␊ |
147 | ␉␉␉uint32_t * output)␊ |
148 | {␊ |
149 | ␉int i, j;␊ |
150 | ␉uint32_t * input;␊ |
151 | ␊ |
152 | ␉uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
153 | ␉uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
154 | ␉uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
155 | ␉uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
156 | ␉uint32_t sr0, sr1, sr2, sr3;␊ |
157 | ␊ |
158 | ␉bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
159 | ␉bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
160 | ␉bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
161 | ␉bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
162 | ␊ |
163 | ␉uint32_t tmp1, tmp2, out;␊ |
164 | ␉for (j = 0; j < (height + 2); j++)␊ |
165 | ␉{␊ |
166 | ␉␉input = compressBuffer;␊ |
167 | ␉␉if (j < height)␊ |
168 | ␉␉{␊ |
169 | ␉␉␉input += j;␊ |
170 | ␉␉}␊ |
171 | ␉␉else␊ |
172 | ␉␉{␊ |
173 | ␉␉␉input += height - 1;␊ |
174 | ␉␉}␊ |
175 | ␉␉input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
176 | ␊ |
177 | ␉␉uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
178 | ␉␉sr0 = sr1 = sr2 = sr3 = 0;␊ |
179 | ␊ |
180 | ␉␉for (i = 0; i < (width + 2); i++)␊ |
181 | ␉␉{␊ |
182 | ␉␉␉if (i < width)␊ |
183 | ␉␉␉{␊ |
184 | ␉␉␉␉if (!count)␊ |
185 | ␉␉␉␉{␊ |
186 | ␉␉␉␉␉count = *input++;␊ |
187 | ␉␉␉␉␉repeat = (count & 0xff000000);␊ |
188 | ␉␉␉␉␉count ^= repeat;␊ |
189 | ␉␉␉␉␉fetch = true;␊ |
190 | ␉␉␉␉}␊ |
191 | ␉␉␉␉else␊ |
192 | ␉␉␉␉{␊ |
193 | ␉␉␉␉␉fetch = (0 == repeat);␊ |
194 | ␉␉␉␉}␊ |
195 | ␊ |
196 | ␉␉␉␉count--;␊ |
197 | ␊ |
198 | ␉␉␉␉if (fetch)␊ |
199 | ␉␉␉␉{␊ |
200 | ␉␉␉␉␉data = *input++;␊ |
201 | ␊ |
202 | ␉␉␉␉␉// grayscale␊ |
203 | ␉␉␉␉␉// srgb 13933, 46871, 4732␊ |
204 | ␉␉␉␉␉// ntsc 19595, 38470, 7471␊ |
205 | ␉␉␉␉␉data = 13933 * (0xff & (data >> 24))␊ |
206 | ␉␉␉␉␉␉+ 46871 * (0xff & (data >> 16))␊ |
207 | ␉␉␉␉␉␉+ 4732 * (0xff & data);␊ |
208 | ␉␉␉␉␉data >>= 16;␊ |
209 | ␊ |
210 | ␉␉␉␉␉// 70% white, 30 % black␊ |
211 | ␉␉␉␉␉data *= 19661;␊ |
212 | ␉␉␉␉␉data += (103 << 16);␊ |
213 | ␉␉␉␉␉data >>= 16;␊ |
214 | ␉␉␉␉}␊ |
215 | ␉␉␉}␊ |
216 | ␊ |
217 | ␉␉␉// gauss blur␊ |
218 | ␉␉␉tmp2 = sr0 + data;␊ |
219 | ␉␉␉sr0 = data;␊ |
220 | ␉␉␉tmp1 = sr1 + tmp2;␊ |
221 | ␉␉␉sr1 = tmp2;␊ |
222 | ␉␉␉tmp2 = sr2 + tmp1;␊ |
223 | ␉␉␉sr2 = tmp1;␊ |
224 | ␉␉␉tmp1 = sr3 + tmp2;␊ |
225 | ␉␉␉sr3 = tmp2;␊ |
226 | ␊ |
227 | ␉␉␉tmp2 = sc0[i] + tmp1;␊ |
228 | ␉␉␉sc0[i] = tmp1;␊ |
229 | ␉␉␉tmp1 = sc1[i] + tmp2;␊ |
230 | ␉␉␉sc1[i] = tmp2;␊ |
231 | ␉␉␉tmp2 = sc2[i] + tmp1;␊ |
232 | ␉␉␉sc2[i] = tmp1;␊ |
233 | ␉␉␉out = (128 + sc3[i] + tmp2) >> 8;␊ |
234 | ␉␉␉sc3[i] = tmp2;␊ |
235 | ␊ |
236 | ␉␉␉out &= 0xff;␊ |
237 | ␉␉␉if ((i > 1) && (j > 1))␊ |
238 | ␉␉␉{␊ |
239 | ␉␉␉␉output[i-2] = out | (out << 8) | (out << 16);␊ |
240 | ␉␉␉}␊ |
241 | ␉␉}␊ |
242 | ␊ |
243 | ␉␉if (j > 1)␊ |
244 | ␉␉{␊ |
245 | ␉␉␉output += row;␊ |
246 | ␉␉}␊ |
247 | ␉}␊ |
248 | ␊ |
249 | ␉free(sc3);␊ |
250 | ␉free(sc2);␊ |
251 | ␉free(sc1);␊ |
252 | ␉free(sc0);␊ |
253 | }␊ |
254 | ␊ |
255 | void *␊ |
256 | DecompressData(void *srcbase, int *dw, int *dh, int *bitsPerPixel)␊ |
257 | {␊ |
258 | ␉uint32_t * src = (uint32_t *) srcbase;␊ |
259 | ␉void * ret;␊ |
260 | ␊ |
261 | ␉*bitsPerPixel = 8 * ((int) src[0]);␊ |
262 | ␉*dw = (int) src[1];␊ |
263 | ␉*dh = (int) src[2];␊ |
264 | ␉␊ |
265 | ␉ret = malloc ((*dw * *dh * *bitsPerPixel)/ 8);␊ |
266 | ␊ |
267 | ␉switch(*bitsPerPixel)␊ |
268 | ␉{␊ |
269 | ␉␉case 32:␊ |
270 | ␉␉␉PreviewDecompress32((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
271 | ␉␉␉return ret;␊ |
272 | ␉␉case 16:␊ |
273 | ␉␉␉PreviewDecompress16((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
274 | ␉␉␉return ret;␊ |
275 | ␉␉default:␊ |
276 | ␉␉␉return 0;␊ |
277 | ␉}␊ |
278 | }␊ |
279 | |