# $Id: Subtitle.pm,v 1.6 2004/04/11 23:36:19 joern Exp $

#-----------------------------------------------------------------------
# Copyright (C) 2001-2003 Jörn Reder <joern AT zyn.de>.
# All Rights Reserved. See file COPYRIGHT for details.
# 
# This module is part of Video::DVDRip, which is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#-----------------------------------------------------------------------

package Video::DVDRip::Subtitle;
use Locale::TextDomain qw (video.dvdrip);

use base Video::DVDRip::Base;

use Carp;
use strict;

sub title			{ shift->{title}			}
sub id				{ shift->{id}				}
sub lang			{ shift->{lang}				}
sub preview_images		{ shift->{preview_images}		}
sub tc_preview_img_cnt		{ shift->{tc_preview_img_cnt}		}
sub tc_preview_timecode		{ shift->{tc_preview_timecode}		}
sub tc_vobsub			{ shift->{tc_vobsub}			}
sub tc_render			{ shift->{tc_render}			}
sub tc_vertical_offset		{ shift->{tc_vertical_offset}		}
sub tc_time_shift		{ shift->{tc_time_shift}		}
sub tc_postprocess		{ shift->{tc_postprocess}		}
sub tc_antialias		{ shift->{tc_antialias}			}
sub tc_color_manip		{ shift->{tc_color_manip}		}
sub tc_color_a			{ shift->{tc_color_a}			}
sub tc_color_b			{ shift->{tc_color_b}			}
sub tc_assign_color_a		{ shift->{tc_assign_color_a}		}
sub tc_assign_color_b		{ shift->{tc_assign_color_b}		}
sub tc_test_image_cnt		{ shift->{tc_test_image_cnt}		}

sub set_title			{ shift->{title}		= $_[1]	}
sub set_id			{ shift->{id}			= $_[1]	}
sub set_lang			{ shift->{lang}			= $_[1]	}
sub set_preview_images		{ shift->{preview_images}	= $_[1]	}
sub set_tc_preview_img_cnt	{ shift->{tc_preview_img_cnt}	= $_[1]	}
sub set_tc_preview_timecode	{ shift->{tc_preview_timecode}	= $_[1]	}
sub set_tc_vobsub		{ shift->{tc_vobsub}		= $_[1]	}
sub set_tc_render		{ shift->{tc_render}		= $_[1]	}
sub set_tc_vertical_offset	{ shift->{tc_vertical_offset}	= $_[1]	}
sub set_tc_time_shift		{ shift->{tc_time_shift}	= $_[1]	}
sub set_tc_postprocess		{ shift->{tc_postprocess}	= $_[1]	}
sub set_tc_antialias		{ shift->{tc_antialias}		= $_[1]	}
sub set_tc_color_manip		{ shift->{tc_color_manip}	= $_[1]	}
sub set_tc_color_a		{ shift->{tc_color_a}		= $_[1]	}
sub set_tc_color_b		{ shift->{tc_color_b}		= $_[1]	}
sub set_tc_assign_color_a	{ shift->{tc_assign_color_a}	= $_[1]	}
sub set_tc_assign_color_b	{ shift->{tc_assign_color_b}	= $_[1]	}
sub set_tc_test_image_cnt	{ shift->{tc_test_image_cnt}	= $_[1]	}

