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