#!/usr/bin/perl

########################################################################
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# This program is distributed in the hope that it will be useful,      #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         #
# GNU General Public License for more details at http://www.gnu.org    #
########################################################################

#use diagnostics;

use constant VERSION => 0.1.1;

#use POSIX;
use locale;
use Env;
use Tk;
#use Tk::ErrorDialog;
require Tk::LabFrame;
require Tk::NoteBook;
require Tk::Dialog;
require Tk::ProgressBar;
require Tk::Entry;

use lib "$HOME/bin"; #where to look for Exactocr.pm module
use Exactocr 0.1; #my module with exact match OCR algorithm


#needed by spinner sub
$def_bitmaps = 0;

$min_space_width = 6;
#def $min_space_width - default setting of the minimum pixel width of the space between words
$max_diacr_dist  = 3;
#def $max_diacr_dist - default setting of the maximum pixel distance of the diacritics from the belonging char
$max_char_over   = 4;
#def $max_char_over - default setting of the maximum vertical pixel overlap of neighbouring italics characters
$min_height      = 10;
#def $min_height - default setting of the minimum pixel height of one subtitle row - must be greater
#			than the diacritics height plus maximum diacritics distance from the character

$check_for_I = 0; #whether the I letter was already entered - if no warning l/I is diplayed

$pix_size = 1;
#def pix_size - multiple of pixel size for displaying PGM subtitle preview

$pgm_min = 5;
#def pgm_min - time in minutes to generate from the subtitle for preview

$txt_sub_overwrite = false;
#def txt_sub_overwrite - for testing whether the second book_page should be activated or disabled

$char_in_database = 0;
$preview_shaded = 0;
$rectangle_drawn = 0;
$lines_drawn = 0;
$pgm_drawn = 0;

#*******************************************************************
# GUI   W I D G E T S    D E F I N I T I O N S
#*******************************************************************


#============================== PGM subtitles generation Window ==============================================

$mw = MainWindow -> new();
$mw -> title("pgm2srt - DVD subtitles conversion into SRT (txt) format");
#$mw -> useinputmethods(1);

$note_book = $mw -> NoteBook() -> pack( -expand => 1, -fill => 'both' );

$page1 = $note_book -> add( 'page1', 
			    -label => 'Files and Paths',
			  );
$page2 = $note_book -> add( 'page2',
			    -label => 'PGM subtitles generation',
			    -state => 'disabled',
			    -raisecmd => \&show_pgm_sub
			  );

$page3 = $note_book -> add( 'page3',
			    -label => 'PGM to SRT - OCR',
			    -state => 'disabled'
			  );

#$page4 = $note_book -> add( 'page4',
#			    -label => 'SRT subtitles check',
#			    -state => 'disabled'
#			  );
			  
#******* PAGE 1 ***********************************

#------- Subtitles load dialog box ----------------
$page1_frame1 = $page1 -> LabFrame( -label => 'File containing DVD subtitles (generated e.g. by tcextract)',
				    -labelside => 'acrosstop'
				  ) -> pack( -anchor => 'nw');

$enter_subtitles_file = $page1_frame1 -> Entry( -width => 80 ) -> pack( -side => 'left' );
#$ok_button1 = $page1_frame1 -> Button( -text => 'OK',
#					       -padx => 3,
#					       -pady => 3,
#					       -command => \&check_input_file
#					     ) -> pack( -side => 'left' );
$open_browse_button1 = $page1_frame1 -> Button( -text => 'Browse...',
					       -padx => 3,
					       -pady => 3,
					       -command => \&open_file_dialog
					     ) -> pack( -side => 'left' );

#------- TXT Subtitles save dialog box
$page1_frame2 = $page1 -> LabFrame( -label => 'Save final TXT subtitles as',
				    -labelside => 'acrosstop'
				  ) -> pack( -anchor => 'w');

$enter_save_file = $page1_frame2 -> Entry( -width => 80 ) -> pack( -side => 'left' );
#$ok_button2 = $page1_frame2 -> Button( -text => 'OK',
#					       -padx => 3,
#					       -pady => 3,
#					       -command => \&check_save_file
#					     ) -> pack( -side => 'left' );
$save_browse_button1 = $page1_frame2 -> Button( -text => 'Browse...',
					       -padx => 3,
					       -pady => 3,
					       -command => \&save_file_dialog
					     ) -> pack( -side => 'left' );

#------- OCR database load dialog box
$page1_frame3 = $page1 -> LabFrame( -label => 'Load OCR database from file',
				    -labelside => 'acrosstop'
				  ) -> pack( -anchor => 'w');

$enter_ocr_load_file = $page1_frame3 -> Entry( -width => 80 ) -> pack( -side => 'left' );
#$ok_button3 = $page1_frame3 -> Button( -text => 'OK',
#					       -padx => 3,
#					       -pady => 3,
#					       -command => \&check_ocr_file
#					     ) -> pack( -side => 'left' );
$open_browse_button2 = $page1_frame3 -> Button( -text => 'Browse...',
					       -padx => 3,
					       -pady => 3,
					       -command => \&open_ocr_dialog
					     ) -> pack( -side => 'left' );

#------- OCR database save dialog box
$page1_frame4 = $page1 -> LabFrame( -label => 'Save OCR database as',
				    -labelside => 'acrosstop'
				  ) -> pack( -anchor => 'w');

$enter_ocr_save_file = $page1_frame4 -> Entry( -width => 80 ) -> pack( -side => 'left' );
#$ok_button4 = $page1_frame4 -> Button( -text => 'OK',
#					       -padx => 3,
#					       -pady => 3,
#					       -command => \&check_save_ocr
#					     ) -> pack( -side => 'left' );
$save_browse_button2 = $page1_frame4 -> Button( -text => 'Browse...',
					       -padx => 3,
					       -pady => 3,
					       -command => \&save_ocr_dialog
					     ) -> pack( -side => 'left' );

$check_files_btn = $page1 -> Button( -text => 'Check files and continue',
					       -padx => 3,
					       -pady => 3,
					       -command => \&check_files
					     ) -> pack( -side => 'top' );



#******* PAGE 2 ***********************************

#-------- subtitle2pgm -c parameter choice --------

$page2_frame1 = $page2 -> Frame() -> pack( -side => 'left', -anchor => 'nw', -ipady => 5, -ipadx => 5 );

$page2_frame1_1 = $page2_frame1 -> LabFrame( -label => 'Choose -c parametr for subtitle2pgm',
				    -labelside => 'acrosstop'
				  ) -> pack( -anchor => 'nw', -fill => 'x' );

