How do I fade between two Images

Just starting out? Need help? Post your questions and find answers here.
leodh
Enthusiast
Enthusiast
Posts: 164
Joined: Sun Nov 06, 2005 6:07 am
Location: Perth Western Australia

How do I fade between two Images

Post 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
Regards
Leo
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: How do I fade between two Images

Post 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
Image
Image
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: How do I fade between two Images

Post 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
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: How do I fade between two Images

Post 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
interested in Cybersecurity..
ramme
User
User
Posts: 72
Joined: Fri Mar 31, 2017 8:52 am
Location: Fortuna California

Re: How do I fade between two Images

Post by ramme »

Post Reply