Utilities | Arrays | Imaging | Shaders | POGL | GLUT | OpenGL | GLU |

OpenGL::Image (OGI) objects provide optimized interfaces to imaging modules, such as PerlMagick.

OGI's use OpenGL::Array (OGA) objects as wrappers around image buffers. These buffers are are stored as typed C arrays, and allows sharing image data between imaging libraries and the GPU.

This facilitates loading/modifying/saving textures, FBOs and VBOs - and allows both OpenGL and the imaging libraries to access/modify the same buffers.

The result is performance comparable to C and significantly faster than other OpenGL language bindings.

Google Bookmarks Google
  
Stumble Upon It! Stumbled on It!
  
del.icio.us del.icio.us
  


Enumerating Installed/Supported Imaging Engines

my $engines = OpenGL::Image::GetEngines();

    Returns a hashref of available imaging engines. The hash keys are engine names; hash values are their version.

my $ok = OpenGL::Image::HasEngine('Magick','6.3.5');

    Tests whether a particular engine (and optionally minimum version) is available.



Loading Textures

my $tex = new OpenGL::Image(engine=>'Magick',source=>$image_file);

    Create an OGI and load it with an image (engine defaults to the 'Targa' driver if not specified).

my($ifmt,$fmt,$type) = $tex->Get('gl_internalformat','gl_format','gl_type');
my($w,$h) = $tex->Get('width','height');

    Retrieve image info.

if (!$tex->IsPowerOf2()) return;

    Determine if image dimensions are powers of 2.

my $size = $tex->GetPowerOf2();

    Return largest power of 2 dimension that fits within image's boundary.

glTexImage2D_c(GL_TEXTURE_2D, 0, $ifmt, $w, $h, 0, $fmt, $type, $tex->Ptr());

    $tex->Ptr() returns a C pointer to the image buffer.



Capturing, Modifying and Drawing a Frame

my $frame = new OpenGL::Image(engine=>'Magick', width=>$w, height=>$h);
my($fmt,$size) = $frame->Get('gl_format','gl_type');

    Instantiate an OGI; get GL info.

glReadPixels_c(0, 0, $w, $h, $fmt, $size, $frame->Ptr());
$frame->Sync();

    Read pixels from GPU to image buffer, then sync the buffer. Unless the image needs to be paged into cache, Sync is generally a NOP.

$frame->Native->Blur(radius=>2,sigma=>2);

    Get the imaging lib's native handle to process the image.

glRasterPos2f(0,0);
glDrawPixels_c($w, $h, $fmt, $size, $frame->Ptr());

    Write the pixels back to the GPU.



Saving a Frame

my $frame = new OpenGL::Image(width=>$w, height=>$h);
my($fmt,$size) = $frame->Get('gl_format','gl_type');
glReadPixels_c(0, 0, $w, $h, $fmt, $size, $frame->Ptr());
$frame->Save('MyImage.tga');



Getting and Setting Pixels

my($r,$g,$b,$a) = $img->GetPixel($x,$y);

$img->SetPixel($x,$y, 1.0, 0.5, 0.0, 1.0);

$frame->Sync();

    This interface assumes RGBA and that all values are normalized to 1.0; Sync is generally a NOP.



Get an OGI's OGA

my $oga = $img->GetArray();

    Allows OGA operations on an image buffer.



Get Parameter Values

  • alpha - 1 if has alpha channel, -1 if has inverted alpha channel; 0 if none
  • components - number of pixel components
  • endian - 1 if big endian; otherwise 0
  • flipped - 1 bit set if cache scanlines are top to bottom; others reserved
  • size - bytes per component
  • source - source image, if defined
  • version version of the engine

  • width - width of image in pixels
  • height - height of image in pixels
  • pixels - number of pixels
  • length - cache size in bytes

  • gl_internalformat - internal GL pixel format. eg: GL_RGBA8, GL_RGBA16
  • gl_format - GL pixel format. eg: GL_RGBA, GL_BGRA
  • gl_type - GL data type. eg: GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT