FloatImg/funcs/fimg-tiff.c

95 lines
2.2 KiB
C
Raw Normal View History

2019-03-03 16:22:55 +01:00
/*
* FLOATIMG
* import/export to/from TIFF files
*/
#include <stdio.h>
2020-08-22 02:16:13 +02:00
#include <stdlib.h>
2021-05-20 09:31:28 +02:00
#include <stdint.h>
2020-08-22 02:16:13 +02:00
#include <tiffio.h>
2019-03-03 16:22:55 +01:00
#include "../floatimg.h"
/* --------------------------------------------------------------------- */
2020-08-22 02:16:13 +02:00
extern int verbosity;
2019-03-03 16:22:55 +01:00
/* --------------------------------------------------------------------- */
2020-08-22 02:16:13 +02:00
int fimg_write_as_tiff(FloatImg *src, char *fname, int flags)
2019-09-02 10:42:03 +02:00
{
2020-08-22 02:16:13 +02:00
TIFF *tiff;
unsigned short *linebuff, *ptr;
int x, y, idx, foo;
char ligne[100];
double maximum, fk;
2019-09-02 10:42:03 +02:00
2022-07-06 10:27:55 +02:00
if (flags) fprintf(stderr, "in %s, flags are 0x%04x, must be 0\n",
__func__, flags);
2019-09-02 10:42:03 +02:00
/* bon, tout cela semble bien tortueux ! */
2020-08-22 02:16:13 +02: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-22 17:38:27 +02:00
if (NULL==tiff) {
2021-10-15 23:21:02 +02:00
fprintf(stderr, "erreur TIFFOpen\n");
2024-04-04 02:17:50 +02:00
free(linebuff);
2020-08-22 17:38:27 +02:00
return -6;
}
2020-08-22 02:16:13 +02: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);
2021-03-17 18:32:51 +01:00
TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
2020-08-22 02:16:13 +02:00
2021-03-17 18:32:51 +01:00
sprintf(ligne, "lib FloatImg v %d by tTh", FIMG_VERSION);
2020-08-22 02:16:13 +02:00
TIFFSetField(tiff, TIFFTAG_SOFTWARE, ligne);
foo = src->width * 3;
foo = TIFFDefaultStripSize(tiff, foo);
2020-08-22 17:38:27 +02:00
if (verbosity) fprintf(stderr, "default strip size %d\n", foo);
2020-08-22 02:16:13 +02: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 10:53:45 +01:00
2020-08-22 08:42:07 +02:00
free(linebuff);
2020-08-22 12:09:53 +02:00
return 0;
2019-09-02 10:42:03 +02:00
}
2019-03-03 16:22:55 +01:00
/* --------------------------------------------------------------------- */