Ruby/Tk how to display a raw image residing in RAM

I have an image stored as an array of RGB tuples.
8 bits for each of red, green, and blue.

I would like to use Tk to display it.
I am completely clueless.

I have looked at the Tk documentation and found the following functions:
Tk_PhotoBlank
Tk_PhotoExpand
Tk_PhotoGetImage
Tk_PhotoGetSize
Tk_PhotoPutBlock
Tk_PhotoPutZoomedBlock
Tk_PhotoPhotoSetSize

The Tk documentation is limited with no examples, so I don’t know how
to do it in Tcl/Tk.

I can’t figure out the Ruby bindings either.
It appears that there is simply
TkPhotoImage

I found a nice example using TkPhotoImage to load from file:
image = TkPhotoImage.new() { file “javaguy.gif” }
label = TkLabel.new(root) { image image; height 50 }.pack(“anchor”=>“e”)

I looked at the Ruby/Tk bindings source code for a while.
I couldn’t figure out how the file() function was mapped, much less the
other functions.

Any help here would be appreciated.

Hi,

···

From: jcb@iteris.com (MetalOne)
Subject: Ruby/Tk how to display a raw image residing in RAM.
Date: Tue, 13 May 2003 08:48:13 +0900
Message-ID: 92c59a2c.0305121545.78a6fe76@posting.google.com

I have an image stored as an array of RGB tuples.
8 bits for each of red, green, and blue.
I would like to use Tk to display it.

Does this sample script help you?

#!/usr/bin/env ruby
require ‘tk’

dat = [
[‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’],
[‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’],
[‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’],
[‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’],
[‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’,‘#000000’],
[‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’],
[‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’],
[‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’],
[‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’],
[‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’,‘#ff0000’],
[‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’],
[‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’],
[‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’],
[‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’],
[‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’,‘#00ff00’],
[‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’],
[‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’],
[‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’],
[‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’],
[‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’,‘#0000ff’],
[‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’],
[‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’],
[‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’],
[‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’],
[‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’,‘#ffffff’],
]

photo = TkPhotoImage.new
photo.put(dat)
photo.put(dat, 20, 0, 50, 50)

TkButton.new(nil, ‘image’=>photo, ‘command’=>proc{exit}).pack
Tk.mainloop


Hidetoshi Nagai (nagai@ai.kyutech.ac.jp)