File Coverage

blib/lib/Image/Base/Gtk2/Gdk/Image.pm
Criterion Covered Total %
statement 15 64 23.4
branch 0 30 0.0
condition 0 17 0.0
subroutine 5 11 45.4
pod 3 5 60.0
total 23 127 18.1


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of Image-Base-Gtk2.
4             #
5             # Image-Base-Gtk2 is free software; you can redistribute it and/or modify it
6             # under the terms of the GNU General Public License as published by the Free
7             # Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Image-Base-Gtk2 is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with Image-Base-Gtk2. If not, see .
17              
18              
19             package Image::Base::Gtk2::Gdk::Image;
20 1     1   26122 use 5.008;
  1         5  
  1         48  
21 1     1   6 use strict;
  1         2  
  1         111  
22 1     1   5 use warnings;
  1         2  
  1         44  
23 1     1   5 use Carp;
  1         1  
  1         135  
24              
25             our $VERSION = 11;
26 1     1   6 use base 'Image::Base';
  1         2  
  1         5011  
27              
28             # uncomment this to run the ### lines
29             #use Smart::Comments;
30              
31              
32             sub new {
33 0     0 1   my ($class, %params) = @_;
34             ### Image-GdkImage new: \%params
35              
36             # $obj->new(...) means make a copy, with some extra settings
37 0 0         if (ref $class) {
38 0           croak "Cannot clone a GdkImage yet";
39             }
40              
41 0 0         if (! exists $params{'-gdkimage'}) {
42             ### create new GdkImage
43              
44 0   0       my $image_type = delete $params{'-image_type'} || 'fastest';
45 0   0       my $visual = delete $params{'-visual'}
46             || ($params{'-colormap'} && $params{'-colormap'}->get_visual)
47             || Gtk2::Gdk::Visual->get_system;
48             ### $image_type
49             ### $visual
50              
51 0           $params{'-gdkimage'} = Gtk2::Gdk::Image->new ($image_type,
52             $visual,
53             delete $params{'-width'},
54             delete $params{'-height'});
55             }
56              
57 0           my $self = bless {}, $class;
58 0           $self->set (%params);
59             ### $self
60 0           return $self;
61             }
62              
63             my %attr_to_get_method = (-colormap => 'get_colormap',
64             -visual => 'get_visual',
65             -width => 'get_width',
66             -height => 'get_height',
67             -depth => 'get_depth',
68              
69             # not documented yet, maybe a more specific name ...
70             -image_type => 'get_image_type',
71             );
72             sub _get {
73 0     0     my ($self, $key) = @_;
74              
75 0 0         if (my $method = $attr_to_get_method{$key}) {
76 0           return $self->{'-gdkimage'}->$method;
77             }
78 0           return $self->SUPER::_get($key);
79             }
80              
81             sub set {
82 0     0 1   my ($self, %params) = @_;
83             ### Image-GdkImage set(): \%params
84              
85 0           %$self = (%$self, %params);
86              
87 0 0         if (defined (my $colormap = delete $self->{'-colormap'})) {
88 0           $self->{'-gdkimage'}->set_colormap ($colormap);
89             }
90             ### set leaves: $self
91             }
92              
93             #------------------------------------------------------------------------------
94             # drawing
95              
96             sub xy {
97 0     0 1   my ($self, $x, $y, $colour) = @_;
98              
99 0           my $gdkimage = $self->{'-gdkimage'};
100 0 0 0       unless ($x >= 0
      0        
      0        
101             && $y >= 0
102             && $x < $gdkimage->get_width
103             && $y < $gdkimage->get_height) {
104             ### outside 0,0,width,height ...
105 0           return undef; # fetch or store
106             }
107              
108 0 0         if (@_ >= 4) {
109             ### Image-GdkImage xy: "$x, $y, $colour"
110 0           $gdkimage->put_pixel ($x,$y, $self->colour_to_pixel($colour));
111             } else {
112 0           return $self->pixel_to_colour($gdkimage->get_pixel ($x,$y))
113             }
114             }
115              
116             sub colour_to_pixel {
117 0     0 0   my ($self, $colour) = @_;
118             ### colour_to_pixel: $colour
119 0 0         if (defined (my $pixel = $self->{'-colour_to_pixel'})) {
120 0           return $pixel;
121             }
122 0 0         if ($colour =~ /^\d+$/) {
123 0           return $colour;
124             }
125 0 0         if ($colour eq 'set') {
126 0           return 1;
127             }
128 0 0         if ($colour eq 'clear') {
129 0           return 0;
130             }
131              
132 0           my $gdkimage = $self->{'-gdkimage'};
133 0 0         if (my $colormap = $gdkimage->get_colormap) {
134             # think parse and rgb_find are client-side operations, no need to cache
135             # the results
136             #
137 0   0       my $colorobj = Gtk2::Gdk::Color->parse ($colour)
138             || croak "Cannot parse colour: $colour";
139 0           $colormap->rgb_find_color ($colorobj);
140             ### rgb_find_color: $colorobj->to_string
141             ### pixel: $colorobj->pixel
142 0           return $colorobj->pixel;
143             }
144 0 0         if ($gdkimage->get_depth == 1) {
145 0 0         if ($colour =~ /^#(000)+$/) {
    0          
146 0           return 0;
147             } elsif ($colour =~ /^#(FFF)+$/i) {
148 0           return 1;
149             }
150             }
151 0           croak "No colormap to interpret colour: $colour";
152             }
153              
154             sub pixel_to_colour {
155 0     0 0   my ($self, $pixel) = @_;
156             ### pixel_to_colour: $pixel
157 0 0         if (my $colormap = $self->{'-gdkimage'}->get_colormap) {
158 0           my $colorobj = $colormap->query_color($pixel);
159             ### in colormap: $colorobj->to_string
160             ### pixel: $colorobj->pixel
161 0           return sprintf '#%04X%04X%04X',
162             $colorobj->red, $colorobj->green, $colorobj->blue;
163             } else {
164 0           return $pixel;
165             }
166             }
167              
168             1;
169             __END__