#!/usr/bin/perl -w

# Image to HTML converter
# Copyright (C) 2003 Neil Fraser, Scotland
# http://neil.fraser.name/

# This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation.
# 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.  http://www.gnu.org/

# Loads JPEGs, PNGs and GIFs from websites and converts them to HTML.
# Usage:  img2html.pl?img=www.example.com/image.jpg

# Modified by G. Cantallops Ramis in 2007 to allow command line use _without_ CGI dependences, downscaling or original image removal. Path issues eased, too.

use strict;
use GD;

# The only argument should be the file name
my $filename = $ARGV[0];

# Load the original image.
my $oldsize = int((-s "$filename")/1024 + 0.5);
GD::Image->trueColor(1);
my $image = GD::Image->new("$filename");
if (!$image && index(`file $filename`, ' GIF ') != -1) {
  # This image may be a GIF.  Try converting it to PNG.
  `gif2png $filename`;
  $filename =~ s/.gif$/.png/i;
  $image = GD::Image->new("$filename");
} elsif (!$image && index(`file $filename`, ' JPEG ') != -1) {
  # This image is a JPEG but isn't in JFIF.  Try converting it.
  my $newfilename = 'jpg2jpg_'.$filename;
  `jpegtran $filename > $newfilename`;
  $filename = $newfilename;
  $image = GD::Image->new("$filename");
}

$image || die("Can't load image:", "$filename", $!, 'Note that only JPEGs, PNGs and GIFs are supported.');

# Scan the image pixel by pixel and build the HTML table.
my $table = '';
my $row;
my ($x, $y);
my ($newX, $newY) = $image->getBounds();
my ($r, $g, $b, $rgb);
my ($prev_rgb, $span);
my $firstrow = 1; # Disable RLE for first row of each table segment (dodge Mozilla bug)
for($y=0; $y<$newY; $y++) {
  $row = '';
  for($x=0; $x<=$newX; $x++) {
    ($r,$g,$b) = $image->rgb($image->getPixel($x, $y));
    if ($x == $newX) {
      # Dummy run to clear the colspan buffer.
      $rgb='';
    } else {
      $rgb = sprintf('%02lx%02lx%02lx', $r,$g,$b);
    }
    if ($x == 0) { # Initialise the RLE (Run Length Encoding)
      $prev_rgb = $rgb;
      $span = 0;
    }
    $span++;
    if ($rgb ne $prev_rgb || $firstrow) {
      if ($span == 1) { # One pixel.
        $row .= "<TD BGCOLOR=#$prev_rgb><img width=1 height=1></TD>";
      } else { # A run of multiple pixels with the same colour.
        $row .= "<TD BGCOLOR=#$prev_rgb COLSPAN=$span><img width=1 height=1></TD>";
      }
      $span = 0;
      $prev_rgb = $rgb;
    }
  }
  $table .= "<TR>$row</TR>\n";

  # Segment the table so that MSIE renders it in pieces instead of waiting till the end.
  if ($y != 0 && ($y == 5 || $y % 15 == 0) && $y < $newY-10) {
    $table .= "</TABLE><TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>\n";
    $firstrow = 1;
  } else {
    $firstrow = 0;
  }
}
my $newsize = int(length($table)/1024 + 0.5);

# We're done.  Now print it all out (this is what takes the time).
print <<END;
<HTML>
<HEAD>
<TITLE>img2html-noncgi: $filename</TITLE>
</HEAD>

<BODY>
<DL>
<DT>
<DD>
<P>
<DT>
<DD>
<DD>

<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
$table
</TABLE>

<P>
<DT>
</DL>
</BODY></HTML>
END
