xref: /libtiff-4.0.7/test/ascii_tag.c (revision 65f22eac)
1 /* $Id: ascii_tag.c,v 1.8 2013-12-17 14:41:57 bfriesen Exp $ */
2 
3 /*
4  * Copyright (c) 2004, Andrey Kiselev  <[email protected]>
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 ASCII tags read/write functions.
30  */
31 
32 #include "tif_config.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 
41 #include "tiffio.h"
42 
43 static const char filename[] = "ascii_test.tiff";
44 
45 static const struct {
46 	ttag_t		tag;
47 	const char	*value;
48 } ascii_tags[] = {
49 	{ TIFFTAG_DOCUMENTNAME, "Test TIFF image" },
50 	{ TIFFTAG_IMAGEDESCRIPTION, "Temporary test image" },
51 	{ TIFFTAG_MAKE, "This is not scanned image" },
52 	{ TIFFTAG_MODEL, "No scanner" },
53 	{ TIFFTAG_PAGENAME, "Test page" },
54 	{ TIFFTAG_SOFTWARE, "Libtiff library" },
55 	{ TIFFTAG_DATETIME, "2004:09:10 16:09:00" },
56 	{ TIFFTAG_ARTIST, "Andrey V. Kiselev" },
57 	{ TIFFTAG_HOSTCOMPUTER, "Debian GNU/Linux (Sarge)" },
58 	{ TIFFTAG_TARGETPRINTER, "No printer" },
59 	{ TIFFTAG_COPYRIGHT, "Copyright (c) 2004, Andrey Kiselev" },
60 	{ TIFFTAG_FAXSUBADDRESS, "Fax subaddress" },
61 	/* DGN tags */
62 	{ TIFFTAG_UNIQUECAMERAMODEL, "No camera" },
63 	{ TIFFTAG_CAMERASERIALNUMBER, "1234567890" }
64 };
65 #define NTAGS   (sizeof (ascii_tags) / sizeof (ascii_tags[0]))
66 
67 static const char ink_names[] = "Red\0Green\0Blue";
68 const int ink_names_size = 15;
69 
70 int
main()71 main()
72 {
73 	TIFF		*tif;
74 	size_t		i;
75 	unsigned char	buf[] = { 0, 127, 255 };
76 	char		*value;
77 
78 	/* Test whether we can write tags. */
79 	tif = TIFFOpen(filename, "w");
80 	if (!tif) {
81 		fprintf (stderr, "Can't create test TIFF file %s.\n", filename);
82 		return 1;
83 	}
84 
85 	if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, 1)) {
86 		fprintf (stderr, "Can't set ImageWidth tag.\n");
87 		goto failure;
88 	}
89 	if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, 1)) {
90 		fprintf (stderr, "Can't set ImageLength tag.\n");
91 		goto failure;
92 	}
93 	if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8)) {
94 		fprintf (stderr, "Can't set BitsPerSample tag.\n");
95 		goto failure;
96 	}
97 	if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sizeof(buf))) {
98 		fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
99 		goto failure;
100 	}
101 	if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) {
102 		fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
103 		goto failure;
104 	}
105 	if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)) {
106 		fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
107 		goto failure;
108 	}
109 
110 	for (i = 0; i < NTAGS; i++) {
111 		if (!TIFFSetField(tif, ascii_tags[i].tag,
112 				  ascii_tags[i].value)) {
113 			fprintf(stderr, "Can't set tag %lu.\n",
114 				(unsigned long)ascii_tags[i].tag);
115 			goto failure;
116 		}
117 	}
118 
119 	/* InkNames tag has special form, so we handle it separately. */
120 	if (!TIFFSetField(tif, TIFFTAG_NUMBEROFINKS, 3)) {
121 		fprintf (stderr, "Can't set tag %d (NUMBEROFINKS).\n",
122                          TIFFTAG_NUMBEROFINKS);
123 		goto failure;
124 	}
125 	if (!TIFFSetField(tif, TIFFTAG_INKNAMES, ink_names_size, ink_names)) {
126 		fprintf (stderr, "Can't set tag %d (INKNAMES).\n",
127 			 TIFFTAG_INKNAMES);
128 		goto failure;
129 	}
130 
131 	/* Write dummy pixel data. */
132 	if (TIFFWriteScanline(tif, buf, 0, 0) == -1) {
133 		fprintf (stderr, "Can't write image data.\n");
134 		goto failure;
135 	}
136 
137 	TIFFClose(tif);
138 
139 	/* Ok, now test whether we can read written values. */
140 	tif = TIFFOpen(filename, "r");
141 	if (!tif) {
142 		fprintf (stderr, "Can't open test TIFF file %s.\n", filename);
143 		return 1;
144 	}
145 
146 	for (i = 0; i < NTAGS; i++) {
147 		if (!TIFFGetField(tif, ascii_tags[i].tag, &value)
148 		    || strcmp(value, ascii_tags[i].value)) {
149 			fprintf(stderr, "Can't get tag %lu.\n",
150 				(unsigned long)ascii_tags[i].tag);
151 			goto failure;
152 		}
153 	}
154 
155 	if (!TIFFGetField(tif, TIFFTAG_INKNAMES, &value)
156 	    || memcmp(value, ink_names, ink_names_size)) {
157 		fprintf (stderr, "Can't get tag %d (INKNAMES).\n",
158 			 TIFFTAG_INKNAMES);
159 		goto failure;
160 	}
161 
162 	TIFFClose(tif);
163 
164 	/* All tests passed; delete file and exit with success status. */
165 	unlink(filename);
166 	return 0;
167 
168 failure:
169 	/*
170 	 * Something goes wrong; close file and return unsuccessful status.
171 	 * Do not remove the file for further manual investigation.
172 	 */
173 	TIFFClose(tif);
174 	return 1;
175 }
176 
177 /* vim: set ts=8 sts=8 sw=8 noet: */
178