Advertisement
triclops200

Barcodereader-pgmfile

Jul 2nd, 2011
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std;
  6. class PGM{
  7.     private:
  8.         int width;
  9.         int** data;
  10.         int height;
  11.         int baseline;
  12.         bool binopen(ifstream& file_h,string filename){
  13.             int pos =file_h.tellg();
  14.             ifstream file;
  15.             file.open(filename.c_str(),ios::binary);
  16.             file.seekg(pos);
  17.             data = new int*[height];
  18.             for(int y = 0; y< height;y++){
  19.                 data[y] = new int[width];
  20.                 for(int x = 0; x< width; x++){
  21.                     char buff[2];
  22.                     file.read(buff,1);
  23.                     if((int)buff[0]<0)
  24.                         data[y][x]=(int)buff[0]+baseline+1;
  25.                 }      
  26.             }
  27.             file.close();
  28.         }
  29.         bool asciiopen(ifstream & file,string filename){
  30.         }
  31.         bool startfile(string filename){
  32.             bool bin;
  33.             int dim[2];
  34.             int x=0;
  35.             string tmp;
  36.             string temp = "";
  37.             ifstream file_h;
  38.             file_h.open(filename.c_str());
  39.             getline(file_h,tmp);
  40.             bin = tmp=="P5";
  41.             getline(file_h,tmp);
  42.             getline(file_h,tmp);
  43.             for(int i = 0; i< tmp.length();i++){
  44.                 if(tmp[i]==' ') {
  45.                     i++;
  46.                     dim[x]=atoi(temp.c_str());
  47.                     x++;
  48.                     temp="";                   
  49.                 }
  50.                 temp+=tmp[i];
  51.             }
  52.             dim[x]=atoi(temp.c_str());
  53.             width = dim[0];
  54.             height = dim[1];
  55.             getline(file_h,tmp);
  56.             baseline=atoi(tmp.c_str());
  57.  
  58.             if(bin){
  59.                 binopen(file_h,filename);
  60.             }else{
  61.                 asciiopen(file_h,filename);
  62.             }
  63.             file_h.close();
  64.         }  
  65.     public:
  66.  
  67.         PGM(string filename){
  68.             startfile(filename);
  69.         }
  70.         ~PGM(){
  71.             for(int y = 0;y<height;y++){
  72.                 delete [] data[y];
  73.             }
  74.         }
  75.  
  76.  
  77.  
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement