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