Advertisement
Guest User

doompicture lump converter

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