Page 1 of 1

How do I fade between two Images

Posted: Tue May 16, 2017 7:31 am
by leodh
HI all,

I was trying to do a little compare image program and I wanted to fade between two images
But I just can not find a good example in the forum.

Does anyone have an example of how to fade between two images

Cheers
Leo

Re: How do I fade between two Images

Posted: Tue May 16, 2017 8:09 am
by RSBasic
A very simple example:

Code: Select all

EnableExplicit

Define FadeValue = 0

CreateImage(1, 200, 200, 24, RGB(255, 0, 0))
CreateImage(2, 200, 200, 24, RGB(255, 255, 0))

Procedure FadeImage(FadeValue)
  CopyImage(1, 3)
  If StartDrawing(ImageOutput(3))
    DrawAlphaImage(ImageID(2), 0, 0, FadeValue)
    
    StopDrawing()
  EndIf
  SetGadgetState(1, ImageID(3))
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ImageGadget(1, 10, 10, 0, 0, ImageID(1), 0)
  
  AddWindowTimer(0, 1, 10)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Timer
        Select EventTimer()
          Case 1
            If FadeValue < 255
              FadeValue + 1
              FadeImage(FadeValue)
            EndIf
        EndSelect
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf

Re: How do I fade between two Images

Posted: Tue May 16, 2017 5:32 pm
by ar-s
Yo,
It's a better way to use BindEvent stuff to not lock fading (or something else) during moving the window.

Code: Select all


Global FadeValue = 0
Declare TimerEvent()
Declare FadeImage(v)

Procedure TimerEvent()
  
  If FadeValue < 255
  FadeValue+1
  If StartDrawing(CanvasOutput(1))
    DrawAlphaImage(ImageID(1), 0, 0, 255)
    DrawAlphaImage(ImageID(2), 0, 0, FadeValue)
    StopDrawing()
  EndIf
  Debug FadeValue
EndIf

    
  
EndProcedure

CreateImage(1, 200, 200, 24, RGB(255, 0, 0)) ; RED
CreateImage(2, 200, 200, 24, RGB(0, 255, 0)) ; GREEN