$sub2pgm_parm = 0;
$sub2pgm_c[1]="255,0,255,255";
$sub2pgm_c[2]="255,255,0,255";
$sub2pgm_c[3]="255,255,255,0";
$sub2pgm_c[4]="0,255,255,255";
$sub2pgm_c[5]="0,0,255,255";
$sub2pgm_c[6]="255,0,0,255";
$sub2pgm_c[7]="255,255,0,0";
$sub2pgm_c[8]="0,255,0,255";
$sub2pgm_c[9]="0,255,255,0";
$sub2pgm_c[10]="255,0,255,0";

$sub2pgm_parm1 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[1],
						-value => 1,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm2 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[2],
						-value => 2,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm3 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[3],
						-value => 3,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm4 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[4],
						-value => 4,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm5 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[5],
						-value => 5,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm6 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[6],
						-value => 6,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm7 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[7],
						-value => 7,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm8 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[8],
						-value => 8,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm9 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[9],
						-value => 9,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm10 = $page2_frame1_1 -> Radiobutton( -variable => \$sub2pgm_parm,
						-text => $sub2pgm_c[10],
						-value => 10,
						-command => \&show_pgm_sub
						);

$sub2pgm_parm1 -> pack( -anchor => 'w');
$sub2pgm_parm2 -> pack( -anchor => 'w');
$sub2pgm_parm3 -> pack( -anchor => 'w');
$sub2pgm_parm4 -> pack( -anchor => 'w');
$sub2pgm_parm5 -> pack( -anchor => 'w');
$sub2pgm_parm6 -> pack( -anchor => 'w');
$sub2pgm_parm7 -> pack( -anchor => 'w');
$sub2pgm_parm8 -> pack( -anchor => 'w');
$sub2pgm_parm9 -> pack( -anchor => 'w');
$sub2pgm_parm10 -> pack( -anchor => 'w');

#--------------------------- Previous/Next PGM-subtitle buttons ----------------------------------------------------

$page2_frame1_2 = $page2_frame1 -> Frame() -> pack( -fill => 'x' );

$prev_pgm_button = $page2_frame1_2 -> Button( -text => 'Previous PGM subtitle',
					      -command => sub { 
								if ( $pgm_min >= 2 and $sub2pgm_parm ) {
								    $pgm_min -= 2;
								    &show_pgm_sub;
								}
							      }
					    );

$next_pgm_button = $page2_frame1_2 -> Button( -text => 'Next PGM subtitle',
					      -command => sub { 
								if ( $pgm_min <= 57 and $sub2pgm_parm ) {
								    $pgm_min += 2;
								    &show_pgm_sub;
								}
							      }
					    );

$prev_pgm_button -> pack( -side => 'left');
$next_pgm_button -> pack( -side => 'left', -fill => 'x', -expand => 'y' );

#----------------------------------------------------------------------------------------------------------


#--------------------------- Continue button ----------------------------------------------------

$continue_button = $page2_frame1 -> Button( -text => ' >>> Continue (generate PGM subtitles) >>> ',
					    -command => sub { &make_subtitles; }
					  );

$continue_button -> pack( -side => 'left', -fill => 'x', -expand => 'y' );

#----------------------------------------------------------------------------------------------------------


#--------------------------- PGM subtitle display area ----------------------------------------------------

$sub_disp = $page2 -> Canvas() -> pack ( -side => 'left', -anchor => 'n',
					 -ipady => 5, -ipadx => 5, -fill => 'both',
					 -expand => 'y' );




#----------------------------------------------------------------------------------------------------------

#***************************************************


#******* PAGE 3 ******************************************************************

#------------------- PGM subtitle view area ---------------



$page3_main_frame1 = $page3 -> LabFrame( -label => 'PGM subtitle view',
				    -labelside => 'acrosstop'
				  ) -> pack( -expand => 'y', -fill => 'both' );

$sub_view = $page3_main_frame1 -> Canvas( -height => 100
					) -> pack ( -side => 'top', -anchor => 'n',
						    -ipady => 5, -ipadx => 5, -fill => 'both',
						    -expand => 'y' );

$page3_main_frame3 = $page3 -> Frame() -> pack( -fill => 'both' );

#$main_frame3_label1 = $page3_main_frame3 -> Label( -text => '+/- height of the text row') -> pack( -side => 'left' );
#&NumEntry($page3_main_frame3,-textvariable=>\$row_height,-width=>0,-minvalue=>10,-maxvalue=>99) -> pack( -side => 'left');
#$main_frame3_label2 = $page3_main_frame3 -> Label( -text => 'Move up/down the text row') -> pack( -side => 'left');
#&NumEntry($page3_main_frame3,-textvariable=>\$row_position,-width=>0,-minvalue=>0,-maxvalue=>999) -> pack( -side => 'left');


$page3_main_frame2 = $page3 -> Frame() -> pack( -fill => 'both' );

$page3_frame1 = $page3_main_frame2 -> Frame() -> pack ( -side => 'left' );

$page3_frame2 = $page3_main_frame2 -> Frame() -> pack ( -side => 'right', -fill => 'both', -expand => 'y' );

$txt_sub_view = $page3_frame2 -> Scrolled('Text', -width => 50, -height => 10, -scrollbars => 'e' ) -> pack( -fill => 'both', -expand => 'y' );

$err_log_frame = $page3_frame2 -> LabFrame ( -label => 'Error log view',
					     -labelside => 'acrosstop'
					   ) -> pack( -fill => 'both' );

$err_log_view = $err_log_frame -> Scrolled('Text', -width => 50, -height => 4, -scrollbars => 'e' ) -> pack( -fill => 'both' );

$resume_btn = $page3_frame2 -> Button( -text => 'Resume',
					       -padx => 3,
					       -pady => 3,
					       -command => \&look_for_pix_char
					     ) -> pack( -side => 'left' );

$skip_btn = $page3_frame2 -> Button( -text => 'Skip the subtitle',
					       -padx => 3,
					       -pady => 3,
					       -command => \&skip_sub
					     ) -> pack( -side => 'left' );

$txt_btn2 = $page3_frame2 -> Button( -text => 'Save and exit',
					       -padx => 3,
					       -pady => 3,
					       -command => \&save_exit
					     ) -> pack( -side => 'left' );

#--------------------------------- pix row moving buttons ------------------------------------
$page3_frame1_subframe = $page3_frame1 -> LabFrame( -label => 'Subtitle row correction',
						    -labelside => 'acrosstop'
						  ) -> pack( -fill => 'both' );