sub new {
	my $class = shift;
	my %par = @_;
	my  ($id, $lang, $tc_preview_img_cnt, $tc_preview_timecode) =
	@par{'id','lang','tc_preview_img_cnt','tc_preview_timecode'};
	my  ($tc_vobsub, $tc_render, $tc_vertical_offset, $tc_time_shift) =
	@par{'tc_vobsub','tc_render','tc_vertical_offset','tc_time_shift'};
	my  ($tc_antialias, $tc_color_a, $tc_color_b, $tc_assign_color_a) =
	@par{'tc_antialias','tc_color_a','tc_color_b','tc_assign_color_a'};
	my  ($tc_assign_color_b, $tc_test_image_cnt, $title, $tc_color_manip) =
	@par{'tc_assign_color_b','tc_test_image_cnt','title','tc_color_manip'};
	my  ($tc_postprocess) =
	@par{'tc_postprocess'};

	$tc_preview_img_cnt  = 3          if not defined $tc_preview_img_cnt;
	$tc_preview_timecode = "00:00:00" if not defined $tc_preview_timecode;

	$tc_test_image_cnt 	||= 1;
	$tc_color_a		||= 0;
	$tc_color_b		||= 0;
	$tc_assign_color_a	||= 0;
	$tc_assign_color_b	||= 0;
	$tc_antialias		||= 1;
	$tc_vertical_offset	||= 0;
	$tc_time_shift		||= 0;
	$tc_color_manip		||= 0;
	$tc_postprocess		||= 0;

	my $self = {
		title			=> $title,
		id			=> $id,
		lang			=> $lang,
		preview_images		=> [],
		tc_preview_img_cnt	=> $tc_preview_img_cnt,
		tc_preview_timecode	=> $tc_preview_timecode,
		tc_vobsub		=> $tc_vobsub,
		tc_render		=> $tc_render,
		tc_vertical_offset	=> $tc_vertical_offset,
		tc_time_shift		=> $tc_time_shift,
		tc_postprocess		=> $tc_postprocess,
		tc_antialias		=> $tc_antialias,
		tc_color_manip		=> $tc_color_manip,
		tc_color_a		=> $tc_color_a,
		tc_color_b		=> $tc_color_b,
		tc_assign_color_a	=> $tc_assign_color_a,
		tc_assign_color_b	=> $tc_assign_color_b,
		tc_test_image_cnt	=> $tc_test_image_cnt,
    	};
	
	return bless $self, $class;
}

sub reset_preview_images {
	my $self = shift;
	
	$self->set_preview_images ([]);
	
	1;
}

sub add_preview_image {
	my $self = shift;
	my %par = @_;
	my ($filename, $time) = @par{'filename','time'};

	my $image = Video::DVDRip::Subtitle::PreviewImage->new (
		filename => $filename,
		time     => $time,
	);
	
	push @{$self->preview_images}, $image;
	
	return 1;
}

sub vobsub_prefix {
	my $self = shift;
	my %par = @_;
	my ($file_nr) = @par{'file_nr'};

	my $title = $self->title;

	my $file = "";
	if ( defined $file_nr ) {
		if ( $self->title->is_ogg ) {
			$file = sprintf ("%06d-", $file_nr + 1);
		} else {
			$file = sprintf ("%04d-", $file_nr);
		}
	}

	return sprintf (
		"%s-%03d-${file}sid%02d",
		$title->project->name,
		$title->nr,
		$self->id
	);
}

sub ifo_file {
	my $self = shift;
	my %par = @_;
	my ($nr) = @par{'nr'};

	$nr ||= 0;

	my @ifo_files = glob($self->title->project->ifo_dir."/*");
	$nr = 0 if $nr > @ifo_files - 1;

	return $ifo_files[$nr];
}

sub ps1_file {
	my $self = shift;
	
	return $self->title->project->snap_dir."/".$self->vobsub_prefix.".ps1";
}

sub vobsub_file_exists {
	my $self = shift;
	
	my $mask = $self->title->avi_dir."/".$self->vobsub_prefix.".{sub,rar}";
	my @files = glob($mask);
	
	return scalar(@files);
}


package Video::DVDRip::Subtitle::PreviewImage;
use Locale::TextDomain qw (video.dvdrip);

sub filename			{ shift->{filename}			}
sub time			{ shift->{time}				}
sub height			{ shift->{height}			}
sub width			{ shift->{width}			}

sub new {
	my $class = shift;
	my %par = @_;
	my ($filename, $time) = @par{'filename','time'};

	my $catch = qx[identify -ping $filename 2>&1];
	my ($width, $height);
	($width, $height) = ( $catch =~ /(\d+)x(\d+)/ );

	my $self = {
		filename	=> $filename,
		time		=> $time,
		height		=> $height,
		width		=> $width,
	};
	
	return bless $self, $class;
}

1;
