Murga-Projects Forums

Full Version: slicing images
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

How do you create a buffer ?

I know less about that than you. I tried modifying the offScreen example to use an image rather than shape, but got nowhere. It looks like it would require the use of features found in a few of John's latest examples...I just can't make any sense of it.

fl_read_image() was broken on Intel-based Macs (STR #1490)
Fixed Quartz fl_read_image
Its been a while since a compiled murgaLua looks like I get my chance.

You may not need to do that. The changelog includes everything that was changed since 1.1.7, but murgaLua has been including the newer 1.1.x releases (the stuff that's released as weekly snapshots).

mikshaw Wrote:

How do you create a buffer ?

I know less about that than you. I tried modifying the offScreen example to use an image rather than shape, but got nowhere. It looks like it would require the use of features found in a few of John's latest examples...I just can't make any sense of it.

Of course you know it! you used it in your sliding puzzle. (remember what data() returns)

> image=fltk.Fl_PNG_Image("murgaLua.png")
> image_data=image:data()
> =type(image_data)
> for field,value in pairs(image_data) do print(field,type(value)) end
1       string
d       number
h       number
w       number
ld      number
> =image_data.d
> =image_data.h
> =image_data.w
> =image_data.ld

Here is a small example that should make it clear:

w=fltk.Fl_Double_Window(200,200,"Image buffer Test")

box2=fltk.Fl_Box(25,175,150,25,"That sure wasn't hard!")


Of course you know it! you used it in your sliding puzzle. (remember what data() returns)

Not really. That was a direct copy of one of John's examples. I thought I understood *what* it was doing (I've looked into it as you did here in the first example), but not *how* it was doing it. I thought this was not the same as creating a buffer in a format that could be used by fl_read_image, particularly considering that was one of the things I tried and failed.
One of the things I found frustrating was that some RGB images seem to have no useable value for image_data[1].

So you're saying an image buffer is nothing more than a number representing the desired length of a string?
That sounds much too simple to be true =o)

In any case, I think what I'm having the most trouble understanding is putting an actual image file (possibly an indexed image) into that buffer so it can be read back as RGB or RGBA. The drawing docs don't seem to say much about using existing image data, although it must be possible or else it would be nearly pointless to create an empty RGB image. Maybe I'm making it more difficult than it is...I do have a tendency to overlook the obvious....

Well, I'm finally making some progress!
Although I still don't understand using offscreen buffers for storing image data (as in data from loaded image files), I realized that I don't need it for this particular project. Since the image preview is exactly the image that needs to be converted to RGB for slicing, I used that area to grab the data with fl_read_image()

I didn't understand before that it was simply a matter of specifying an onscreen area from which to grab the image, but after poking around with various parts of Johns examples, here's something that will tile any image file that murgaLua can read.

I still don't have a solution to the line accross the bottom, but that's minor for now. Maybe a series of fl_read_image and Fl_RGB_Image could create the tiles instead of using getTiles(), but I think that might slow things down. There is already a little pause from doing it just once.


-- slide puzzle for MurgaLua 0.6.4+
-- 2008 mikshaw

-- window and image size are determined by these variables
ts=80 -- tile size
tc=4  -- number of tiles in one line
fr=5 -- frame size

if not Fl_Image.getTiles then
err="This program requires murgaLua 0.6.4 or newer.\n\n"..
if fltk then fltk.fl_alert(err) else print(err) end

function load_callback(object)
-- a stew of pieces from John Murga's functions
fileName = fltk.fl_file_chooser("Choose an RGB image", "Image Files (*.{jpg,png,bmp,xbm,xpm,gif})", img_dir, nil)
if fileName then
  if image1 then image1:uncache() end -- don't know if this is needed
  image1 = Fl_Shared_Image.get(fileName,ts*tc,ts*tc)
  start=1 -- used for controlling window callback and scramble behavior
  preview:image(image1); preview:redraw(); preview:show() -- load the preview
  Fl:check() -- seems to keep fl_read_image from grabbing the file chooser
  imageString=fltk.fl_read_image(fr,fr,ts*tc,ts*tc) -- grab the preview image
  image2 = fltk:Fl_RGB_Image(imageString,ts*tc,ts*tc,3) -- create RGB image for tiling
  myImages = image2:getTiles(ts,ts)
  for key,value in ipairs(myImages) do
    -- make sure all tile locations are reset correctly
    -- fixes a bug if image is changed while tiles are scrambled
  scrambutt:label("load an image")

function move_tile(t)
if Fl:event_button() >1 then preview:show() elseif image2 then
  local my_x,my_y,movex,movey=t:x(),t:y()
  -- tile must be adjacent to the missing piece
  if (my_x == tile[hidden]:x() and math.abs(my_y-tile[hidden]:y()) == ts)
  or (my_y == tile[hidden]:y() and math.abs(my_x-tile[hidden]:x()) == ts)
    -- swap selected tile with the hidden one
  -- check to see if puzzle is solved
  -- "ok" is incremented each time a tile is found in its proper place
  if start==0 then -- don't check puzzle during scramble
  local ok=0
  for i=0,tc^2-1 do
    if tile[i]:x()==pos[i].col and tile[i]:y()==pos[i].row then
    if ok==tc^2 then -- if ok == total number of tiles

function scramble()
-- this picks a random tile to attempt to move and
-- repeats the process many times. Simply placing
-- tiles in random locations could potentially make
-- the puzzle impossible to solve
if hidden then -- reset hidden tile
-- turn a random tile into the missing piece
local scram=0
while scram < 10000 do

plize={"Great Job!", "Hooray for You!", "You don't suck!", "WIN!", "GODLIKE!", "CONGRATULATE YOU!"}

Fl:visible_focus(0) -- get rid of the ants
Fl:set_boxtype(fltk.FL_UP_BOX,fltk.FL_THIN_UP_BOX) -- looks a little more like tiles
w=fltk:Fl_Double_Window(ts*tc+fr*2,ts*(tc+0.5)+fr*3,"slide puzzle")

scrambutt=fltk:Fl_Button(fr,ts*tc+fr*2,ts*tc,ts/2,"load an image")
Click this button at any time to load a new image.

** Puzzle Controls **
Left click:  Move a tile.
Right or Middle click:  Toggle image display.
Esc:  Quit program, or close image display if it is visible.

-- array of tiles, top left to right, then move down
tile={}; pos={}
-- allow space for the frame
row=fr; col=fr
-- subtract one is used because the number of tiles starts at one
-- but the table starts at zero (easier to position them from zero)
for i=0,tc^2-1 do
  -- next piece is ts pixels to the right
  -- start the next row
  if col == ts*tc+fr then col=fr;row=row+ts end

-- image preview (cheat)
if start==1 then load_callback()
else preview:hide() end

  -- Esc just hides the preview if it's visible
  if preview:visible()==1 and start==0 then preview:hide()
  else os.exit() end


Pages: 1 2 3 4 5
Reference URL's