intproto.cpp

来自「一个google的OCR源码」· C++ 代码 · 共 1,826 行 · 第 1/4 页

CPP
1,826
字号
              1, File) != 1)      cprintf("Bad read of inttemp!\n");    if (swap)      reverse32 (&Templates->NumClasses);  }  for (i = 0; i < unicharset_size; ++i) {    if (fread(&Templates->IndexFor[i], sizeof(CLASS_INDEX), 1, File) != 1)      cprintf("Bad read of inttemp!\n");  }  for (i = 0; i < NumClassesIn (Templates); ++i) {    if (fread(&Templates->ClassIdFor[i], sizeof(CLASS_ID), 1, File) != 1)      cprintf("Bad read of inttemp!\n");  }  if (swap) {    for (i = 0; i < MAX_CLASS_ID + 1; i++)      reverse16 (&Templates->IndexFor[i]);    for (i = 0; i < MAX_NUM_CLASSES; i++)      reverse32 (&Templates->ClassIdFor[i]);  }  /* then read in the class pruners */  for (i = 0; i < NumClassPrunersIn (Templates); i++) {    Pruner = (CLASS_PRUNER) Emalloc (sizeof (CLASS_PRUNER_STRUCT));    if ((nread =      fread ((char *) Pruner, 1, sizeof (CLASS_PRUNER_STRUCT),      File)) != sizeof (CLASS_PRUNER_STRUCT))      cprintf ("Bad read of inttemp!\n");    if (swap) {      for (j = 0; j < NUM_CP_BUCKETS; j++) {        for (x = 0; x < NUM_CP_BUCKETS; x++) {          for (y = 0; y < NUM_CP_BUCKETS; y++) {            for (z = 0; z < WERDS_PER_CP_VECTOR; z++) {              reverse32 (&Pruner[j][x][y][z]);            }          }        }      }    }    Templates->ClassPruner[i] = Pruner;  }  /* then read in each class */  for (i = 0; i < NumClassesIn (Templates); i++) {    /* first read in the high level struct for the class */    Class = (INT_CLASS) Emalloc (sizeof (INT_CLASS_STRUCT));    if (fread(&Class->NumProtos, sizeof(Class->NumProtos), 1, File) != 1 ||        fread(&Class->NumProtoSets, sizeof(Class->NumProtoSets), 1, File) != 1 ||        fread(&Class->NumConfigs, sizeof(Class->NumConfigs), 1, File) != 1)      cprintf ("Bad read of inttemp!\n");    if (version_id == 0) {      // Only version 0 writes 5 pointless pointers to the file.      for (j = 0; j < 5; ++j) {        int junk;        if (fread(&junk, sizeof(junk), 1, File) != 1)          cprintf ("Bad read of inttemp!\n");      }    }    for (j = 0; j < MAX_NUM_CONFIGS; ++j) {      if (fread(&Class->ConfigLengths[j], sizeof(uinT16), 1, File) != 1)        cprintf ("Bad read of inttemp!\n");    }    if (swap) {      reverse16 (&Class->NumProtos);      for (j = 0; j < MAX_NUM_CONFIGS; j++)        reverse16 (&Class->ConfigLengths[j]);    }    ClassForIndex (Templates, i) = Class;    /* then read in the proto lengths */    Lengths = (uinT8 *) Emalloc (sizeof (uinT8) *      MaxNumIntProtosIn (Class));    if ((nread = fread ((char *) Lengths, sizeof (uinT8),      MaxNumIntProtosIn (Class),      File)) != MaxNumIntProtosIn (Class))      cprintf ("Bad read of inttemp!\n");    Class->ProtoLengths = Lengths;    /* then read in the proto sets */    for (j = 0; j < NumProtoSetsIn (Class); j++) {      ProtoSet = (PROTO_SET) Emalloc (sizeof (PROTO_SET_STRUCT));      if ((nread =        fread ((char *) ProtoSet, 1, sizeof (PROTO_SET_STRUCT),        File)) != sizeof (PROTO_SET_STRUCT))        cprintf ("Bad read of inttemp!\n");      if (swap) {        for (x = 0; x < NUM_PP_PARAMS; x++)          for (y = 0; y < NUM_PP_BUCKETS; y++)            for (z = 0; z < WERDS_PER_PP_VECTOR; z++)              reverse32 (&ProtoSet->ProtoPruner[x][y][z]);        for (x = 0; x < PROTOS_PER_PROTO_SET; x++)          for (y = 0; y < WERDS_PER_CONFIG_VEC; y++)            reverse32 (&ProtoSet->Protos[x].Configs[y]);      }      ProtoSetIn (Class, j) = ProtoSet;    }  }  return (Templates);}                                /* ReadIntTemplates *//*---------------------------------------------------------------------------*/#ifndef GRAPHICS_DISABLEDvoid ShowMatchDisplay() {/* **	Parameters: none **	Globals: **		FeatureShapes	display list containing feature matches **		ProtoShapes	display list containing proto matches **	Operation: This routine sends the shapes in the global display **		lists to the match debugger window. **	Return: none **	Exceptions: none **	History: Thu Mar 21 15:47:33 1991, DSJ, Created. */  void *window;  /* Size of drawable */  if (IntMatchWindow == NULL) {    IntMatchWindow = c_create_window ("IntMatchWindow", 50, 200,      520, 520,      -130.0, 130.0, -130.0, 130.0);    SVMenuNode* popup_menu = new SVMenuNode();    popup_menu->AddChild("Debug Adapted classes", IDA_ADAPTIVE,                         "x", "Class to debug");    popup_menu->AddChild("Debug Static classes", IDA_STATIC,                         "x", "Class to debug");    popup_menu->AddChild("Debug Both", IDA_BOTH,                         "x", "Class to debug");    popup_menu->BuildMenu(IntMatchWindow, false);  }  else    c_clear_window(IntMatchWindow);  window = IntMatchWindow;  c_line_color_index(window, Grey);  /* Default size of drawing */  if (NormMethod == baseline) {    c_move (window, -1000.0, INT_BASELINE);    c_draw (window, 1000.0, INT_BASELINE);    c_move (window, -1000.0, INT_DESCENDER);    c_draw (window, 1000.0, INT_DESCENDER);    c_move (window, -1000.0, INT_XHEIGHT);    c_draw (window, 1000.0, INT_XHEIGHT);    c_move (window, -1000.0, INT_CAPHEIGHT);    c_draw (window, 1000.0, INT_CAPHEIGHT);    c_move (window, INT_MIN_X, -1000.0);    c_draw (window, INT_MIN_X, 1000.0);    c_move (window, INT_MAX_X, -1000.0);    c_draw (window, INT_MAX_X, 1000.0);  }  else {    c_move (window, INT_XCENTER - INT_XRADIUS, INT_YCENTER - INT_YRADIUS);    c_draw (window, INT_XCENTER + INT_XRADIUS, INT_YCENTER - INT_YRADIUS);    c_move (window, INT_XCENTER - INT_XRADIUS, INT_YCENTER + INT_YRADIUS);    c_draw (window, INT_XCENTER + INT_XRADIUS, INT_YCENTER + INT_YRADIUS);    c_move (window, INT_XCENTER - INT_XRADIUS, INT_YCENTER - INT_YRADIUS);    c_draw (window, INT_XCENTER - INT_XRADIUS, INT_YCENTER + INT_YRADIUS);    c_move (window, INT_XCENTER + INT_XRADIUS, INT_YCENTER - INT_YRADIUS);    c_draw (window, INT_XCENTER + INT_XRADIUS, INT_YCENTER + INT_YRADIUS);    c_move(window, INT_MIN_X, INT_MIN_Y);    c_draw(window, INT_MIN_X, INT_MAX_Y);    c_move(window, INT_MIN_X, INT_MIN_Y);    c_draw(window, INT_MAX_X, INT_MIN_Y);    c_move(window, INT_MAX_X, INT_MAX_Y);    c_draw(window, INT_MIN_X, INT_MAX_Y);    c_move(window, INT_MAX_X, INT_MAX_Y);    c_draw(window, INT_MAX_X, INT_MIN_Y);  }  IntMatchWindow->ZoomToRectangle(INT_MIN_X, INT_MIN_Y,                                  INT_MAX_X, INT_MAX_Y);}                                /* ShowMatchDisplay */#endif/*---------------------------------------------------------------------------*/void WriteIntTemplates(FILE *File, INT_TEMPLATES Templates,                       const UNICHARSET& target_unicharset) {/* **	Parameters: **		File		open file to write templates to **		Templates	templates to save into File **	Globals: none **	Operation: This routine writes Templates to File.  The format **		is an efficient binary format.  File must already be open **		for writing. **	Return: none **	Exceptions: none **	History: Wed Feb 27 11:48:46 1991, DSJ, Created. */  int i, j;  INT_CLASS Class;  int unicharset_size = target_unicharset.size();  int version_id = -1;  // When negated by the reader -1 becomes +1 etc.  /* first write the high level template struct */  fwrite(&unicharset_size, sizeof(unicharset_size), 1, File);  fwrite(&version_id, sizeof(version_id), 1, File);  fwrite(&Templates->NumClassPruners, sizeof(Templates->NumClassPruners),         1, File);  fwrite(&Templates->NumClasses, sizeof(Templates->NumClasses), 1, File);  fwrite(&Templates->IndexFor[0], sizeof(Templates->IndexFor[0]),         unicharset_size, File);  fwrite(&Templates->ClassIdFor[0], sizeof(Templates->ClassIdFor[0]),         NumClassesIn(Templates), File);  /* then write out the class pruners */  for (i = 0; i < NumClassPrunersIn (Templates); i++)    fwrite(Templates->ClassPruner[i],           sizeof(CLASS_PRUNER_STRUCT), 1, File);  /* then write out each class */  for (i = 0; i < NumClassesIn(Templates); i++) {    Class = ClassForIndex(Templates, i);    /* first write out the high level struct for the class */    fwrite(&Class->NumProtos, sizeof(Class->NumProtos), 1, File);    fwrite(&Class->NumProtoSets, sizeof(Class->NumProtoSets), 1, File);    fwrite(&Class->NumConfigs, sizeof(Class->NumConfigs), 1, File);    for (j = 0; j < MAX_NUM_CONFIGS; ++j) {      fwrite(&Class->ConfigLengths[j], sizeof(uinT16), 1, File);    }    /* then write out the proto lengths */    fwrite ((char *) (Class->ProtoLengths), sizeof (uinT8),      MaxNumIntProtosIn (Class), File);    /* then write out the proto sets */    for (j = 0; j < NumProtoSetsIn (Class); j++)      fwrite ((char *) ProtoSetIn (Class, j),        sizeof (PROTO_SET_STRUCT), 1, File);  }}                                /* WriteIntTemplates *//**----------------------------------------------------------------------------              Private Code----------------------------------------------------------------------------**//*---------------------------------------------------------------------------*/FLOAT32 BucketStart(int Bucket, FLOAT32 Offset, int NumBuckets) {/* **	Parameters: **		Bucket		bucket whose start is to be computed **		Offset		offset used to map params to buckets **		NumBuckets	total number of buckets **	Globals: none **	Operation: This routine returns the parameter value which **		corresponds to the beginning of the specified bucket. **		The bucket number should have been generated using the **		BucketFor() function with parameters Offset and NumBuckets. **	Return: Param value corresponding to start position of Bucket. **	Exceptions: none **	History: Thu Feb 14 13:24:33 1991, DSJ, Created. */  return (((FLOAT32) Bucket / NumBuckets) - Offset);}                                /* BucketStart *//*---------------------------------------------------------------------------*/FLOAT32 BucketEnd(int Bucket, FLOAT32 Offset, int NumBuckets) {/* **	Parameters: **		Bucket		bucket whose end is to be computed **		Offset		offset used to map params to buckets **		NumBuckets	total number of buckets **	Globals: none **	Operation: This routine returns the parameter value which **		corresponds to the end of the specified bucket. **		The bucket number should have been generated using the **		BucketFor() function with parameters Offset and NumBuckets. **	Return: Param value corresponding to end position of Bucket. **	Exceptions: none **	History: Thu Feb 14 13:24:33 1991, DSJ, Created. */  return (((FLOAT32) (Bucket + 1) / NumBuckets) - Offset);}                                /* BucketEnd *//*---------------------------------------------------------------------------*/void DoFill(FILL_SPEC *FillSpec,            CLASS_PRUNER Pruner,            register uinT32 ClassMask,            register uinT32 ClassCount,            register uinT32 WordIndex) {/* **	Parameters: **		FillSpec	specifies which bits to fill in pruner **		Pruner		class pruner to be filled **		ClassMask	indicates which bits to change in each word **		ClassCount	indicates what to change bits to **		WordIndex	indicates which word to change **	Globals: none **	Operation: This routine fills in the section of a class pruner **		corresponding to a single x value for a single proto of **		a class. **	Return: none **	Exceptions: none **	History: Tue Feb 19 11:11:29 1991, DSJ, Created. */  register int X, Y, Angle;  register uinT32 OldWord;  X = FillSpec->X;  if (X < 0)    X = 0;  if (X >= NUM_CP_BUCKETS)    X = NUM_CP_BUCKETS - 1;  if (FillSpec->YStart < 0)    FillSpec->YStart = 0;  if (FillSpec->YEnd >= NUM_CP_BUCKETS)    FillSpec->YEnd = NUM_CP_BUCKETS - 1;  for (Y = FillSpec->YStart; Y <= FillSpec->YEnd; Y++)    for (Angle = FillSpec->AngleStart;  TRUE; CircularIncrement (Angle, NUM_CP_BUCKETS)) {    OldWord = Pruner[X][Y][Angle][WordIndex];    if (ClassCount > (OldWord & ClassMask)) {      OldWord &= ~ClassMask;      OldWord |= ClassCount;      Pruner[X][Y][Angle][WordIndex] = OldWord;    }    if (Angle == FillSpec->AngleEnd)      break;  }}                                /* DoFill *//*---------------------------------------------------------------------------*/BOOL8 FillerDone(TABLE_FILLER *Filler) {/* **	Parameters: **		Filler		table filler to check if done **	Globals: none **	Operation: Return TRUE if the specified table filler is done, i.e. **		if it has no more lines to fill. **	Return: TRUE if no more lines to fill, FALSE otherwise. **	Exceptions: none **	History: Tue Feb 19 10:08:05 1991, DSJ, Created. */  FILL_SWITCH *Next;  Next = &(Filler->Switch[Filler->NextSwitch]);  if (Filler->X > Next->X && Next->Type == LastSwitch)    return (TRUE);  else    return (FALSE);}                                /* FillerDone *//*---------------------------------------------------------------------------*/voidFillPPCircularBits (uinT32 ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR],int Bit, FLOAT32 Center, FLOAT32 Spread) {/* **	Parameters: **		ParamTable	table of bit vectors, one per param bucket **		Bit		bit position in vectors to be filled **		Center		center of filled area **		Spread		spread of filled area **	Globals: none **	Operation: This routine sets Bit in each bit vector whose **		bucket lies within the range Center +- Spread.  The fill **		is done for a circular dimension, i.e. bucket 0 is adjacent **		to the last bucket.  It is assumed that Center and Spread **		are expressed in a circular coordinate system whose range **		is 0 to 1. **	Return: none **	Exceptions: none **	History: Tue Oct 16 09:26:54 1990, DSJ, Created. */  int i, FirstBucket, LastBucket;  if (Spread > 0.5)    Spread = 0.5;  FirstBucket = (int) floor ((Center - Spread) * NUM_PP_BUCKETS);  if (FirstBucket < 0)    FirstBucket += NUM_PP_BUCKETS;  LastBucket = (int) floor ((Center + Spread) * NUM_PP_BUCKETS);  if (LastBucket >= NUM_PP_BUCKETS)    LastBucket -= NUM_PP_BUCKETS;  if (LearningDebugLevel >= 2)    cprintf ("Circular fill from %d to %d", FirstBucket, LastBucket);  for (i = FirstBucket; TRUE; CircularIncrement (i, NUM_PP_BUCKETS)) {    SET_BIT (ParamTable[i], Bit);    /* exit loop after we have set the bit for the last bucket */    if (i == LastBucket)      break;  }}                                /* FillPPCircularBits *//*---------------------------------------------------------------------------*/voidFillPPLinearBits (uinT32 ParamTable[NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR],int Bit, FLOAT32 Center, FLOAT32 Spread) {/* **	Parameters: **		ParamTable	table of bit vectors, one per param bucket **		Bit		bit number being filled **		Center		center of filled area **		Spread		spread of filled area **	Globals: none **	Operation: This routine sets Bit in each bit vector whose **		bucket lies within the range Center +- Spread.  The fill **		is done for a linear dimension, i.e. there is no wrap-around **		for this dimension.  It is assumed that Center and Spread **		are expressed in a linear coordinate system whose range **		is approximately 0 to 1.  Values outside this range will **		be clipped. **	Return: none **	Exceptions: none **	History: Tue Oct 16 09:26:54 1990, DSJ, Created. */  int i, FirstBucket, LastBucket;  FirstBucket = (int) floor ((Center - Spread) * NUM_PP_BUCKETS);  if (FirstBucket < 0)    FirstBucket = 0;  LastBucket = (int) floor ((Center + Spread) * NUM_PP_BUCKETS);  if (LastBucket >= NUM_PP_BUCKETS)    LastBucket = NUM_PP_BUCKETS - 1;  if (LearningDebugLevel >= 2)    cprintf ("Linear fill from %d to %d", FirstBucket, LastBucket);  for (i = FirstBucket; i <= LastBucket; i++)    SET_BIT (ParamTable[i], Bit);}                                /* FillPPLinearBits *//*---------------------------------------------------------------------------*/#ifndef GRAPHICS_DISABLEDCLASS_ID GetClassToDebug(const char *Prompt) {/* **	Parameters: **		Prompt	prompt to print while waiting for input from window **	Globals: none **	Operation: This routine prompts the user with Prompt and waits **		for the user to enter something in the debug window. **	Return: Character entered in the debug window. **	Exceptions: none **	History: Thu Mar 21 16:55:13 1991, DSJ, Created. */  tprintf("%s\n", Prompt);  SVEvent* ev;  SVEventType ev_type;  // Wait until a click or popup event.  do {    ev = IntMatchWindow->AwaitEvent(SVET_ANY);    ev_type = ev->type;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?