]> git.saurik.com Git - apple/boot.git/blob - gen/libsaio/unpackbits.c
boot-132.tar.gz
[apple/boot.git] / gen / libsaio / unpackbits.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1988, 1989, 1990, 1991 Sam Leffler
26 * Copyright (c) 1991 Silicon Graphics, Inc.
27 *
28 * Permission to use, copy, modify, distribute, and sell this software and
29 * its documentation for any purpose is hereby granted without fee, provided
30 * that (i) the above copyright notices and this permission notice appear in
31 * all copies of the software and related documentation, and (ii) the names of
32 * Sam Leffler and Silicon Graphics may not be used in any advertising or
33 * publicity relating to the software without the specific, prior written
34 * permission of Sam Leffler and Silicon Graphics.
35 *
36 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
37 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
38 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
39 *
40 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
41 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
42 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
44 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
45 * OF THIS SOFTWARE.
46 */
47
48 /*
49 * TIFF Library.
50 *
51 * PackBits Compression Algorithm Support
52 */
53 #import "bitmap.h"
54 #import "libsa.h"
55
56 typedef unsigned int u_int;
57 typedef unsigned char u_char;
58
59 int
60 PackBitsDecode(tif, op, occ, s)
61 TIFF *tif;
62 register u_char *op;
63 register int occ;
64 u_int s;
65 {
66 register char *bp;
67 register int n, b;
68 register int cc;
69
70 bp = tif->tif_rawcp; cc = tif->tif_rawcc;
71 while (cc > 0 && occ > 0) {
72 n = (int) *bp++; cc--;
73 /*
74 * Watch out for compilers that
75 * don't sign extend chars...
76 */
77 if (n >= 128)
78 n -= 256;
79 if (n < 0) { /* replicate next byte -n+1 times */
80 cc--;
81 if (n == -128) /* nop */
82 continue;
83 n = -n + 1;
84 occ -= n;
85 for (b = *bp++; n-- > 0;)
86 *op++ = b;
87 } else { /* copy next n+1 bytes literally */
88 bcopy(bp, op, ++n);
89 op += n; occ -= n;
90 bp += n; cc -= n;
91 }
92 }
93 tif->tif_rawcp = bp;
94 tif->tif_rawcc = cc;
95 if (occ > 0) {
96 // TIFFError(tif->tif_name,
97 // "PackBitsDecode: Not enough data for scanline %d",
98 // tif->tif_row);
99 // reallyPrint("Not enough data\n");
100 return (0);
101 }
102 /* check for buffer overruns? */
103 return (1);
104 }