If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  CanvasGadget(1, 10, 10, 200, 200)
 
  AddWindowTimer(0, 1, 10)
  
  
  BindEvent(#PB_Event_Timer, @TimerEvent(),0,1)

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf

Re: How do I fade between two Images

Posted: Tue May 16, 2017 6:10 pm
by CELTIC88
hello , here a example to compare two images with Gdiplus

Image

results of Compare
Image

Code: Select all

EnableExplicit

;////////////////////////////////////
;          Author Celtic88(c)
;///////////////////////////////////

Prototype GdiplusStartup(a,b,c)
Prototype GdipDisposeImage(a)
Prototype GdiplusShutdown(a)
Prototype GdipGetImageWidth(a,b)
Prototype GdipGetImageHeight(a,b)
Prototype GdipCreateBitmapFromFile(a.s,b)
Prototype GdipBitmapLockBits(a,b,c,d,e)
Prototype GdipBitmapUnlockBits(a,b)
Prototype GdipGetImageGraphicsContext(a,b)
Prototype GdipDeleteGraphics(a)
Prototype GdipSaveImageToFile(a,b.s,c,d)
Prototype GdipCreatePen1(a,b.f,c,d)
Prototype GdipDrawRectangle(a,b,c.f,d.f,e.f,f.f)
Prototype GdipSetPenDashStyle(a,b)
Prototype GdipDeletePen(a)

Global  lib_gdiplus = OpenLibrary(#PB_Any, "GDIPlus.dll") 

Global  GdiplusStartup_.GdiplusStartup=GetFunction(lib_gdiplus,"GdiplusStartup")
Global  GdiplusShutdown_.GdiplusShutdown=GetFunction(lib_gdiplus,"GdiplusShutdown")
Global  GdipDisposeImage_.GdipDisposeImage=GetFunction(lib_gdiplus,"GdipDisposeImage")
Global  GdipGetImageWidth_.GdipGetImageWidth=GetFunction(lib_gdiplus,"GdipGetImageWidth")
Global  GdipGetImageHeight_.GdipGetImageHeight=GetFunction(lib_gdiplus,"GdipGetImageHeight")
Global  GdipCreateBitmapFromFile_.GdipCreateBitmapFromFile=GetFunction(lib_gdiplus,"GdipCreateBitmapFromFile")
Global  GdipBitmapLockBits_.GdipBitmapLockBits=GetFunction(lib_gdiplus,"GdipBitmapLockBits")
Global  GdipBitmapUnlockBits_.GdipBitmapUnlockBits=GetFunction(lib_gdiplus,"GdipBitmapUnlockBits")
Global  GdipGetImageGraphicsContext_.GdipGetImageGraphicsContext=GetFunction(lib_gdiplus,"GdipGetImageGraphicsContext")
Global  GdipDeleteGraphics_.GdipDeleteGraphics=GetFunction(lib_gdiplus,"GdipDeleteGraphics")
Global  GdipSaveImageToFile_.GdipSaveImageToFile=GetFunction(lib_gdiplus,"GdipSaveImageToFile")
Global  GdipDrawRectangle_.GdipDrawRectangle=GetFunction(lib_gdiplus,"GdipDrawRectangle")
Global  GdipCreatePen1_.GdipCreatePen1=GetFunction(lib_gdiplus,"GdipCreatePen1")
Global  GdipSetPenDashStyle_.GdipSetPenDashStyle=GetFunction(lib_gdiplus,"GdipSetPenDashStyle")
Global  GdipDeletePen_.GdipDeletePen=GetFunction(lib_gdiplus,"GdipDeletePen")


Structure GdiplusStartupInput 
  GdiPlusVersion.l 
  *EventCallback._EVENT
  SuppressBackgroundThread.l 
  SuppressExternalCodecs.l 
EndStructure 

Structure GDIPBITMAPDATA 
  Width.l
  Height.l
  Stride.l
  Format.l
  *Scan0
  Reserved.i
EndStructure

#GDP_Ok=0
#GDIP_ILMREAD = $0001
#GDIP_ILMWRITE = $0002
#GDIP_PXF32RGB = $00022009 ; 32 bpp; 8 bits for each RGB. No alpha.

Procedure.b Gdiplus_Startup(*token)
  Protected GdiplusStartupInput.GdiplusStartupInput\GdiPlusVersion=1
  ProcedureReturn Bool(GdiplusStartup_(@*token, @GdiplusStartupInput,0)=0)
EndProcedure
Procedure Gdiplus_GetImageWidth(hImage)
  Protected xw:GdipGetImageWidth_(hImage,@xw):ProcedureReturn xw
EndProcedure
Procedure Gdiplus_GetImageHeight(hImage)
  Protected xy:GdipGetImageHeight_(hImage,@xy):ProcedureReturn xy
EndProcedure
Procedure Gdiplus_CreateBitmapFromFile(Ipath.s)
  Protected hbitmap:GdipCreateBitmapFromFile_(Ipath,@hbitmap):ProcedureReturn hbitmap
EndProcedure
Procedure Gdiplus_BitmapLockBits(hBitmap, iLeft, iTop, iWidth, iHeight, iFlags , iFormat,*GDIPBITMAPDATA.GDIPBITMAPDATA)
  Protected irec.rect
  irec\left=iLeft
  irec\top=iTop
  irec\right=iWidth
  irec\bottom=iHeight
  ProcedureReturn Bool(GdipBitmapLockBits_(hBitmap,@irec,iFlags , iFormat,*GDIPBITMAPDATA) = #GDP_Ok)
EndProcedure

Define token
Gdiplus_Startup(@token)

Define hbitmap1=Gdiplus_CreateBitmapFromFile("img0.jpg")
Define hbitmap2=Gdiplus_CreateBitmapFromFile("img.jpg")

Define iw=Gdiplus_GetImageWidth(hbitmap1)
Define ih=Gdiplus_GetImageHeight(hbitmap1)

Define  iX, iY,iRowOffset
NewList dpoint.Point()
Define GDIPBITMAPDATA1.GDIPBITMAPDATA,GDIPBITMAPDATA2.GDIPBITMAPDATA

Define timer=ElapsedMilliseconds()

Gdiplus_BitmapLockBits(hbitmap1, 0, 0, iw, ih, #GDIP_ILMREAD, #GDIP_PXF32RGB,@GDIPBITMAPDATA1)
Gdiplus_BitmapLockBits(hbitmap2, 0, 0, iw, ih, #GDIP_ILMREAD, #GDIP_PXF32RGB,@GDIPBITMAPDATA2)
For iY = 0 To ih-1
  iRowOffset=iY*iw*4
  For iX = 0 To iw-1
    If  PeekL(GDIPBITMAPDATA1\Scan0+iRowOffset+iX*4) <> PeekL(GDIPBITMAPDATA2\Scan0+iRowOffset+iX*4)
      AddElement(dpoint())
      dpoint()\x=iX
      dpoint()\y=iY
    EndIf
  Next
Next

MessageRequester(Str(ElapsedMilliseconds()-timer) +" ms",Str((ListSize(dpoint())*100)/(iw* ih)) +" % différences.")

Define a,b,c,d
SortStructuredList(dpoint(),#PB_Sort_Ascending,OffsetOf(Point\x),#PB_Integer)
FirstElement(dpoint())
a=dpoint()\x
LastElement(dpoint())
b=dpoint()\x
SortStructuredList(dpoint(),#PB_Sort_Ascending,OffsetOf(Point\y),#PB_Integer)
FirstElement(dpoint())
c=dpoint()\y
LastElement(dpoint())
d=dpoint()\y

GdipBitmapUnlockBits_(hBitmap1,@GDIPBITMAPDATA1)
GdipBitmapUnlockBits_(hBitmap2,@GDIPBITMAPDATA2)
Define hbgcx,hPen
GdipGetImageGraphicsContext_(hBitmap2,@hbgcx)
GdipCreatePen1_($FFFFFF00,4,2,@hPen)
GdipSetPenDashStyle_(hPen,2)
GdipDrawRectangle_(hbgcx,hPen,a,c,b-a,d-c)
GdipSaveImageToFile_(hBitmap2,"hhhhh.png",?Png_Clsid,0)
GdipDeletePen_(hPen)
GdipDeleteGraphics_(hbgcx)
GdipDisposeImage_(hBitmap1)
GdipDisposeImage_(hBitmap2)

GdiplusShutdown_(token)

DataSection
  Png_Clsid: ; clsid for png image format
  Data.l $557CF406
  Data.w $1A04
  Data.w $11D3
  Data.b $9A,$73,$00,$00,$F8,$1E,$F3,$2E
EndDataSection

Re: How do I fade between two Images

Posted: Wed May 17, 2017 7:42 am
by ramme