1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
|
#!/opt/bin/perl
# A photo-tiling pluggin. Take an image, and tile it (much like the
# mosaic operation, only using other images).
#
# Written in 1998 (c) by Aaron Sherman <ajs@ajs.com>.
# This plugin may be distributed under the same terms as The Gimp itself.
# See http://www.gimp.org/ for more information on The Gimp.
#
# TODO:
#
# o Fix undo
# o Handle input drawable correctly (for working on selections)
# o Find faster ways to sample sub-images.
# o More control over cropping
# o Scaling vs cropping of sub-images
# o Better color matching algorithms
# o Test (fix?) non-interactive use...
# o Allow tile aspect selection independant of base image
#
# DONE:
# o 04-22-2001, peter@kirchgessner.net: fix problems with functions
# that changed number of arguments
#
#12/5/03: <sjburges@gimp.org>
# s/gimp_convert/gimp_image_convert/
# s/gimp_drawable_image/gimp_drawable_get_image/
use Gimp qw(:auto __ N_);
use Gimp::Fu;
use Fcntl qw(O_RDWR O_CREAT O_TRUNC);
use Gimp::Feature;
BEGIN { eval "use DB_File";
$@ and Gimp::Feature::missing('Berkeley DB interface module') }
# use strict;
# use vars qw($DO_HSV $debug);
$DO_HSV = 0;
$debug = 0;
# This function takes:
# Gimp-provided:
# Image -- The gimp image to be operated on.
# Drawable -- Its drawable
# User-provided:
# X Tiles -- Number of images to be tiled across.
# Y Tiles -- Number of images to be tiled down.
# X Cells -- Number of color samples across for each image.
# Y Cells -- Number of color samples down for each image.
# Duplicates Weight -- Amount of weight to apply against images that
# have already been used.
# Directories -- Space separated list of directories.
#
# It will tile the images from the given directories over the given
# image to form a mosaic of the original.
sub perl_fu_image_tile {
my $image = shift;
my $drawable = shift;
my $xtiles = shift;
my $ytiles = shift;
my $xcells = shift;
my $ycells = shift;
my $dupweight = shift;
my $dirs = shift;
my $cleanup = shift;
my $subimages = 0;
my $TOP = "$ENV{HOME}/.gimp";
if (! -d $TOP) {
$TOP = "/tmp";
if (! -d $TOP) {
die "Don't know where to put temporary files!";
}
}
# Use C-Shell style file globbing to expand given directories, and
# allow them to be space-separated.
my @dirs = map {glob $_} split /\s+/, $dirs;
print "DEBUG: Dir list is ", join(", ", @dirs), "\n" if $debug;
my $dir;
my $imgwidth = gimp_drawable_width($drawable);
my $imgheight = gimp_drawable_height($drawable);
my $xtilewidth = int($imgwidth/$xtiles);
my $ytileheight = int($imgheight/$ytiles);
my $aspect = $xtilewidth/$ytileheight;
my $s_aspect = sprintf("%0.3f",$aspect);
my $type = gimp_image_base_type($image);
my $ndone=0;
gimp_image_undo_disable($image);
gimp_progress_init("Image Tiling...",-1);
my %tile_cache; # Tied to image tile database
my %wt_cache;
my $stored_keys = 0; # Number of keys stored to date.
my $db; # DB_File database reference
my $wdb;
# One cache file holds the image color samples, which may get re-used
# between runs.
my $cache_file = "$TOP/image_tile.${s_aspect}.${xcells}X${ycells}";
if (!defined($db = tie(%tile_cache, 'DB_File',
$cache_file, O_RDWR|O_CREAT,
0644, $DB_HASH))) {
die "Failed to create tile sample database: $!";
}
# The other cache file contains image re-use weights, which only get
# used once.
my $wt_file = "$TOP/image_tile.$$";
if (!defined($wdb=tie(%wt_cache,'DB_File',$wt_file,
O_RDWR|O_CREAT|O_TRUNC,0644,$DB_HASH))) {
die "Failed to create weight database: $!";
}
# Loop over directories, looking for images
foreach $dir (@dirs) {
print "DEBUG: **** load images from $dir\n" if $debug;
gimp_progress_update((40/@dirs)*($ndone++)/100);
local *DIR;
if (opendir(DIR,$dir)) {
my $file;
# Only take files with an extension, as Gimp won't be able to
# open others.
my @files = sort grep {/\.\w+$/} readdir DIR;
closedir(DIR);
my $filesdone=0;
foreach $file (@files) {
print "DEBUG: Load file: $file\n" if $debug;
gimp_progress_update((40/@dirs)*($ndone-1+($filesdone/@files))/100);
$filesdone++;
next unless -f "$dir/$file" && -s "$dir/$file";
if (defined $tile_cache{"$dir/$file"}) {
$wt_cache{"$dir/$file"} = 0;
$subimages++;
} else {
my $short = $file;
my $img;
$file = "$dir/$file";
# Open the sub-image, record info about it and close it.
eval {
$img = gimp_file_load($file,$file);
};
next if $@ || !defined($img) || !$img;
my $subtype = gimp_image_base_type($img);
if ($subtype != $type) {
if ($type == RGB_IMAGE) {
gimp_image_convert_rgb($img);
} elsif ($type == GRAY_IMAGE) {
gimp_image_convert_grayscale($img);
} elsif ($type == INDEXED_IMAGE) {
gimp_image_convert_indexed($img,1,256);
}
}
my $cells = get_image_cells($img,$xcells,$ycells,
$xtilewidth/$ytileheight);
$wt_cache{$file} = 0;
$tile_cache{$file} = $$cells;
$subimages++;
$db->sync(0) if ++$stored_keys % 16 == 0;
gimp_image_delete($img);
}
}
} else {
gimp_message("Cannot open $dir: $!");
}
}
if ($subimages == 0) {
die "$0: No subimages loaded.";
}
$db->sync(0);
$wdb->sync(0);
# Now store color info for target image
my $dup = gimp_image_new($imgwidth,$imgheight,RGB_IMAGE);
gimp_edit_copy($drawable); # gimp 1.1 -deleted $image
my $back =
gimp_layer_new($dup,$imgwidth,$imgheight,RGB_IMAGE,"Target",100,NORMAL_MODE);
gimp_image_add_layer($dup,$back,0);
my $sel = gimp_edit_paste($back,0); # gimp 1.1 -deleted $dup
gimp_floating_sel_anchor($sel);
my $oimage = get_image_cells($dup,$xtiles*$xcells,$ytiles*$ycells,
$imgwidth/$imgheight,40,60);
gimp_image_delete($dup);
undef $sel;
undef $back;
undef $dup;
gimp_progress_update(60/100);
# Now we have the image data, so it's time to start mapping
# in the sub-images.
$ndone=0;
# Randomize the order in which tiles will be mapped (this reduces the
# impact from weighting image re-use)
my @todo;
for(my $x=0;$x<$xtiles;$x++) {
for(my $y=0;$y<$ytiles;$y++) {
push(@todo, "$x,$y");
}
}
for(my $i=0;$i<@todo;$i++) {
# Don't need srand(), because we don't need to do it differently every
# time.
my $target = int(rand(@todo));
my $tmp = $todo[$i];
$todo[$i] = $todo[$target];
$todo[$target] = $tmp;
}
my @ocells;
# Now, map in the sub-images according to the random order determined, above
foreach my $coord (@todo) {
my($x,$y) = split /,/,$coord,2;
gimp_progress_update((60+40/($xtiles*$ytiles)*($ndone++))/100);
my $minmatch = undef;
my $matchid;
# Create a cache of all of the cell samples from the original image for
# this tile only.
for(my $xcell=0;$xcell<$xcells;$xcell++) {
for(my $ycell=0;$ycell<$ycells;$ycell++) {
$ocells[$xcell][$ycell] =
substr($$oimage,
(($x*$xcells + $xcell)*$ytiles*$ycells+
$y*$ycells +
$ycell) * 3, 3);
}
}
my $subimg;
my $weight;
# Loop through all available sub-images and find best fit for this
# tile.
while(($subimg,$weight)=each %wt_cache) {
my $match = 0;
for(my $xcell=0;$xcell<$xcells;$xcell++) {
my $subfile = $subimg;
for(my $ycell=0;$ycell<$ycells;$ycell++) {
# Cell samples are stored as packed 3-byte values
my($o1,$o2,$o3) = unpack 'CCC', $ocells[$xcell][$ycell];
my($n1,$n2,$n3) = unpack 'CCC',
substr($tile_cache{$subfile},($xcell*$ycells+$ycell)*3,3);
# 2 methods of comparing: by RGB and by HSV. HSV seems to
# give a more accurate map, as it stresses the matching of light
# and darkness. We do some weighting of the HSV match so that
# we don't care about hue as much if saturation or value is
# low, and we don't care about saturation as much if value is low
# The net effect is that for a black pixel, you don't care
# what color hue tells you it is, because it's always black
my $c3_delta = abs($o3 - $n3);
my $c2_delta;
my $c1_delta;
if ($DO_HSV) {
# c1 == H, c2 == S, c3 == V
$c2_delta = abs($o2 - $n2) * $o3 / 255;
$c1_delta = hue_dist($o1,$n1)* 2 * ($o3*$o2/(255**2));
} else {
# c1 == R, c2 == G, c3 == B
$c2_delta = abs($o2 - $n2);
$c1_delta = abs($o1 - $n1);
}
# Keep a running score of the differences between samples for this
# sub-image vs. this tile from the orginal
$match += $c1_delta + $c2_delta + $c3_delta;
}
}
# Weight for image duplicates.
$match += $wt_cache{$subimg};
if (!defined($minmatch) || $match < $minmatch) {
$minmatch = $match;
$matchid = $subimg;
}
}
if (!defined($matchid)) {
die("image_tile: No subimages selected!");
}
# Actually insert the selected image.
overlay_image($drawable, $matchid,
$xtilewidth*$x, $ytileheight*$y,
$xtilewidth, $ytileheight);
$wt_cache{$matchid} += $dupweight;
}
# Finish up.
undef $db;
untie %tile_cache;
undef $wdb;
untie %wt_cache;
unlink($wt_file);
unlink($cache_file) if $cleanup;
gimp_progress_update(1);
gimp_image_undo_enable($image);
gimp_displays_flush();
}
# Take IMAGE, XCELLS, YCELLS, TARGET_ASPECT.
# Works destructively on IMAGE, and returns a list of anon-lists which
# contain the color samples for the given IMAGE.
sub get_image_cells {
my $img = shift;
my $xcells = shift;
my $ycells = shift;
my $target_aspect = shift;
my $start_complete = shift;
my $end_complete = shift;
# print "Target aspect: $target_aspect\n";
my $file = gimp_image_get_filename($img);
# print "$file: ";
my $width = gimp_image_width($img)+0;
# print "width: $width ";
my $height = gimp_image_height($img)+0;
# print "height: $height\n";
my $cells = "\0\0\0" x ($xcells * $ycells);
return () if $width < 1 || $height < 1;
# First crop to fit tiles
match_aspect($img,$target_aspect,$width,$height);
# Now, scale down to xcells by ycells for color sampling
# NOTE: We will re-open this image later if it is chosen.
# This scaling is just to get color samples.
gimp_image_scale($img,$xcells,$ycells);
my $draw = gimp_image_active_drawable($img);
for(my $x=0;$x<$xcells;$x++) {
if (defined($start_complete)) {
gimp_progress_update(($start_complete+
($end_complete-$start_complete)*$x/$xcells)/100);
}
for(my $y=0;$y<$ycells;$y++) {
my $color = gimp_color_picker($draw,$x,$y,0,1,1.0); # Gimp 1.1 -deleted $img
my @c;
if ($DO_HSV) {
@c = rgb2hsv(@$color);
} else {
@c = @$color;
}
substr($cells,($x*$ycells+$y)*3,3) = pack('CCC',@c);
}
}
return \$cells;
}
# Take IMAGE, TARGET_ASPECT, WIDTH (of image), HEIGHT (of image)
# Crops IMAGE to match aspect ratio of TARGET_ASPECT.
sub match_aspect {
my $img = shift;
my $target_aspect = shift;
my $width = shift;
my $height = shift;
my $aspect = $width/$height;
if ($aspect < $target_aspect) {
my $oldheight=$height;
$height = int($width/$target_aspect);
# print "Image was $width X $oldheight, cropping to $width X $height\n";
gimp_image_crop($img,$width,$height,0,int(($oldheight-$height)/2));
} elsif ($aspect > $target_aspect) {
my $oldwidth=$width;
$width = int($target_aspect*$height);
# print "Image was $oldwidth X $height, cropping to $width X $height\n";
gimp_image_crop($img,$width,$height,int(($oldwidth-$width)/2),0);
}
}
# Take DRAWABLE, INFO, X, Y, WIDTH, HEIGHT
# Opens image referenced by INFO->{name} and scale/crop to fit in rectagnle
# described by X,Y,WIDTH,HEIGHT
sub overlay_image {
my $draw = shift;
my $file = shift;
my $x = shift;
my $y = shift;
my $width = shift;
my $height = shift;
my $img = gimp_file_load($file,$file);
my $subwidth = gimp_image_width($img);
my $subheight = gimp_image_height($img);
match_aspect($img,$width/$height,$subwidth,$subheight);
gimp_image_scale($img,$width,$height);
gimp_edit_copy(gimp_image_active_drawable($img)); #gimp 1.1 -deleted $img
my $baseimg = gimp_drawable_get_image($draw);
gimp_rect_select($baseimg,$x,$y,$width,$height,CHANNEL_OP_REPLACE,0,0);
my $sel = gimp_edit_paste($draw,0); # gimp 1.1 -deleted $baseimg
gimp_floating_sel_anchor($sel);
gimp_image_delete($img);
}
# Take a Red, Green, Blue color value and return Hue, Saturation and Value
# RGB and HSV data should be in the range 0-255 (note Hue is usually
# represented as 0-360, but here is scaled to be 0-255).
sub rgb2hsv {
my $r = shift;
my $g = shift;
my $b = shift;
my($h,$s,$v);
my $min = undef;
my $max = 0;
foreach my $color ($r, $g, $b) {
$min = $color if !defined($min) || $min>$color;
$max = $color if $color > $max;
}
$v = $max;
$s = $max?int(($max-$min)/$max*255+0.5):0;
if ($s == 0) {
$h = 0;
} else {
my $d = $max - $min;
if ($r == $max) {
$h = ($g-$b)/$d;
} elsif ($g == $max) {
$h = 2+($b-$r)/$d;
} else {
$h = 4+($r-$g)/$d;
}
# This:
# $h *= 60;
# $h += 360 if $h < 0;
# $h *= (256/360);
# , simplified is this:
$h= int(($h+($h<0?6:0)) * 128 / 3 + 0.5);
}
return ($h,$s,$v);
}
# Caclulate the "distance" between to HSV hue values in the range 0-255.
sub hue_dist {
my $h1 = shift;
my $h2 = shift;
my $d = abs($h1-$h2);
return($d>128?(256-$d):$d);
}
# Gimp::Fu registration routine for placing this function into gimp's PDB
register
"image_tile",
"Tile images to form a larger Image",
"Use Image Tile to take a directory of images and use it to
construct a single, existing image, sort of like the
Filters/Artistic/Mosaic plugin, but with images as the
tiles.",
"Aaron Sherman", "Aaron Sherman (c)", "1999-03-15",
N_"<Image>/Filters/Map/Image Tile...",
"*",
[
# Image and drawable are given for free...
# [PF_IMAGE, "Input image", undef],
# [PF_DRAWABLE, "Input drawable", undef],
[PF_INT32, "tiles_x", "Number of tiles (X)", 10],
[PF_INT32, "tiles_y", "Number of tiles (Y)", 10],
[PF_INT32, "samples_x", "Number of sample cells per tile (X)", 4],
[PF_INT32, "samples_y", "Number of sample cells per tile (Y)", 4],
[PF_INT32, "duplicates", "Duplicates (0[lots] - 100[none])", 5],
[PF_STRING, "image_dirs", "Sub-image directories (space speparated)"],
[PF_TOGGLE, "delete_cached", "Delete cached image samples?", 0]
],
\&perl_fu_image_tile;
exit main;
__END__
=head1 NAME
image_tile - An image tiling plug-in for The Gimp
=head1 SYNOPSIS
B<image_tile> is called from The Gimp under the Perl-Fu image menu.
=head1 DESCRIPTION
B<image_tile> is a plug-in for The Gimp that re-creates an image by tiling many
sub-images which are in turn chosen for their likeness to a part of the original.
In other words, you give image_tile a base image (the one you open in The
Gimp, and call image_tile on) and a list of directories to find other images
in. It then tiles small versions of the images over the original image in
such a way that you can still make out the original if you squint hard
enough.
=head1 LIMITATIONS
B<image_tile> requires a large number of image to work from. This is because
it needs to divide up your original image and for each tile, find another
image which looks like that tile. This can require anywhere from 2000 to tens
of thousands of component images.
image_tile will use as much disk space as is required to store the sampling
information that it creates for each of the sub images. However, its use of
memory is much more conservative. The assumption being that a lot more people
have a Gig of disk free than a Gig of RAM. So, expect a large file to be
created in your .gimp directory (you can select automatic cleanup of this
file if you wish).
=head1 PARAMETERS
When you bring up the image tiler, you are given several options. Each of these
is detailed below:
=over 5
=item Number of tiles
The number of tiles in the X and Y directions must be given. This is the
number of sub-images that will be tiled across and down your original image.
=item Number of cells
In each tile, the image tiler will sample color areas to determine a match.
The more color areas you sample, the more accurate the match, but this also
increases memory, disk and time usage. The default of 4 cells in each direction
is good for most tiling which is meant to be viewed on-line. Print-quality
tiling will have to use more samples to get even finer details right.
=item Duplicate weight
This is a number from 0 to 100 (actually, there is no real upper bound, but
100 is a practical upper limit). This is a weight applied to each sub-image
each time it has been selected. Thus, if you use a hight weight, images
will tend to be chosen only once. If you use a low weight, images will be
chosen as many times as they happen to be the best fit. A weight of 0 will lead
to the most accurate match, but due to the repetition of some images, you
may find the resulting image to be difficult to make out.
=item Sub-Image directories
This is a space-separated list of the directories in which The Gimp will
be able to load sub-images. You may use B<csh>-style file "globing" such
as C</tmp/images/image_dir_*> or C</mnt/cdrom/images_[1234]>.
=item Delete cached image samples
This toggle button will tell the image tiler whether or not to delete the
cached image samples that it creates while reading the sub-images. If you
are planning to attempt matching these sub-images against this base image
again (say, adding a few new files, or brightening the base image first),
you will probably want to keep them around, as the time savings is
huge. However, since these samples are based on aspect ratio and number
of cells, you cannot re-use the samples if you change the number of tiles
or number of cells. Sorry.
=back
=head1 AUTHOR
Written in 1998 (c) by Aaron Sherman <ajs@ajs.com>
=head1 BUGS
Most of the I<bugs> in the image tiler are actually just design limitations.
For example:
=over 5
=item *
The images must all be the same aspect ratio, so B<image_tile> will crop them
to match the target aspect ratio. Because of the large number of images
involved, it is impractical to specify a crop area for each one, so the
center of the image is chosen.
=item *
If The Gimp library were multi-threaded the image tiler would read more than
one sub-image at a time.
=item *
Directory selection is crude. Gimp needs a file/directory selection model for
plug-ins similar to the color selection model.
=item *
If some of your sub-images are bad, the image tiler will display an error
message when trying to load them. This can result in a I<lot> of messages
for a multi-thousand image database.
=item *
URLs should be handled as image directories.
=item *
Some text describing what image directory is being searched would be nice, but
this would require more code than I want to write right now.
=back
=head1 LICENSE
Copyright Aaron Sherman.
This plugin may be distributed under the same terms as The Gimp itself.
=head1 SEE ALSO
L<gimp>, L<perl>, L<Gimp>: the Gimp module for perl.
=cut
|