$page3_frame1_subframe1 = $page3_frame1_subframe -> Frame() -> pack( -side => 'left' );

$moveup_button = $page3_frame1_subframe1 -> Button ( -text => 'Move up',
						     -command => sub { &move_pix_row("up"); }
						   ) -> pack( -side => 'top', -fill => 'both' );

$movedown_button = $page3_frame1_subframe1 -> Button ( -text => 'Move down',
						       -command => sub { &move_pix_row("down"); }
						     ) -> pack( -fill => 'both' );

$page3_frame1_subframe2 = $page3_frame1_subframe -> Frame() -> pack( -side => 'left', -fill => 'both', -expand => 'y' );

$taller_button = $page3_frame1_subframe2 -> Button ( -text => 'Taller',
						     -command => sub { &move_pix_row("taller"); }
						   ) -> pack( -side => 'top', -fill => 'both' );

$shorter_button = $page3_frame1_subframe2 -> Button ( -text => 'Shorter',
						      -command => sub { &move_pix_row("shorter"); }
						    ) -> pack( -fill => 'both' );
#----------------------------------------------------------------------------------------------


$page3_labframe1 = $page3_frame1 -> LabFrame( -label => 'Enter recognised char(s)',
					      -labelside => 'acrosstop'
					    ) -> pack( -fill => 'both' );

$char_view = $page3_labframe1 -> Canvas( -width => 200, -height => 80 ) -> pack ();

$enter_char = $page3_labframe1 -> Entry( -width => 10, -font => 'Utopia 25' ) -> pack( -side => 'top' );
$enter_char -> bind( '<KeyPress-Return>', \&process_subtitle );

$ignore_button = $page3_labframe1 -> Button ( -text => 'Ignore all such patterns',
					      -command => sub { 
								$recognized_char = "+ignore+";
								unshift(@char_pix_columns,$recognized_char);
								push(@{$ocr_database{$ocr_key}},[@char_pix_columns]);
								$enter_char -> delete( '0', 'end' );
								&process_subtitle;
					    		      }
					    ) -> pack ( -fill => 'both' );

$page3_labframe2 = $page3_frame1 -> LabFrame( -label => 'OCR parameters',
				    -labelside => 'acrosstop',
				  ) -> pack( -fill => 'both' );

$parms_subframe1 = $page3_labframe2 -> Frame() -> pack( -anchor => 'w' );

$ocr_parm_label1 = $parms_subframe1 -> Frame() -> pack( -side => 'left' );
$ocr_parm1_label = $ocr_parm_label1 -> Label( -text => 'Min space width:' ) -> pack( -anchor => 'w' );
$ocr_parm2_label = $ocr_parm_label1 -> Label( -text => 'Max diacritics distance:' ) -> pack( -anchor => 'w' );
$ocr_parm3_label = $ocr_parm_label1 -> Label( -text => 'Max characters overlapping:' ) -> pack( -anchor => 'w' );

$ocr_parm_entry1 = $parms_subframe1 -> Frame() -> pack();
#$ocr_parm1_entry = $ocr_parm_entry1 -> Entry() -> pack();
&NumEntry($ocr_parm_entry1,-textvariable=>\$min_space_width,-width=>2,-minvalue=>1,-maxvalue=>99) -> pack( -anchor => 'w' );
&NumEntry($ocr_parm_entry1,-textvariable=>\$max_diacr_dist ,-width=>2,-minvalue=>1,-maxvalue=>99) -> pack( -anchor => 'w' );
&NumEntry($ocr_parm_entry1,-textvariable=>\$max_char_over  ,-width=>2,-minvalue=>0,-maxvalue=>9 ) -> pack( -anchor => 'w' );

$split_chars = 1;
#$split_chars_checkbtn = $page3_labframe2 -> Checkbutton( -text => 'Split merged chars',
#							 -variable => \$split_chars
#							) -> pack( -anchor => 'w' );

$page3_labframe3 = $page3 -> LabFrame( -label => 'Subtitles progression',
				    -labelside => 'acrosstop'
				  ) -> pack( -fill => 'both' );

$progress_bar = $page3_labframe3 -> ProgressBar(
						 -from => 0,
						 -to => 500,
						 -blocks => 500,
						 -gap => 0,
						 -colors => [0,'blue']
						) -> pack( -side => 'bottom', -fill => 'both' );




# $enter_char -> focus;

#----------------------------------------------------------

#*******************************************************************
# E N D   OF   GUI   F O R M S   D E F I N I T I O N S
#*******************************************************************


#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
#/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


#*******************************************************************
# HERE STARTS THE MAIN CODE
#*******************************************************************


$counter = 0; #progression bar counter

MainLoop();

print "here the code continues in non-Tk end-part\n";

#---  CLOSE AND DELETE TEMPORARY FILES ---

print "Enjoy the subtitles ;-)\n";

#*******************************************************************
# HERE ENDS THE MAIN CODE
#*******************************************************************





#*******************************************************************
# P R O C E D U R E S   S E C T I O N
#*******************************************************************


#===============================================================================
# sub is called by pressing the "Save and exit" button on PAGE3
sub save_exit {

	open(OUTPUT, ">$txt_sub_file");

	$output_text = $txt_sub_view -> get('0.0','end');

	print OUTPUT "$output_text";
	close(OUTPUT);

	$mw -> destroy();
}
#-------------------------------------------------------------------------------


#===============================================================================
sub save_subtitles {

	open(OUTPUT, ">$txt_sub_file");

	$output_text = $txt_sub_view -> get('0.0','end');

	print OUTPUT "$output_text";
	close(OUTPUT);

}
#-------------------------------------------------------------------------------


