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