#!/usr/bin/env perl # -*- Mode: perl; tab-width: 8; indent-tabs-mode: t -*- # Copyright (c) 2007,2009 Jeon, Myoung-jin # License: MIT use warnings; use strict; use Carp; # carp use Cairo; BEGIN { die "png backend not supported" unless Cairo::HAS_PNG_FUNCTIONS; } #package CairoExamples; #use Exporter qw(import); #our @EXPORT_OK = qw(draw_reflection); sub draw_reflection(%) { my %args = @_; my ( $context, $source, $rratio, $opacity ) = @args{ qw(context image_surface reflect_ratio reflect_opacity) }; $rratio = 0.5 unless defined $rratio and 0 < $rratio and $rratio <= 1.0; $opacity = 0.5 unless defined $opacity and 0 < $opacity and $opacity <= 1.0; # FIXME: more arguments checks ... my $ref_image = Cairo::ImageSurface->create( 'argb32', @args{qw(image_width image_height)} ); my $cr = Cairo::Context->create( $ref_image ); unless ( $cr->status eq 'success' ) { carp $cr->status_to_string; return; } # note: we're going to draw from the bottom $cr->translate( 0.0, $args{'image_height'} ); $cr->scale( 1.0, -1.0 ); $cr->set_source_surface( $source, (0.) x 2 ); # source and offset(x,y) # fade out mask my $fade_mask = Cairo::LinearGradient-> create( (0.) x 3, $args{'image_height'} ); $fade_mask->add_color_stop_rgba( 1.0-$rratio, (0.) x 4 ); # offset, r,g,b,a $fade_mask->add_color_stop_rgba( 1.0, (0.) x 3, $opacity ); $cr->mask( $fade_mask ); # draw reflection $cr->set_source_rgba( (0.) x 4 ); # this need to draw correctly. $cr->paint(); # set pattern from surface $context->set_source_surface( $ref_image, (0.) x 2 ); $context->paint(); } # draw_reflection(%) package main; use Getopt::Long; # GetOptions my $APP_NAME = 'cairo-reflect'; sub DEF_REFLECTION_RATIO { 0.5 } sub DEF_REFLECTION_HEIGHT { 0.5 } sub show_help_message { print qq{ Usage: $APP_NAME [options] image.png output.png Options: -rr|--rratio reflection height ratio (0.0 < value <= 1.0) -op|--opactiy reflection opacity (0.0 < value <= 1.0) }; 1; } my ( $reflect_ratio, $reflect_opacity ) = ( DEF_REFLECTION_RATIO, DEF_REFLECTION_HEIGHT ); GetOptions( "rratio|rr=f", => \$reflect_ratio, "opacity||op=f", => \$reflect_opacity, ); unless ( 0 < $reflect_ratio and $reflect_ratio <= 1.0 ) { carp "incorrect value for reflection ratio: ". "$reflect_ratio: revert to ".($reflect_ratio=DEF_REFLECTION_RATIO)."\n"; } unless ( 0 < $reflect_opacity and $reflect_opacity <= 1.0 ) { carp "incorrect value for opacity: $reflect_opacity: ". "revert to ".($reflect_opacity = DEF_REFLECTION_HEIGHT )."\n"; } if ( scalar( @ARGV ) < 2 ) { show_help_message and exit 1; } my ( $filename, $output_filename ) = @ARGV[ 0, 1 ]; # image surface from source file my $image = Cairo::ImageSurface->create_from_png( $filename ); unless ( $image->status() eq 'success' ) { carp "open `$filename' failed.\n". $image->status_to_string(); exit 2; } my ( $width, $height ) = ( $image->get_width(), $image->get_height() ); my $new_height = $height + $height * $reflect_ratio; my $output_image = Cairo::ImageSurface->create( 'argb32', $width, $new_height ); my $cr = Cairo::Context->create( $output_image ); unless ( $cr->status eq 'success' ) { carp "$APP_NAME: Cairo::Context->create() failed.\n"; exit 2; } # draw original image $cr->set_source_surface( $image, 0.0, 0.0 ); $cr->paint(); # draw reflection $cr->translate( 0.0, $height ); draw_reflection( 'context' => $cr, 'image_surface' => $image, 'image_width' => $width, 'image_height' => $height, 'reflect_ratio', => $reflect_ratio, 'reflect_opacity' => $reflect_opacity ); # create png file $output_image->write_to_png( $output_filename ); unless ( $output_image->status eq 'success' ) { carp "$APP_NAME: output file creation failed.\n"; exit 3; } __END__ =pod = head COPYRIGHT Copyright (C) 2007 JEON Myoung-jin =head1 LICENSE Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.