Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- using namespace std;
- #define Byte unsigned char
- #define Word unsigned short int
- #define DWord unsigned long int
- int main(int argc, char *argv[])
- {
- int size;
- char * memblock;
- //Read Input file in memory
- ifstream infile;
- infile.open ("SHELA0.lmp", ios::in|ios::binary);
- if (infile.is_open())
- {
- //Get length of file:
- infile.seekg (0, ios::end); //stream pointer to end of file.
- size = (int) infile.tellg(); //returns position of stream pointer.
- infile.seekg (0, ios::beg); //stream pointer to beginning of file.
- //allocate memory to hold the entire file
- memblock = new char [size];
- //read data to memoryblock
- infile.read (memblock, size);
- infile.close();
- cout << "the complete file content is in memory\n";
- }
- else
- {
- cout << "Unable to open input file.\n";
- system("PAUSE");
- exit(EXIT_FAILURE);
- }
- //Process picture data
- Word width, height, leftOffset, topOffset;
- DWord * column_array;
- Byte pixel_count;
- Byte pixel;
- ofstream outfile;
- outfile.open ("output.txt", ios::out);
- if (outfile.is_open())
- {
- int memPos = 0;
- memmove (&width,memblock+memPos,sizeof(Word)); //Width
- memPos += sizeof(Word);//move 2 bytes //memPos = 2
- memmove (&height,memblock+memPos,sizeof(Word)); //Height
- memPos += sizeof(Word);//memPos = 4
- memmove (&leftOffset,memblock+memPos,sizeof(Word)); //leftOffset
- memPos += sizeof(Word);//memPos = 6
- memmove (&topOffset,memblock+memPos,sizeof(Word)); //topOffset
- cout << "width is: " << width << endl;
- cout << "height is: " << height << endl;
- cout << "left is: " << leftOffset << endl;
- cout << "top is: " << topOffset << endl;
- outfile << "width is: " << width << endl;
- outfile << "height is: " << height << endl;
- outfile << "left is: " << leftOffset << endl;
- outfile << "top is: " << topOffset << endl;
- column_array = new DWord [width];
- memPos += sizeof(Word);//memPos = 8
- memcpy(column_array, memblock+memPos, width*sizeof(DWord));
- /*for(int i = 0; i < width; i++)
- {
- cout << "column n." << i+1 << " start_offset:" << column_array[i] << endl;
- outfile << "column n." << i+1 << " start_offset:" << column_array[i] << endl;
- }*/
- system("PAUSE");
- for (int i = 0; i < width; i++) //loop through columns
- {
- memPos = column_array[i]; //move to start of column
- int rowStart = 0;
- while(rowStart != 255) //loop through posts
- {
- memmove (&rowStart,memblock+memPos,sizeof(Byte)); //Read rowstart, 1 byte
- if(rowStart == 255) break;
- memPos += sizeof(Byte); //move 1 byte
- memmove (&pixel_count,memblock+memPos,sizeof(Byte)); //Read pixel_count, 1 byte
- //cout << "Pixel_count: " << (int)pixel_count << endl;
- memPos += sizeof(Byte)*2; //skip dummy_value, 2 bytes
- for(int j = 0; j < pixel_count; j++)
- {
- memmove (&pixel,memblock+memPos,sizeof(Byte)); //Read pixel, 1 byte
- //pixel = *(memblock+memPos);
- memPos += sizeof(Byte);
- //write Pixel to image, j + rowstart = row, i = column
- cout << "Pixel: " << (int)pixel << " ,x:" << i << " ,y:" << j+rowStart << endl;
- outfile << "Pixel: " << (int)pixel << " ,x:" << i << " ,y:" << j+rowStart << endl;
- }
- memPos += sizeof(Byte); //skip dummy_value, 1 byte
- system("PAUSE");
- }
- }
- outfile.close();
- }
- else cout << "Unable to open output file.\n";
- delete[] memblock;
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement