Page 1 of 1

ImageFilter lib

Posted: Mon Aug 24, 2009 8:11 pm
by wilbert
A small ImageFilter lib (windows only) using floating point color values (a,r,g,b range between 0 and 1).
http://www.w73.nl/pb/ImageFilter.zip

Two structures

Code: Select all

Structure ImageFilterPixel
  b.f
  g.f
  r.f
  a.f
EndStructure

Structure ImageFilterCallback
  x.l
  y.l
  width.l
  height.l
  in.ImageFilterPixel
  out.ImageFilterPixel
  value.l
EndStructure
Three functions

Code: Select all

*ifc.ImageFilterCallback = ImageFilter_SetCallback (@Callback())

Result.l = ImageFilter_Apply (ImageID, [value.l])

*px.ImageFilterPixel = ImageFilter_Pixel (x.l, y.l)
The last one to get a pixel can only be called from within a callback function.

Code example:

Code: Select all

Structure ImageFilterPixel
  b.f
  g.f
  r.f
  a.f
EndStructure

Structure ImageFilterCallback
  x.l
  y.l
  width.l
  height.l
  in.ImageFilterPixel
  out.ImageFilterPixel
  value.l
EndStructure

Procedure CB_Create(*ifc.ImageFilterCallback)

  dx.f = 1 - 2 * *ifc\x / *ifc\width
  dy.f = 1 - 2 * *ifc\y / *ifc\height
  f.f = 1 - Sqr(dx * dx + dy * dy)
  *ifc\out\r = dx.f
  *ifc\out\g = dy.f
  *ifc\out\b = f
  
EndProcedure

Procedure CB_Scanlines(*ifc.ImageFilterCallback)

  If *ifc\y % 2
    *ifc\out\r = *ifc\in\r * 0.1
    *ifc\out\g = *ifc\in\g * 0.1
    *ifc\out\b = *ifc\in\b * 0.1
  EndIf
    
EndProcedure

Procedure CB_Vignet(*ifc.ImageFilterCallback)

  dx.f = 1 - 2 * *ifc\x / *ifc\width
  dy.f = 1 - 2 * *ifc\y / *ifc\height
  *ifc\out\a = 1.3 - Sqr(dx * dx + dy * dy)
  
EndProcedure

Procedure CB_Pixelate(*ifc.ImageFilterCallback)

  *px.ImageFilterPixel = ImageFilter_Pixel(Int(*ifc\x / 16) * 16, Int(*ifc\y / 16) * 16)
  *ifc\out\a = *px\a
  *ifc\out\r = *px\r
  *ifc\out\g = *px\g
  *ifc\out\b = *px\b
  
EndProcedure

If OpenWindow(0, 100, 100, 400, 300, "PureBasic - ImageFilter")

  CreateImage(0, 400, 300, 32)
  
  ImageFilter_SetCallback(@CB_Create())
  ImageFilter_Apply(ImageID(0))

  ImageFilter_SetCallback(@CB_Pixelate())
  ImageFilter_Apply(ImageID(0))

  ImageFilter_SetCallback(@CB_Scanlines())
  ImageFilter_Apply(ImageID(0))

  ImageFilter_SetCallback(@CB_Vignet())
  ImageFilter_Apply(ImageID(0))
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Repaint
        StartDrawing(WindowOutput(0))
          DrawAlphaImage(ImageID(0), 0, 0)
      StopDrawing()
    Case #PB_Event_CloseWindow
      Break
    EndSelect
  ForEver  
  
EndIf

Re: ImageFilter lib

Posted: Mon Aug 24, 2009 9:25 pm
by UserOfPure
Doesn't compile:

Code: Select all

Line 49: ImageFilter_Pixel() is not a function, array, macro or linked list.

Posted: Tue Aug 25, 2009 4:34 am
by wilbert
The ImageFilter file (without filename extension) in the zip archive has to be placed in the UserLibraries directory (probably C:\Program Files\PureBasic\PureLibraries\UserLibraries ).