]>
git.saurik.com Git - apple/boot.git/blob - i386/util/tif_packbits.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 static char rcsid
[] = "$Header: /cvs/Darwin/System/boot/i386/util/tif_packbits.c,v 1.1.1.2 1999/08/04 21:17:19 wsanchez Exp $";
29 * Copyright (c) 1988, 1989, 1990, 1991 Sam Leffler
30 * Copyright (c) 1991 Silicon Graphics, Inc.
32 * Permission to use, copy, modify, distribute, and sell this software and
33 * its documentation for any purpose is hereby granted without fee, provided
34 * that (i) the above copyright notices and this permission notice appear in
35 * all copies of the software and related documentation, and (ii) the names of
36 * Sam Leffler and Silicon Graphics may not be used in any advertising or
37 * publicity relating to the software without the specific, prior written
38 * permission of Sam Leffler and Silicon Graphics.
40 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
41 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
42 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
44 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
45 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
46 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
47 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
48 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
55 * PackBits Compression Algorithm Support
59 typedef unsigned int u_int
;
60 typedef unsigned char u_char
;
63 TIFFAppendToStrip(tif
, strip
, data
, cc
)
74 * Encode a scanline of pixels.
77 PackBitsEncode(tif
, bp
, cc
, s
)
83 register char *op
, *lastliteral
;
85 enum { BASE
, LITERAL
, RUN
, LITERAL_RUN
} state
;
90 ep
= tif
->tif_rawdata
+ tif
->tif_rawdatasize
;
95 * Find the longest string of identical bytes.
97 b
= *bp
++, cc
--, n
= 1;
98 for (; cc
> 0 && b
== *bp
; cc
--, bp
++)
101 if (op
+ 2 >= ep
) { /* insure space for new data */
103 * Be careful about writing the last
104 * literal. Must write up to that point
105 * and then copy the remainder to the
106 * front of the buffer.
108 if (state
== LITERAL
|| state
== LITERAL_RUN
) {
109 slop
= op
- lastliteral
;
110 tif
->tif_rawcc
+= lastliteral
- tif
->tif_rawcp
;
114 for (; slop
-- > 0; *op
++ = *lastliteral
++)
116 lastliteral
= tif
->tif_rawcp
;
118 tif
->tif_rawcc
+= op
- tif
->tif_rawcp
;
125 case BASE
: /* initial state, set run/literal */
143 case LITERAL
: /* last object was literal string */
152 *op
++ = -(n
-1); /* encode run */
154 } else { /* extend literal */
155 if (++(*lastliteral
) == 127)
160 case RUN
: /* last object was run */
177 case LITERAL_RUN
: /* literal followed by a run */
179 * Check to see if previous run should
180 * be converted to a literal, in which
181 * case we convert literal-run-literal
182 * to a single literal.
184 if (n
== 1 && op
[-2] == (char)-1 &&
185 *lastliteral
< 126) {
186 state
= (((*lastliteral
) += 2) == 127 ?
188 op
[-2] = op
[-1]; /* replicate */
194 tif
->tif_rawcc
+= op
- tif
->tif_rawcp
;