]>
Commit | Line | Data |
---|---|---|
8414a40c VZ |
1 | /* $Id$ */ |
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 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 | ||
80ed523f | 43 | static const char filename[] = "ascii_test.tiff"; |
8414a40c | 44 | |
80ed523f | 45 | static const struct { |
8414a40c VZ |
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" }, | |
80ed523f VZ |
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" } | |
8414a40c VZ |
64 | }; |
65 | #define NTAGS (sizeof (ascii_tags) / sizeof (ascii_tags[0])) | |
66 | ||
80ed523f | 67 | static const char ink_names[] = "Red\0Green\0Blue"; |
8414a40c VZ |
68 | const int ink_names_size = 15; |
69 | ||
70 | int | |
80ed523f | 71 | main() |
8414a40c VZ |
72 | { |
73 | TIFF *tif; | |
80ed523f VZ |
74 | size_t i; |
75 | unsigned char buf[] = { 0, 127, 255 }; | |
8414a40c VZ |
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 | } | |
80ed523f | 97 | if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sizeof(buf))) { |
8414a40c VZ |
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)) { | |
80ed523f VZ |
113 | fprintf(stderr, "Can't set tag %lu.\n", |
114 | (unsigned long)ascii_tags[i].tag); | |
8414a40c VZ |
115 | goto failure; |
116 | } | |
117 | } | |
118 | ||
119 | /* InkNames tag has special form, so we handle it separately. */ | |
120 | if (!TIFFSetField(tif, TIFFTAG_NUMBEROFINKS, 3)) { | |
80ed523f VZ |
121 | fprintf (stderr, "Can't set tag %d (NUMBEROFINKS).\n", |
122 | TIFFTAG_NUMBEROFINKS); | |
8414a40c VZ |
123 | goto failure; |
124 | } | |
125 | if (!TIFFSetField(tif, TIFFTAG_INKNAMES, ink_names_size, ink_names)) { | |
80ed523f VZ |
126 | fprintf (stderr, "Can't set tag %d (INKNAMES).\n", |
127 | TIFFTAG_INKNAMES); | |
8414a40c VZ |
128 | goto failure; |
129 | } | |
130 | ||
131 | /* Write dummy pixel data. */ | |
132 | if (!TIFFWriteScanline(tif, buf, 0, 0) < 0) { | |
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)) { | |
80ed523f VZ |
149 | fprintf(stderr, "Can't get tag %lu.\n", |
150 | (unsigned long)ascii_tags[i].tag); | |
8414a40c VZ |
151 | goto failure; |
152 | } | |
153 | } | |
154 | ||
155 | if (!TIFFGetField(tif, TIFFTAG_INKNAMES, &value) | |
156 | || memcmp(value, ink_names, ink_names_size)) { | |
80ed523f VZ |
157 | fprintf (stderr, "Can't get tag %d (INKNAMES).\n", |
158 | TIFFTAG_INKNAMES); | |
8414a40c VZ |
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: | |
80ed523f VZ |
169 | /* |
170 | * Something goes wrong; close file and return unsuccessful status. | |
171 | * Do not remove the file for further manual investigation. | |
172 | */ | |
8414a40c | 173 | TIFFClose(tif); |
8414a40c VZ |
174 | return 1; |
175 | } | |
176 | ||
177 | /* vim: set ts=8 sts=8 sw=8 noet: */ |