|
kusano |
7d535a |
/* $Id: strip.c,v 1.4 2008/03/28 01:42:06 bfriesen Exp $ */
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Copyright (c) 2004, Andrey Kiselev <dron@ak4719.spb.edu></dron@ak4719.spb.edu>
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
kusano |
7d535a |
* its documentation for any purpose is hereby granted without fee, provided
|
|
kusano |
7d535a |
* that (i) the above copyright notices and this permission notice appear in
|
|
kusano |
7d535a |
* all copies of the software and related documentation, and (ii) the names of
|
|
kusano |
7d535a |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
kusano |
7d535a |
* publicity relating to the software without the specific, prior written
|
|
kusano |
7d535a |
* permission of Sam Leffler and Silicon Graphics.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
kusano |
7d535a |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
kusano |
7d535a |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
kusano |
7d535a |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
kusano |
7d535a |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
kusano |
7d535a |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
kusano |
7d535a |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
kusano |
7d535a |
* OF THIS SOFTWARE.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* TIFF Library
|
|
kusano |
7d535a |
*
|
|
kusano |
7d535a |
* Functions to test strip interface of libtiff.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include <stdio.h></stdio.h>
|
|
kusano |
7d535a |
#include <string.h></string.h>
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
#include "tiffio.h"
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
write_strips(TIFF *tif, const tdata_t array, const tsize_t size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tstrip_t strip, nstrips;
|
|
kusano |
7d535a |
tsize_t stripsize, offset;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
stripsize = TIFFStripSize(tif);
|
|
kusano |
7d535a |
if (!stripsize) {
|
|
kusano |
7d535a |
fprintf (stderr, "Wrong size of strip.\n");
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
nstrips = TIFFNumberOfStrips(tif);
|
|
kusano |
7d535a |
for (offset = 0, strip = 0;
|
|
kusano |
7d535a |
offset < size && strip < nstrips;
|
|
kusano |
7d535a |
offset+=stripsize, strip++) {
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Properly write last strip.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
tsize_t bufsize = size - offset;
|
|
kusano |
7d535a |
if (bufsize > stripsize)
|
|
kusano |
7d535a |
bufsize = stripsize;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (TIFFWriteEncodedStrip(tif, strip, (char *)array + offset,
|
|
kusano |
7d535a |
bufsize) != bufsize) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't write strip %lu.\n",
|
|
kusano |
7d535a |
(unsigned long)strip);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
read_strips(TIFF *tif, const tdata_t array, const tsize_t size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
tstrip_t strip, nstrips;
|
|
kusano |
7d535a |
tsize_t stripsize, offset;
|
|
kusano |
7d535a |
tdata_t buf = NULL;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
stripsize = TIFFStripSize(tif);
|
|
kusano |
7d535a |
if (!stripsize) {
|
|
kusano |
7d535a |
fprintf (stderr, "Wrong size of strip.\n");
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
buf = _TIFFmalloc(stripsize);
|
|
kusano |
7d535a |
if (!buf) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't allocate space for strip buffer.\n");
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
nstrips = TIFFNumberOfStrips(tif);
|
|
kusano |
7d535a |
for (offset = 0, strip = 0;
|
|
kusano |
7d535a |
offset < size && strip < nstrips;
|
|
kusano |
7d535a |
offset+=stripsize, strip++) {
|
|
kusano |
7d535a |
/*
|
|
kusano |
7d535a |
* Properly read last strip.
|
|
kusano |
7d535a |
*/
|
|
kusano |
7d535a |
tsize_t bufsize = size - offset;
|
|
kusano |
7d535a |
if (bufsize > stripsize)
|
|
kusano |
7d535a |
bufsize = stripsize;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (TIFFReadEncodedStrip(tif, strip, buf, -1) != bufsize) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't read strip %lu.\n",
|
|
kusano |
7d535a |
(unsigned long)strip);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (memcmp(buf, (char *)array + offset, bufsize) != 0) {
|
|
kusano |
7d535a |
fprintf (stderr, "Wrong data read for strip %lu.\n",
|
|
kusano |
7d535a |
(unsigned long)strip);
|
|
kusano |
7d535a |
_TIFFfree(buf);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
_TIFFfree(buf);
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
create_image_striped(const char *name, uint32 width, uint32 length,
|
|
kusano |
7d535a |
uint32 rowsperstrip, uint16 compression,
|
|
kusano |
7d535a |
uint16 spp, uint16 bps, uint16 photometric,
|
|
kusano |
7d535a |
uint16 sampleformat, uint16 planarconfig,
|
|
kusano |
7d535a |
const tdata_t array, const tsize_t size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF *tif;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Test whether we can write tags. */
|
|
kusano |
7d535a |
tif = TIFFOpen(name, "w");
|
|
kusano |
7d535a |
if (!tif)
|
|
kusano |
7d535a |
goto openfailure;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set ImageWidth tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, length)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set ImageLength tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set BitsPerSample tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, spp)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set SamplesPerPixel tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set RowsPerStrip tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, planarconfig)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set PlanarConfiguration tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photometric)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't set PhotometricInterpretation tag.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (write_strips(tif, array, size) < 0) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't write image data.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
failure:
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
openfailure:
|
|
kusano |
7d535a |
fprintf (stderr, "Can't create test TIFF file %s:\n"
|
|
kusano |
7d535a |
" ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
|
|
kusano |
7d535a |
" BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
|
|
kusano |
7d535a |
" PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
|
|
kusano |
7d535a |
name, (long) width, (long) length, (long) rowsperstrip,
|
|
kusano |
7d535a |
compression, bps, spp, sampleformat, planarconfig,
|
|
kusano |
7d535a |
photometric);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
read_image_striped(const char *name, uint32 width, uint32 length,
|
|
kusano |
7d535a |
uint32 rowsperstrip, uint16 compression,
|
|
kusano |
7d535a |
uint16 spp, uint16 bps, uint16 photometric,
|
|
kusano |
7d535a |
uint16 sampleformat, uint16 planarconfig,
|
|
kusano |
7d535a |
const tdata_t array, const tsize_t size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
TIFF *tif;
|
|
kusano |
7d535a |
uint16 value_u16;
|
|
kusano |
7d535a |
uint32 value_u32;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* Test whether we can read written values. */
|
|
kusano |
7d535a |
tif = TIFFOpen(name, "r");
|
|
kusano |
7d535a |
if (!tif)
|
|
kusano |
7d535a |
goto openfailure;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (TIFFIsTiled(tif)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't read image %s, it is tiled.\n",
|
|
kusano |
7d535a |
name);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &value_u32)
|
|
kusano |
7d535a |
|| value_u32 != width) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGEWIDTH);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &value_u32)
|
|
kusano |
7d535a |
|| value_u32 != length) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &value_u16)
|
|
kusano |
7d535a |
|| value_u16 != bps) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_BITSPERSAMPLE);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &value_u16)
|
|
kusano |
7d535a |
|| value_u16 != photometric) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PHOTOMETRIC);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &value_u16)
|
|
kusano |
7d535a |
|| value_u16 != spp) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_SAMPLESPERPIXEL);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &value_u32)
|
|
kusano |
7d535a |
|| value_u32 != rowsperstrip) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_ROWSPERSTRIP);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &value_u16)
|
|
kusano |
7d535a |
|| value_u16 != planarconfig) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_PLANARCONFIG);
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (read_strips(tif, array, size) < 0) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't read image data.\n");
|
|
kusano |
7d535a |
goto failure;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
failure:
|
|
kusano |
7d535a |
TIFFClose(tif);
|
|
kusano |
7d535a |
openfailure:
|
|
kusano |
7d535a |
fprintf (stderr, "Can't read test TIFF file %s:\n"
|
|
kusano |
7d535a |
" ImageWidth=%ld, ImageLength=%ld, RowsPerStrip=%ld, Compression=%d,\n"
|
|
kusano |
7d535a |
" BitsPerSample=%d, SamplesPerPixel=%d, SampleFormat=%d,\n"
|
|
kusano |
7d535a |
" PlanarConfiguration=%d, PhotometricInterpretation=%d.\n",
|
|
kusano |
7d535a |
name, (long) width, (long) length, (long) rowsperstrip,
|
|
kusano |
7d535a |
compression, bps, spp, sampleformat, planarconfig,
|
|
kusano |
7d535a |
photometric);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
int
|
|
kusano |
7d535a |
write_scanlines(TIFF *tif, const tdata_t array, const tsize_t size)
|
|
kusano |
7d535a |
{
|
|
kusano |
7d535a |
uint32 length, row;
|
|
kusano |
7d535a |
tsize_t scanlinesize, offset;
|
|
kusano |
7d535a |
(void) size;
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
if (!TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length)) {
|
|
kusano |
7d535a |
fprintf (stderr, "Can't get tag %d.\n", TIFFTAG_IMAGELENGTH);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
scanlinesize = TIFFScanlineSize(tif);
|
|
kusano |
7d535a |
if (!scanlinesize) {
|
|
kusano |
7d535a |
fprintf (stderr, "Wrong size of scanline.\n");
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
for (offset = 0, row = 0; row < length; offset+=scanlinesize, row++) {
|
|
kusano |
7d535a |
if (TIFFWriteScanline(tif, (char *)array + offset, row, 0) < 0) {
|
|
kusano |
7d535a |
fprintf (stderr,
|
|
kusano |
7d535a |
"Can't write image data at row %lu.\n", (long) row);
|
|
kusano |
7d535a |
return -1;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
return 0;
|
|
kusano |
7d535a |
}
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
/* vim: set ts=8 sts=8 sw=8 noet: */
|