[Done] Fix ResizeImage as parameter 0 cause problems

Post bugreports for the Windows version here
User avatar
Saki
Enthusiast
Enthusiast
Posts: 682
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Make ResizeImage bullet-proof, as Parameter 0 cause prob

Post by Saki »

Hi, AMpos,
yeah, sure you can do that.

It's also very easy to explain.
The value zero was simply not treated as a possible cause of a bug.
Probably zero was not intercepted because there was no visible misconduct.

The easiest thing to do is not to execute the function when zero is passed.
It will return zero anyway, then everything is OK.
It seems like nothing happens when you enter zero.

Unfortunately, zero results in a very nasty hidden bug.

Bugs are detected and should be fixed, it's simple, also the fix self is very simple.

Especially in a Basic dialect everything should be as bulletproof as possible, many people use one because of the ease of learning and handling.
If everyone is talking about the 101 of programming, then you should also know that it is essential to make any function that is made available to others as safe as possible.
Checking the parameter passing is a basic exercise

@oreopa
It's OK, the answers are a bit more robust in case of verbal attacks.
But you should already have a basic knowledge if you're going to post something like that.
Last edited by Saki on Sat Aug 01, 2020 7:15 pm, edited 1 time in total.
地球上の平和
User avatar
mk-soft
Addict
Addict
Posts: 2977
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Make ResizeImage bullet-proof, as parameter 0 cause prob

Post by mk-soft »

I like this extended functions :wink:

Code: Select all

;-TOP

; Only Images without #PB_Any

Global NewMap OrginalImages()

Procedure ResizeImageEx(Image, Width, Height, Mode = 0)
  If Width <= 0
    Width = 1
  EndIf
  If Height <= 0
    Height = 1
  EndIf
  If FindMapElement(OrginalImages(), Str(Image))
    CopyImage(OrginalImages(), Image)
  Else
    AddMapElement(OrginalImages(), Str(Image))
    OrginalImages() = CopyImage(Image, #PB_Any)
  EndIf
  ResizeImage(Image, Width, Height, Mode)
EndProcedure

Procedure FreeImageEx(Image)
  If FindMapElement(OrginalImages(), Str(Image))
    FreeImage(OrginalImages())
    DeleteMapElement(OrginalImages())
  EndIf
  FreeImage(Image)
EndProcedure

; ****

;-Example

UseJPEGImageDecoder()

Enumeration Windows
  #Main
EndEnumeration

Enumeration Gadgets
  #MainImage
EndEnumeration

Enumeration Status
  #MainStatusBar
EndEnumeration

Enumeration Images
  #Image
EndEnumeration

file.s = OpenFileRequester("Images", "", "", 0)
If file
  LoadImage(#Image, file)
Else
  End
EndIf

Procedure doEventResizeWindow()
  Protected width, height
  width = WindowWidth(#Main) - 10
  height = WindowHeight(#Main) - 10
  ResizeImageEx(#Image, width, height)
  ResizeGadget(#MainImage, 5, 5, width, height)
  SetGadgetState(#MainImage, ImageID(#Image))
EndProcedure

BindEvent(#PB_Event_SizeWindow, @doEventResizeWindow())

Procedure Main()
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 600, 400, "Window" , #PB_Window_SystemMenu|#PB_Window_SizeGadget)
    ImageGadget(#MainImage, 5, 5, 590, 390, ImageID(#Image))
    
    doEventResizeWindow()
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Break
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
User avatar
Saki
Enthusiast
Enthusiast
Posts: 682
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Make ResizeImage bullet-proof, as Parameter 0 cause prob

Post by Saki »

Hi mk-soft
The future are dynamic functions anyway.

Code optimization for your code above :

Code: Select all

Procedure ResizeImageEx(Image, Width, Height, Mode = 0)
  If Height < 1 Or Width < 1
    ProcedureReturn
  ElseIf FindMapElement(OrginalImages(), Str(Image))
    CopyImage(OrginalImages(), Image)
  Else
    AddMapElement(OrginalImages(), Str(Image))
    OrginalImages() = CopyImage(Image, #PB_Any)
  EndIf
  ResizeImage(Image, Width, Height, Mode)
EndProcedure
地球上の平和
Fred
Administrator
Administrator
Posts: 14215
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Make ResizeImage bullet-proof, as parameter 0 cause prob

Post by Fred »

It shouldn't enter an infinite loop for such wrong input. Added a debugger check and fixed it.
Post Reply