1 /* Copyright (C) 1994, 2000 artofcode LLC.  All rights reserved.
2 
3   This program is free software; you can redistribute it and/or modify it
4   under the terms of the GNU General Public License as published by the
5   Free Software Foundation; either version 2 of the License, or (at your
6   option) any later version.
7 
8   This program is distributed in the hope that it will be useful, but
9   WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   General Public License for more details.
12 
13   You should have received a copy of the GNU General Public License along
14   with this program; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA, 02111-1307.
16 
17 */
18 
19 /*$Id: gdevtifs.h,v 1.3.6.1.2.1 2003/01/17 00:49:01 giles Exp $ */
20 /* Definitions for writing TIFF file formats. */
21 
22 #ifndef gdevtifs_INCLUDED
23 #  define gdevtifs_INCLUDED
24 
25 /* ================ TIFF specification ================ */
26 
27 /* Based on TIFF specification version 6.0 obtained from */
28 /* sgi.com:graphics/tiff/TIFF6.ps.Z. */
29 
30 /*
31  * The sizes of TIFF data types are system-independent.  Therefore,
32  * we cannot use short, long, etc., but must use types of known sizes.
33  */
34 #if arch_sizeof_short == 2
35 typedef short TIFF_short;	/* no plausible alternative */
36 typedef unsigned short TIFF_ushort;
37 
38 #endif
39 #if arch_sizeof_int == 4
40 typedef int TIFF_long;
41 typedef unsigned int TIFF_ulong;
42 
43 #else
44 # if arch_sizeof_long == 4
45 typedef long TIFF_long;
46 typedef unsigned long TIFF_ulong;
47 
48 # endif
49 #endif
50 
51 /*
52  * Define the TIFF file header.
53  */
54 typedef struct TIFF_header_s {
55     TIFF_ushort magic;		/* magic number (defines byte order) */
56     TIFF_ushort version;	/* TIFF version number */
57     TIFF_ulong diroff;		/* byte offset to first directory */
58 } TIFF_header;
59 
60 #define	TIFF_magic_big_endian		0x4d4d	/* 'MM' */
61 #define	TIFF_magic_little_endian	0x4949	/* 'II' */
62 
63 #define	TIFF_version_value	42
64 
65 /*
66  * Define an individual entry in a TIFF directory.  Within a directory,
67  * the entries must be sorted by increasing tag value.
68  *
69  * The value field contains either the offset of the field data in the file,
70  * or, if the value fits in 32 bits, the value itself, left-justified.
71  * Field data may appear anywhere in the file, so long as each data block is
72  * aligned on a 32-bit boundary and is disjoint from all other data blocks.
73  */
74 typedef struct TIFF_dir_entry_s {
75     TIFF_ushort tag;		/* TIFF_tag */
76     TIFF_ushort type;		/* TIFF_data_type */
77     TIFF_ulong count;		/* number of items (spec calls this 'length') */
78     TIFF_ulong value;		/* byte offset to field data, */
79     /* or actual value if <=4 bytes */
80 } TIFF_dir_entry;
81 
82 /*
83  * Define the tag data type values.
84  */
85 typedef enum {
86     TIFF_BYTE = 1,		/* 8-bit unsigned integer */
87     TIFF_ASCII = 2,		/* 8-bit bytes with last byte null */
88     TIFF_SHORT = 3,		/* 16-bit unsigned integer */
89     TIFF_LONG = 4,		/* 32-bit unsigned integer */
90     TIFF_RATIONAL = 5,		/* 64-bit unsigned fraction */
91     /* (ratio of two 32-bit unsigned integers) */
92     TIFF_SBYTE = 6,		/* 8-bit signed integer */
93     TIFF_UNDEFINED = 7,		/* 8-bit untyped data */
94     TIFF_SSHORT = 8,		/* 16-bit signed integer */
95     TIFF_SLONG = 9,		/* 32-bit signed integer */
96     TIFF_SRATIONAL = 10,	/* 64-bit signed fraction */
97     /* (ratio of two 32-bit signed integers) */
98     TIFF_FLOAT = 11,		/* 32-bit IEEE floating point */
99     TIFF_DOUBLE = 12,		/* 64-bit IEEE floating point */
100     /* A flag to indicate the value is indirect. */
101     /* This is only used internally; it is not part of the */
102     /* TIFF specification (although it should be!). */
103     TIFF_INDIRECT = 128
104 } TIFF_data_type;
105 
106 /*
107  * Define the tag values we need.  Note that this is only a very small subset
108  * of all the values defined in the TIFF specification; we will add more
109  * as the need arises.
110  */
111 typedef enum {
112     TIFFTAG_SubFileType = 254,	/* subfile data descriptor */
113 #define	    SubFileType_reduced_image	0x1	/* reduced resolution version */
114 #define	    SubFileType_page		0x2	/* one page of many */
115 #define	    SubFileType_mask		0x4	/* transparency mask */
116     TIFFTAG_ImageWidth = 256,	/* image width in pixels */
117     TIFFTAG_ImageLength = 257,	/* image height in pixels */
118     TIFFTAG_BitsPerSample = 258,	/* bits per channel (sample) */
119     TIFFTAG_Compression = 259,	/* data compression technique */
120 #define	    Compression_none		1	/* dump mode */
121 #define	    Compression_CCITT_RLE	2	/* CCITT modified Huffman RLE */
122 #define	    Compression_CCITT_T4	3	/* CCITT T.4 fax encoding */
123 #define	    Compression_CCITT_T6	4	/* CCITT T.6 fax encoding */
124 #define	    Compression_LZW		5	/* Lempel-Ziv  & Welch */
125 #define	    Compression_JPEG		6	/* !JPEG compression */
126 #define	    Compression_NeXT		32766	/* NeXT 2-bit RLE */
127 #define	    Compression_CCITT_RLEW	32771	/* #1 w/ word alignment */
128 #define	    Compression_PackBits	32773	/* Macintosh RLE */
129 #define	    Compression_Thunderscan	32809	/* ThunderScan RLE */
130     TIFFTAG_Photometric = 262,	/* photometric interpretation */
131 #define	    Photometric_min_is_white	0	/* min value is white */
132 #define	    Photometric_min_is_black	1	/* min value is black */
133 #define	    Photometric_RGB		2	/* RGB color model */
134 #define	    Photometric_palette		3	/* color map indexed */
135 #define	    Photometric_mask		4	/* $holdout mask */
136 #define	    Photometric_separated	5	/* !color separations */
137 #define	    Photometric_YCbCr		6	/* !CCIR 601 */
138 #define	    Photometric_CIE_Lab		8	/* !1976 CIE L*a*b* */
139     TIFFTAG_FillOrder = 266,	/* data order within a byte */
140 #define	    FillOrder_MSB2LSB		1	/* most significant -> least */
141 #define	    FillOrder_LSB2MSB		2	/* least significant -> most */
142     TIFFTAG_StripOffsets = 273,	/* offsets to data strips */
143     TIFFTAG_Orientation = 274,	/* +image Orientation */
144 #define	    Orientation_top_left	1	/* row 0 top, col 0 lhs */
145 #define	    Orientation_top_right	2	/* row 0 top, col 0 rhs */
146 #define	    Orientation_bot_right	3	/* row 0 bottom, col 0 rhs */
147 #define	    Orientation_bot_left	4	/* row 0 bottom, col 0 lhs */
148 #define	    Orientation_left_top	5	/* row 0 lhs, col 0 top */
149 #define	    Orientation_right_top	6	/* row 0 rhs, col 0 top */
150 #define	    Orientation_right_bot	7	/* row 0 rhs, col 0 bottom */
151 #define	    Orientation_left_bot	8	/* row 0 lhs, col 0 bottom */
152     TIFFTAG_SamplesPerPixel = 277,	/* samples per pixel */
153     TIFFTAG_RowsPerStrip = 278,	/* rows per strip of data */
154     TIFFTAG_StripByteCounts = 279,	/* bytes counts for strips */
155     TIFFTAG_XResolution = 282,	/* pixels/resolution in x */
156     TIFFTAG_YResolution = 283,	/* pixels/resolution in y */
157     TIFFTAG_PlanarConfig = 284,	/* storage organization */
158 #define	    PlanarConfig_contig		1	/* single image plane */
159 #define	    PlanarConfig_separate	2	/* separate planes of data */
160     TIFFTAG_T4Options = 292,	/* 32 flag bits */
161 #define	    T4Options_2D_encoding	0x1	/* 2-dimensional coding */
162 #define	    T4Options_uncompressed	0x2	/* data not compressed */
163 #define	    T4Options_fill_bits		0x4	/* fill to byte boundary */
164     TIFFTAG_T6Options = 293,	/* 32 flag bits */
165 #define	    T6Options_uncompressed	0x2	/* data not compressed */
166     TIFFTAG_ResolutionUnit = 296,	/* units of resolutions */
167 #define	    ResolutionUnit_none		1	/* no meaningful units */
168 #define	    ResolutionUnit_inch		2	/* english */
169 #define	    ResolutionUnit_centimeter	3	/* metric */
170     TIFFTAG_PageNumber = 297,	/* page number if multi-page */
171     TIFFTAG_Software = 305,	/* software name & release */
172     TIFFTAG_DateTime = 306,	/* creation date and time */
173 /*
174  * The CleanFaxData tag isn't in the TIFF 6 documentation, and many
175  * TIFF-reading applications don't recognize it.  Don't use it!
176  */
177     TIFFTAG_CleanFaxData = 327	/* regenerated line info */
178 #define	    CleanFaxData_clean		0	/* no errors detected */
179 #define	    CleanFaxData_regenerated	1	/* receiver regenerated lines */
180 #define	    CleanFaxData_unclean	2	/* uncorrected errors exist */
181 } TIFF_tag;
182 
183 /* ================ Implementation ================ */
184 
185 /*
186  * Define the added driver state for TIFF writing.  Note that we provide
187  * no GC descriptor, so this structure must exist only on the stack,
188  * never in allocated storage.
189  */
190 typedef struct gdev_tiff_state_s {
191     gs_memory_t *mem;
192     long prev_dir;		/* file offset of previous directory offset */
193     long dir_off;		/* file offset of next write */
194     int ntags;			/* # of tags in directory */
195     long strip_index;		/* current strip being output, 0 = first */
196     long strip_count;
197     long rows;
198     /* Record offsets of values - these may be indirect if more than one strip */
199     int offset_StripOffsets;
200     int offset_StripByteCounts;
201     TIFF_ulong *StripOffsets;
202     TIFF_ulong *StripByteCounts;
203 } gdev_tiff_state;
204 
205 /*
206  * Begin writing a TIFF page.  This procedure supplies a standard set of
207  * tags; the client can provide additional tags (pre-sorted) and
208  * indirect values.
209  */
210 int gdev_tiff_begin_page(P8(gx_device_printer * pdev, gdev_tiff_state * tifs,
211 			    FILE * fp,
212 			    const TIFF_dir_entry * entries, int entry_count,
213 			    const byte * values, int value_size,
214 			    long max_strip_size));
215 
216 /*
217  * Finish writing a TIFF strip.  All data written since begin or last
218  * end_strip is considered to be a single strip.
219  */
220 int gdev_tiff_end_strip(P2(gdev_tiff_state * tifs, FILE * fp));
221 
222 /*
223  * Finish writing a TIFF page.  StripOffsets and StripByteCounts are
224  * patched into the file.
225  */
226 int gdev_tiff_end_page(P2(gdev_tiff_state * tifs, FILE * fp));
227 
228 #endif /* gdevtifs_INCLUDED */
229