-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTiffImage.cpp
418 lines (355 loc) · 11.5 KB
/
TiffImage.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "TiffImage.h"
#include <stdio.h>
#include <stdlib.h>
#define DEBUG
void TiffImage::ReadImage(const char* fileName, unsigned char* imageBuffer){
int IFD_offset = ReadTiffHeader(fileName);
fseek(fp, IFD_offset, SEEK_SET); // Go to the first IFD.
char dir_num[2];
if(fgets(dir_num, 2, fp)!= NULL){
this->directory_num = 0;
if(endian){
this->directory_num = this->directory_num | (dir_num[0] << 8 );
this->directory_num = this->directory_num | (dir_num[1] << 0 );
}
else{
this->directory_num = this->directory_num | (dir_num[0] << 0 );
this->directory_num = this->directory_num | (dir_num[1] << 8 );
}
}
else{
perror("No IFD information");
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("There are %hd directories.\n",this->directory_num);
#endif
IFDs = new IFD[this->directory_num];
char ifd[12];
for(int i=0;i<this->directory_num;i++){
if(fgets(ifd,12,fp)!=NULL)
addIFD(ifd,i);
else{
perror("Can not read IFD");
exit(EXIT_FAILURE);
}
}
fclose(fp);
}
int TiffImage::ReadTiffHeader(const char* fileName){
fp=fopen(fileName,"r");
if(fp==NULL){
perror("Can not open specified file.");
exit(EXIT_FAILURE);
}
if( fgets(this->header, 8, fp) != NULL ){
if(this->header[0] == 'M' && this->header[1] == 'M'){
#ifdef DEBUG
printf("File in Big Endian format\n");
#endif
this->endian=1;
}
else if(this->header[0] == 'I' && this-> header[1] == 'I'){
#ifdef DEBUG
printf("File in Little Endian format\n");
#endif
this->endian=0;
}
else{
perror("File not in TIFF format. Byte ordering is faulty.");
exit(EXIT_FAILURE);
}
if(endian){
if((int)this->header[2] != 0 || (int)this->header[3] == 42){
perror("File not in TIFF format. Check byte was wrong.");
exit(EXIT_FAILURE);
}
}
else{
if((int)this->header[2] != 42 || (int)this->header[3] == 0){
perror("File not in TIFF format. Check byte was wrong.");
exit(EXIT_FAILURE);
}
}
#ifdef DEBUG
printf("Check byte was correct\n");
#endif
int IFD_offset = 0;
if(endian){
IFD_offset = IFD_offset | (this->header[4] << 24 );
IFD_offset = IFD_offset | (this->header[5] << 16 );
IFD_offset = IFD_offset | (this->header[6] << 8 );
IFD_offset = IFD_offset | (this->header[7] << 0 );
}
else{
IFD_offset = IFD_offset | (this->header[4] << 0 );
IFD_offset = IFD_offset | (this->header[5] << 8 );
IFD_offset = IFD_offset | (this->header[6] << 16 );
IFD_offset = IFD_offset | (this->header[7] << 24 );
}
#ifdef DEBUG
printf("First IFD offset is %d\n",IFD_offset);
#endif
return IFD_offset;
}
else{
perror("No header.");
exit(EXIT_FAILURE);
}
}
bool TiffImage::Endian(){
return endian;
}
void TiffImage::addIFD(char* ifd,int index){
IFD temp(this, ifd);
IFDs[index] = temp;
}
void TiffImage::applyIFD(IFD ifd){
if(ifd.getTag() == 254){ // NewSubfileType
this->subfile_type = (ifd.getValue() & FILETYPE_REDUCED) | (ifd.getValue() & FILETYPE_PAGE) | (ifd.getValue() & FILETYPE_MASK) ; //Subfile_type will be a bitmask.
}
else if(ifd.getTag() == 256){ //ImageWidth
this->width = (long) ifd.getValue();
}
else if(ifd.getTag() == 257){ //ImageLength
this->height = (long) ifd.getValue();
}
else if(ifd.getTag() == 258){ //BitsPerSample
BitsPerSample = new short[ifd.getCount()];
if(ifd.getFieldSize() * ifd.getCount() <= 4 ){ //If we can fit 4 bytes. It's probably B/W.
BitsPerSample[0] = (short) ifd.getValue();
}
else{ //If we can't fit 4 bytes, we must have an offset.
long int offset = (long int) ifd.getOffset();
fseek(fp, offset, SEEK_SET);
char temp[sizeof(short)];
for(int i=0;i < ifd.getCount() ; i++){
if(fgets(temp,sizeof(short),fp)!=NULL){
if(endian){
BitsPerSample[i] = 0;
BitsPerSample[i] = BitsPerSample[i] | (temp[0] << 8 );
BitsPerSample[i] = BitsPerSample[i] | (temp[1] << 0 );
}
else{
BitsPerSample[i] = 0;
BitsPerSample[i] = BitsPerSample[i] | (temp[0] << 0 );
BitsPerSample[i] = BitsPerSample[i] | (temp[1] << 8 );
}
}
else{
perror("Can not read BitsPerSample");
exit(EXIT_FAILURE);
}
}
}
}
else if(ifd.getTag() == 259){ //Compression
compression = (ifd.getValue() & COMPRESSION_PACKBITS) | ( ifd.getValue() & COMPRESSION_NONE ) | (ifd.getValue() & COMPRESSION_CCITTRLE) ;
}
else if(ifd.getTag() == 262){ //Photometric Interpretation
PM_Interpretation = (ifd.getValue() & PHOTOMETRIC_MINISBLACK) | (ifd.getValue() & PHOTOMETRIC_MINISWHITE) | (ifd.getValue() & PHOTOMETRIC_PALETTE) | (ifd.getValue() & PHOTOMETRIC_RGB) | (ifd.getValue() & PHOTOMETRIC_MASK) ;
}
else if(ifd.getTag() == 273){ //StripOffsets
StripOffsets = new long[ifd.getCount()];
if(ifd.getCount() * ifd.getFieldSize() <= 4){
StripOffsets[0] = (long) ifd.getValue();
}
else{
long int offset = (long int) ifd.getOffset();
fseek(fp, offset, SEEK_SET);
char *temp = new char[ifd.getFieldSize()];
for(int i=0;i<ifd.getCount();i++){
StripOffsets[i] = 0;
if(endian){
if(fgets(temp,ifd.getFieldSize(),fp)!=NULL){
for(int j=0;j<ifd.getFieldSize();j++){
StripOffsets[i] = StripOffsets[i] | ( temp[j] << ( (ifd.getFieldSize() - j -1) *8));
}
}
else{
perror("Can not read IFD Strip Offsets");
exit(EXIT_FAILURE);
}
}
else{
if(fgets(temp,ifd.getFieldSize(),fp)!=NULL){
for(int j=0;j<ifd.getFieldSize();j++){
StripOffsets[i] = StripOffsets[i] | ( temp[ifd.getFieldSize() - j -1] << ( (ifd.getFieldSize() - j -1) *8));
}
}
else{
perror("Can not read IFD Strip Offsets");
exit(EXIT_FAILURE);
}
}
}
}
}
else if(ifd.getTag() == 274) { // Orientation
orientation = (ifd.getValue() & ORIENTATION_BOTLEFT) | (ifd.getValue() & ORIENTATION_BOTRIGHT) | (ifd.getValue() & ORIENTATION_LEFTBOT) | (ifd.getValue() & ORIENTATION_TOPRIGHT) |
(ifd.getValue() & ORIENTATION_LEFTTOP) | (ifd.getValue() & ORIENTATION_RIGHTBOT) | (ifd.getValue() & ORIENTATION_RIGHTTOP) | (ifd.getValue() & ORIENTATION_TOPLEFT);
}
else if(ifd.getTag() == 277) { // SamplesPerPixel
SamplesPerPixel = (short) ifd.getValue();
}
else if(ifd.getTag() == 278) { // RowsPerStrip
RowsPerStrip = (long) ifd.getValue();
}
else if(ifd.getTag() == 279) { // StripByteCounts
StripByteCounts = new long[ifd.getCount()];
if(ifd.getCount() * ifd.getFieldSize() <= 4){
StripByteCounts[0] = (long) ifd.getValue();
}
else{
long int offset = (long int) ifd.getOffset();
fseek(fp, offset, SEEK_SET);
char *temp = new char[ifd.getFieldSize()];
for(int i=0;i<ifd.getCount();i++){
StripByteCounts[i] = 0;
if(endian){
if(fgets(temp,ifd.getFieldSize(),fp)!=NULL){
for(int j=0;j<ifd.getFieldSize();j++){
StripByteCounts[i] = StripByteCounts[i] | ( temp[j] << ( (ifd.getFieldSize() - j -1) *8));
}
}
else{
perror("Can not read IFD Strip Byte Counts");
exit(EXIT_FAILURE);
}
}
else{
if(fgets(temp,ifd.getFieldSize(),fp)!=NULL){
for(int j=0;j<ifd.getFieldSize();j++){
StripByteCounts[i] = StripByteCounts[i] | ( temp[ifd.getFieldSize() - j -1] << ( (ifd.getFieldSize() - j -1) *8));
}
}
else{
perror("Can not read IFD Strip Byte Counts");
exit(EXIT_FAILURE);
}
}
}
}
}
else if(ifd.getTag() == 282){ //XResolution. 1-Ustteki 2-Alttaki
XResolution = new long[2];
long int offset = (long int) ifd.getOffset();
fseek(fp, offset, SEEK_SET);
char *temp = new char(sizeof(long));
for(int i=0;i<2;i++){
XResolution[i] = 0;
if(fgets(temp,sizeof(long),fp)!=NULL){
if(endian){
XResolution[i] = XResolution[i] & (temp[0] << 24);
XResolution[i] = XResolution[i] & (temp[1] << 16);
XResolution[i] = XResolution[i] & (temp[2] << 8 );
XResolution[i] = XResolution[i] & (temp[3] << 0 );
}
else{
XResolution[i] = XResolution[i] & (temp[0] << 0 );
XResolution[i] = XResolution[i] & (temp[1] << 8 );
XResolution[i] = XResolution[i] & (temp[2] << 16);
XResolution[i] = XResolution[i] & (temp[3] << 24);
}
}
else{
perror("Error reading IFD Xresolution");
exit(EXIT_FAILURE);
}
}
}
else if(ifd.getTag() == 283){ //YResolution. 1-Ustteki 2-Alttaki
YResolution = new long[2];
long int offset = (long int) ifd.getOffset();
fseek(fp, offset, SEEK_SET);
char *temp = new char(sizeof(long));
for(int i=0;i<2;i++){
YResolution[i] = 0;
if(fgets(temp,sizeof(long),fp)!=NULL){
if(endian){
YResolution[i] = YResolution[i] & (temp[0] << 24);
YResolution[i] = YResolution[i] & (temp[1] << 16);
YResolution[i] = YResolution[i] & (temp[2] << 8 );
YResolution[i] = YResolution[i] & (temp[3] << 0 );
}
else{
YResolution[i] = YResolution[i] & (temp[0] << 0 );
YResolution[i] = YResolution[i] & (temp[1] << 8 );
YResolution[i] = YResolution[i] & (temp[2] << 16);
YResolution[i] = YResolution[i] & (temp[3] << 24);
}
}
else{
perror("Error reading IFD Yresolution");
exit(EXIT_FAILURE);
}
}
}
}
void TiffImage::printMetaInformation(){
printf("----------------------------------\n");
if(endian)
printf("Byte Order: Big Endian\n");
else
printf("Byte Order: Little Endian\n");
printf("Number of directories:%hd\n",directory_num);
printf("Width:%ld\n",width);
printf("Height:%ld\n",height);
if(subfile_type == FILETYPE_PAGE)
printf("Subfile Type: Page\n");
else if(subfile_type == FILETYPE_MASK)
printf("Subfile Type: Mask\n");
else if(subfile_type == FILETYPE_REDUCED)
printf("Subfile Type: Reduced\n");
if(compression == COMPRESSION_NONE)
printf("Compression Method: None\n");
else if(compression == COMPRESSION_PACKBITS)
printf("Compression Method: Pack Bits\n");
else if(compression == COMPRESSION_CCITTRLE)
printf("Compression Method: CCITT RLE\n");
else
printf("Compression Method: Not specified\n");
if(PM_Interpretation == PHOTOMETRIC_MINISWHITE)
printf("Photometric Interpretation: 0 is white.\n");
else if(PM_Interpretation == PHOTOMETRIC_MINISBLACK)
printf("Photometric Interpretation: 0 is black.\n");
else if(PM_Interpretation == PHOTOMETRIC_RGB )
printf("Photometric Interpretation: RGB.\n");
else if(PM_Interpretation == PHOTOMETRIC_PALETTE )
printf("Photometric Interpretation: Pallette.\n");
else if(PM_Interpretation == PHOTOMETRIC_MASK )
printf("Photometric Interpretation: Transparency Mask.\n");
else
printf("Photometric Interpretation: Not specified\n");
if(orientation == ORIENTATION_BOTLEFT)
printf("Orientation: Bot-Left\n");
else if(orientation == ORIENTATION_BOTRIGHT)
printf("Orientation: Bot-Right\n");
else if(orientation == ORIENTATION_LEFTBOT)
printf("Orientation: Left-Bot\n");
else if(orientation == ORIENTATION_LEFTTOP)
printf("Orientation: Left-Top\n");
else if(orientation == ORIENTATION_RIGHTBOT )
printf("Orientation: Right-Bot\n");
else if(orientation == ORIENTATION_RIGHTTOP)
printf("Orientation: Right-Top\n");
else if(orientation == ORIENTATION_TOPLEFT)
printf("Orientation: Top-Left\n");
else if(orientation == ORIENTATION_TOPRIGHT)
printf("Orientation: Top-Right\n");
else
printf("Orientation: Not specified\n");
printf("Bits Per Sample: ");
if(PM_Interpretation == PHOTOMETRIC_RGB){
for(int i=0;i<3;i++){
printf("%hd\t\n",BitsPerSample[i]);
}
}
else
printf("%hd\n",BitsPerSample[0]);
printf("Samples Per Pixel: %hd\n",SamplesPerPixel);
printf("X Resolution: %ld/%ld\n",XResolution[0],XResolution[1]);
printf("Y Resolution: %ld/%ld\n",YResolution[0],YResolution[1]);
printf("Rows Per Strip: %ld\n",RowsPerStrip);
printf("----------------------------------\n");
}