Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 1059