View difference between Paste ID: 5QLFV4pA and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | #include <cstdlib> |
2 | #include <iostream> | |
3 | #include <fstream> | |
4 | #include <sstream> | |
5 | #include <string> | |
6 | using namespace std; | |
7 | ||
8 | #define Byte unsigned char | |
9 | #define Word unsigned short int | |
10 | #define DWord unsigned long int | |
11 | ||
12 | int main(int argc, char *argv[]) | |
13 | { | |
14 | int size; | |
15 | char * memblock; | |
16 | ||
17 | //Read Input file in memory | |
18 | ifstream infile; | |
19 | infile.open ("SHELA0.lmp", ios::in|ios::binary); | |
20 | if (infile.is_open()) | |
21 | { | |
22 | //Get length of file: | |
23 | infile.seekg (0, ios::end); //stream pointer to end of file. | |
24 | size = (int) infile.tellg(); //returns position of stream pointer. | |
25 | infile.seekg (0, ios::beg); //stream pointer to beginning of file. | |
26 | ||
27 | //allocate memory to hold the entire file | |
28 | memblock = new char [size]; | |
29 | ||
30 | //read data to memoryblock | |
31 | infile.read (memblock, size); | |
32 | infile.close(); | |
33 | ||
34 | cout << "the complete file content is in memory\n"; | |
35 | } | |
36 | else | |
37 | { | |
38 | cout << "Unable to open input file.\n"; | |
39 | system("PAUSE"); | |
40 | exit(EXIT_FAILURE); | |
41 | } | |
42 | ||
43 | //Process picture data | |
44 | Word width, height, leftOffset, topOffset; | |
45 | DWord * column_array; | |
46 | Byte pixel_count; | |
47 | Byte pixel; | |
48 | ||
49 | ofstream outfile; | |
50 | outfile.open ("output.txt", ios::out); | |
51 | if (outfile.is_open()) | |
52 | { | |
53 | int memPos = 0; | |
54 | memmove (&width,memblock+memPos,sizeof(Word)); //Width | |
55 | memPos += sizeof(Word);//move 2 bytes //memPos = 2 | |
56 | memmove (&height,memblock+memPos,sizeof(Word)); //Height | |
57 | ||
58 | memPos += sizeof(Word);//memPos = 4 | |
59 | memmove (&leftOffset,memblock+memPos,sizeof(Word)); //leftOffset | |
60 | memPos += sizeof(Word);//memPos = 6 | |
61 | memmove (&topOffset,memblock+memPos,sizeof(Word)); //topOffset | |
62 | ||
63 | cout << "width is: " << width << endl; | |
64 | cout << "height is: " << height << endl; | |
65 | cout << "left is: " << leftOffset << endl; | |
66 | cout << "top is: " << topOffset << endl; | |
67 | ||
68 | outfile << "width is: " << width << endl; | |
69 | outfile << "height is: " << height << endl; | |
70 | outfile << "left is: " << leftOffset << endl; | |
71 | outfile << "top is: " << topOffset << endl; | |
72 | ||
73 | column_array = new DWord [width]; | |
74 | ||
75 | memPos += sizeof(Word);//memPos = 8 | |
76 | memcpy(column_array, memblock+memPos, width*sizeof(DWord)); | |
77 | ||
78 | /*for(int i = 0; i < width; i++) | |
79 | { | |
80 | cout << "column n." << i+1 << " start_offset:" << column_array[i] << endl; | |
81 | outfile << "column n." << i+1 << " start_offset:" << column_array[i] << endl; | |
82 | }*/ | |
83 | ||
84 | system("PAUSE"); | |
85 | ||
86 | for (int i = 0; i < width; i++) //loop through columns | |
87 | { | |
88 | memPos = column_array[i]; //move to start of column | |
89 | ||
90 | int rowStart = 0; | |
91 | ||
92 | while(rowStart != 255) //loop through posts | |
93 | { | |
94 | memmove (&rowStart,memblock+memPos,sizeof(Byte)); //Read rowstart, 1 byte | |
95 | ||
96 | if(rowStart == 255) break; | |
97 | ||
98 | memPos += sizeof(Byte); //move 1 byte | |
99 | memmove (&pixel_count,memblock+memPos,sizeof(Byte)); //Read pixel_count, 1 byte | |
100 | //cout << "Pixel_count: " << (int)pixel_count << endl; | |
101 | ||
102 | memPos += sizeof(Byte)*2; //skip dummy_value, 2 bytes | |
103 | ||
104 | for(int j = 0; j < pixel_count; j++) | |
105 | { | |
106 | memmove (&pixel,memblock+memPos,sizeof(Byte)); //Read pixel, 1 byte | |
107 | //pixel = *(memblock+memPos); | |
108 | memPos += sizeof(Byte); | |
109 | ||
110 | //write Pixel to image, j + rowstart = row, i = column | |
111 | cout << "Pixel: " << (int)pixel << " ,x:" << i << " ,y:" << j+rowStart << endl; | |
112 | outfile << "Pixel: " << (int)pixel << " ,x:" << i << " ,y:" << j+rowStart << endl; | |
113 | } | |
114 | ||
115 | memPos += sizeof(Byte); //skip dummy_value, 1 byte | |
116 | system("PAUSE"); | |
117 | } | |
118 | } | |
119 | outfile.close(); | |
120 | } | |
121 | else cout << "Unable to open output file.\n"; | |
122 | ||
123 | delete[] memblock; | |
124 | ||
125 | system("PAUSE"); | |
126 | return EXIT_SUCCESS; | |
127 | } |