#! /usr/bin/perl -w # # $Id$ # Copyright (c) 2007 patrick keshishian use strict; use GD; # global vars my $self = `basename $0`; chomp($self); # proto-sub sub main(); # main main(); exit(0); # subs sub main() { my ($wm_file, $src_file, $out_file); my ($iw, $is, $io); my $percent = 55.0; # transparency # set-up $wm_file = 'circ.png'; $src_file = 'test_in.jpg'; $out_file = sprintf('test_out_v2_%04.1f.jpg', $percent); GD::Image->trueColor(1); # open source image $is = GD::Image->new($src_file) or die(sprintf("Failed to open \"%s\"", $src_file)); # open watermark image $iw = GD::Image->new($wm_file) or die(sprintf("Failed to open \"%s\"", $wm_file)); printf("iw->trueColor() = %d$/", $iw->trueColor()); printf("is->trueColor() = %d$/", $is->trueColor()); # Turn off alpha-blending temporarily for the watermark image. If # we don't do this here, as we adjust each pixel colour and call # setPixel(), GD will go through gdAlphaBlend() process for the # current pixel colour and what we want to set the colour to. $iw->alphaBlending(0); # Go through every pixel in the watermark image and adjust the # alpha channel value appropriately. # # This is most likely not portable, as I am not too sure about # then endianess of PNG data. $percent /= 100.0; for (my $h = 0; $h < $iw->height; ++$h) { for (my $x = 0; $x < $iw->width; ++$x) { my ($px, $alpha); # Get pixel's colour value (a.k.a., index) $px = $iw->getPixel($x, $h); # Get the alpha channel value $alpha = ($px >> 24) & 0xff; # If it is completely transparent (0x7f = 127) skip if ($alpha == 127) { next; } $alpha += int((127 - $alpha) * $percent); $alpha = 127 if ($alpha > 127); # Cap (paranoia) # Adjust the new colour value based on the adjusted # alpha channel value and set it. $px = ($px & 0x00ffffff) | ($alpha << 24); $iw->setPixel($x, $h, $px); } } # Turn on alpha-blending now. $iw->alphaBlending(1); # copy watermark onto source image $is->copy($iw, 50, 50, 0, 0, $iw->width, $iw->height); # open destination/out file for writing open(OUT, ">$out_file") or die(sprintf("Failed to write \"%s\": %s", $out_file, $!)); # write out resulting image print(OUT $is->jpeg(70)); close(OUT); exit(0); }