forked from Dwayne-Phillips/CIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.c
executable file
·263 lines (215 loc) · 7.57 KB
/
header.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/***********************************************
*
* file d:\cips\header.c
*
* Functions: This file contains
* main
* verbose_read_tiff_header
*
* Purpose:
* This program reads the header of a tiff
* file and prints the tag type numbers
* on the screen.
*
* External Calls:
*
* Modifications:
* 9 April 1998 - create
*
*************************************************/
#include "cips.h"
int verbose_read_tiff_header();
int extract_long_from_buffer();
int extract_short_from_buffer();
int main(argc, argv)
int argc;
char *argv[];
{
char name[80];
struct tiff_header_struct image_header;
if(argc < 2){
printf("\n\nNot enough parameters:");
printf("\n");
printf("\n usage: header image-file \n");
exit(0);
}
strcpy(name, argv[1]);
verbose_read_tiff_header(name, &image_header);
} /* ends main */
/***********************************************
*
* verbose_read_tiff_header(...
*
* This function reads the header of a TIFF
* file and places the needed information into
* the struct tiff_header_struct.
*
***********************************************/
int verbose_read_tiff_header(file_name, image_header)
char file_name[];
struct tiff_header_struct *image_header;
{
char buffer[12], response[80];
FILE *image_file;
int bytes_read,
closed,
i,
j,
lsb,
not_finished,
position;
long bits_per_pixel,
image_length,
image_width,
length_of_field,
offset_to_ifd,
strip_offset,
subfile,
value;
short entry_count,
field_type,
s_bits_per_pixel,
s_image_length,
s_image_width,
s_strip_offset,
tag_type;
image_file = fopen(file_name, "rb");
if(image_file != NULL){
printf("\nHeader> OK opened file %s", file_name);
/*************************************
*
* Determine if the file uses MSB
* first or LSB first
*
*************************************/
bytes_read = fread(buffer, 1, 8, image_file);
if(buffer[0] == 0x49)
lsb = 1;
else
lsb = 0;
/*************************************
*
* Read the offset to the IFD
*
*************************************/
extract_long_from_buffer(buffer, lsb, 4,
&offset_to_ifd);
printf("\nHeader> Offset is %ld", offset_to_ifd);
not_finished = 1;
while(not_finished){
/*************************************
*
* Seek to the IFD and read the
* entry_count, i.e. the number of
* entries in the IFD.
*
*************************************/
position = fseek(image_file, offset_to_ifd,
SEEK_SET);
bytes_read = fread(buffer, 1, 2, image_file);
extract_short_from_buffer(buffer, lsb, 0,
&entry_count);
printf("\nHeader> entry count = %d", entry_count);
/***************************************
*
* Now loop over the directory entries.
* Look only for the tags we need. These
* are:
* ImageLength
* ImageWidth
* BitsPerPixel(BitsPerSample)
* StripOffset
*
*****************************************/
for(i=0; i<entry_count; i++){
bytes_read = fread(buffer, 1, 12, image_file);
extract_short_from_buffer(buffer, lsb, 0,
&tag_type);
printf("\nHeader> Loop i %3d tag type %4d", i, tag_type);
switch(tag_type){
case 255: /* Subfile Type */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
extract_long_from_buffer(buffer, lsb, 8,
&subfile);
break;
case 256: /* ImageWidth */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_image_width);
image_width = s_image_width;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&image_width);
break;
case 257: /* ImageLength */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_image_length);
image_length = s_image_length;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&image_length);
break;
case 258: /* BitsPerSample */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_bits_per_pixel);
bits_per_pixel = s_bits_per_pixel;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&bits_per_pixel);
break;
case 273: /* StripOffset */
extract_short_from_buffer(buffer, lsb, 2,
&field_type);
extract_short_from_buffer(buffer, lsb, 4,
&length_of_field);
if(field_type == 3){
extract_short_from_buffer(buffer, lsb, 8,
&s_strip_offset);
strip_offset = s_strip_offset;
}
else
extract_long_from_buffer(buffer, lsb, 8,
&strip_offset);
break;
default:
break;
} /* ends switch tag_type */
} /* ends loop over i directory entries */
bytes_read = fread(buffer, 1, 4, image_file);
extract_long_from_buffer(buffer, lsb, 0,
&offset_to_ifd);
if(offset_to_ifd == 0) not_finished = 0;
} /* ends while not_finished */
image_header->lsb = lsb;
image_header->bits_per_pixel = bits_per_pixel;
image_header->image_length = image_length;
image_header->image_width = image_width;
image_header->strip_offset = strip_offset;
closed = fclose(image_file);
} /* ends if file opened ok */
else{
printf("\n\nTIFF.C> ERROR - could not open "
"tiff file --%s--", file_name);
}
return(1);
} /* ends verbose_read_tiff_header */