0% found this document useful (0 votes)
47 views

Tutorial 1&2

Uploaded by

Hafiz Halim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Tutorial 1&2

Uploaded by

Hafiz Halim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EXTRACT .

TIFF FILE

Source Code

#include <iostream>
#include <fstream>

using namespace std;

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;

cout << "Insert file name (*.tif) : ";


cin >> fileName;

fin.open(fileName,ios::in|ios::binary|ios::binary);
fout.open("Output.txt");

if(fin.fail())
{
cout<<"Sorry, file not recognise ";
}

fin.read((char*) &head.byteOrder, 2);

1
fin.read((char*) &head.version, 2);
fin.read((char*) &head.offset, 4);
fin.read((char*) &numberOfDE, 2);

DE *de;
de = new DE[numberOfDE];

for(i=0; i<numberOfDE; i++)


{

fin.read((char*) &de[i].tag, 2);


fin.read((char*) &de[i].type, 2);
fin.read((char*) &de[i].length, 4);
fin.read((char*) &de[i].value, 4);

if (de[i].tag==257)
{
length = de[i].value;
}
}

fin.close(); // To disconnect connect the ifstream "fin" to file it is connected

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<<"******************Directory Entry (DE)******************\n\n\n";

for (i=0; i<numberOfDE; i++)


{

fout.setf(ios::hex);
fout<<"Tag : "<<de[i].tag;

switch(de[i].tag)
{

case 254: fout<<" NewSubFileType ";


break;

case 255: fout<<" SubFileType ";


break;

case 256: fout<<" ImageWidth ";


break;

case 257: fout<<" ImageLength ";


break;

2
case 258: fout<<" BitsPerSample ";
break;

case 259: fout<<" Compression ";


break;

case 262: fout<<" PhotometricInterpretation ";


break;

case 269: fout<<" DocumentName ";


break;

case 273: fout<<" StripOffsets ";


break;

case 277: fout<<" SamplePerPixel ";


break;

case 278: fout<<" RowPerStrip ";


break;

case 279: fout<<" StripByteCounts ";


break;

case 282: fout<<" XResolution ";


break;

case 283: fout<<" YResolution ";


break;

case 284: fout<<" PlanarConfiguration ";


break;

case 296: fout<<" ResolutionUnit ";


break;

case 317: fout<<" Predictor ";


break;

default : fout<<" Other Tag ";


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********************

Byte Order : 18761

Version : 42

Offset :8

******************Directory Entry (DE)******************

Tag : 254 NewSubFileType

Type : 4

Length : 1

Value : 0

Tag : 256 ImageWidth

Type : 3

Length : 1

Value : 400

Tag : 257 ImageLength

Type : 3

Length : 1

Value : 400

Tag : 258 BitsPerSample

Type : 3

Length : 1

Value : 8

Tag : 259 Compression

Type : 3

Length : 1

4
Value : 1

Tag : 262 PhotometricInterpretation

Type : 3

Length : 1

Value : 1

Tag : 273 StripOffsets

Type : 4

Length : 1

Value : 11740

Tag : 277 SamplePerPixel

Type : 3

Length : 1

Value : 1

Tag : 278 RowPerStrip

Type : 3

Length : 1

Value : 400

Tag : 279 StripByteCounts

Type : 4

Length : 1

Value : 160000

5
Tag : 282 XResolution

Type : 5

Length : 1

Value : 242

Tag : 283 YResolution

Type : 5

Length : 1

Value : 250

Tag : 296 ResolutionUnit

Type : 3

Length : 1

Value : 2

Tag : 305 Other Tag

Type : 2

Length : 20

Value : 258

Tag : 306 Other Tag

Type : 2

Length : 20

Value : 278

Tag : 700 Other Tag

Type : 1

Length : 4649

Value : 298

6
Tag : -31159 Other Tag

Type : 1

Length : 6792

Value : 4948

Tag : -30871 Other Tag

Type : 4

Length : 1

Value : 171740

Tag : -27812 Other Tag

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>

using namespace std;

#define ROW 1000


#define COLUMN 1000

short unsigned int raw[ROW][COLUMN];

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;

cout<<"Please enter the .tif name: ";


cin>>fileName;

strcat(fileName,x);

input.open(fileName,ios::in|ios::binary);

if(input.fail())
{
cout<<"Invalid filename!"<<endl;
}

cout<<"Please enter the .raw name: ";


cin>>rawName;

strcat(rawName,y);

output.open (rawName, ios::out | ios::binary);

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);

for(int i = 0; i<image.length; i++)


{
for(int j = 0; j<image.width; j++)
{
input.read((char*) &raw[i][j],1);
output.write((char*) &raw[i][j],1);

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

You might also like