#===============================================================================
sub check_files {

    my( $continue ) = 1;

    $subtitles_file = $enter_subtitles_file -> get();	#file containing picture subtitles
    $txt_sub_file = $enter_save_file -> get();		#file to save txt subtitles into
    $ocr_load_file = $enter_ocr_load_file -> get();	#file containing ocr database to be used
    $ocr_save_file = $enter_ocr_save_file -> get();	#file to save ocr database into
    
    if ( ! -d $HOME."/.gpgm2srt" ) {

	$call_status = system( "mkdir ~/.gpgm2srt" );

	if ( $call_status != 0 ) {

	    $answer = $mw -> messageBox( -title => 'pgm2srt Warning',
					 -message => "Can't create directory\n~/.gpgm2srt\nand exiting.",
					 -type => 'OK',
					 -icon => 'warning'
	    				);

			exit();

	}

    }

    if ( ! -r $subtitles_file ) { 
    
	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Can't read subtitles source file\n$subtitles_file",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	$continue = 0;

    }

    if ( $txt_sub_file eq "" ) { $txt_sub_file = $subtitles_file.".srt"; }

    if ( -e $txt_sub_file ) {

	$answer = $mw -> messageBox( -title => 'pgm2srt warning',
				       -message => "The target SRT file\n$txt_sub_file\nalready exists. Overwrite it?",
				       -type => 'YesNo',
				       -icon => 'warning',
				       -default => 'No'
				     );

	if ( $answer eq "No" ) {
	
	    $txt_sub_file = "";
	    $continue = 0;

	} else {

	    unlink($txt_sub_file);

	}
	
    }

    if ( $ocr_load_file ne "" and ! -r $ocr_load_file ) { 
    
	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Can't read OCR database source file\n$subtitles_file",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	$continue = 0;

    }

    if ( -e $ocr_save_file ) {

	$answer = $mw -> messageBox( -title => 'pgm2srt warning',
				       -message => "The target OCR database file\n$ocr_save_file\nalready exists. Overwrite it?",
				       -type => 'YesNo',
				       -icon => 'warning',
				       -default => 'No'
				     );

	if ( $answer eq "No" ) {
	
	    $ocr_save_file = "";
	    $continue = 0;

	} else {

	    unlink($ocr_save_file);

	}
	
    }

    if ( $txt_sub_file ne "" and $continue == 1 ) { $continue = &test_file_creation_possibility($txt_sub_file); }
    if ( $ocr_save_file ne "" and $continue == 1 ) { $continue = &test_file_creation_possibility($ocr_save_file); }

    if ( $continue == 1 ) {

	$note_book -> pageconfigure('page1', -state => 'disabled' );
	$note_book -> pageconfigure('page2', -state => 'normal' );
	$sub2pgm_parm = 1;
	$note_book -> raise('page2');

        print "subtitles_file:$subtitles_file\n";
	print "srt_sub_file:$srt_sub_file\n";

    }

}
#-------------------------------------------------------------------------------


#===============================================================================
sub test_file_creation_possibility {

my( $check_file ) = @_;
my( $check_file_test );

    $check_file_test = $check_file.".pgm2srt.write.ability.test";

    if ( open(TEST,">$check_file_test") ) {

	close(TEST);
	unlink($check_file_test);    
	return 1;

    } else {

	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Can't create file\n$check_file",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	return 0;

    }

}
#-------------------------------------------------------------------------------


#===============================================================================
sub open_file_dialog {

my ( $old_file );

    $subtitles_file = $enter_subtitles_file -> get();	#file containing picture subtitles

    if ( -e $subtitles_file ) { $old_file = $subtitles_file; }
    $subtitles_file = $mw -> getOpenFile( -title => 'Open DVD subtitles file' );
    
    if ( -e $subtitles_file ) { 
    
	$enter_subtitles_file -> delete( '0', 'end' ); 
	$enter_subtitles_file -> insert( '0', $subtitles_file );

    } else {

	if ( -e $old_file ) {

	    $subtitles_file = $old_file;

	} else {

	    $enter_subtitles_file -> delete( '0', 'end' );
	    $subtitles_file = "";

	}

    }

}
#-------------------------------------------------------------------------------


#===============================================================================
sub save_file_dialog {

    $subtitles_file = $enter_subtitles_file -> get();	#file containing picture subtitles

    if ( -e $subtitles_file ) {
	($srt_sub_file) = $subtitles_file =~ /.*\/([^\/].*)\..*/;

	if ( $srt_sub_file eq "" ) {
	    ($srt_sub_file) = $subtitles_file =~ /.*\/([^\/].*)/;
	}

	$srt_sub_file .= ".srt";
	($srt_sub_folder) = $subtitles_file =~ /(.*)\/.*/;

    }
    else {
	$srt_sub_file = "subtitles.srt";
    }

    $txt_sub_file = $mw -> getSaveFile( -title => 'Save SRT(TXT) subtitles as',
					  -defaultextension => '.srt',
					  -filetypes => [ ['SRT subtitles files',['.srt','.srtx']],
							  ['All Files','*']
							],
					  -initialfile => $srt_sub_file,
					  -initialdir => $srt_sub_folder
				        );

    $srt_sub_file = $txt_sub_file;
    $enter_save_file -> delete( '0', 'end' );
    $enter_save_file -> insert( '0', $txt_sub_file );

#    print "subtitles_file:$subtitles_file\nsrt_sub_file:$srt_sub_file\ntxt_sub_file:$txt_sub_file\n";
#    print "srt_sub_folder:$srt_sub_folder\n";

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
sub open_ocr_dialog {

my ( $old_file );

    $subtitles_file = $enter_subtitles_file -> get();	#file containing picture subtitles

    if ( -e $subtitles_file ) { $old_file = $subtitles_file; }
    $subtitles_file = $mw -> getOpenFile( -title => 'Open DVD subtitles file' );
    
    if ( -e $subtitles_file ) { 
    
	$enter_subtitles_file -> delete( '0', 'end' ); 
	$enter_subtitles_file -> insert( '0', $subtitles_file );

    } else {

	if ( -e $old_file ) {

	    $subtitles_file = $old_file;

	} else {

	    $enter_subtitles_file -> delete( '0', 'end' );
	    $subtitles_file = "";

	}

    }

}
#-------------------------------------------------------------------------------



#===============================================================================
sub open_ocr_dialog {

#    if ( -e $ocr_file ) { $ocr_file_old = $ocr_file; }
    $ocr_file = $mw -> getOpenFile( -title => 'Open pgm2srt OCR database' );
    
    if ( -e $ocr_file ) { 
    
	&load_ocr_database;

	if ( $ocr_database_loaded == true ) {

	    $srtx_file = $mw -> getOpenFile( -title => 'Open srtx subtitles timing file' );

	    if ( -e $srtx_file ) {

		$note_book -> pageconfigure('page3', -state => 'normal' );
		$note_book -> pageconfigure('page1', -state => 'disabled' );
		$note_book -> pageconfigure('page2', -state => 'disabled' );

		$note_book -> raise('page3');


		#spustit OCR rozpoznavani?


	    } else {

		$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
					       -message => "Can't open file\n$srtx_file",
					       -type => 'OK',
					       -icon => 'warning'
					     );

	    }

	}

    } else {

	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Can't open file\n$ocr_file",
				       -type => 'OK',
				       -icon => 'warning'
				     );

    }

}
#----------------------------------------------------------------------------------------------------------











