-
Notifications
You must be signed in to change notification settings - Fork 0
/
IFD.cpp
156 lines (144 loc) · 3.36 KB
/
IFD.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
#include "IFD.h"
IFD::IFD(){
}
IFD::IFD(TiffImage *image, char* ifd){
bool endian = image->Endian();
///////////////TAG///////////////
tag = 0;
if(endian){
tag = tag | (ifd[0] << 8 );
tag = tag | (ifd[1] << 0 );
}
else{
tag = tag | (ifd[0] << 0 );
tag = tag | (ifd[1] << 8 );
}
#ifdef DEBUG
printf("Tag is %hd \n",tag);
#endif
//////////FIELD TYPE/////////////
short ftype = 0;
if(endian){
ftype = ftype | (ifd[2] << 8 );
ftype = ftype | (ifd[3] << 0 );
}
else{
ftype = ftype | (ifd[2] << 0 );
ftype = ftype | (ifd[3] << 8 );
}
switch(ftype){
case 1 :
this->type = BYTE;
this->fieldSize = 1;
#ifdef DEBUG
printf("Field type is BYTE\n");
#endif
break;
case 2 :
this->type = ASCII;
this->fieldSize = sizeof(char);
#ifdef DEBUG
printf("Field type is ASCII\n");
#endif
break;
case 3 :
this->type = SHORT;
this->fieldSize = sizeof(short);
#ifdef DEBUG
printf("Field type is SHORT\n");
#endif
break;
case 4 :
this->type = LONG;
this->fieldSize = sizeof(long);
#ifdef DEBUG
printf("Field type is LONG\n");
#endif
break;
case 5 :
this->type = RATIONAL;
this->fieldSize = 2*sizeof(long);
#ifdef DEBUG
printf("Field type is RATIONAL\n");
#endif
break;
default :
printf("Field type is %hd\n",ftype);
perror("Field Type is undefined");
exit(EXIT_FAILURE);
}
//////////////COUNT//////////////
this->count = 0;
if(endian){
this->count = this->count | ( ifd[4] << 24 );
this->count = this->count | ( ifd[5] << 16 );
this->count = this->count | ( ifd[6] << 8 );
this->count = this->count | ( ifd[7] << 0 );
}
else{
this->count = this->count | ( ifd[4] << 0 );
this->count = this->count | ( ifd[5] << 8 );
this->count = this->count | ( ifd[6] << 16 );
this->count = this->count | ( ifd[7] << 24 );
}
#ifdef DEBUG
printf("Count is %u\n", this->count);
#endif
////////////VALUE/OFFSET/////////
if(this->count * this->fieldSize <= 4){ // If we can fit the value into 4 bytes, we just write the value. Otherwise, we write the offset.
this->value = 0;
this->offset = NULL;
if(endian){
this->value = this->value | (ifd[8] << 24 );
this->value = this->value | (ifd[9] << 16 );
this->value = this->value | (ifd[10] << 8 );
this->value = this->value | (ifd[11] << 0 );
}
else{
this->value = this->value | (ifd[8] << 0 );
this->value = this->value | (ifd[9] << 8 );
this->value = this->value | (ifd[10] << 16 );
this->value = this->value | (ifd[11] << 24 );
}
#ifdef DEBUG
printf("Value fits 4 bytes. Value is %u\n",this->value);
#endif
}
else{
this->offset = 0;
this->value = NULL;
if(endian){
this->offset = this->offset | (ifd[8] << 24 );
this->offset = this->offset | (ifd[9] << 16 );
this->offset = this->offset | (ifd[10] << 8 );
this->offset = this->offset | (ifd[11] << 0 );
}
else{
this->offset = this->offset | (ifd[8] << 0 );
this->offset = this->offset | (ifd[9] << 8 );
this->offset = this->offset | (ifd[10] << 16 );
this->offset = this->offset | (ifd[11] << 24 );
}
#ifdef DEBUG
printf("Value doesn't fit 4 bytes. Offset is %u\n",this->offset);
#endif
}
}
short IFD::getTag(){
return tag;
}
fieldType IFD::getType(){
return type;
}
short IFD::getFieldSize(){
return fieldSize;
}
unsigned int IFD::getCount(){
return count;
}
unsigned int IFD::getValue(){
return value;
}
unsigned int IFD::getOffset(){
return offset;
}