1 /* $Id: short_tag.c,v 1.9 2013-12-17 14:41:58 bfriesen Exp $ */
2 
3 /*
4  * Copyright (c) 2004, Andrey Kiselev  <dron@ak4719.spb.edu>
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and
7  * its documentation for any purpose is hereby granted without fee, provided
8  * that (i) the above copyright notices and this permission notice appear in
9  * all copies of the software and related documentation, and (ii) the names of
10  * Sam Leffler and Silicon Graphics may not be used in any advertising or
11  * publicity relating to the software without the specific, prior written
12  * permission of Sam Leffler and Silicon Graphics.
13  *
14  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17  *
18  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23  * OF THIS SOFTWARE.
24  */
25 
26 /*
27  * TIFF Library
28  *
29  * Module to test SHORT tags read/write functions.
30  */
31 
32 #include "tif_config.h"
33 
34 #include <stdio.h>
35 
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 
40 #include "tiffio.h"
41 #include "tifftest.h"
42 
43 static const char filename[] = "short_test.tiff";
44 
45 #define	SPP	3		/* Samples per pixel */
46 const uint16	width = 1;
47 const uint16	length = 1;
48 const uint16	bps = 8;
49 const uint16	photometric = PHOTOMETRIC_RGB;
50 const uint16	rows_per_strip = 1;
51 const uint16	planarconfig = PLANARCONFIG_CONTIG;
52 
53 static const struct {
54 	const ttag_t	tag;
55 	const uint16	value;
56 } short_single_tags[] = {
57 	{ TIFFTAG_COMPRESSION, COMPRESSION_NONE },
58 	{ TIFFTAG_FILLORDER, FILLORDER_MSB2LSB },
59 	{ TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT },
60 	{ TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH },
61 	{ TIFFTAG_MINSAMPLEVALUE, 23 },
62 	{ TIFFTAG_MAXSAMPLEVALUE, 241 },
63 	{ TIFFTAG_INKSET, INKSET_MULTIINK },
64 	{ TIFFTAG_NUMBEROFINKS, SPP },
65 	{ TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT }
66 };
67 #define NSINGLETAGS   (sizeof(short_single_tags) / sizeof(short_single_tags[0]))
68 
69 static const struct {
70 	const ttag_t	tag;
71 	const uint16	values[2];
72 } short_paired_tags[] = {
73 	{ TIFFTAG_PAGENUMBER, {1, 1} },
74 	{ TIFFTAG_HALFTONEHINTS, {0, 255} },
75 	{ TIFFTAG_DOTRANGE, {8, 16} },
76 	{ TIFFTAG_YCBCRSUBSAMPLING, {2, 1} }
77 };
78 #define NPAIREDTAGS   (sizeof(short_paired_tags) / sizeof(short_paired_tags[0]))
79 
80 int
main()81 main()
82 {
83 	TIFF		*tif;
84 	size_t		i;
85 	unsigned char	buf[SPP] = { 0, 127, 255 };
86 
87 	/* Test whether we can write tags. */
88 	tif = TIFFOpen(filename, "w");
89 	if (!tif) {
90 		fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
91 		return 1;
92 	}
93 
94 	if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
95 		fprintf (stderr, "Can't set ImageWidth tag.\n");
96 		goto failure;
97 	}
98 	if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
99 		fprintf (stderr, "Can't set ImageLength tag.\n");
100 		goto failure;
101 	}
102 	if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
103 		fprintf (stderr, "Can't set BitsPerSample tag.\n");
104 		goto failure;
105 	}
106 	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP)) {
107 		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
108 		goto failure;
109 	}
110 	if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) {
111 		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
112 		goto failure;
113 	}
114 	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) {
115 		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
116 		goto failure;
117 	}
118 	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
119 		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
120 		goto failure;
121 	}
122 
123 	for (i = 0; i < NSINGLETAGS; i++) {
124 		if (!TIFFSetField(tif, short_single_tags[i].tag,
125 				  short_single_tags[i].value)) {
126 			fprintf(stderr, "Can't set tag %lu.\n",
127 				(unsigned long)short_single_tags[i].tag);
128 			goto failure;
129 		}
130 	}
131 
132 	for (i = 0; i < NPAIREDTAGS; i++) {
133 		if (!TIFFSetField(tif, short_paired_tags[i].tag,
134 				  short_paired_tags[i].values[0],
135 				  short_paired_tags[i].values[1])) {
136 			fprintf(stderr, "Can't set tag %lu.\n",
137 				(unsigned long)short_paired_tags[i].tag);
138 			goto failure;
139 		}
140 	}
141 
142 	/* Write dummy pixel data. */
143 	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
144 		fprintf (stderr, "Can't write image data.\n");
145 		goto failure;
146 	}
147 
148 	TIFFClose(tif);
149 
150 	/* Ok, now test whether we can read written values. */
151 	tif = TIFFOpen(filename, "r");
152 	if (!tif) {
153 		fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
154 		return 1;
155 	}
156 
157 	if (CheckLongField(tif, TIFFTAG_IMAGEWIDTH, width) < 0)
158 		goto failure;
159 
160 	if (CheckLongField(tif, TIFFTAG_IMAGELENGTH, length) < 0)
161 		goto failure;
162 
163 	if (CheckShortField(tif, TIFFTAG_BITSPERSAMPLE, bps) < 0)
164 		goto failure;
165 
166 	if (CheckShortField(tif, TIFFTAG_PHOTOMETRIC, photometric) < 0)
167 		goto failure;
168 
169 	if (CheckShortField(tif, TIFFTAG_SAMPLESPERPIXEL, SPP) < 0)
170 		goto failure;
171 
172 	if (CheckLongField(tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip) < 0)
173 		goto failure;
174 
175 	if (CheckShortField(tif, TIFFTAG_PLANARCONFIG, planarconfig) < 0)
176 		goto failure;
177 
178 	for (i = 0; i < NSINGLETAGS; i++) {
179 		if (CheckShortField(tif, short_single_tags[i].tag,
180 				    short_single_tags[i].value) < 0)
181 			goto failure;
182 	}
183 
184 	for (i = 0; i < NPAIREDTAGS; i++) {
185 		if (CheckShortPairedField(tif, short_paired_tags[i].tag,
186 					  short_paired_tags[i].values) < 0)
187 			goto failure;
188 	}
189 
190 	TIFFClose(tif);
191 
192 	/* All tests passed; delete file and exit with success status. */
193 	unlink(filename);
194 	return 0;
195 
196 failure:
197 	/*
198 	 * Something goes wrong; close file and return unsuccessful status.
199 	 * Do not remove the file for further manual investigation.
200 	 */
201 	TIFFClose(tif);
202 	return 1;
203 }
204 
205 /* vim: set ts=8 sts=8 sw=8 noet: */
206