]> git.saurik.com Git - apple/boot.git/blob - i386/util/tif_packbits.c
820f13b627b817c1cc8d8dfac9f3f44da94f13ce
[apple/boot.git] / i386 / util / tif_packbits.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #ifndef lint
26 static char rcsid[] = "$Header: /cvs/root/boot/i386/util/tif_packbits.c,v 1.1.1.2 1999/08/04 21:17:19 wsanchez Exp $";
27 #endif
28
29 /*
30 * Copyright (c) 1988, 1989, 1990, 1991 Sam Leffler
31 * Copyright (c) 1991 Silicon Graphics, Inc.
32 *
33 * Permission to use, copy, modify, distribute, and sell this software and
34 * its documentation for any purpose is hereby granted without fee, provided
35 * that (i) the above copyright notices and this permission notice appear in
36 * all copies of the software and related documentation, and (ii) the names of
37 * Sam Leffler and Silicon Graphics may not be used in any advertising or
38 * publicity relating to the software without the specific, prior written
39 * permission of Sam Leffler and Silicon Graphics.
40 *
41 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
42 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
43 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
44 *
45 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
46 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
47 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
48 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
49 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
50 * OF THIS SOFTWARE.
51 */
52
53 /*
54 * TIFF Library.
55 *
56 * PackBits Compression Algorithm Support
57 */
58 #import "bitmap.h"
59
60 typedef unsigned int u_int;
61 typedef unsigned char u_char;
62
63 static
64 TIFFAppendToStrip(tif, strip, data, cc)
65 TIFF *tif;
66 u_int strip;
67 u_char *data;
68 u_int cc;
69 {
70 }
71
72
73
74 /*
75 * Encode a scanline of pixels.
76 */
77 int
78 PackBitsEncode(tif, bp, cc, s)
79 TIFF *tif;
80 u_char *bp;
81 register int cc;
82 u_int s;
83 {
84 register char *op, *lastliteral;
85 register int n, b;
86 enum { BASE, LITERAL, RUN, LITERAL_RUN } state;
87 char *ep;
88 int slop;
89
90 op = tif->tif_rawcp;
91 ep = tif->tif_rawdata + tif->tif_rawdatasize;
92 state = BASE;
93 lastliteral = 0;
94 while (cc > 0) {
95 /*
96 * Find the longest string of identical bytes.
97 */
98 b = *bp++, cc--, n = 1;
99 for (; cc > 0 && b == *bp; cc--, bp++)
100 n++;
101 again:
102 if (op + 2 >= ep) { /* insure space for new data */
103 /*
104 * Be careful about writing the last
105 * literal. Must write up to that point
106 * and then copy the remainder to the
107 * front of the buffer.
108 */
109 if (state == LITERAL || state == LITERAL_RUN) {
110 slop = op - lastliteral;
111 tif->tif_rawcc += lastliteral - tif->tif_rawcp;
112 // no space left
113 return (-1);
114 op = tif->tif_rawcp;
115 for (; slop-- > 0; *op++ = *lastliteral++)
116 ;
117 lastliteral = tif->tif_rawcp;
118 } else {
119 tif->tif_rawcc += op - tif->tif_rawcp;
120 // no space left
121 return (-1);
122 op = tif->tif_rawcp;
123 }
124 }
125 switch (state) {
126 case BASE: /* initial state, set run/literal */
127 if (n > 1) {
128 state = RUN;
129 if (n > 128) {
130 *op++ = -127;
131 *op++ = b;
132 n -= 128;
133 goto again;
134 }
135 *op++ = -(n-1);
136 *op++ = b;
137 } else {
138 lastliteral = op;
139 *op++ = 0;
140 *op++ = b;
141 state = LITERAL;
142 }
143 break;
144 case LITERAL: /* last object was literal string */
145 if (n > 1) {
146 state = LITERAL_RUN;
147 if (n > 128) {
148 *op++ = -127;
149 *op++ = b;
150 n -= 128;
151 goto again;
152 }
153 *op++ = -(n-1); /* encode run */
154 *op++ = b;
155 } else { /* extend literal */
156 if (++(*lastliteral) == 127)
157 state = BASE;
158 *op++ = b;
159 }
160 break;
161 case RUN: /* last object was run */
162 if (n > 1) {
163 if (n > 128) {
164 *op++ = -127;
165 *op++ = b;
166 n -= 128;
167 goto again;
168 }
169 *op++ = -(n-1);
170 *op++ = b;
171 } else {
172 lastliteral = op;
173 *op++ = 0;
174 *op++ = b;
175 state = LITERAL;
176 }
177 break;
178 case LITERAL_RUN: /* literal followed by a run */
179 /*
180 * Check to see if previous run should
181 * be converted to a literal, in which
182 * case we convert literal-run-literal
183 * to a single literal.
184 */
185 if (n == 1 && op[-2] == (char)-1 &&
186 *lastliteral < 126) {
187 state = (((*lastliteral) += 2) == 127 ?
188 BASE : LITERAL);
189 op[-2] = op[-1]; /* replicate */
190 } else
191 state = RUN;
192 goto again;
193 }
194 }
195 tif->tif_rawcc += op - tif->tif_rawcp;
196 tif->tif_rawcp = op;
197 return (1);
198 }
199