reference/ocr-simple/convertMap.cc
changeset 0 6b8091ca909a
equal deleted inserted replaced
-1:000000000000 0:6b8091ca909a
       
     1 /**  convertMap.h
       
     2 
       
     3 Functions for converting  from one Map form to another 
       
     4 and extracting smaller maps. There are four polymorphic
       
     5 functions.   The general format is
       
     6 
       
     7 convertMap(MapType * source,MapType * target, Point ul, Point lr)
       
     8 
       
     9 MapTypes can be - BitMap or  RLEMap,
       
    10 source - the map to be converted from
       
    11 target - The map to be converted to.  Memory will be allocated
       
    12          for target's contents. 
       
    13 ul and lr mark a range to copy.   If both are the global var NOPNT 
       
    14 the entire map will  be copied.
       
    15 
       
    16 Functions return a MapStatus which will be VALID or OTHERERROR
       
    17 if there was an error in the conversion.  An error might be due
       
    18 to a user specifying an out of range ul and lr
       
    19 
       
    20 **************************************************************/
       
    21 #include "system.h"
       
    22 #include "Point.h"
       
    23 #include "BitMap.h"
       
    24 #include "RLEMap.h"
       
    25 #include "convertMap.h"
       
    26 
       
    27 /****  BitMap Conversion ***/
       
    28 
       
    29 MapStatus convertMap(BitMap * source,  RLEMap * target, Point ul, Point lr)
       
    30 /*--------------------------------------------------------------
       
    31 Primary Purpose:  Convert a bitMap to a RLEMap
       
    32 Arguments:    Right now just does full bit map coversion ul/lr dont work
       
    33 Return Value: Returns valid if copy did not encounter any errors.
       
    34 Effects:
       
    35 Constraints: target is a pointer to an RLEMap
       
    36 Rev: 10/24/95
       
    37 ---------------------------------------------------------------*/
       
    38 {
       
    39 
       
    40   uchar * rowdata;
       
    41   int numChars;
       
    42 
       
    43   assert(target != NULL);  // target must be allocated with new RLEMap
       
    44                           // before being passed to this function.
       
    45   assert(source !=NULL);  
       
    46   target->imageWidth() = source->imageWidth();
       
    47   target->imageLength() = source->imageLength();
       
    48   target->status() = source->status();
       
    49 
       
    50   target->fMapData = new (RLEPairs*)[target->imageLength()]; 
       
    51   for (int i = 0; i < source->imageLength(); i++)
       
    52     {
       
    53 
       
    54       numChars= source->imageWidth() /8 + 1;  // number of char entries
       
    55       // Create a list of RLEPairs for this row and fill with buffer data
       
    56 
       
    57       rowdata = source->row(i);
       
    58 
       
    59       target->fMapData[i] = new RLEPairs(i);  
       
    60       target->fMapData[i]->fill(rowdata, numChars, i);
       
    61 
       
    62     }
       
    63 
       
    64 
       
    65 return VALID;
       
    66 }
       
    67 
       
    68 
       
    69 
       
    70 /**** RLEMap Conversion   ***/
       
    71 MapStatus convertMap(RLEMap * source,  BitMap * target, Point ul, Point lr)
       
    72 /*--------------------------------------------------------------
       
    73 Purpose: Converts an RLEMap to a BitMap;
       
    74 Arguments:    Right now just does full bit map coversion ul/lr dont work
       
    75 Return Value: Returns valid if copy did not encounter any errors.
       
    76 Effects:
       
    77 Constraints: target is a pointer to a BitMap which must be previously
       
    78 allocated with new BitMap;
       
    79 Rev: 10/24/95
       
    80 ---------------------------------------------------------------*/
       
    81 {
       
    82 
       
    83   
       
    84   RLEPairs * rmapRowData;     
       
    85   RLEPair * item;
       
    86   int numChars,startX,endX;
       
    87   
       
    88   assert(target != NULL);  // target must be allocated with new BitMap
       
    89                           // before being passed to this function.
       
    90   assert(source !=NULL);
       
    91   
       
    92   target->imageWidth() = source->imageWidth();
       
    93   target->imageLength() = source->imageLength();
       
    94   target->status() = source->status();
       
    95 
       
    96   target->fMapData = new (uchar *)[target->imageLength()]; 
       
    97  
       
    98  for (int i = 0; i < source->imageLength(); i++)
       
    99     {
       
   100 
       
   101       numChars= source->imageWidth() /8 + 1;  // number of char entries
       
   102       // Convert RLEPairs to uchar array
       
   103       rmapRowData = source->row(i);
       
   104 
       
   105       target->fMapData[i] = new uchar[numChars];
       
   106       for(int j=0; j < numChars; j++)target->fMapData[i][j] = 0;
       
   107       // convert this row from RLE to uchars
       
   108 
       
   109       ListElement *ptr = rmapRowData->first;
       
   110       if (ptr == NULL) cout << "0" << endl;
       
   111       for (; ptr != NULL; ptr = ptr->next) 
       
   112 	{
       
   113 	  item = (RLEPair *)(ptr->item);
       
   114 	  startX = item->start;	  
       
   115           endX = item->end;	  
       
   116 	  setRange(target->fMapData[i],  startX, endX);
       
   117 	}
       
   118       cout << endl << endl;
       
   119 
       
   120     }
       
   121 
       
   122 
       
   123 return VALID;
       
   124 
       
   125 
       
   126 }
       
   127 
       
   128 
       
   129 //MapStatus convertMap(RLEMap * source,  RLEMap * target, Point ul, Point lr)
       
   130 /*--------------------------------------------------------------
       
   131 Primary Purpose:
       
   132 Arguments:
       
   133 Return Value:
       
   134 Effects:
       
   135 Constraints:
       
   136 Rev:
       
   137 ---------------------------------------------------------------*/
       
   138 //{
       
   139 
       
   140 //return EMPTY;
       
   141 //}
       
   142 
       
   143 
       
   144 
       
   145 void testConvertMap(char * filename)
       
   146 // Reads in BitMap and converts to RLEMap then prints on screen
       
   147 // Next converts back to BitMap, back to RLEMap and prints
       
   148 {
       
   149     BitMap * m = new BitMap;
       
   150     RLEMap * rm = new RLEMap;
       
   151 
       
   152    
       
   153     m->readMap(filename);
       
   154     convertMap(m,rm);
       
   155 
       
   156     cout << "length chk " << m->imageLength()<< "==" << rm->imageLength()<< endl;
       
   157     cout << "width chk " << m->imageWidth()<< "==" << rm->imageWidth() << endl;
       
   158     cout << "status chk"<<m->status() << "==" << rm->status() << endl;
       
   159 
       
   160     printMap(rm);
       
   161 
       
   162     // Now test conversion the other way
       
   163     delete m;
       
   164     m = new BitMap;
       
   165     convertMap(rm,m);
       
   166     delete rm;
       
   167     rm = new RLEMap;
       
   168     convertMap(m,rm);
       
   169     printMap(rm);
       
   170 
       
   171 
       
   172 } 
       
   173 
       
   174 
       
   175 
       
   176 
       
   177 
       
   178 
       
   179 
       
   180 
       
   181 
       
   182 
       
   183