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