Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/boot2/bmdecompress.c

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

Archive Download this file

Revision: 2347