arrayfire / arrayfire-haskell

Haskell bindings to ArrayFire
http://hackage.haskell.org/package/arrayfire
BSD 3-Clause "New" or "Revised" License
59 stars 5 forks source link

How to use drawImage? #13

Closed noughtmare closed 4 years ago

noughtmare commented 4 years ago

Your documentation contains an example:

>>> drawImage window ('constant' \@'Int' 1) ('Cell' 10 10 "test" 'ColorMapSpectrum')

I adapted it to:

main = do
  w <- createWindow 512 512 "Test"
  drawImage w (constant @Int [512,512] 1) (Cell 10 10 "test" ColorMapSpectrum)

That gives the error:

AFException {afExceptionType = TypeError, afExceptionCode = 204, afExceptionMsg = "Function does not support this data type"}
dmjio commented 4 years ago

@noughtmare I'd try using the loadImage function, if that still gives you problems I can look into it tonight. The example I have might be outdated.

noughtmare commented 4 years ago

@dmjio loadImage loads an image from disk, I want to generate an image programmatically (and then show it in a window).

dmjio commented 4 years ago

@noughtmare I think the type needs to be CBool (b8). Can confirm soon.

noughtmare commented 4 years ago

@dmjio If I use CBool then I get the error:

free(): invalid size
Aborted
dmjio commented 4 years ago

@noughtmare there's been some fixes (related to double free'ing of pointers) that are now in HEAD, am working on the Graphics module now with success. drawSurface in particular.

As I inspect more with what the C++ is doing before it sends it to the C, we'll have better working visualization code, and examples !

main :: IO ()                                                                                                                                                                                                   
main = do                                                                                                                                                                                                       
  window <- createWindow 800 600 "hey"                                                                                                                                                                          
  let x = iota [60,1] [1,60] / 29                                                                                                                                                                               
      y = iota [1,60] [60,1] / 29                                                                                                                                                                               
  go window (0 :: Array Float) x y                                                                                                                                                                              
    where                                                                                                                                                                                                       
      go window t x y = do                                                                                                                                                                                      
        let z = 10*x*(-abs(y)) * cos(x*x*(y+t))+sin(y*(x+t))-1.5                                                                                                                                                
        drawSurface window x y z (Cell (-1) (-1) "there" ColorMapDefault)                                                                                                                                       
        closed <- isWindowClosed window                                                                                                                                                                         
        unless closed $ go window (t + 0.07) x y  

image

dmjio commented 4 years ago

@noughtmare, for drawImage, try something like this:

main :: IO ()                                                                                                                                                                                                   
main = render =<< createWindow 800 600 "hey"                                                                                                                                                                          
    where                                                                                                                                                                                                       
      render window = do                                                                                                                                                                                            
        let x = iota [1,60] [60,1] / (30 :: Array Float)                                                                                                                                                        
            cell = Cell (-1) (-1) "" ColorMapDefault                                                                                                                                                        
        drawImage window x cell                                                                                                                                                                                 
        closed <- isWindowClosed window                                                                                                                                                                         
        unless closed (render window)

image

I'll update the documentation to reflect this.