⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blread.cpp

📁 一OCR的相关资料。.希望对研究OCR的朋友有所帮助.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/********************************************************************** * File:        blread.cpp  (Formerly pdread.c) * Description: Friend function of BLOCK to read the uscan pd file. * Author:		Ray Smith * Created:		Mon Mar 18 14:39:00 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. * **********************************************************************/#include "mfcpch.h"#include          <stdlib.h>#ifdef __UNIX__#include          <assert.h>#endif#include          "scanutils.h"#include          "fileerr.h"#include          "imgtiff.h"#include          "pdclass.h"#include          "rwpoly.h"#include          "blread.h"#define PD_EXT        ".pd"#define VEC_EXT       ".vec"     //accupage file#define HPD_EXT       ".bl"      //hand pd file                                 //unlv zone file#define UNLV_EXT            ".uzn"#define BLOCK_EXPANSION   8      //boundary expansion#define EXTERNEXTERN BOOL_EVAR (ignore_weird_blocks, TRUE, "Don't read weird blocks");static BOX convert_vec_block(                        //make non-rect block                             VEC_ENTRY *entries,     //vectors                             UINT16 entry_count,     //no of entries                             INT32 ysize,            //image size                             ICOORDELT_IT *left_it,  //block sides                             ICOORDELT_IT *right_it);/********************************************************************** * BLOCK::read_pd_file * * Read a whole pd file to make a list of blocks, or use the whole page. **********************************************************************/BOOL8 read_pd_file(                    //print list of sides                   STRING name,        //basename of file                   INT32 xsize,        //image size                   INT32 ysize,        //image size                   BLOCK_LIST *blocks  //output list                  ) {  FILE *pdfp;                    //file pointer  BLOCK *block;                  //current block  INT32 block_count;             //no of blocks  INT32 junk_count;              //no of junks to read  INT32 junks[4];                //junk elements  INT32 vertex_count;            //boundary vertices  INT32 xcoord;                  //current coords  INT32 ycoord;  INT32 prevx;                   //previous coords  INT32 prevy;  BLOCK_IT block_it = blocks;    //block iterator  ICOORDELT_LIST dummy;          //for constructor  ICOORDELT_IT left_it = &dummy; //iterator  ICOORDELT_IT right_it = &dummy;//iterator  if (read_hpd_file (name, xsize, ysize, blocks))    return TRUE;                 //succeeded  if (read_vec_file (name, xsize, ysize, blocks))    return TRUE;                 //succeeded  if (read_unlv_file (name, xsize, ysize, blocks))    return TRUE;                 //succeeded  name += PD_EXT;                //add extension  if ((pdfp = fopen (name.string (), "r")) == NULL) {                                 //make rect block    block = new BLOCK (name.string (), TRUE, 0, 0, 0, 0, xsize, ysize);    block_it.add_to_end (block); //on end of list    return FALSE;                //didn't read one  }  else {    if (fread (&block_count, sizeof (block_count), 1, pdfp) != 1)      READFAILED.error ("read_pd_file", EXIT, "Block count");    tprintf ("%d blocks in .pd file.\n", block_count);    while (block_count > 0) {      if (fread (&junk_count, sizeof (junk_count), 1, pdfp) != 1)        READFAILED.error ("read_pd_file", EXIT, "Junk count");      if (fread (&vertex_count, sizeof (vertex_count), 1, pdfp) != 1)        READFAILED.error ("read_pd_file", EXIT, "Vertex count");      block = new BLOCK;         //make a block                                 //on end of list      block_it.add_to_end (block);      left_it.set_to_list (&block->leftside);      right_it.set_to_list (&block->rightside);                                 //read a pair      get_pd_vertex (pdfp, xsize, ysize, &block->box, xcoord, ycoord);      vertex_count -= 2;         //count read ones      prevx = xcoord;      do {        if (xcoord == prevx) {          if (!right_it.empty ()) {            if (right_it.data ()->x () <= xcoord + BLOCK_EXPANSION)              right_it.data ()->set_y (right_it.data ()->y () +                BLOCK_EXPANSION);            else              right_it.data ()->set_y (right_it.data ()->y () -                BLOCK_EXPANSION);          }          right_it.            add_before_then_move (new            ICOORDELT (xcoord + BLOCK_EXPANSION,            ycoord));        }        prevx = xcoord;          //remember previous        prevy = ycoord;        get_pd_vertex (pdfp, xsize, ysize, &block->box, xcoord, ycoord);        vertex_count -= 2;       //count read ones      }      while (ycoord <= prevy);      right_it.data ()->set_y (right_it.data ()->y () - BLOCK_EXPANSION);                                 //start of left      left_it.add_to_end (new ICOORDELT (prevx - BLOCK_EXPANSION, prevy - BLOCK_EXPANSION));      do {        prevx = xcoord;          //remember previous        get_pd_vertex (pdfp, xsize, ysize, &block->box, xcoord, ycoord);        vertex_count -= 2;        if (xcoord != prevx && vertex_count > 0) {          if (xcoord > prevx)            left_it.              add_to_end (new              ICOORDELT (xcoord - BLOCK_EXPANSION,              ycoord + BLOCK_EXPANSION));          else            left_it.              add_to_end (new              ICOORDELT (xcoord - BLOCK_EXPANSION,              ycoord - BLOCK_EXPANSION));        }        else if (vertex_count == 0)          left_it.add_to_end (new ICOORDELT (prevx - BLOCK_EXPANSION,              ycoord + BLOCK_EXPANSION));      }      while (vertex_count > 0);  //until all read      while (junk_count > 0) {        if (fread (junks, sizeof (INT32), 4, pdfp) != 4)          READFAILED.error ("read_pd_file", EXIT, "Junk coords");        junk_count--;      }      block_count--;             //count read blocks    }  }  fclose(pdfp);  return TRUE;                   //read one}/********************************************************************** * get_pd_vertex * * Read a pair of coords, invert the y and clip to image limits. * Also update the bounding box. * * Read a whole pd file to make a list of blocks, or use the whole page. **********************************************************************/void get_pd_vertex(                //get new vertex                   FILE *pdfp,     //file to read                   INT32 xsize,    //image size                   INT32 ysize,    //image size                   BOX *box,       //bounding box                   INT32 &xcoord,  //output coords                   INT32 &ycoord) {  BOX new_coord;                 //expansion box                                 //get new coords  if (fread (&xcoord, sizeof (xcoord), 1, pdfp) != 1)    READFAILED.error ("read_pd_file", EXIT, "Xcoord");  if (fread (&ycoord, sizeof (ycoord), 1, pdfp) != 1)    READFAILED.error ("read_pd_file", EXIT, "Xcoord");  ycoord = ysize - ycoord;       //invert y  if (xcoord < BLOCK_EXPANSION)    xcoord = BLOCK_EXPANSION;    //clip to limits  if (xcoord > xsize - BLOCK_EXPANSION)    xcoord = xsize - BLOCK_EXPANSION;  if (ycoord < BLOCK_EXPANSION)    ycoord = BLOCK_EXPANSION;  if (ycoord > ysize - BLOCK_EXPANSION)    ycoord = ysize - BLOCK_EXPANSION;  new_coord =    BOX (ICOORD (xcoord - BLOCK_EXPANSION, ycoord - BLOCK_EXPANSION),    ICOORD (xcoord + BLOCK_EXPANSION, ycoord + BLOCK_EXPANSION));  (*box) += new_coord;}/********************************************************************** * BLOCK::read_hpd_file * * Read a whole hpd file to make a list of blocks. * Return FALSE if the .vec fiel cannot be found **********************************************************************/BOOL8 read_hpd_file(                    //print list of sides                    STRING name,        //basename of file                    INT32 xsize,        //image size                    INT32 ysize,        //image size                    BLOCK_LIST *blocks  //output list                   ) {  FILE *pdfp;                    //file pointer  PAGE_BLOCK_LIST *page_blocks;  INT32 block_no;                //no of blocks  BLOCK_IT block_it = blocks;    //block iterator  name += HPD_EXT;               //add extension  if ((pdfp = fopen (name.string (), "r")) == NULL) {    return FALSE;                //can't find it  }  fclose(pdfp);  page_blocks = read_poly_blocks (name.string ());  block_no = 0;  scan_hpd_blocks (name.string (), page_blocks, block_no, &block_it);  tprintf ("Text region count=%d\n", block_no);  return TRUE;                   //read one}/********************************************************************** * BLOCK::scan_hpd_blocks * * Read a whole hpd file to make a list of blocks. * Return FALSE if the .vec fiel cannot be found **********************************************************************/void scan_hpd_blocks(                               //print list of sides                     const char *name,              //block label                     PAGE_BLOCK_LIST *page_blocks,  //head of full pag                     INT32 &block_no,               //no of blocks                     BLOCK_IT *block_it             //block iterator                    ) {  BLOCK *block;                  //current block                                 //page blocks  PAGE_BLOCK_IT pb_it = page_blocks;  PAGE_BLOCK *current_block;  TEXT_REGION_IT tr_it;  TEXT_BLOCK *tb;  TEXT_REGION *tr;  BOX *block_box;                //from text region  for (pb_it.mark_cycle_pt (); !pb_it.cycled_list (); pb_it.forward ()) {    current_block = pb_it.data ();    if (current_block->type () == PB_TEXT) {      tb = (TEXT_BLOCK *) current_block;      if (!tb->regions ()->empty ()) {        tr_it.set_to_list (tb->regions ());        for (tr_it.mark_cycle_pt ();        !tr_it.cycled_list (); tr_it.forward ()) {

⌨️ 快捷键说明

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