Debian Codegraph @

Image/Magick.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 ############################################################
2 #
3 # OpenGL::Image::Magick - Copyright 2007 Graphcomp - ALL RIGHTS RESERVED
4 # Author: Bob "grafman" Free - [email protected]
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8 #
9 ############################################################
10
11 package OpenGL::Image::Magick;
12
13 require Exporter;
14
15 use Carp;
16
17 use vars qw($VERSION $DESCRIPTION @ISA);
18 $VERSION = '1.02';
19
20 $DESCRIPTION = qq
21 {Supports optimized internal interfaces to the ImageMagick library.};
22
23 use OpenGL::Image::Common;
24 @ISA = qw(Exporter OpenGL::Image::Common);
25
26 use OpenGL(':constants');
27
28
29
30 =head1 NAME
31
32 OpenGL::Image::Magick - copyright 2007 Graphcomp - ALL RIGHTS RESERVED
33 Author: Bob "grafman" Free - [email protected]
34
35 This program is free software; you can redistribute it and/or
36 modify it under the same terms as Perl itself.
37
38
39 =head1 DESCRIPTION
40
41 This is a driver module for use with the OpenGL module.
42 While it may be called directly, it will more often be called
43 by the OpenGL::Image abstraction module.
44
45 Note: OpenGL::Image defaults to this module.
46
47 This is a subclass of the OpenGL::Image::Common module.
48
49 Requires the Image::Magick module; 6.3.5 or newer is recommended.
50
51
52 =head1 SYNOPSIS
53
54 ##########
55 # Check for installed imaging engines
56
57 use OpenGL::Image;
58 my $img = new OpenGL::Image(engine=>'Magick',source=>'MyImage.png');
59
60
61 ##########
62 # Methods defined in the OpenGL::Image::Common module:
63
64 # Get native engine object
65 # Note: must not change image dimensions
66 my $obj = $img->Native;
67 $obj->Quantize() if ($obj);
68
69 # Alternately (Assuming the native engine supports Blur):
70 $img->Native->Blur();
71
72 # Test if image width is a power of 2
73 if ($img->IsPowerOf2());
74
75 # Test if all listed values are a power of 2
76 if ($img->IsPowerOf2(@list));
77
78 # Get largest power of 2 size within dimensions of image
79 my $size = $img->GetPowerOf2();
80
81 # Get all parameters as a hashref
82 my $params = $img->Get();
83
84 # Get one or more parameter values
85 my @values = $img->Get(@params);
86
87 # Get/Set Pixel values (normalized to 1.0)
88 my($r,$g,$b,$a) = $img->GetPixel($x,$y);
89
90 # Sync cache after done modifying pixels
91 $img->SetPixel($x,$y,$r,$g,$b,$a);
92 $frame->Sync();
93
94
95 ##########
96 # Supported parameters:
97
98 # source - source image, if defined
99 # width - width of image in pixels
100 # height - height of image in pixels
101 # pixels - number of pixels
102 # components - number of pixel components
103 # size - bytes per component
104 # length - cache size in bytes
105 # endian - 1 if big endian; otherwise 0
106 # alpha - 1 if has alpha channel, -1 if has inverted alpha channel; 0 if none
107 # flipped - 1 bit set if cache scanlines are top to bottom; others reserved
108 # gl_internalformat - internal GL pixel format. eg: GL_RGBA8, GL_RGBA16
109 # gl_format - GL pixel format. eg: GL_RGBA, GL_BGRA
110 # gl_type - GL data type. eg: GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT
111
112
113 ##########
114 # APIs defined in this module:
115
116 # Get engine version
117 my $ver = OpenGL::Image::THIS_MODULE::EngineVersion();
118
119 # Get engine description
120 my $desc = OpenGL::Image::ENGINE_MODULE::EngineDescription();
121
122
123 ##########
124 # Methods defined in this module:
125
126 # Sync the image cache after modifying pixels.
127 # Used by some engines for paged caches; otherwise a NOP.
128 $img->Sync();
129
130 # Return the image's cache as an OpenGL::Array object.
131 # Note: OGA may change after a cache update
132 my $oga = $img->GetArray();
133
134 # Return a C pointer to the image's cache.
135 # For use with OpenGL's "_c" APIs.
136 # Note: pointer may change after a cache update
137 $img->Ptr();
138
139 # Save file - automatically does a Sync before write
140 $img->Save('MyImage.png');
141
142 # Get image blob.
143 my $blob = $img->GetBlob();
144
145 =cut
146
147 eval 'use Image::Magick';
148
149
150 # Get engine version
151 sub EngineVersion
152 {
153 return $Image::Magick::VERSION;
154 }
155
156 # Get engine description
157 sub EngineDescription
158 {
159 return $DESCRIPTION;
160 }
161
162 # Base constructor
163 sub new
164 {
165 my $this = shift;
166 my $class = ref($this) || $this;
167
168 my $self = new OpenGL::Image::Common(@_);
169 return undef if (!$self);
170
171 $self->{params}->{engine} = 'Magick';
172 $self->{params}->{version} = EngineVersion();
173
174 $self->{params}->{components} = 4;
175 $self->{params}->{alpha} = 0;
176 $self->{params}->{flipped} = 1;
177
178 # Use source image if supplied
179 my $img;
180 if ($self->{params}->{source})
181 {
182 $img = new Image::Magick();
183 return undef if (!$img);
184
185 my $stat = $img->Read($self->{params}->{source});
186 return undef if ($stat);
187
188 ($self->{params}->{width},$self->{params}->{height}) =
189 $img->Get('Width','Height');
190 return undef if (!$self->{params}->{width} || !$self->{params}->{height});
191
192 $self->{native} = $img;
193 }
194 # Otherwise create uninitialized image
195 else
196 {
197 my $w = $self->{params}->{width};
198 my $h = $self->{params}->{height};
199 my $blob = $self->{params}->{blob};
200
201 if ($w && $h)
202 {
203 my $dim = $w.'x'.$h;
204 $self->{native} = new Image::Magick(size=>$dim, magick=>'RGBA', depth=>8);
205 }
206 elsif ($blob)
207 {
208 $self->{native} = new Image::Magick();
209 }
210 return undef if (!$self->{native});
211 $img = $self->{native};
212
213 # Populate with blob
214 if ($blob)
215 {
216 my $stat = $img->BlobToImage($blob);
217 return undef if ($stat);
218
219 if (!$w || !$h)
220 {
221 ($self->{params}->{width},$self->{params}->{height}) =
222 $img->Get('Width','Height');
223 }
224 }
225 # Otherwise fill with 'none'
226 else
227 {
228 my $stat = $img->Read('xc:none');
229 return undef if ($stat);
230 $img->Set(type=>'truecolormatte');
231 }
232 }
233
234 my $alpha = $img->Get('matte');
235 $img->Set('matte'=>'True') if (!$alpha);
236
237 # Good to go
238 bless($self,$class);
239
240 # Init params
241 return undef if (!$self->init());
242 $self->SyncOGA();
243
244 return $self;
245 }
246
247 # Initialize object
248 sub init
249 {
250 my($self) = @_;
251
252 my $w = $self->{params}->{width};
253 my $h = $self->{params}->{height};
254 $self->{params}->{pixels} = $w * $h;
255
256 my $elements = $self->{params}->{pixels} * $self->{params}->{components};
257
258 my $img = $self->{native};
259
260 # Use C pointer to image cache, if supported
261 if ($self->{params}->{version} ge '6.3.5')
262 {
263 my $q = $img->Get('quantum');
264
265 if ($q == 8)
266 {
267 $self->{params}->{gl_internalformat} = GL_RGBA8;
268 $self->{params}->{gl_type} = GL_UNSIGNED_BYTE;
269 $self->{params}->{size} = 1;
270 }
271 elsif ($q == 16)
272 {
273 $self->{params}->{gl_internalformat} = GL_RGBA16;
274 $self->{params}->{gl_type} = GL_UNSIGNED_SHORT;
275 $self->{params}->{size} = 2;
276 }
277 else
278 {
279 print "Unsupported pixel quantum\n";
280 }
281
282 if ($self->{params}->{gl_type})
283 {
284 $self->{params}->{gl_format} =
285 $self->{params}->{endian} ? GL_RGBA : GL_BGRA;
286
287 $self->{params}->{length} = $self->{params}->{size} * $elements;
288
289 $self->{oga} = OpenGL::Array->new_pointer($self->{params}->{gl_type},
290 $img->GetImagePixels(rows=>$h),$elements);
291
292 $self->{params}->{alpha} = -1;
293
294 return $self->{oga};
295 }
296 }
297
298 # Fall back to using standard PerlMagick interface
299 $self->{blobs} = 1;
300 $self->{params}->{gl_internalformat} = GL_RGBA8;
301 $self->{params}->{gl_type} = GL_UNSIGNED_BYTE;
302 $self->{params}->{size} = 1;
303 $self->{params}->{gl_format} = GL_RGBA;
304 $self->{params}->{alpha} = 1;
305
306 $self->{params}->{length} =
307 $self->{params}->{pixels} * $self->{params}->{components} *
308 $self->{params}->{size};
309
310 $img->Set(magick=>'RGBA',depth=>8);
311 $self->{oga} = OpenGL::Array->new_scalar($self->{params}->{gl_type},
312 $img->ImageToBlob(),$elements);
313
314 return $self->{oga};
315 }
316
317 # Sync from GPU framebuffer (OGA/blob) to IM
318 # Call before using native calls
319 sub Sync
320 {
321 my($self) = @_;
322
323 my $img = $self->{native};
324 if ($self->{blobs})
325 {
326 $img->BlobToImage($self->{oga}->retrieve_data());
327 }
328 else
329 {
330 $img->SyncImagePixels();
331 }
332
333 $img->Negate(channel=>'Alpha') if ($self->{params}->{alpha} < 0);
334 $img->Flip();
335 }
336
337 # Sync from IM to GPU framebuffer (OGA/blob)
338 # Call after using native calls
339 sub SyncOGA
340 {
341 my($self) = @_;
342
343 my $img = $self->{native};
344
345 $img->Flip();
346 $img->Negate(channel=>'Alpha') if ($self->{params}->{alpha} < 0);
347
348 my($w,$h) = $img->Get('width','height');
349 my $pixels = $w * $h;
350 my $elements = $pixels * $self->{params}->{components};
351
352 if ($self->{blobs})
353 {
354 $img->Set(magick=>'RGBA',depth=>8);
355
356 $self->{oga} = OpenGL::Array->new_scalar($self->{params}->{gl_type},
357 $img->ImageToBlob(),$elements);
358 }
359
360 if ($w == $self->{params}->{width} && $h == $self->{params}->{height})
361 {
362 return if ($self->{blobs});
363 $self->{oga}->update_pointer($img->GetImagePixels(rows=>$h));
364 }
365
366 $self->{params}->{width} = $w;
367 $self->{params}->{height} = $h;
368 $self->{params}->{pixels} = $pixels;
369 $self->{params}->{length} = $elements * $self->{params}->{size};
370
371 return if ($self->{blobs});
372
373 $self->{oga} = OpenGL::Array->new_pointer($self->{params}->{gl_type},
374 $img->GetImagePixels(rows=>$h),$elements);
375 }
376
377 # Get OpenGL::Array object
378 sub GetArray
379 {
380 my($self) = @_;
381 return $self->{oga};
382 }
383
384 # Get C pointer to image cache
385 sub Ptr
386 {
387 my($self) = @_;
388 return undef if (!$self->{oga});
389 return $self->{oga}->ptr();
390 }
391
392 # Save image
393 sub Save
394 {
395 my($self,$file,%user_params) = @_;
396 my $img = $self->{native};
397
398 $self->Sync();
399
400 my $blob;
401 if ($file)
402 {
403 if ($self->{blobs} && $img->[1])
404 {
405 $img->[1]->Write(filename=>$file,%user_params);
406 }
407 else
408 {
409 $img->[0]->Write(filename=>$file,%user_params);
410 }
411 }
412 else
413 {
414 %user_params = (magick=>'RGBA',depth=>8) if (!scalar(%user_params));
415 delete($user_params{filename});
416
417 my $clone = $img->Clone();
418 $clone->Set(%user_params);
419 ($blob) = $clone->ImageToBlob();
420 }
421
422 $self->SyncOGA();
423
424 return $blob;
425 }
426
427 # Get image blob
428 sub GetBlob
429 {
430 my($self,%params) = @_;
431 return $self->Save(undef,%params);
432 }
433
434 1;
435 __END__
436