Version 4 of megaimage tutorial

Updated 2008-01-02 08:08:30 by SupremeXeNcom_KommandEr

George Peter Staplin Oct 5, 2007 - Here is a simple tutorial for those of you using megaimage and related extensions from megapkg.

Common setup code

 set dir [file dirname [info script]]
 set ext [info sharedlibextension]
 load [file join $dir megaimage$ext]
 load [file join $dir megaimagetk$ext]
 load [file join $dir jpegext2$ext] Jpegext2 ;#caveat
 load [file join $dir pngext2$ext] Pngext2

Decoding a JPEG, adding a blue rectangle, and then encoding a JPEG

 set fd [open "some.jpg" r]
 fconfigure $fd -translation binary
 set buf [jpegext2:decode [read $fd]]
 close $fd

 set obj [megaimage $buf]
 set x 20
 set y 30
 set width 200
 set height 200
 set color [list 0 0 255 255]
 $obj rectangle $x $y $width $height $color

 set fd [open "output.jpg" w]
 fconfigure $fd -translation binary
 puts -nonewline $fd [jpegext2:encode [$obj getdata]]
 close $fd

jpegext2:encode takes an optional argument after the megaimage data for the quality. Quality should be an integer in the range of 1-100.


Blending Objects Over a Base

 set obj [megaimage-blank 800 600]
 $obj setall [list 127 127 127 255]
 set overlay [megaimage-blank 200 200]
 $overlay setall [list 100 100 200 200]; #RGBA

 set x 0
 set y 0
 $obj blendobj $x $y $overlay
 set x 180
 set y 180
 $obj blendobj $x $y $overlay

pngext2:decode and pngext2:encode are similar to jpegext2, but have some important differences:

% pngext2:decode wrong # args: should be "pngext2:decode file|data filename|data"

% pngext2:encode wrong # args: should be "pngext2:encode data ?-alpha boolean?"

-alpha 1 is the default. -alpha is mostly of interest if you want to make the image a little smaller by eliminating the alpha channel.

Note: if pngext2:decode file $filename is causing segfaults it's probably due to a problem with Tcl_StatBuf size differing. In your build.conf you'll probably want to add the necessary -D to make tcl.h use the right struct stat when building pngext2. For example with unix-like systems: -D_LARGEFILE64_SOURCE=1 -DTCL_WIDE_INT_TYPE=long\\\ long


Using Megaimagetk

 # Making a megaimage object from a photo image.
 set obj [megaimage [megaimage.from.photo $photo_handle]]

 # Make a photo instance have the same data as a megaimage object.
 set p [image create photo]
 megaimage.to.photo [$obj getdata] $p

megaimagetk has more features, but those can get you started.


Cleaning up Megaimage Objects

 set obj [megaimage-blank 800 600]
 rename $obj {}

 set obj [megaimage $someBufferSource]
 rename $obj {}

Q. What is the data returned by jpegext2:decode?

A. It's a ByteArray result with a header at the start, followed by RGBA data.

Q. What is the data returned by jpegext2:encode?

A. It's a ByteArray result with the JPEG header and data, that can be written to the disk and opened in other programs that understand the JPEG format.


Category Graphics