/
tiff_goodness.c
160 lines (140 loc) · 3.73 KB
/
tiff_goodness.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Handles reading/writing TIFF files using libtiff
*
* Copyright (C) 2014 Bryance Oyang
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <tiffio.h>
/*
* read tiff, assumes tiff file has 3 channels per pixel, RGB,
* with 16-bit channels
*
* returns a malloced uint16_t* that needs to be freed, or NULL if
* failed
*/
uint16_t *read_tiff16(char *filename, int *width, int *height)
{
int i;
TIFF *tif;
uint16_t *result;
int scanline_size;
if ((tif = TIFFOpen(filename, "r")) == NULL) {
fprintf(stderr, "read_tiff: could not open %s\n",
filename);
fflush(stderr);
goto out_no_open;
}
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height);
scanline_size = TIFFScanlineSize(tif);
if (scanline_size != 3 * (*width) * sizeof(*result)) {
fprintf(stderr, "read_tiff: %s is not in correct format. TIFF file should have 16-bit channels in RGBRGB format.\n",
filename);
fflush(stderr);
goto out_wrong_format;
}
result = malloc((*height) * scanline_size);
if (result == NULL)
goto out_nomem;
for (i = 0; i < (*height); i++) {
if (TIFFReadScanline(tif, result + 3 * (*width) * i,
i, 0)
== -1) {
fprintf(stderr, "read_tiff: error in reading %s row %d\n",
filename, i);
fflush(stderr);
goto out_read_err;
}
}
TIFFClose(tif);
return result;
out_read_err:
free(result);
out_nomem:
out_wrong_format:
TIFFClose(tif);
out_no_open:
return NULL;
}
/*
* read tiff, assumes tiff file has 3 channels per pixel, RGB,
* with 8-bit channels
*
* returns a malloced uint8_t* that needs to be freed, or NULL if failed
*/
uint8_t *read_tiff8(char *filename, int *width, int *height)
{
int i;
TIFF *tif;
uint8_t *result;
int scanline_size;
if ((tif = TIFFOpen(filename, "r")) == NULL) {
fprintf(stderr, "read_tiff: could not open %s\n",
filename);
fflush(stderr);
goto out_no_open;
}
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height);
scanline_size = TIFFScanlineSize(tif);
if (scanline_size != 3 * (*width) * sizeof(*result)) {
fprintf(stderr, "read_tiff: %s is not in correct format. TIFF file should have 8-bit channels in RGBRGB format.\n",
filename);
fflush(stderr);
goto out_wrong_format;
}
result = malloc((*height) * scanline_size);
if (result == NULL)
goto out_nomem;
for (i = 0; i < (*height); i++) {
if (TIFFReadScanline(tif, result + 3 * (*width) * i,
i, 0)
== -1) {
fprintf(stderr, "read_tiff: error in reading %s row %d\n",
filename, i);
fflush(stderr);
goto out_read_err;
}
}
TIFFClose(tif);
return result;
out_read_err:
free(result);
out_nomem:
out_wrong_format:
TIFFClose(tif);
out_no_open:
return NULL;
}
/* write tiff with 3 channels per pixel, RGB, 16-bit per channel */
int write_tiff16(char *filename, uint16_t *image_data, int width, int height)
{
int i;
TIFF *out;
out = TIFFOpen(filename, "w");
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 16);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
for (i = 0; i < height; i++) {
if (TIFFWriteScanline(out, image_data + 3 * i
* width, i, 0) == -1) {
fprintf(stderr, "write_tiff: error in writing %s on row %d",
filename, i);
fflush(stderr);
TIFFClose(out);
return -1;
}
}
TIFFClose(out);
return 0;
}