1 /* PDFlib GmbH cvsid:
2  * $Id: tif_dumpmode.c,v 1.11 2005/12/21 13:22:05 rjs Exp $ */
3 
4 /*
5  * Copyright (c) 1988-1997 Sam Leffler
6  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and
9  * its documentation for any purpose is hereby granted without fee, provided
10  * that (i) the above copyright notices and this permission notice appear in
11  * all copies of the software and related documentation, and (ii) the names of
12  * Sam Leffler and Silicon Graphics may not be used in any advertising or
13  * publicity relating to the software without the specific, prior written
14  * permission of Sam Leffler and Silicon Graphics.
15  *
16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
21  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25  * OF THIS SOFTWARE.
26  */
27 
28 /*
29  * TIFF Library.
30  *
31  * "Null" Compression Algorithm Support.
32  */
33 #include "tiffiop.h"
34 
35 /*
36  * Encode a hunk of pixels.
37  */
38 #ifdef PDFLIB_TIFFWRITE_SUPPORT
39 static int
DumpModeEncode(TIFF * tif,tidata_t pp,tsize_t cc,tsample_t s)40 DumpModeEncode(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s)
41 {
42 	(void) s;
43 	while (cc > 0) {
44 		tsize_t n;
45 
46 		n = cc;
47 		if (tif->tif_rawcc + n > tif->tif_rawdatasize)
48 			n = tif->tif_rawdatasize - tif->tif_rawcc;
49 
50                 assert( n > 0 );
51 
52 		/*
53 		 * Avoid copy if client has setup raw
54 		 * data buffer to avoid extra copy.
55 		 */
56 		if (tif->tif_rawcp != pp)
57 			_TIFFmemcpy(tif->tif_rawcp, pp, n);
58 		tif->tif_rawcp += n;
59 		tif->tif_rawcc += n;
60 		pp += n;
61 		cc -= n;
62 		if (tif->tif_rawcc >= tif->tif_rawdatasize &&
63 		    !TIFFFlushData1(tif))
64 			return (-1);
65 	}
66 	return (1);
67 }
68 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
69 
70 /*
71  * Decode a hunk of pixels.
72  */
73 static int
DumpModeDecode(TIFF * tif,tidata_t buf,tsize_t cc,tsample_t s)74 DumpModeDecode(TIFF* tif, tidata_t buf, tsize_t cc, tsample_t s)
75 {
76 	(void) s;
77 	if (tif->tif_rawcc < cc) {
78 		_TIFFError(tif, tif->tif_name,
79 		    "DumpModeDecode: Not enough data for scanline %d",
80 		    tif->tif_row);
81 		return (0);
82 	}
83 	/*
84 	 * Avoid copy if client has setup raw
85 	 * data buffer to avoid extra copy.
86 	 */
87 	if (tif->tif_rawcp != buf)
88 		_TIFFmemcpy(buf, tif->tif_rawcp, cc);
89 	tif->tif_rawcp += cc;
90 	tif->tif_rawcc -= cc;
91 	return (1);
92 }
93 
94 /*
95  * Seek forwards nrows in the current strip.
96  */
97 static int
DumpModeSeek(TIFF * tif,uint32 nrows)98 DumpModeSeek(TIFF* tif, uint32 nrows)
99 {
100 	tif->tif_rawcp += nrows * tif->tif_scanlinesize;
101 	tif->tif_rawcc -= nrows * tif->tif_scanlinesize;
102 	return (1);
103 }
104 
105 /*
106  * Initialize dump mode.
107  */
108 int
TIFFInitDumpMode(TIFF * tif,int scheme)109 TIFFInitDumpMode(TIFF* tif, int scheme)
110 {
111 	(void) scheme;
112 	tif->tif_decoderow = DumpModeDecode;
113 	tif->tif_decodestrip = DumpModeDecode;
114 	tif->tif_decodetile = DumpModeDecode;
115 #ifdef PDFLIB_TIFFWRITE_SUPPORT
116 	tif->tif_encoderow = DumpModeEncode;
117 	tif->tif_encodestrip = DumpModeEncode;
118 	tif->tif_encodetile = DumpModeEncode;
119 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
120 	tif->tif_seek = DumpModeSeek;
121 	return (1);
122 }
123