FloatImg/funcs/fimg-tiff.c

92 lines
2.1 KiB
C
Raw Normal View History

2019-03-04 02:22:55 +11:00
/*
* FLOATIMG
* import/export to/from TIFF files
*/
#include <stdio.h>
2020-08-22 11:16:13 +11:00
#include <stdlib.h>
#include <tiffio.h>
2019-03-04 02:22:55 +11:00
#include "../floatimg.h"
/* --------------------------------------------------------------------- */
2020-08-22 11:16:13 +11:00
extern int verbosity;
2019-03-04 02:22:55 +11:00
/* --------------------------------------------------------------------- */
2020-08-22 11:16:13 +11:00
int fimg_write_as_tiff(FloatImg *src, char *fname, int flags)
2019-09-02 19:42:03 +11:00
{
2020-08-22 11:16:13 +11:00
TIFF *tiff;
unsigned short *linebuff, *ptr;
int x, y, idx, foo;
char ligne[100];
double maximum, fk;
2019-09-02 19:42:03 +11:00
/* bon, tout cela semble bien tortueux ! */
2020-08-22 11:16:13 +11:00
if (FIMG_TYPE_RGB != src->type) {
fprintf(stderr, "%s: src bad type %d\n", __func__, src->type);
return -2;
}
linebuff = calloc(src->width, 3*sizeof(unsigned short));
if (NULL==linebuff) {
fprintf(stderr, "%s: fatal memory error\n", __func__);
return -7;
}
maximum = (double)fimg_get_maxvalue(src);
fk = maximum / 65535.0;
if (verbosity) {
fprintf(stderr, "%s : maxv %f fk %f\n", __func__, maximum, fk);
}
tiff = TIFFOpen(fname, "w");
2020-08-23 02:38:27 +11:00
if (NULL==tiff) {
return -6;
}
2020-08-22 11:16:13 +11:00
TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, src->width);
TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, src->height);
TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 3); // RGB
TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 16); // 0->65535
TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
sprintf(ligne, "lib FloatImg %d by tTh", FIMG_VERSION);
TIFFSetField(tiff, TIFFTAG_SOFTWARE, ligne);
foo = src->width * 3;
foo = TIFFDefaultStripSize(tiff, foo);
2020-08-23 02:38:27 +11:00
if (verbosity) fprintf(stderr, "default strip size %d\n", foo);
2020-08-22 11:16:13 +11:00
TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, foo);
for (y=0; y<src->height; y++) {
ptr = linebuff;
idx = y * src->width;
for (x=0; x<src->width; x++) {
*ptr++ = (unsigned short) (src->R[idx] / fk);
*ptr++ = (unsigned short) (src->G[idx] / fk);
*ptr++ = (unsigned short) (src->B[idx] / fk);
idx++;
}
TIFFWriteScanline(tiff, linebuff, y, 0);
idx += src->width;
}
TIFFClose(tiff);
2020-01-17 20:53:45 +11:00
2020-08-22 17:42:07 +11:00
free(linebuff);
2020-08-22 21:09:53 +11:00
return 0;
2019-09-02 19:42:03 +11:00
}
2019-03-04 02:22:55 +11:00
/* --------------------------------------------------------------------- */