It is currently Wed Sep 30, 2020 11:42 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Make ResizeImage bullet-proof, as Parameter 0 cause prob
PostPosted: Fri Jul 31, 2020 12:45 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 234
Location: Pandora
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.

Top
 Profile  
Reply with quote  
 Post subject: Re: Make ResizeImage bullet-proof, as parameter 0 cause prob
PostPosted: Fri Jul 31, 2020 1:34 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2663
Location: Germany
I like this extended functions :wink:
Code:
;-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


Top
 Profile  
Reply with quote  
 Post subject: Re: Make ResizeImage bullet-proof, as Parameter 0 cause prob
PostPosted: Fri Jul 31, 2020 2:34 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 234
Location: Pandora
Hi mk-soft
The future are dynamic functions anyway.

Code optimization for your code above :
Code:
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

_________________
地球上の平和


Top
 Profile  
Reply with quote  
 Post subject: Re: Make ResizeImage bullet-proof, as parameter 0 cause prob
PostPosted: Mon Sep 14, 2020 10:33 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14090
Location: France
It shouldn't enter an infinite loop for such wrong input. Added a debugger check and fixed it.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye