use Image::Magick;
use diagnostics; 
use File::Basename;




# 0)Input

@suffixes = ('.jpg', '.jp2', '.png');


# 0.1) counting variable, folders, file
$p = '1'; #counting variable
$sourcefolder_path = 'C:/ART/IM_PlaneCovering/1_Sourceimages/'; 
$sourcefile_name = 'image.jpg';
$pmm_resultfolder_path = "C:/ART/IM_PlaneCovering/2_Tiles/pmm-Tiles/";


# 0.2) definition of lokal parameters
$rectangle_width_min_Faktor = 0.4;
$rectangle_width_max_Faktor = 0.6;
$rectangle_height_min_Faktor = 0.4;
$rectangle_height_max_Faktor = 0.6;




# 1) definition of PerlMagick objects
$pmm_G = new Image::Magick;



# 2) inilisation of random number generator
srand;



# 3) read source image
$sourcefile_fullname = $sourcefolder_path . $sourcefile_name; 
$pmm_G->Read("$sourcefile_fullname");




# 4) local variable calculations: $rectangle_width, $rectangle_height, $x1, $y1
$image_width = $pmm_G->Get('columns');
$image_height = $pmm_G->Get('rows');
if ($image_width < $image_height) {$image_min = $image_width} else {$image_min = $image_height};
$rectangle_width_min = $rectangle_width_min_Faktor * $image_min;
$rectangle_width_max = $rectangle_width_max_Faktor * $image_min;
$rectangle_height_min = $rectangle_height_min_Faktor * $image_min;
$rectangle_height_max = $rectangle_height_max_Faktor * $image_min;
$rectangle_width = $rectangle_width_min + (int(rand($rectangle_width_max - $rectangle_width_min)) + 1);
$rectangle_height = $rectangle_height_min + (int(rand($rectangle_height_max - $rectangle_height_min)) + 1);
$x_allowed = $image_width - $rectangle_width;
$y_allowed = $image_height - $rectangle_height;
$x1 = int(rand($x_allowed))+1;
$y1 = int(rand($y_allowed))+1;




# 5) generate pmm_G by cropping the source image
$pmm_G->Crop(geometry => "$rectangle_width x $rectangle_hight+$x1+$y1");



# 6) convert pmm_G.png -flop pmm_G_flop.png
$pmm_G_flop = $pmm_G->Clone();
$pmm_G_flop->Flop();


# 7) convert pmm_G.png pmm_G_flop.png +append pmm_tile_row.png
$q = $pmm_G->Clone();
push(@$q, $pmm_G_flop);
$pmm_tile_row = $q->Append(stack=>'false');



# 8) convert pmm_tile_row.png -flip pmm_tile_row_flip.png
$pmm_tile_row_flip = $pmm_tile_row->Clone();
$pmm_tile_row_flip->Flip();



# 9) convert pmm_tile_row.png pmm_tile_row_flip.png -append pmm_tile.png
@$q = ();
$q = $pmm_tile_row->Clone();
push(@$q, $pmm_tile_row_flip);
$pmm_tile = $q->Append(stack=>'true');



# 10) make comment with parameter values
$comment_string = $sourcefile_name . "*" . $rectangle_width . "*" . $rectangle_height  . "*" . $x1 . "*" . $y1 . "*");
$pmm_comment = $pmm_tile->Comment($comment_string); 



# 11) generate name of the result image
my($filename) = fileparse($sourcefile_fullname, @suffixes); 
if ($p <= 9) {$resultimage_name = "pmm_tile_" . $filename . "-0" . $p . ".jpg"}
if ($p > 9 and $p <= 99)  else {$resultimage_name = "pmm_tile_" . $filename . "-"  . $p . ".jpg"};

$resultimagepath_name = $pmm_resultfolder_path . $resultimage_name;




# 12) save result image 
$pmm_tile->Write(filename=>"$resultimagepath_name", compression=>'JPEG', quality=>'95');






