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