News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Pages (5): « First < Previous 1 2 3 4 [5] Last »
slicing images
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #41
RE: slicing images

Quote:
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.

Quote:
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).

This post was last modified: 02-29-2008 11:37 PM by mikshaw.

02-29-2008 11:36 PM
Find all posts by this user Quote this message in a reply
Juergen
Member
***


Posts: 81
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #42
RE: slicing images

mikshaw Wrote:

Quote:
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)

Code:
> image=fltk.Fl_PNG_Image("murgaLua.png")
> image_data=image:data()
> =type(image_data)
table
> 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
3
> =image_data.h
155
> =image_data.w
167
> =image_data.ld
0


Here is a small example that should make it clear:

Code:
w=fltk.Fl_Double_Window(200,200,"Image buffer Test")
red=string.char(255,0,0)
green=string.char(0,255,0)
blue=string.char(0,0,255)

buffer=string.rep(string.rep(red,50)..string.rep(green,50)..string.rep(blue,50),
150)
image=fltk.Fl_RGB_Image(buffer,150,150,3,0)
box=fltk.Fl_Box(25,25,150,150)
box:image(image)
box2=fltk.Fl_Box(25,175,150,25,"That sure wasn't hard!")
w:show()
Fl:run()


Juergen

03-01-2008 04:27 AM
Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #43
RE: slicing images

Quote:
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....

03-01-2008 06:57 AM
Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #44
RE: slicing images

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.

Code:
#!/usr/bin/murgaLua

-- 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
--img_dir="/mnt/hda4/shared/image/slide_puzzle"
--img_dir="/home/mik/image/slide_puzzle"

if not Fl_Image.getTiles then
err="This program requires murgaLua 0.6.4 or newer.\n\n"..
"http://www.murga-projects.com/murgaLua/index.html"
if fltk then fltk.fl_alert(err) else print(err) end
os.exit(1)
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
    tile[key-1]:image(value)
    -- make sure all tile locations are reset correctly
    -- fixes a bug if image is changed while tiles are scrambled
    tile[key-1]:position(pos[key-1].col,pos[key-1].row)
  end
  scramble()
  scrambutt:label("load an image")
end
end

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)
  then
    -- swap selected tile with the hidden one
    movex=tile[hidden]:x()
    movey=tile[hidden]:y()
    tile[hidden]:position(my_x,my_y)
    t:position(movex,movey)
    w:redraw()
  end
  -- 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
      ok=ok+1
    if ok==tc^2 then -- if ok == total number of tiles
      fltk.fl_beep()
      scrambutt:label(plize[math.random(1,table.getn(plize))])
      preview:show()
      start=1
      break
    end
    end
  end
  end
end
end

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
preview:hide()
if hidden then -- reset hidden tile
  tile[hidden]:box(fltk.FL_UP_BOX)
  tile[hidden]:labeltype(fltk.FL_NORMAL_LABEL)
end
-- turn a random tile into the missing piece
hidden=math.random(0,tc^2-1)
tile[hidden]:labeltype(fltk.FL_NO_LABEL)
tile[hidden]:box(fltk.FL_DOWN_BOX)
local scram=0
while scram < 10000 do
  move_tile(tile[math.random(0,tc^2-1)])
  scram=scram+1
end
start=0
w:redraw()
end

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

fltk.fl_register_images()
math.randomseed(os.time())
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
Fl:set_boxtype(fltk.FL_DOWN_BOX,fltk.FL_THIN_DOWN_BOX)
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")
scrambutt:callback(load_callback)
scrambutt:tooltip([[
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
  tile[i]=fltk:Fl_Button(col,row,ts,ts)
  tile[i]:align(80)
  tile[i]:callback(move_tile)
  pos[i]={col=col,row=row}
  -- next piece is ts pixels to the right
  col=col+ts
  -- start the next row
  if col == ts*tc+fr then col=fr;row=row+ts end
end

-- image preview (cheat)
preview=fltk:Fl_Button(fr,fr,ts*tc,ts*tc)
preview:callback(function()
if start==1 then load_callback()
else preview:hide() end
end)

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

start=1
w:show()
Fl:run()

03-01-2008 09:20 AM
Find all posts by this user Quote this message in a reply
Pages (5): « First < Previous 1 2 3 4 [5] Last »
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: