1 | /*␊ |
2 | * Copyright (c) 1995-2002 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * ␊ |
5 | * The contents of this file constitute Original Code as defined in and␊ |
6 | * are subject to the Apple Public Source License Version 1.1 (the␊ |
7 | * "License"). You may not use this file except in compliance with the␊ |
8 | * License. Please obtain a copy of the License at␊ |
9 | * http://www.apple.com/publicsource and read it before using this file.␊ |
10 | * ␊ |
11 | * This Original Code and all software distributed under the License are␊ |
12 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
13 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
14 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
15 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
16 | * License for the specific language governing rights and limitations␊ |
17 | * under the License.␊ |
18 | *␊ |
19 | */␊ |
20 | ␊ |
21 | #include "libsa.h"␊ |
22 | ␊ |
23 | static void ␊ |
24 | PreviewDecompress16(uint32_t * compressBuffer, ␊ |
25 | uint32_t width, uint32_t height, uint32_t row, ␊ |
26 | uint16_t * output)␊ |
27 | {␊ |
28 | int i, j;␊ |
29 | uint32_t * input;␊ |
30 | ␊ |
31 | uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
32 | uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
33 | uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
34 | uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
35 | uint32_t sr0, sr1, sr2, sr3;␊ |
36 | ␊ |
37 | bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
38 | bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
39 | bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
40 | bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
41 | ␊ |
42 | uint32_t tmp1, tmp2, out;␊ |
43 | for (j = 0; j < (height + 2); j++)␊ |
44 | {␊ |
45 | input = compressBuffer;␊ |
46 | if (j < height)␊ |
47 | input += j;␊ |
48 | else␊ |
49 | input += height - 1;␊ |
50 | input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
51 | ␊ |
52 | uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
53 | sr0 = sr1 = sr2 = sr3 = 0;␊ |
54 | ␊ |
55 | for (i = 0; i < (width + 2); i++)␊ |
56 | {␊ |
57 | if (i < width)␊ |
58 | {␊ |
59 | if (!count)␊ |
60 | {␊ |
61 | count = *input++;␊ |
62 | repeat = (count & 0xff000000);␊ |
63 | count ^= repeat;␊ |
64 | fetch = true;␊ |
65 | }␊ |
66 | else␊ |
67 | fetch = (0 == repeat);␊ |
68 | ␊ |
69 | count--;␊ |
70 | ␊ |
71 | if (fetch)␊ |
72 | {␊ |
73 | ␉␉␉␉␉data = *((uint16_t *)input);␊ |
74 | (*((uint16_t *)input))++;␊ |
75 | ␊ |
76 | // grayscale␊ |
77 | // srgb 13933, 46871, 4732␊ |
78 | // ntsc 19595, 38470, 7471␊ |
79 | data = 13933 * (0x1f & (data >> 10))␊ |
80 | + 46871 * (0x1f & (data >> 5))␊ |
81 | + 4732 * (0x1f & data);␊ |
82 | data >>= 13;␊ |
83 | ␊ |
84 | // 70% white, 30 % black␊ |
85 | data *= 19661;␊ |
86 | data += (103 << 16);␊ |
87 | data >>= 16;␊ |
88 | }␊ |
89 | }␊ |
90 | ␊ |
91 | // gauss blur␊ |
92 | tmp2 = sr0 + data;␊ |
93 | sr0 = data;␊ |
94 | tmp1 = sr1 + tmp2;␊ |
95 | sr1 = tmp2;␊ |
96 | tmp2 = sr2 + tmp1;␊ |
97 | sr2 = tmp1;␊ |
98 | tmp1 = sr3 + tmp2;␊ |
99 | sr3 = tmp2;␊ |
100 | ␊ |
101 | tmp2 = sc0[i] + tmp1;␊ |
102 | sc0[i] = tmp1;␊ |
103 | tmp1 = sc1[i] + tmp2;␊ |
104 | sc1[i] = tmp2;␊ |
105 | tmp2 = sc2[i] + tmp1;␊ |
106 | sc2[i] = tmp1;␊ |
107 | out = (128 + sc3[i] + tmp2) >> 11;␊ |
108 | sc3[i] = tmp2;␊ |
109 | ␊ |
110 | out &= 0x1f;␊ |
111 | if ((i > 1) && (j > 1))␊ |
112 | output[i-2] = out | (out << 5) | (out << 10);␊ |
113 | }␊ |
114 | ␊ |
115 | if (j > 1)␊ |
116 | output += row;␊ |
117 | }␊ |
118 | free(sc3);␊ |
119 | free(sc2);␊ |
120 | free(sc1);␊ |
121 | free(sc0);␊ |
122 | }␊ |
123 | ␊ |
124 | static void ␊ |
125 | PreviewDecompress32(uint32_t * compressBuffer, ␊ |
126 | uint32_t width, uint32_t height, uint32_t row, ␊ |
127 | uint32_t * output)␊ |
128 | {␊ |
129 | int i, j;␊ |
130 | uint32_t * input;␊ |
131 | ␊ |
132 | uint16_t * sc0 = malloc((width+2) * sizeof(uint16_t));␊ |
133 | uint16_t * sc1 = malloc((width+2) * sizeof(uint16_t));␊ |
134 | uint16_t * sc2 = malloc((width+2) * sizeof(uint16_t));␊ |
135 | uint16_t * sc3 = malloc((width+2) * sizeof(uint16_t));␊ |
136 | uint32_t sr0, sr1, sr2, sr3;␊ |
137 | ␊ |
138 | bzero(sc0, (width+2) * sizeof(uint16_t));␊ |
139 | bzero(sc1, (width+2) * sizeof(uint16_t));␊ |
140 | bzero(sc2, (width+2) * sizeof(uint16_t));␊ |
141 | bzero(sc3, (width+2) * sizeof(uint16_t));␊ |
142 | ␊ |
143 | uint32_t tmp1, tmp2, out;␊ |
144 | for (j = 0; j < (height + 2); j++)␊ |
145 | {␊ |
146 | input = compressBuffer;␊ |
147 | if (j < height)␊ |
148 | input += j;␊ |
149 | else␊ |
150 | input += height - 1;␊ |
151 | input = (uint32_t *)(input[3] + ((uint8_t *)compressBuffer));␊ |
152 | ␊ |
153 | uint32_t data = 0, repeat = 0, fetch = 0, count = 0;␊ |
154 | sr0 = sr1 = sr2 = sr3 = 0;␊ |
155 | ␊ |
156 | for (i = 0; i < (width + 2); i++)␊ |
157 | {␊ |
158 | if (i < width)␊ |
159 | {␊ |
160 | if (!count)␊ |
161 | {␊ |
162 | count = *input++;␊ |
163 | repeat = (count & 0xff000000);␊ |
164 | count ^= repeat;␊ |
165 | fetch = true;␊ |
166 | }␊ |
167 | else␊ |
168 | fetch = (0 == repeat);␊ |
169 | ␊ |
170 | count--;␊ |
171 | ␊ |
172 | if (fetch)␊ |
173 | {␊ |
174 | data = *input++;␊ |
175 | ␊ |
176 | // grayscale␊ |
177 | // srgb 13933, 46871, 4732␊ |
178 | // ntsc 19595, 38470, 7471␊ |
179 | data = 13933 * (0xff & (data >> 24))␊ |
180 | + 46871 * (0xff & (data >> 16))␊ |
181 | + 4732 * (0xff & data);␊ |
182 | data >>= 16;␊ |
183 | ␊ |
184 | // 70% white, 30 % black␊ |
185 | data *= 19661;␊ |
186 | data += (103 << 16);␊ |
187 | data >>= 16;␊ |
188 | }␊ |
189 | }␊ |
190 | ␊ |
191 | // gauss blur␊ |
192 | tmp2 = sr0 + data;␊ |
193 | sr0 = data;␊ |
194 | tmp1 = sr1 + tmp2;␊ |
195 | sr1 = tmp2;␊ |
196 | tmp2 = sr2 + tmp1;␊ |
197 | sr2 = tmp1;␊ |
198 | tmp1 = sr3 + tmp2;␊ |
199 | sr3 = tmp2;␊ |
200 | ␊ |
201 | tmp2 = sc0[i] + tmp1;␊ |
202 | sc0[i] = tmp1;␊ |
203 | tmp1 = sc1[i] + tmp2;␊ |
204 | sc1[i] = tmp2;␊ |
205 | tmp2 = sc2[i] + tmp1;␊ |
206 | sc2[i] = tmp1;␊ |
207 | out = (128 + sc3[i] + tmp2) >> 8;␊ |
208 | sc3[i] = tmp2;␊ |
209 | ␊ |
210 | out &= 0xff;␊ |
211 | if ((i > 1) && (j > 1))␊ |
212 | output[i-2] = out | (out << 8) | (out << 16);␊ |
213 | }␊ |
214 | ␊ |
215 | if (j > 1)␊ |
216 | output += row;␊ |
217 | }␊ |
218 | ␊ |
219 | free(sc3);␊ |
220 | free(sc2);␊ |
221 | free(sc1);␊ |
222 | free(sc0);␊ |
223 | }␊ |
224 | ␊ |
225 | void *␊ |
226 | DecompressData(void *srcbase, int *dw, int *dh, int *bitsPerPixel)␊ |
227 | {␊ |
228 | uint32_t * src = (uint32_t *) srcbase;␊ |
229 | ␉void * ret;␊ |
230 | ␊ |
231 | *bitsPerPixel = 8 * ((int) src[0]);␊ |
232 | ␉*dw = (int) src[1];␊ |
233 | ␉*dh = (int) src[2];␊ |
234 | ␉␊ |
235 | ␉ret = malloc ((*dw * *dh * *bitsPerPixel)/ 8);␊ |
236 | ␊ |
237 | switch(*bitsPerPixel)␊ |
238 | {␊ |
239 | case 32:␊ |
240 | PreviewDecompress32((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
241 | return ret;␊ |
242 | case 16:␊ |
243 | PreviewDecompress16((uint32_t *)srcbase, *dw, *dh, *dw, ret);␊ |
244 | return ret;␊ |
245 | default:␊ |
246 | return 0;␊ |
247 | }␊ |
248 | }␊ |
249 | |