| 1 | package CSS::SpriteBuilder::ImageDriver::GD; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | CSS::SpriteBuilder::ImageDriver::GD - Class for image manipulation using GD module. | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use GD; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use base 'CSS::SpriteBuilder::ImageDriver::Common'; | ||
| 13 | |||
| 14 | our %COLOR_MAP = map { | ||
| 15 | chomp; | ||
| 16 | my ($name, @rgb) = split /\s+/; | ||
| 17 | $name = [ map { hex $_ } @rgb ]; | ||
| 18 | } <DATA>; | ||
| 19 | |||
| 20 | # Use truecolor by default | ||
| 21 | GD::Image->trueColor(1); | ||
| 22 | |||
| 23 | sub driver { 'GD' } | ||
| 24 | sub width { $_[0]->{_image} ? $_[0]->{_image}->width() : 0 } | ||
| 25 | sub height { $_[0]->{_image} ? $_[0]->{_image}->height() : 0 } | ||
| 26 | |||
| 27 | sub reset { | ||
| 28 | my ($self, $image) = @_; | ||
| 29 | |||
| 30 | $self->{_image} = $image ? $image->{_image}->clone() : undef; | ||
| 31 | |||
| 32 | return; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub read { | ||
| 36 | my ($self, $filename) = @_; | ||
| 37 | |||
| 38 | $self->{_image} = GD::Image->new($filename) | ||
| 39 | or die "Failed to read image from '$filename' due: $!"; | ||
| 40 | |||
| 41 | $self->{_image}->alphaBlending(0); | ||
| 42 | $self->{_image}->saveAlpha(1); | ||
| 43 | |||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub write { | ||
| 48 | my ($self, $filename) = @_; | ||
| 49 | |||
| 50 | return unless $self->{_image}; | ||
| 51 | |||
| 52 | if ($filename =~ /\.(png|jpg|gif)$/i) { | ||
| 53 | my $ext = lc $1; | ||
| 54 | my $data; | ||
| 55 | if ($ext eq 'png') { | ||
| 56 | $data = $self->{_image}->png(); | ||
| 57 | } | ||
| 58 | elsif ($ext eq 'jpg') { | ||
| 59 | $data = $self->{_image}->jpeg( $self->{_quality} ); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $data = $self->{_image}->gif(); | ||
| 63 | } | ||
| 64 | die "Failed to write image due: $!" unless $data; | ||
| 65 | |||
| 66 | open(my $fh, '>', $filename) or die "Failed to open file '$filename' due: $!"; | ||
| 67 | binmode $fh; | ||
| 68 | print $fh $data; | ||
| 69 | close $fh or die "Failed to close file '$filename' due: $!"; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | die "Unknown extension of the file '$filename'"; | ||
| 73 | } | ||
| 74 | |||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub set_transparent_color { | ||
| 79 | my ($self, $color) = @_; | ||
| 80 | |||
| 81 | return unless $self->{_image}; | ||
| 82 | |||
| 83 | my $rgb = $COLOR_MAP{ lc $color } or die "Unknown color '$color'"; | ||
| 84 | my $index = $self->{_image}->colorClosest(@$rgb); | ||
| 85 | $self->{_image}->transparent($index); | ||
| 86 | |||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub extent { | ||
| 91 | my ($self, $width, $height) = @_; | ||
| 92 | |||
| 93 | my $old_image = $self->{_image}; | ||
| 94 | my $new_image = GD::Image->new($width, $height, 1); | ||
| 95 | |||
| 96 | $new_image->alphaBlending(0); | ||
| 97 | $new_image->saveAlpha(1); | ||
| 98 | $new_image->copy( $old_image, 0, 0, 0, 0, $old_image->width(), $old_image->height() ) | ||
| 99 | if $old_image; | ||
| 100 | |||
| 101 | $self->{_image} = $new_image; | ||
| 102 | |||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub composite { | ||
| 107 | my ($self, $image, $x, $y) = @_; | ||
| 108 | |||
| 109 | $self->{_image}->copy( $image->{_image}, $x, $y, 0, 0, $image->width(), $image->height() ); | ||
| 110 | |||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __DATA__ | ||