#===============================================================================
# called by buttons on NOTEPAD Page 2 when selecting -c parameter
# shows preview of subtitles under selected -c parameter

sub show_pgm_sub {

    $pgm_file = $HOME."/.gpgm2srt/test_pgm_subtitle0001.pgm";
    &extract_pgm_sub;
    
    if ( $call_status == 0 ) {
        &pgm_to_text;
        &draw_pgm_sub;
    } else {

	$sub_disp -> delete('all');

	$sub_disp -> create( 'text',
			     150, 100,
			     -text => 'Nothing to display. Choose other option.',
			     -fill => 'red'
			   );

    }

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
#input: array @pixel_column_as_txt must contain PGM-subtitle transformed into text columns
# draws pgm subtitle preview in NOTEBOOK Page 2
sub draw_pgm_sub {

    $sub_disp -> delete('all');

    for ( $i=0; $i < $pgm_height; $i++ ) {

	for ( $j=0; $j < $pgm_width; $j++ ) {

#	    if ( $i*$pix_size+$pix_size < PREV_HEIGHT and $j*$pix_size+$pix_size < PREV_WIDTH and substr($pixel_column_as_txt[$j],$i,1) eq "#" ) {
	    if ( substr($pixel_column_as_txt[$j],$i,1) eq "#" ) {

		$sub_disp -> create( 'line',
				     $j*$pix_size, #x1_coord
				     $i*$pix_size, #y1_coord
				     $j*$pix_size+$pix_size, #x2_coord
				     $i*$pix_size+$pix_size, #y2_coord
				     -fill => 'black'
				   );

	    }

	}

    }

#    $sub_disp -> after(500, sub { $sub_disp -> itemconfigure('all', -fill => 'grey'); } )

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
#input: variable $subtitles_file must contain name (full path) of file with subtitle stream for subtitle2pgm
# extracts only one PGM subtitle file in order to show its preview
sub extract_pgm_sub {


    $subtitles_file_bash = $subtitles_file;
    $subtitles_file_bash =~ s/ /\\ /g;
    $call_status = system("subtitle2pgm -o ~/.gpgm2srt/test_pgm_subtitle -c $sub2pgm_c[$sub2pgm_parm] -C 5 -e 0:$pgm_min:0,1 < $subtitles_file_bash");

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
# called by the CONTINUE button on NOTEPAD Page 2
sub make_subtitles {

# TEMPORARY COMMENT
    &generate_subtitles;
    
    $note_book -> pageconfigure('page3', -state => 'normal' );
    $note_book -> raise('page3');
    $note_book -> pageconfigure('page2', -state => 'disabled' );
    
    &exact_match_ocr;
}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
#input: variable $subtitles_file_bash must contain bash-like full path to file with subtitles stream for subtitle2pgm
# using subtitle2pgm generates all PGM subtile files contained in file genetaded by tcexctact
sub generate_subtitles {


    $sub_disp -> delete('all');

    $sub_disp -> create( 'text',
			  150, 100,
			  -text => 'Please wait while generating PGM subtitles...',
			  -fill => 'red'
			   );
    $sub_disp -> update();

    unlink(<$HOME/.gpgm2srt/*>); #deletes all files in gpgm2srt directory

    $call_status = system("subtitle2pgm -o ~/.gpgm2srt/subtitles -c $sub2pgm_c[$sub2pgm_parm] -C 5 < $subtitles_file_bash");

    if ( $call_status != 0 ) {
    
	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Error occured when generating PGM subtitles. See console for subtitle2pgm's error messages. Exiting...",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	exit();    

    }

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================
#input:  variable $pgm_file must content full path to PGM subtitle file
#output: array @pixel_column_as_txt containing PGM subtitle in text form
sub pgm_to_text {

my ( $record, @header );
    
    open( PGM_SOURCE, $pgm_file) or die ("Can't open file \'$pgm_soubor\'!\n");

    for ( $i=1 ; $i <= 3 ; $i++) {
	    
        $header[$i] = <PGM_SOURCE>;
		    
    }
			
    $header_length = length($header[1].$header[2].$header[3])+4;
    ( $pgm_width, $pgm_height ) = $header[3] =~ /(\d+)\s(\d+)/;

    close(PGM_SOURCE);
	
    open( PGM_SOURCE, $pgm_file) or die ("Can't open file \'$pgm_soubor\'!\n");

    read( PGM_SOURCE, $record, $header_length ); #skip in PGM-file behind header

    @pixel_column_as_txt = ();
    #def pixel_column_as_txt - array contains txt representation of the whole PGM subtitle picture
    #			     - first upper row has index 0

    @pixel_row_filled = ();
    #def pixel_row_filled - marks pixel rows containing at least one filled pixel in order to extract subtitle text rows
    #			  - first upper row has index 0

    for ( $j=0 ; $j < $pgm_height ; $j++) { #pixel rows iteration

	read( PGM_SOURCE, $record, $pgm_width ); #reads the whole pixel row

        for ( $i = 0; $i < $pgm_width ; $i++ ) { #pixel columns iteration for one row

	    if ( ord(substr($record,$i,1)) > 0  ) {
	    
		$pixel_column_as_txt[$i] .= " ";

	    } else {
	    
		$pixel_column_as_txt[$i] .= "#";
		$pixel_row_filled[$j] = 1;
		
	    }

	}

    }

    close(PGM_SOURCE);

}
#-------------------------------------------------------------------------------


#===============================================================================
sub exact_match_ocr {

    $subtitles_count = &sub_count;

#    print "number of subtitles:$subtitles_count\n";
    
    if ( $subtitles_count == 0 ) {
    
	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "No PGM subtitles found. Exiting...",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	exit(0);

    }


    unless ( open ( SRTX_SOURCE, $HOME."/.gpgm2srt/subtitles.srtx" ) ) { 
    
	$answer = $mw -> messageBox( -title => 'pgm2srt Warning',
				       -message => "Can't open file\n$HOME./.gpgm2srt/subtitles.srtx\n. Exiting...",
				       -type => 'OK',
				       -icon => 'warning'
				     );

	exit();

    }

    $sub_number = 0; #text subtile counter
    $sub_timing_avail = 0; #whether to look up for subtitle timing or not
#    $sub_processed = 0; #whether to print subtitle
    $all_processed = 0; #whether to end OCR process
    $ocr_column = 0;
    $origin_ocr_column = 0;
    $restart = 0;	#restart given subtitle OCR when manualy shifted the row

    if ( $sub_timing_avail == 0 ) { &get_sub_timing; }

    if ( $sub_timing_avail == 1 ) { &look_for_pix_char; }

}
#-----------------------------------------------------------------------------------------------


#===============================================================================================
# called every time new PGM subtitle is needed i.e. the current PGM subtitle is processed
sub get_sub_timing {

#    print "getting sub timing...\n";
    if ( $subtitle_text ne "" ) {

	&print_subtitle;
	$subtitle_text = "";

    }

    while ( chomp($sub_timing = <SRTX_SOURCE>) ) {
	if ( $sub_timing =~ /^\s*$/ ) { next; }

	#looks up for subtitle timing
	while ( $sub_timing !~ /-->/ ) {
	    chomp($sub_timing = <SRTX_SOURCE>);
	}

	#next row after subtlitle timing should be PGM file full path
	chomp($pgm_file = <SRTX_SOURCE>);

    	#looks up for png subtitle file name
	while ( $pgm_file !~ /pgm/ ) {
	    chomp($pgm_file = <SRTX_SOURCE>);
	}

	if ( $pgm_file ne "" ) {

	    ( $pgm_file ) = $pgm_file =~ /(.*).txt/;

	    if ( -e $pgm_file ) {

		$sub_number ++;
		$sub_timing_avail = 1;
		$sub_row_avail = 0; #whether to look for/extract pixels forming one subtitle row
		$counter = $sub_number/$subtitles_count*500;
		$progress_bar -> value($counter);
 		$progress_bar -> update();
		$top_row = 0;
		$bottom_row = 0;
		$pgm_drawn = 0;
		&pgm_to_text;
		&pgm_preview;
		last;

	    } else {

		$answer = $mw -> messageBox( -title => 'gpgm2srt Message',
					     -message => "No proper PGM file\n$pgm_file\nfound. Exiting...",
					     -type => 'OK',
					     -icon => 'warning'
				    	    );
		$mw -> destroy();

	    }

	} else {

    	    print "Probably end of SRTX file.\n";
    	    close (SRTX_SOURCE);

	}

    }

    if ( $sub_timing eq "" ) {

    	print "Probably end of SRTX file - setting all_processed\n";
    	close (SRTX_SOURCE);
	$all_processed = 1;

    }

#	print "$sub_number\n";
#	print "$sub_timing\n";
#	print "$pgm_file\n";

}
#----------------------------------------------------------------------------------------------------------


#===============================================================================================
#This procedure is called by Entry box biding (after Enter pressed)
sub process_subtitle {

    $recognized_char = $enter_char -> get();

#    $sub_view -> focus; #take focus off the entry

    if ( $recognized_char eq "I" ) {
    
	$check_for_I++;
    
	$answer = $mw -> messageBox( -title => 'gpgm2srt Message',
				     -message => "It is recomended to enter lowercase L instead of uppercase i.\nDVD subtitles usually doesn't distinguish these patterns\nand for subsequent text postprocessing it's more easy to replace l with I.",
				     -type => 'OK',
				     -icon => 'warning'
				    );

	$enter_char -> delete( '0', 'end' ); 
	$enter_char -> insert( 'end', 'l' );
    
    } else {

	if ( $recognized_char ne "" ) {
    
	    $subtitle_text .= $recognized_char;
	    #SAVE RECOGNIZED PATTERN INTO MEMORY - GLOBAL HASH:
	    unshift(@char_pix_columns,$recognized_char);
	    push(@{$ocr_database{$ocr_key}},[@char_pix_columns]);
#	    print"Saving pattern with key $ocr_key\n";

	}

	if ( $sub_timing_avail == 0 ) { &get_sub_timing; }

	if ( $all_processed == 1 ) {
    
	    #OCR process may end on two places - here is place 1
#	    &page4_raise;
	    print "All the subtitles processed(place 1).\n";
	    &save_subtitles;
	    $mw -> destroy();
	
	} else {
    
	    &look_for_pix_char;
    
	}


    }

}
#------------------------------------------------------------------------------------------------


#==========================================================================================================
# This procedure is called for the first time by sub exact_match_ocr (after png subs generation) and
# every next time is called by the sub process_subtitle
sub look_for_pix_char {

    $enter_char -> delete( '0', 'end' ); 
    $preview_shaded = 0; #whether the subtitle preview is active (unknown pattern) or inactive (known pattern)

    # sub row fully processed
    if ( $ocr_column >= @process_pixel_columns and $bottom_row > $min_height ) { #column index is array size - 1

	$sub_row_avail = 0;
	$top_row = $bottom_row + 1;

    }

    if ( $restart == 1 ) {
    
	&extract_sub_row;    
	$restart = 0;
	$ocr_column = 0;
	$origin_ocr_column = 0;
	$subtitle_text = "";
	#doplnit dalsi upravy promennych pro restart OCR po posunu hranic radku

	print "OCR restarted...\n";

    }

    do {

	&guess_sub_row if ( $sub_row_avail == 0 );

	if ( $all_processed == 0 ) {

	    $prev_ocr_column = $ocr_column;
	    $char_in_database = 0;

	    #procedure in Exactocr module
	    ($ocr_column,$space_detected,$error_occured,@char_pix_columns) = &find_char($ocr_column,length($process_pixel_columns[0]),$min_space_width,$max_diacr_dist,$max_char_over,$split_chars,' ','#',@process_pixel_columns);

	    if ( $error_occured == 1 ) {
	    #some more handling of catched error ???
	    
		$err_log_view -> insert( 'end', "Problem occured in subtitle no. $sub_number\n" );
#		print ERROR_LOG "Problem occured in subtitle no. $sub_number\n";
		print "Problem occured in subtitle no. $sub_number\n";

	    }

	    if ( $space_detected == 1 ) { $subtitle_text .= " "; }

	    if ( @char_pix_columns > 0 ) {

    	    	$recognized_char = &check_ocr_database(@char_pix_columns);

		if ( $recognized_char ne "" ) { 

		    $char_in_database = 1;
		    if ( $preview_shaded == 0 ) { &shade_preview(1); }
		    if ( $recognized_char ne "+ignore+" ) { $subtitle_text .= $recognized_char; }

		} else {

		    $char_in_database = 0;
		    if ( $preview_shaded == 1 and $pgm_drawn == 1 ) { &shade_preview(0); }
		    if ( $pgm_drawn == 0 ) { &pgm_preview; }
		    if ( $lines_drawn == 0 ) { &draw_row_lines; }

		}

	    } else {

		#PGM subtitle processed - procees with next	
		$char_in_database = 1;	   #to keep looping in while (...
		$recognized_char = "+ignore+"; #to skip subtitle_text adding
		$sub_row_avail = 0;
		$top_row = $bottom_row + 1;

		#TEST NA ALL_PROCESSED ???


	    }

	    $ocr_column ++;

	}

    } while ( $char_in_database == 1 and $all_processed == 0 );


    if ( $char_in_database == 0 and $all_processed == 0 ) {

	&pattern_preview;
	$enter_char -> focus;

    }

    if ( $all_processed == 1 ) {

	#OCR process may end on two places - here is place 2
#	&page4_raise;
	print "All the subtitles processed(place 2).\n";
	&save_subtitles;
	$mw -> destroy();

    }

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
# When known pattern is found shade subtitle preview and delete pattern preview area
# parm (1) tells to shade the pattern
# parm (0) tells to unshade the pattern
sub shade_preview {

    my ( $shade ) = @_;

    if ( $shade == 1 ) {

	$char_view -> delete('all');
#	$char_view -> itemconfigure( 'points', -fill => 'grey' );
#	$sub_view  -> itemconfigure( 'points', -fill => 'grey' );
	$char_view -> update();
#	$sub_view -> update();
	$preview_shaded = 1;
#	print"shading preview...\n";

    } else {

#	$char_view -> itemconfigure( 'points', -fill => 'red' );
#	$sub_view  -> itemconfigure( 'points', -fill => 'black' );
#	$char_view -> update();
#	$sub_view -> update();
	$preview_shaded = 0;

    }

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
#at the beginning counts the number of PGM files
sub sub_count {

    my ( $subtitles_count ) = 1;

    $pgm_sub_file = $HOME."/.gpgm2srt/subtitles".sprintf("%04d",$subtitles_count).".pgm";

    while ( -e $pgm_sub_file ) {
    
	$subtitles_count ++;
	$pgm_sub_file = $HOME."/.gpgm2srt/subtitles".sprintf("%04d",$subtitles_count).".pgm";

    }

    $subtitles_count --;

    return $subtitles_count;

}
#----------------------------------------------------------------------------------------------------------

#==========================================================================================================
#input: array @pixel_column_as_txt must contain PGM-subtitle transformed into text columns
# displays the whole PGM subtitle
sub pgm_preview {

    if ( $char_in_database == 0 ) {

	$sub_view -> delete('all');
	$rectangle_drawn = 0;

        for ( $i=0; $i < $pgm_height; $i++ ) {

	    for ( $j=0; $j < $pgm_width; $j++ ) {

#	    	if ( $i*$pix_size+$pix_size < PREV_HEIGHT and $j*$pix_size+$pix_size < PREV_WIDTH and substr($pixel_column_as_txt[$j],$i,1) eq "#" )
		if ( substr($pixel_column_as_txt[$j],$i,1) eq "#" ) {

		    $sub_view -> create( 'line',
					  $j, #x1_coord
					  $i, #y1_coord
				          $j+1, #x2_coord
					  $i+1, #y2_coord
					  -fill => 'black',
					  -tags => 'points'
					);

		}

	    }

	}

	$sub_view -> update();
	$pgm_drawn = 1;

    } else {

	$sub_view -> itemconfigure( 'points', -fill => 'grey' );
	$sub_view -> delete('pattern');
	$rectangle_drawn = 0;
	$sub_view -> update();

    }

#    $sub_disp -> after(500, sub { $sub_disp -> itemconfigure('all', -fill => 'grey'); } )

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
sub pattern_preview {

    if ( $rectangle_drawn == 1 ) { $sub_view -> delete( 'pattern' ); }

    $sub_view -> create( 'rectangle',
			  $left_frontier+$ocr_column-@char_pix_columns-1,
			  $top_row,
			  $left_frontier+$ocr_column-2,
			  $bottom_row,
			  -outline => 'red',
			  -tags => 'pattern'
			);

    $rectangle_drawn = 1;

    $char_view -> delete('all');

    for ( $j=0; $j<@char_pix_columns; $j++ ) { #loop through columns

	for ( $i=0; $i<=length($char_pix_columns[$j]); $i++ ) { #loop through rows

	    if ( substr($char_pix_columns[$j],$i,1) eq "#" ) {

		$char_view -> create( 'line',
				     $j+10, #x1_coord
				     70-$i, #y1_coord
				     $j+11, #x2_coord
				     71-$i, #y2_coord
				     -fill => 'red',
				     -tags => 'points'
				   );

	    }

	}

    }

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
# Tries to guess what pixel rows form one subtitle text row
# Also draws primary frontier lines
sub guess_sub_row {

#    print "guess sub row...\n";

    if ( $subtitle_text ne "" ) { $subtitle_text .= '|'; }

    $sub_view -> delete('top_frontier');
    $sub_view -> delete('bottom_frontier');
    $lines_drawn = 0;

    $sub_row_avail = 0;

    while ( $pixel_row_filled[$top_row] == 0 and $top_row < $pgm_height ) {
	
        $top_row ++;

    }
    
    if ( ($top_row + $min_height) <= $pgm_height ) { # should be only less than ???
    
	$bottom_row = $top_row + 1; # $top_row contains the first filled pixel row
	
	while ( $pixel_row_filled[$bottom_row] == 1 or ( $bottom_row - $top_row < $min_height ) ) { #should substract 1?
	
    	    $bottom_row ++;

	}
    
	$bottom_row --;

	if ( $bottom_row - $top_row > $min_height ) { #should substract 1?

	    if ( $char_in_database == 0 ) { &draw_row_lines; }
	    &extract_sub_row;
	    $ocr_column = 0;
	    $origin_ocr_column = 0;

	}




    } else {
    
#	$sub_processed = 1;
#	print "sub processed (guess_sub_row)\n";
	&get_sub_timing;

    }



}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
sub draw_row_lines {

    $sub_view -> create( 'line',
			 0, #x1_coord
			 $top_row, #y1_coord
			 $pgm_width, #x2_coord
			 $top_row, #y2_coord
			 -fill => 'blue',
			 -tags => 'top_frontier'
			);

    $sub_view -> create( 'line',
			 0, #x1_coord
			 $bottom_row, #y1_coord
			 $pgm_width, #x2_coord
			 $bottom_row, #y2_coord
			 -fill => 'blue',
			 -tags => 'bottom_frontier'
			);

    $lines_drawn = 1;

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
# Extracts (from @$pixel_column_as_txt) and re-orders (to @process_pixel_columns) pixel rows forming one text subtitle row
sub extract_sub_row {

    #substract relating pixels
    #pixel rows must be in reverse order (because of Exactocr subs requirements)
    #leading and trailing empty columns should be cutted off
	
    @process_pixel_columns = ();
	
    #look for the first and last non-empty pixel column

    # NOT MUCH EXACT METHOD AND THIS WAY USELESS - MUST CHECK ONLY PART OF PIX COLUMN NOT ENTIRE COLUMN

    $right_frontier = $pgm_width - 1;
    while ( index( substr($pixel_column_as_txt[$right_frontier],$top_row,$bottom_row-$top_row+1) ,"#") < 0 ) {

	$right_frontier --;

    }
    
    $left_frontier = 0;
    
    while ( index( substr($pixel_column_as_txt[$left_frontier],$top_row,$bottom_row-$top_row+1) ,"#") < 0 ) {

	    $left_frontier ++;

    }

    for ( $i = $left_frontier ; $i <= $right_frontier ; $i++ ) { #pixel columns iteration

	for ( $j = $bottom_row ; $j >= $top_row ; $j-- ) { #pixel rows iteration

	    $process_pixel_columns[$i-$left_frontier] .= substr($pixel_column_as_txt[$i],$j,1);

	}

    }

    $sub_row_avail = 1;
    $max_shift_down = 0;

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
sub print_subtitle {

    @subtitle_rows = split(/\|/,$subtitle_text);

    #save every new subtitle to avoid data/text lost
#    if ( -e $HOME."/.gpgm2srt/subtitle.srt" ) {
#    
#	open (SRT_NEW, ">>$HOME/.gpgm2srt/subtitle.srt") #append if file already exists
#    
#    } else {
#    
#	open (SRT_NEW, ">$HOME/.gpgm2srt/subtitle.srt") #create file
#    
#    }
#
#    print SRT_NEW "$sub_number\n$sub_timing\n";
#    foreach $subtitle_row ( @subtitle_rows ) {
#	print SRT_NEW "$subtitle_row\n";
#    }
#    print SRT_NEW "\n";
#
#    close (SRT_NEW);


    #print newly recognized subtitle into the text box
    $txt_sub_view -> insert( 'end', "$sub_number\n$sub_timing\n" );

    foreach $subtitle_row ( @subtitle_rows ) {

	$txt_sub_view -> insert( 'end', "$subtitle_row\n" );

    }

    $txt_sub_view -> insert( 'end', "\n" );

    $txt_sub_view -> see('end');

}
#----------------------------------------------------------------------------------------------------------


#==========================================================================================================
sub move_pix_row {

my ( $action ) = @_;

    $restart = 1;
    $enter_char -> delete( '0', 'end' ); 
    $sub_view -> delete( 'pattern' );
    $rectangle_drawn = 0;
    $sub_row_available = 0;

    if ( $action eq "up" ) {
    
	if ( $top_row > 0 ) {

	    $sub_view -> move( 'top_frontier', 0, -1 );
	    $sub_view -> move( 'bottom_frontier', 0, -1 );
	    $top_row --;
	    $bottom_row --;

	}
    
    } elsif ( $action eq "down" ) {
    
	if ( $bottom_row < $pgm_height ) {

	    $sub_view -> move( 'top_frontier', 0, 1 );
	    $sub_view -> move( 'bottom_frontier', 0, 1 );
	    $top_row ++;
	    $bottom_row ++;

	}

    } elsif ( $action eq "taller" ) {

	if ( $bottom_row < $pgm_height ) {

	    $sub_view -> move( 'bottom_frontier', 0, 1 );
	    $bottom_row ++;

	}

    } elsif ( $action eq "shorter" ) {
    
	if ( $bottom_row > $top_row + $min_height ) {

	    $sub_view -> move( 'bottom_frontier', 0, -1 );
	    $bottom_row --;

	}

    }

}
#----------------------------------------------------------------------------------------------------------


#======================================================================================
sub skip_sub {

    &print_subtitle;
    $subtitle_text = "";
    &get_sub_timing;
    &look_for_pix_char;

}
#----------------------------------------------------------------------------------------------------------


#======================================================================================
sub save_ocr_database {





}
#----------------------------------------------------------------------------------------------------------


#======================================================================================
sub load_ocr_database {





}
#----------------------------------------------------------------------------------------------------------


#======================================================================================
# This code is not mine - copied from vptk_w.pl by XXX and slightly modified:
# Additionaly that code is partially copied from original NumEntry
# Reason: the original widget does not support -textvariable (sic!)
# Problems: No strict syntax control, No FireButton functionality

sub NumEntry {

my ($parent,%par)=@_;
my $numentry;
my $minvalue=delete $par{'-minvalue'};
my $maxvalue=delete $par{'-maxvalue'};

    unless($def_bitmaps) {
	my $bits = pack("b8"x5,
			"........",
			"...11...",
			"..1111..",
			".111111.",
			"........"
			);

	$mw->DefineBitmap('INCBITMAP' => 8,5, $bits);

        # And of course, decrement is the reverse of increment :-)
        $mw->DefineBitmap('DECBITMAP' => 8,5, scalar reverse $bits);
	$def_bitmaps=1;
    }

    my $result=$parent->Frame();

    if ( $par{'-width'} > 0 ) {

	$numentry=$result->Entry(%par)->pack(-anchor=>'w', -side=>'left');
        $numentry->bind('<Up>',
	    [\&inc_num_controlled,$par{'-textvariable'},1,$minvalue,$maxvalue]);
        $numentry->bind('<Down>',
	    [\&inc_num_controlled,$par{'-textvariable'},-1,$minvalue,$maxvalue]);

    }

    $result->Button(-bitmap=>'INCBITMAP',-cursor=>'left_ptr',-command=>
	[\&inc_num_controlled,$par{'-textvariable'},1,$minvalue,$maxvalue])
	->pack(-anchor=>'nw', -side=>'top');
    $result->Button(-bitmap=>'DECBITMAP',-cursor=>'left_ptr',-command=>
	[\&inc_num_controlled,$par{'-textvariable'},-1,$minvalue,$maxvalue])
        ->pack(-anchor=>'nw', -side=>'top');
      return $result;

}

sub inc_num_controlled {

    shift if ref($_[0]) ne 'SCALAR';
    my ($ptr,$inc,$minvalue,$maxvalue)=@_;

    my $value=$$ptr+$inc;
    $$ptr=$value;
    $$ptr=$minvalue if length $minvalue && $value<$minvalue;
    $$ptr=$maxvalue if length $maxvalue && $value>$maxvalue;

}
#-----------------------------------------------------------------------------------------------
