Brought all packages under eye of strict, warnings and love of Carp, For
[sdlgit/SDL_perl.git] / lib / SDL / Tool / Graphic.pm
CommitLineData
8fde61e3 1#
2# SDL::GraphicTool - zooming and rotating graphic tool
3#
4# Copyright (C) 2002 Russell E. Valentine
5# Copyright (C) 2002 David J. Goehrig
6
7package SDL::Tool::Graphic;
8
084b921f 9use strict;
10use warnings;
11use Carp;
8fde61e3 12use SDL;
13use SDL::Config;
14require SDL::Surface;
15
16sub new {
17 my $proto = shift;
18 my $class = ref($proto) || $proto;
084b921f 19 my $self = {};
8fde61e3 20 bless $self, $class;
21 $self;
22}
23
24
25sub DESTROY {
26 # nothing to do
27}
28
29
30sub zoom {
31 my ( $self, $surface, $zoomx, $zoomy, $smooth) = @_;
084b921f 32 croak "SDL::Tool::Graphic::zoom requires an SDL::Surface\n"
8fde61e3 33 unless ( ref($surface) && $surface->isa('SDL::Surface'));
34 my $tmp = $$surface;
35 $$surface = SDL::GFXZoom($$surface, $zoomx, $zoomy, $smooth);
36 SDL::FreeSurface($tmp);
37 $surface;
38}
39
40sub rotoZoom {
41 my ( $self, $surface, $angle, $zoom, $smooth) = @_;
084b921f 42 croak "SDL::Tool::Graphic::rotoZoom requires an SDL::Surface\n"
8fde61e3 43 unless ( ref($surface) && $surface->isa('SDL::Surface'));
44 my $tmp = $$surface;
45 $$surface = SDL::GFXRotoZoom($$surface, $angle, $zoom, $smooth);
46 SDL::FreeSurface($tmp);
47 $surface;
48}
49
50sub grayScale {
51 my ( $self, $surface ) = @_;
084b921f 52 my $workingSurface;
8fde61e3 53 if($surface->isa('SDL::Surface')) {
084b921f 54 $workingSurface = $$surface;
8fde61e3 55 } else {
56 $workingSurface = $surface;
57 }
58 my $color;
59 my $width = SDL::SurfaceW($workingSurface);
60 my $height = SDL::SurfaceH($workingSurface);
61 for(my $x = 0; $x < $width; $x++){
62 for(my $y = 0; $y < $height; $y++){
63 my $origValue = SDL::SurfacePixel($workingSurface, $x, $y);
64 my $newValue = int(0.3*SDL::ColorR($origValue) + 0.59 * SDL::ColorG($origValue) + 0.11*SDL::ColorB($origValue));
65 SDL::SurfacePixel($workingSurface, $x, $y, SDL::NewColor($newValue, $newValue, $newValue));
66 }
67 }
68
69 if($surface->isa('SDL::Surface')) {
70 $surface = \$workingSurface;
71 } else {
72 $surface = $workingSurface;
73 }
74}
75
76sub invertColor {
77 my ( $self, $surface ) = @_;
084b921f 78 my $workingSurface;
8fde61e3 79 if($surface->isa('SDL::Surface')) {
80 $workingSurface = $$surface;
81 } else {
82 $workingSurface = $surface;
83 }
84 my $width = SDL::SurfaceW($workingSurface);
85 my $height = SDL::SurfaceH($workingSurface);
86 for(my $x = 0; $x < $width; $x++){
87 for(my $y = 0; $y < $height; $y++){
88 my $origValue = SDL::SurfacePixel($workingSurface, $x, $y);
89 my $newValue = int(0.3*SDL::ColorR($origValue) + 0.59 * SDL::ColorG($origValue) + 0.11*SDL::ColorB($origValue));
90 SDL::SurfacePixel($workingSurface, $x, $y, SDL::NewColor(255-SDL::ColorR($origValue), 255 - SDL::ColorG($origValue), 255 - SDL::ColorB($origValue)));
91 }
92 }
93
94 if($surface->isa('SDL::Surface')) {
95 $$surface = $workingSurface;
96 } else {
97 $surface = $workingSurface;
98 }
99}
100
084b921f 101croak "SDL::Tool::Graphic requires SDL_gfx support\n"
8fde61e3 102 unless SDL::Config->has('SDL_gfx');
103
104
1051;
106
107__END__;
108
109=pod
110
111
112
113=head1 NAME
114
115SDL::Tool::Graphic
116
117=head1 DESCRIPTION
118
119L<SDL::Tool::Graphic> is a module for zooming and rotating L<SDL::Surface> objects.
120
121=head1 METHODS
122
123=head2 zoom ( surface, xzoom, yzoom, smooth )
124
125C<SDL::Tool::Graphic::zoom> scales a L<SDL::Surface> along the two axis independently.
126
127=head2 rotoZoom ( surface, angle, zoom, smooth )
128
129C<SDL::Tool::Graphic::rotoZoom> rotates and fixed axis zooms a L<SDL::Surface>.
130
131=head2 grayScale ( surface )
132
133C<SDL::Tool::Graphic::grayScale> rotates and fixed axis zooms a L<SDL::Surface>.
134
135=head2 invertColor ( surface )
136
137C<SDL::Tool::Graphic::invertColor> inverts the color of a <SDL::Surface>.
138
139
140=head1 AUTHOR
141
142Russell E. Valentine
143
144=head1 SEE ALSO
145
146L<perl> L<SDL::Surface>
147
148=cut