#!/usr/bin/perl -w

# textimage.pl - Given an image, creates a new image, created 
# from individual characters.

# Copyright (C) 2000  Ian Pointer <ian.pointer@btinternet.com>
# 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.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Rewritten for GIMP 1.2 and the Graphics Muse Tools CD
# by Michael J. Hammel (mjhammel@graphics-muse.org
# Jan 26, 2002
#
# Version: 0.1
#
# Updates: 0.01 23/9/2000 Basic functionality present
#          0.02 23/9/2000 Progress bar, better method of handling font size,
#                         ignore whitespace option.
#          0.1  24/9/2000 Moved call to load_file to nearer the start of 
#                         the plug-in, so if it can't find the textfile,
#                         it fails a lot quickier than if it has to create
#                         the new image.





use Gimp qw(:auto);
use Gimp::Fu;
use Gimp::Util;

sub text_main {

# Grab arguments

my($img, $drawable, $font, $ignore_whitespace, $filename) = @_;



# Get the height and width from the original image, 
# and create a new image, modifying for size...

my $height = gimp_drawable_height($drawable);
my $width = gimp_drawable_width($drawable);

my $fontsize = (xlfd_size $font)[0];

my $new_height = $height * $fontsize;
my $new_width = $width * $fontsize;

# Grab text file

my @textarray = load_file($filename);
my $number_of_chars = @textarray;

my $newimg = gimp_image_new($new_width, $new_height, RGB);

my $layer = gimp_layer_new($newimg, $new_width, $new_height, RGB, "Layer 1", 
			   100,
			   NORMAL_MODE);


gimp_palette_set_background([255,255,255]);
gimp_edit_fill($layer, WHITE_IMAGE_FILL);



my $character_index=0;
my $pixelxcount=0;
my $pixelycount=0;
my $newpixely=0;
my $progress =0;

my @value;

# Groovy progress bar...in a new window

gimp_progress_init("Creating text-image", -1);



while($pixelycount < $height) {
  while($pixelxcount < $width) {
    
    @value =  $drawable->get_pixel($pixelxcount, $pixelycount);

    # If we're ignoring whitespace, skip on to the next pixel

    if($ignore_whitespace) {
      if($value[0] == 255 && $value[1] ==255 && $value[2]==255) {
#	print "Skipping...Pixel x: $pixelxcount y: $pixelycount\n";
	 $pixelxcount++;

	next;
      }
    }



    Gimp->palette_set_foreground(\@value);
    $newimg->text_fontname($layer, $pixelxcount*$fontsize, $newpixely,
	      $textarray[$character_index % $number_of_chars],
			   -1,0, xlfd_size $font,$font);
    

 #   print "Pixel x: $pixelxcount y: $pixelycount\n";

    $pixelxcount++;
    $character_index++;    
  }

  # Update the progress bar

  $progress = ($pixelycount / $height);
  Gimp->progress_update($progress);

  $pixelycount++;
  $newpixely+=$fontsize;
  $pixelxcount=0;
 
}


gimp_image_add_layer($newimg, $layer,-1);


return $newimg;


}


# Load in a textfile, split it into characters, and return an array
# of those same characters

sub load_file($) {

my $textfile = shift;

# Load in the text file

# Slurrrp...

undef $/;


open TEXTFILE, $textfile or die "Can't open $textfile: $!";

my $text = <TEXTFILE>;

 $/="\n";


$text =~ s/\n//g;
$text =~ s/\s//g;



return split(//, $text);



}





# Register this script with the Gimp's PDB.

register (
   "text_image",                                      
   "Creates a textual representation of an image",   
   "Given a text file, this plug-in creates a new image, in which each pixel of the old image is represented by a character from the text file.\n Parameters:\nFont - Which font to use (set to pixels)\n Ignore Whitespace - Don't plant white-coloured text\n Filename - The text file to use (make sure it exists beforehand!)",                                   
   "Ian Pointer, <ian.pointer\@btinternet.com",   
   "Copyright 2000 Ian Pointer. Released under the GPL.",           
   "Version 0.1: Sept 2000: It works. But verrrrry slow....",                       
   "<Image>/Filters/Render/Text-Image",                  
   "RGB*",                                    
   [                                          
    [ PF_FONT,"font","Font to use in the new image",
                       "-*-helvetica-*-*-*-*-34-*-*-*-*-*-*-*"],
    [ PF_TOGGLE, "ignore_whitespace","Ignore whitespace in original image?",1],
    [ PF_STRING, "filename", "Text file to use",""]
   ],
   \&text_main
);

exit main();
