Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/boot2/bmdecompress.c

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

Archive Download this file

Revision: 2457