1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_fax4.c,v 1.25 1994/09/17 23:22:37 sam Exp $ */
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
5  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26 
27 /*
28  * TIFF Library.
29  *
30  * CCITT Group 4 Facsimile-compatible
31  * Compression Scheme Support.
32  */
33 #include "tiffiop.h"
34 #include "tif_fax3.h"
35 #include "t4.h"
36 
37 /*
38  * Decode the requested amount of data.
39  */
40 static int
Fax4Decode(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)41 Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
42 {
43 	Fax3BaseState *sp = (Fax3BaseState *)tif->tif_data;
44 	int status;
45 
46 	_TIFFmemset(buf, 0, occ);	/* decoding only sets non-zero bits */
47 	while ((long)occ > 0) {
48 		status = Fax3Decode2DRow(tif, buf, sp->rowpixels);
49 		if (status < 0)
50 			return (status == G3CODE_EOF);
51 		_TIFFmemcpy(sp->refline, buf, sp->rowbytes);
52 		buf += sp->rowbytes;
53 		occ -= sp->rowbytes;
54 		if (occ != 0)
55 			tif->tif_row++;
56 	}
57 	return (1);
58 }
59 
60 /*
61  * Encode the requested amount of data.
62  */
63 static int
Fax4Encode(TIFF * tif,tidata_t bp,tsize_t cc,tsample_t s)64 Fax4Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
65 {
66 	Fax3BaseState *sp = (Fax3BaseState *)tif->tif_data;
67 
68 	while ((long)cc > 0) {
69 		if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->rowpixels))
70 			return (0);
71 		_TIFFmemcpy(sp->refline, bp, sp->rowbytes);
72 		bp += sp->rowbytes;
73 		cc -= sp->rowbytes;
74 		if (cc != 0)
75 			tif->tif_row++;
76 	}
77 	return (1);
78 }
79 
80 static
Fax4PostEncode(TIFF * tif)81 Fax4PostEncode(TIFF* tif)
82 {
83 	Fax3BaseState *sp = (Fax3BaseState *)tif->tif_data;
84 
85 	/* terminate strip w/ EOFB */
86 	Fax3PutBits(tif, EOL, 12);
87 	Fax3PutBits(tif, EOL, 12);
88 	if (sp->bit != 8)
89 		Fax3FlushBits(tif, sp);
90 	return (1);
91 }
92 
93 int
TIFFInitCCITTFax4(TIFF * tif)94 TIFFInitCCITTFax4(TIFF* tif)
95 {
96 	TIFFInitCCITTFax3(tif);		/* reuse G3 compression */
97 	tif->tif_decoderow = Fax4Decode;
98 	tif->tif_decodestrip = Fax4Decode;
99 	tif->tif_decodetile = Fax4Decode;
100 	tif->tif_encoderow = Fax4Encode;
101 	tif->tif_encodestrip = Fax4Encode;
102 	tif->tif_encodetile = Fax4Encode;
103 	tif->tif_postencode = Fax4PostEncode;
104 	/*
105 	 * FAX3_NOEOL causes the regular G3 decompression
106 	 * code to not skip to the EOL mark at the end of
107 	 * a row (during normal decoding).  FAX3_CLASSF
108 	 * suppresses RTC generation at the end of an image.
109 	 */
110 	tif->tif_options = FAX3_NOEOL|FAX3_CLASSF;
111 	return (1);
112 }
113