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