1 /* $Id: strip.c,v 1.4 2008/03/28 01:42:06 bfriesen Exp $ */
4 * Copyright (c) 2004, Andrey Kiselev <dron@ak4719.spb.edu>
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.
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.
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
29 * Functions to test strip interface of libtiff.
38 write_strips(TIFF *tif, const tdata_t array, const tsize_t size)
40 tstrip_t strip, nstrips;
41 tsize_t stripsize, offset;
43 stripsize = TIFFStripSize(tif);
45 fprintf (stderr, "Wrong size of strip.\n");
49 nstrips = TIFFNumberOfStrips(tif);
50 for (offset = 0, strip = 0;
51 offset < size && strip < nstrips;
52 offset+=stripsize, strip++) {
54 * Properly write last strip.
56 tsize_t bufsize = size - offset;
57 if (bufsize > stripsize)
60 if (TIFFWriteEncodedStrip(tif, strip, (char *)array + offset,
61 bufsize) != bufsize) {
62 fprintf (stderr, "Can't write strip %lu.\n",
63 (unsigned long)strip);
72 read_strips(TIFF *tif, const tdata_t array, const tsize_t size)
74 tstrip_t strip, nstrips;
75 tsize_t stripsize, offset;
78 stripsize = TIFFStripSize(tif);
80 fprintf (stderr, "Wrong size of strip.\n");
84 buf = _TIFFmalloc(stripsize);
86 fprintf (stderr, "Can't allocate space for strip buffer.\n");
90 nstrips = TIFFNumberOfStrips(tif);
91 for (offset = 0, strip = 0;
92 offset < size && strip < nstrips;
93 offset+=stripsize, strip++) {
95 * Properly read last strip.
97 tsize_t bufsize = size - offset;
98 if (bufsize > stripsize)
101 if (TIFFReadEncodedStrip(tif, strip, buf, -1) != bufsize) {
102 fprintf (stderr, "Can't read strip %lu.\n",
103 (unsigned long)strip);
106 if (memcmp(buf, (char *)array + offset, bufsize) != 0) {
107 fprintf (stderr, "Wrong data read for strip %lu.\n",
108 (unsigned long)strip);
120 create_image_striped(const char *name, uint32 width, uint32 length,
121 uint32 rowsperstrip, uint16 compression,
122 uint16 spp, uint16 bps, uint16 photometric,
123 uint16 sampleformat, uint16 planarconfig,
124 const tdata_t array, const tsize_t size)
128 /* Test whether we can write tags. */
129 tif = TIFFOpen(name, "w");
133 if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
134 fprintf (stderr, "Can't set ImageWidth tag.\n");
137 if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
138 fprintf (stderr, "Can't set ImageLength tag.\n");
141 if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
142 fprintf (stderr, "Can't set BitsPerSample tag.\n");
145 if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp)) {
146 fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
149 if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip)) {
150 fprintf (stderr, "Can't set RowsPerStrip tag.\n");
153 if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) {
154 fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
157 if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
158 fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
162 if (write_strips(tif, array, size) < 0) {
163 fprintf (stderr, "Can't write image data.\n");
173 fprintf (stderr, "Can't create test TIFF file %s:\n"
174 " ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
175 " BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
176 " PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
177 name, (long) width, (long) length, (long) rowsperstrip,
178 compression, bps, spp, sampleformat, planarconfig,
184 read_image_striped(const char *name, uint32 width, uint32 length,
185 uint32 rowsperstrip, uint16 compression,
186 uint16 spp, uint16 bps, uint16 photometric,
187 uint16 sampleformat, uint16 planarconfig,
188 const tdata_t array, const tsize_t size)
194 /* Test whether we can read written values. */
195 tif = TIFFOpen(name, "r");
199 if (TIFFIsTiled(tif)) {
200 fprintf (stderr, "Can't read image %s, it is tiled.\n",
204 if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &value_u32)
205 || value_u32 != width) {
206 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGEWIDTH);
209 if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &value_u32)
210 || value_u32 != length) {
211 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
214 if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &value_u16)
215 || value_u16 != bps) {
216 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_BITSPERSAMPLE);
219 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &value_u16)
220 || value_u16 != photometric) {
221 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PHOTOMETRIC);
224 if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &value_u16)
225 || value_u16 != spp) {
226 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_SAMPLESPERPIXEL);
229 if (!TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &value_u32)
230 || value_u32 != rowsperstrip) {
231 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_ROWSPERSTRIP);
234 if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &value_u16)
235 || value_u16 != planarconfig) {
236 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PLANARCONFIG);
240 if (read_strips(tif, array, size) < 0) {
241 fprintf (stderr, "Can't read image data.\n");
251 fprintf (stderr, "Can't read test TIFF file %s:\n"
252 " ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
253 " BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
254 " PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
255 name, (long) width, (long) length, (long) rowsperstrip,
256 compression, bps, spp, sampleformat, planarconfig,
262 write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size)
265 tsize_t scanlinesize, offset;
268 if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length)) {
269 fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
273 scanlinesize = TIFFScanlineSize(tif);
275 fprintf (stderr, "Wrong size of scanline.\n");
279 for (offset = 0, row = 0; row < length; offset+=scanlinesize, row++) {
280 if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) < 0) {
282 "Can't write image data at row %lu.\n", (long) row);
290 /* vim: set ts=8 sts=8 sw=8 noet: */