Advertisement
triclops200

BarcodeReader Pgm image file

Jul 2nd, 2011
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. /*  This program reads a bar-code in .pgm image format and returns the code.
  2.  In progress, might be buggy
  3. NOTE*< The bar-code format used is EAN >*ETON
  4. For a copy of the test file used, go to triclops200.com/files/test.pgm.tar.gz
  5. */
  6.  
  7. /////////////////barcode.cpp///////////////////
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <stdlib.h>
  13. #include "barcode.h"
  14. #include <map>
  15. using namespace std;
  16. class Barcode
  17. {
  18.     private:
  19.         map<string,char> lookup;
  20.         map<string,char> mapping;      
  21.         int** data_;
  22.         bool* dataline;
  23.         bool* bincode;
  24.         int width;
  25.         int height;
  26.         bool** data;
  27.         string final;
  28.         int pivot;
  29.     public:
  30.         Barcode(string filename,int pivot_){
  31.             pivot = pivot_;
  32.             PGM pgm(filename);
  33.             data_= pgm.getdata();
  34.             width = pgm.getwidth();
  35.             height = pgm.getheight();
  36.             data = new bool*[height];
  37.             for(int y = 0; y<height;y++){
  38.                 data[y] = new bool[width];
  39.                 for(int x = 0; x<width;x++){
  40.                     data[y][x]=data_[y][x]<pivot;
  41.                 }
  42.             }
  43.             consolidate();
  44.         }
  45.         void consolidate(){
  46.             bool ondata=false;
  47.             bool tfdata=false;
  48.             dataline= new bool[width];
  49.             for(int y = 0; y<height;y++){
  50.                 for(int x = 0; x<width;x++){
  51.                     if(ondata){
  52.                         dataline[x]=data[y][x];
  53.                     }
  54.                     if(data[y][x]&&!ondata)
  55.                         tfdata=true;
  56.                 }
  57.                 if(ondata)  {
  58.                     y+= height;
  59.                 }
  60.                 if(tfdata){
  61.                     ondata=true;
  62.                     y+=10;
  63.                 }          
  64.             }
  65.             normalize();
  66.         }
  67.         void normalize()
  68.         {
  69.             int barlen;
  70.             int pos[2];
  71.             bool eof[3] = {1,0,1};
  72.             bool tri[3];
  73.             bool startlen = 0;
  74.  
  75.             for(int x = 0; x< width;x++)
  76.             {
  77.                 if(dataline[x]==1&&!startlen){
  78.                     startlen=true;
  79.                     pos[0] = x;
  80.                 }
  81.                 if(dataline[x]==0&&startlen){
  82.                     pos[1]= x;
  83.                     startlen = 0;
  84.                     x+=width;
  85.                 }      
  86.             }
  87.             barlen = pos[1]-pos[0];
  88.             for(int x = pos[1];x<width;x++)
  89.             {
  90.                 if(dataline[x])
  91.                     pos[1]  = x;
  92.             }
  93.             bincode = new bool[pos[1]-pos[0]];
  94.             lookup["0001101"] ='0';
  95.             mapping["0001101"] ='0';
  96.             lookup["0011001"] ='1';
  97.             mapping["0011001"] ='0';
  98.             lookup["0010011"] ='2';
  99.             mapping["0010011"] ='0';
  100.             lookup["0111101"] ='3';
  101.             mapping["0111101"] ='0';
  102.             lookup["0100011"] ='4';
  103.             mapping["0100011"] ='0';               
  104.             lookup["0110001"] ='5';
  105.             mapping["0110001"] ='0';
  106.             lookup["0101111"] ='6';
  107.             mapping["0101111"] ='0';
  108.             lookup["0111011"] ='7';
  109.             mapping["0111011"] ='0';
  110.             lookup["0110111"] ='8';
  111.             mapping["0110111"] ='0';
  112.             lookup["0001011"] ='9';
  113.             mapping["0001011"] ='0';
  114.             lookup["0100111"] ='0';
  115.             mapping["0100111"] ='1';
  116.             lookup["0110011"] ='1';
  117.             mapping["0110011"] ='1';
  118.             lookup["0011011"] ='2';
  119.             mapping["0011011"] ='1';
  120.             lookup["0100001"] ='3';
  121.             mapping["0100001"] ='1';
  122.             lookup["0011101"] ='4';
  123.             mapping["0011101"] ='1';               
  124.             lookup["0111001"] ='5';
  125.             mapping["0111001"] ='1';
  126.             lookup["0000101"] ='6';
  127.             mapping["0000101"] ='1';
  128.             lookup["0010001"] ='7';
  129.             mapping["0010001"] ='1';
  130.             lookup["0001001"] ='8';
  131.             mapping["0001001"] ='1';
  132.             lookup["0010111"] ='9';
  133.             mapping["0010111"] ='1';
  134.             lookup["1110010"] ='0';
  135.             mapping["1110010"] ='2';
  136.             lookup["1100110"] ='1';
  137.             mapping["1100110"] ='2';
  138.             lookup["1101100"] ='2';
  139.             mapping["1101100"] ='2';
  140.             lookup["1000010"] ='3';
  141.             mapping["1000010"] ='2';
  142.             lookup["1011100"] ='4';
  143.             mapping["1011100"] ='2';               
  144.             lookup["1001110"] ='5';
  145.             mapping["1001110"] ='2';
  146.             lookup["1010000"] ='6';
  147.             mapping["1010000"] ='2';
  148.             lookup["1000100"] ='7';
  149.             mapping["1000100"] ='2';
  150.             lookup["1001000"] ='8';
  151.             mapping["1001000"] ='2';
  152.             lookup["1110100"] ='9';
  153.             mapping["1110100"] ='2';
  154.             mapping["000000"]='0';
  155.             mapping["001011"]='1';
  156.             mapping["001101"]='2';
  157.             mapping["001110"]='3';
  158.             mapping["010011"]='4';
  159.             mapping["011001"]='5';
  160.             mapping["011100"]='6';
  161.             mapping["010101"]='7';
  162.             mapping["010110"]='8';
  163.             mapping["011010"]='9';  
  164.             for(int x = pos[0];x<pos[1];x+=barlen){
  165.                 int v = (x-pos[0])/barlen;
  166.                 bincode[v]=dataline[x];
  167.  
  168.             }
  169.             int length = (pos[1]-pos[0]+barlen)/barlen;
  170.             int consta = 3;
  171.             string str1="";
  172.             string str2="";
  173.             final = "";
  174.             for(int f = 0; f < 12; f++){
  175.                 for(int g = f*7+consta;g<((f+1)*7)+consta;g++){
  176.                     str1+=(char)((int)bincode[g]+48);
  177.                 }
  178.                 final+=lookup[str1];
  179.                 if(f<6)
  180.                     str2+=mapping[str1];
  181.                 str1="";
  182.                 if(f==5){
  183.                     consta+=5;
  184.                 }
  185.             }
  186.             final=mapping[str2]+final;  
  187.         }
  188.         string getstring()
  189.         {
  190.             return final;
  191.         }
  192.         ~Barcode()
  193.         {
  194.             for(int y = 0; y<height;y++)
  195.                 delete[] data[y];
  196.             delete[] dataline;
  197.             delete[] bincode;
  198.         }
  199. };
  200. ////////////pgm.cpp//////////////
  201.  
  202. #include <iostream>
  203. #include <fstream>
  204. #include <string>
  205. #include <stdlib.h>
  206. using namespace std;
  207. class PGM{
  208.     private:
  209.         int width;
  210.         int** data;
  211.         int height;
  212.         int baseline;
  213.         bool binopen(ifstream& file_h,string filename){
  214.             int pos =file_h.tellg();
  215.             ifstream file;
  216.             file.open(filename.c_str(),ios::binary);
  217.             file.seekg(pos);
  218.             data = new int*[height];
  219.             for(int y = 0; y< height;y++){
  220.                 data[y] = new int[width];
  221.                 for(int x = 0; x< width; x++){
  222.                     char buff[2];
  223.                     file.read(buff,1);
  224.                     if((int)buff[0]<0)
  225.                         data[y][x]=(int)buff[0]+baseline+1;
  226.                     else
  227.                         data[y][x]=buff[0];
  228.                    
  229.                 }      
  230.             }
  231.             file.close();
  232.         }
  233.         bool asciiopen(ifstream & file,string filename){
  234.         }
  235.         bool startfile(string filename){
  236.             bool bin;
  237.             int dim[2];
  238.             int x=0;
  239.             string tmp;
  240.             string temp = "";
  241.             ifstream file_h;
  242.             file_h.open(filename.c_str());
  243.             getline(file_h,tmp);
  244.             bin = tmp=="P5";
  245.             getline(file_h,tmp);
  246.             getline(file_h,tmp);
  247.             for(int i = 0; i< tmp.length();i++){
  248.                 if(tmp[i]==' ') {
  249.                     i++;
  250.                     dim[x]=atoi(temp.c_str());
  251.                     x++;
  252.                     temp="";                   
  253.                 }
  254.                 temp+=tmp[i];
  255.             }
  256.             dim[x]=atoi(temp.c_str());
  257.             width = dim[0];
  258.             height = dim[1];
  259.             getline(file_h,tmp);
  260.             baseline=atoi(tmp.c_str());
  261.  
  262.             if(bin){
  263.                 binopen(file_h,filename);
  264.             }else{
  265.                 asciiopen(file_h,filename);
  266.             }
  267.             file_h.close();
  268.         }  
  269.     public:
  270.  
  271.         PGM(string filename){
  272.             startfile(filename);
  273.         }
  274.         ~PGM(){
  275.             for(int y = 0;y<height;y++){
  276.                 delete [] data[y];
  277.             }
  278.         }
  279.         int** getdata()
  280.         {
  281.         return data;
  282.             }
  283.             int getheight()
  284.             {return height;}
  285.             int getwidth()
  286.             {return width;}
  287. };
  288. //////////////////barcode.h////////////////
  289.  
  290. #ifndef _barcode_h
  291. #define _barcode_h
  292. #include "pgm.cpp"
  293. #endif
  294. ///////////////////main.cpp////////////////
  295. #include <iostream>
  296. #include <fstream>
  297. #include "barcode.h"
  298. #include "barcode.cpp"
  299. using namespace std;
  300. int main(int argc, char* argv[]) {
  301.    Barcode barcode(argv[1],20);
  302.    cout<<barcode.getstring()<<endl;
  303.    return 0;
  304. }
  305. //////////////////makefile/////////////////
  306. all:out
  307. out:main.o pgm.o
  308.     g++ main.o pgm.o -o out -O2
  309. main.o: main.cpp barcode.h pgm.o barcode.o
  310.     g++ -c main.cpp -o main.o
  311. pgm.o: pgm.cpp
  312.     g++ -c pgm.cpp -o pgm.o
  313. barcode.o: barcode.cpp pgm.cpp
  314.     g++ -c barcode.cpp -o barcode.o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement