Tutorial 1&2
Tutorial 1&2
TIFF FILE
Source Code
#include <iostream>
#include <fstream>
struct Header
{
short int byteOrder;
short int version;
short int offset;
short int numberOfDE;
};
struct DE
{
short int tag;
short int type;
long int length;
long int value;
};
int main()
{
ifstream fin; // fin belonging to class input-file-stream
ofstream fout; // fout belonging to class output-file-stream
char fileName[10];
int i;
short int numberOfDE = 64;
long int length;
Header head;
fin.open(fileName,ios::in|ios::binary|ios::binary);
fout.open("Output.txt");
if(fin.fail())
{
cout<<"Sorry, file not recognise ";
}
1
fin.read((char*) &head.version, 2);
fin.read((char*) &head.offset, 4);
fin.read((char*) &numberOfDE, 2);
DE *de;
de = new DE[numberOfDE];
if (de[i].tag==257)
{
length = de[i].value;
}
}
fout<<"********************Header File********************\n\n";
fout.setf(ios::hex);
fout<<"Byte Order : "<<head.byteOrder<<endl;
fout<<"Version : "<<head.version<<endl;
fout<<"Offset : "<<head.offset<<endl<<endl<<endl;
fout.setf(ios::hex);
fout<<"Tag : "<<de[i].tag;
switch(de[i].tag)
{
2
case 258: fout<<" BitsPerSample ";
break;
fout<<"\n";
fout<<"Type : "<<de[i].type<<endl;
fout<<"Length : "<<de[i].length<<endl;
fout<<"Value : "<<de[i].value<<endl<<endl<<endl<<endl;
system ("pause");
return 0;
}
3
Notepad Output
********************Header File********************
Version : 42
Offset :8
Type : 4
Length : 1
Value : 0
Type : 3
Length : 1
Value : 400
Type : 3
Length : 1
Value : 400
Type : 3
Length : 1
Value : 8
Type : 3
Length : 1
4
Value : 1
Type : 3
Length : 1
Value : 1
Type : 4
Length : 1
Value : 11740
Type : 3
Length : 1
Value : 1
Type : 3
Length : 1
Value : 400
Type : 4
Length : 1
Value : 160000
5
Tag : 282 XResolution
Type : 5
Length : 1
Value : 242
Type : 5
Length : 1
Value : 250
Type : 3
Length : 1
Value : 2
Type : 2
Length : 20
Value : 258
Type : 2
Length : 20
Value : 278
Type : 1
Length : 4649
Value : 298
6
Tag : -31159 Other Tag
Type : 1
Length : 6792
Value : 4948
Type : 4
Length : 1
Value : 171740
Type : 7
Length : 157132
Value : 171784
Print Screen
Image example
7
CONVERT .TIFF TO RAW
Source code
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstring>
struct Header
{
short int byteOrder;
short int version;
short int firstDE;
short int totalDE;
};
struct DE
{
short int tag;
short int type;
long int length;
long int value;
};
struct Image
{
long int width;
long int length;
long int dataBegin;
long int dataSize;
};
int main()
{
ifstream input;
ofstream output;
Header header;
Image image;
char fileName[20];
char rawName[20];
char *x=".tif";
char *y=".raw";
8
short int totalDE;
strcat(fileName,x);
input.open(fileName,ios::in|ios::binary);
if(input.fail())
{
cout<<"Invalid filename!"<<endl;
}
strcat(rawName,y);
if(output.fail())
{
cout<<"Error !"<<endl;
}
input.read((char*) &header.byteOrder,2);
input.read((char*) &header.version,2);
input.read((char*) &header.firstDE,4);
input.read((char*) &header.totalDE,2);
totalDE=header.totalDE;
DE *de;
de=new DE[header.totalDE];
for(int i=0;i<totalDE;i++)
{
output.setf(ios::dec);
input.read((char*)&de[i].tag,2);
input.read((char*)&de[i].type, 2);
input.read((char*)&de[i].length, 4);
input.read((char*)&de[i].value, 4);
//get the neccessary .raw data(image width, image length, data begin, data
size)
if(de[i].tag == 256)
{
image.width = de[i].value;
}
if(de[i].tag == 257)
{
9
image.length = de[i].value;
}
if(de[i].tag == 273)
{
image.dataBegin = de[i].value;
}
if(de[i].tag == 279)
{
image.dataSize = de[i].value;
}
//-------------------------------start to
convert--------------------------------------
input.seekg(image.dataBegin, ios::beg);
if(i==(image.length))
{
output<<raw[i][j]<<endl;
}
if(j==(image.width))
{
output<<raw[i][j]<<endl;
}
}
}
output.close();
input.close();
10
Print Screen
Sample image
11