reference/ocr-new/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   if (ul == NOPNT) ul = Point(0,0);
       
    47   if (lr == NOPNT) lr = Point(source->imageWidth()-1, 
       
    48 			      source->imageLength()-1);
       
    49   if (!(lr > ul)) return OTHERERROR;
       
    50 
       
    51   target->imageWidth() = lr.x() - ul.x()+1;
       
    52   target->imageLength() = lr.y() - ul.y()+1;
       
    53   target->status() = source->status();
       
    54 
       
    55   target->fMapData = new (RLEPairs*)[target->imageLength()]; 
       
    56   for (int i = 0; i <= (lr.y() - ul.y()); i++)
       
    57     {
       
    58 
       
    59       numChars= target->imageWidth() /8 + 1;  // number of char entries
       
    60       // Create a list of RLEPairs for this row and fill with buffer data
       
    61 
       
    62       rowdata = source->row(i+ul.y());
       
    63 
       
    64       target->fMapData[i] = new RLEPairs(i);  
       
    65       target->fMapData[i]->fill(&(rowdata[ul.x()/8]), numChars, i);
       
    66 
       
    67     }
       
    68 
       
    69 
       
    70 return VALID;
       
    71 }
       
    72 
       
    73 
       
    74 
       
    75 /**** RLEMap Conversion   ***/
       
    76 MapStatus convertMap(RLEMap * source,  BitMap * target, Point ul, Point lr)
       
    77 /*--------------------------------------------------------------
       
    78 Purpose: Converts an RLEMap to a BitMap;
       
    79 Arguments:    Right now just does full bit map coversion ul/lr dont work
       
    80 Return Value: Returns valid if copy did not encounter any errors.
       
    81 Effects:
       
    82 Constraints: target is a pointer to a BitMap which must be previously
       
    83 allocated with new BitMap;
       
    84 Rev: 10/24/95
       
    85 ---------------------------------------------------------------*/
       
    86 {
       
    87 
       
    88   
       
    89   RLEPairs * rmapRowData;     
       
    90   RLEPair * item;
       
    91   int numChars,startX,endX;
       
    92   
       
    93   assert(target != NULL);  // target must be allocated with new BitMap
       
    94                           // before being passed to this function.
       
    95   assert(source !=NULL);
       
    96   
       
    97   target->imageWidth() = source->imageWidth();
       
    98   target->imageLength() = source->imageLength();
       
    99   target->status() = source->status();
       
   100 
       
   101   target->fMapData = new (uchar *)[target->imageLength()]; 
       
   102  
       
   103  for (int i = 0; i < source->imageLength(); i++)
       
   104     {
       
   105 
       
   106       numChars= source->imageWidth() /8 + 1;  // number of char entries
       
   107       // Convert RLEPairs to uchar array
       
   108       rmapRowData = source->row(i);
       
   109 
       
   110       target->fMapData[i] = new uchar[numChars];
       
   111       for(int j=0; j < numChars; j++)target->fMapData[i][j] = 0;
       
   112       // convert this row from RLE to uchars
       
   113 
       
   114       ListElement *ptr = rmapRowData->first;
       
   115      
       
   116       for (; ptr != NULL; ptr = ptr->next) 
       
   117 	{
       
   118 	  item = (RLEPair *)(ptr->item);
       
   119 	  startX = item->start;	  
       
   120           endX = item->end;	  
       
   121 	  setRange(target->fMapData[i],  startX, endX);
       
   122 	}
       
   123 
       
   124     }
       
   125 
       
   126 
       
   127 return VALID;
       
   128 
       
   129 
       
   130 }
       
   131 
       
   132 
       
   133 //MapStatus convertMap(RLEMap * source,  RLEMap * target, Point ul, Point lr)
       
   134 /*--------------------------------------------------------------
       
   135 Primary Purpose:
       
   136 Arguments:
       
   137 Return Value:
       
   138 Effects:
       
   139 Constraints:
       
   140 Rev:
       
   141 ---------------------------------------------------------------*/
       
   142 //{
       
   143 
       
   144 //return EMPTY;
       
   145 //}
       
   146 
       
   147 
       
   148 
       
   149 void testConvertMap(char * filename)
       
   150 // Reads in BitMap and converts to RLEMap then prints on screen
       
   151 // Next converts back to BitMap, back to RLEMap and prints
       
   152 {
       
   153     BitMap * m = new BitMap;
       
   154     RLEMap * rm = new RLEMap;
       
   155 
       
   156    
       
   157     m->readMap(filename);
       
   158     convertMap(m,rm);
       
   159 
       
   160     cout << "length chk " << m->imageLength()<< "==" << rm->imageLength()<< endl;
       
   161     cout << "width chk " << m->imageWidth()<< "==" << rm->imageWidth() << endl;
       
   162     cout << "status chk"<<m->status() << "==" << rm->status() << endl;
       
   163 
       
   164     printMap(rm);
       
   165 
       
   166     // Now test conversion the other way
       
   167     delete m;
       
   168     m = new BitMap;
       
   169     convertMap(rm,m);
       
   170     delete rm;
       
   171     rm = new RLEMap;
       
   172     convertMap(m,rm);
       
   173     printMap(rm);
       
   174 
       
   175 
       
   176 } 
       
   177 
       
   178 
       
   179 
       
   180 
       
   181 
       
   182 
       
   183 
       
   184 
       
   185 
       
   186 
       
   187