Just-in-time compiler

From MMVLWiki
Jump to: navigation, search

The Ruby-extension version 0.33 of HornetsEye makes use of libjit. This means that you can even define and compile methods during runtime. Here is a 46 MByte video demonstrating this capability. I recommend to watch the OGG video instead of the Shockwave video. It is of much higher quality.

Demonstration of an interactive Ruby-session using libjit and HornetsEye. You can also view the OGG-video of the JIT demo which is of higher quality. The video was created using recordMyDesktop. The Hong Kong skyline was used as a background picture
# Load colour image.
img = MultiArray.load_rgb24( "/home/engjw/Documents/pictures/miraculix.jpg" )
# Display the image.
img.show
# Convert the image to grey-level floating point.
gimg = img.to_sfloat
# Display grey-level image.
gimg.show
# Downsample the image with a sampling-rate of 2 in each direction and a sampling-offset of 1.
limg = gimg.downsample( [ 2, 2 ], [ 1, 1 ] )
# Display low-resolution image.
limg.show
# Apply element-wise function to each pixel of the image.
limg.collect { |x| x * x }
# Display processed image.
( limg.collect { |x| x * x } ).show
# Display range of values.
( limg.collect { |x| x * x } ).range
# Display range after normalisation.
( limg.collect { |x| x * x } ).normalise.range
# Display normalised result.
( limg.collect { |x| x * x } ).normalise.show
# Show data as one-dimensional array.
limg.data
# Define an element-wise function for one-dimensional arrays.
Sequence.define_unary_op( :sqr ) { |x| x * x }
# Reuse this function for multidimensional arrays.
MultiArray.define_unary_op( :sqr )
# Call the function.
limg.sqr
# Measure time of operation.
t = Time.new.to_f; limg.sqr; Time.new.to_f - t
# Example function using libJIT.
fun = JITFunction.compile( JITType::LINT, JITType::LINT ) do |f|
   ( JITTerm.param( f, 0 ) * JITTerm.const( f, JITType::LINT, 2 ) ).return_me
end
# Calling the function.
fun.call( 2 )
# Define element-wise operation using JIT.
Sequence.define_unary_jit_op( :sqr ) { |x| x * x }
# Measure time of operation now.
t = Time.new.to_f; limg.sqr; Time.new.to_f - t
# Apply function and display result.
gimg.sqr.normalise.show
# Apply function twice and display result.
gimg.sqr.sqr.normalise.show
# Define a small filter.
f = MultiArray.to_multiarray( [ [ -1, 0, 1 ], [ -2, 0, 2 ], [ -1, 0, 1 ] ] )
# Correlate with this filter (slow).
limg.correlate( f )
# Correlate with filter using native datatypes to facilitate the JIT-based implementation.
limg.correlate( f.to_sint )
# Sobel-X operator.
gimg.sobel_x
# Display Sobel-X and Sobel-Y image.
gimg.sobel_x.normalise.show
gimg.sobel_y.normalise.show
# Define an edge detector.
class MultiArray
  def edges
    Math.sqrt( sobel_x.sqr + sobel_y.sqr )
  end
end
# Compute edges.
gimg.edges
# Display edges.
gimg.edges.normalise.show
# Display edges black-on-white.
( 255 - gimg.edges.normalise ).show
# Same but more efficient.
gimg.edges.normalise( 255..0 ).show

[edit] See Also

[edit] External Links

Bookmark and Share

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox