reference/ocr-simple/project.cc
author viric@llimona
Thu, 18 May 2006 23:12:51 +0200
changeset 0 6b8091ca909a
permissions -rw-r--r--
Init from working directory of svn repository.

#include <stdarg.h>
#include "tcl_interface.h"
#include "project.h"
#include "histogram.h"
#include "bitmap.h"
#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include "status_message.h"

int docommand(char* fmt, ...);



void draw_bitmap(int x, int y, char* xbm_file)
{
  docommand(".t.f.c create bitmap %d %d -bitmap @%s", x, y, xbm_file);
  docommand("update");
}

#define LINE_SKIP 5
#define DRAW_RAYS 0
#define DEBUG_PROJECT_HISTOGRAM 1
#define DRAW_PIXELS 0
#define PRINT_STATUS 1

Histogram* project_histogram(RLEMap* r, double cut_angle)
{
  if (ENABLE_USER_INTERFACE)
      {
	docommand("update");
	if(DRAW_RAYS)
	    {
	      docommand(".main_window.display.work_space delete project_ray");
	    }
	if(DRAW_PIXELS)
	    {
	      docommand(".t.f.c delete project_pixels");
	    }
      }
  int num_values = r->imageLength() / LINE_SKIP;
  int* ret_array = (int*) malloc (sizeof(int) * num_values);
  for(int current_value = 0; current_value < num_values; current_value++)
    {
      ret_array[current_value] = calculate_one_rle_ray_weight(r, cut_angle, current_value*LINE_SKIP);  /* the real work */
    }
  Histogram* h = new Histogram(num_values - 1, ret_array, cut_angle);
  if(PRINT_STATUS && ENABLE_USER_INTERFACE)
      {
	set_status("Deskewing: @%.3lf degrees, standard deviation = %.3lf", cut_angle, h->get_standard_dev());
      }
  if(0)
    h->display();
  return h;
}

inline double deg_to_rad(double deg)
{
  return deg * (M_PI / 180.00);
}

int PRINT_RAYS = 0;

int calculate_one_rle_ray_weight(RLEMap* rlemap, double cut_angle, int row_num)
{
  /* cuts through b at cut_angle (cut angle in DEG) , adding up the weights */ 
  /* of the pixels in the cut line */
  /* RLEMap methodology:  Translate the angular slope into rise/run slope,
     find out how many bits (how much run) to read horizontally before 
     jumping upwards one bit.  Make use of RLEMap::pixels_between(row,st,fi) */
  /* possible optimizations: calculate this initial stuff "run_bits" "slope" 
     once for each histogram, pass as args to this */


  double slope;
  double float_run_bits;
  int y_update;
  double new_x;
  double rad_cut_angle = deg_to_rad(cut_angle);
  
  slope = tan(rad_cut_angle);
  float_run_bits = fabs((((double)1)/sin(rad_cut_angle)) * cos(rad_cut_angle));
  
  int ray_weight = 0;

  double cur_x = 0;
  int cur_y = row_num;

  if(cut_angle < 0) 
    y_update =  -1;
  else
    y_update = 1;

  
  int image_height = rlemap->imageLength();
  int image_width = rlemap->imageWidth();

  while(((new_x = cur_x + float_run_bits) < image_width) &&
	(cur_y >= 0) && (cur_y < image_height))
    {
      /* watch out about going past rlemap bounds */
      ray_weight += rlemap->pixels_between((int)cur_x, (int)new_x, cur_y);
      cur_x = new_x + 1;
      cur_y = cur_y + y_update;
    }

  /* and once more for posterity */
  if((cur_y >= 0) && (cur_y < image_height) && (cur_x < image_width))
    ray_weight += rlemap->pixels_between((int)cur_x, image_width, cur_y);
  
  if(1)
    if(DRAW_RAYS)
	{
	  docommand(".main_window.display.work_space create line %d %d %d %d -fill blue -tags {project_ray IMAGE_TAG}", 0, row_num, (int)cur_x, (int)cur_y);
	} 
  if(PRINT_RAYS)
    printf("---Ray weight for cut angle %lf = %d\n", cut_angle, ray_weight);
  return ray_weight;
}