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