A picture resizing problem

BlitzPlus Forums/BlitzPlus Programming/A picture resizing problem

julianbury(Posted 2010) [#1]
(Doing this project in BlitzPlus)
This is a routine to make tiny versions of pictures to be clicked on to evoke the fullsize picture.

There are two folders called 'big' and 'small' ...

=====================================================================

Global csd
Global file$, bmp$
Global pic, ok, x#, d#
Global sorc$, dest$
Global xres=GadgetWidth(Desktop())
Global yres=GadgetHeight(Desktop())

Graphics xres, yres,32,0
SetBuffer BackBuffer()

sorc$="big/"
dest$="small/"

csd=ReadDir(sorc$)
Repeat
file$=NextFile$(csd)
If Len(file$)>5
bmp$=Left$(file$,Len(file$)-3)+"bmp"
pic=LoadImage(sorc$+file$)
x# = ImageWidth(pic)
d# = 80.0/x#
ScaleImage pic, d#, d#
Cls
DrawImage pic, 0,0
Flip
Delay 200
ok=SaveImage (pic,dest$+bmp$)
FreeImage(pic)
EndIf
Until file$=""

End

=====================================================================

This works just fine, producing the miniatures and depositing them in the small folder.

But when it has done 77 of the 600+ that I need, it crashes and I get to send that report to Microsoft. I tried it with different pictures and different sized pictures but to no avail.

Can anyone suggest why it's crashing? Could it be a buffer overflow? How can I fix it?

Thank you for your kind attention :-)


VIP3R(Posted 2010) [#2]
The OS thinks your program is not responding because of the Repeat/Until loop. The 'Delay 200' won't help, that caught me out too.

Try adding 'WaitEvent(0)' inside the Repeat/Until loop.


julianbury(Posted 2010) [#3]
@ VIP3R

Thanks for the suggestion, I'll give it a go :-)

Cheers!


julianbury(Posted 2010) [#4]
Hi VIP3R,

No luck.

Crashed at 77 again :-(

I've tried every position in the loop. [Karma Sutra?]

The loop looks like this at the moment:
Repeat
    file$=NextFile$(csd)
    If Len(file$)>5
        bmp$=Left$(file$,Len(file$)-3)+"bmp"
        pic=LoadImage(sorc$+file$)
        x# = ImageWidth(pic)
        d# = 80.0/x#
        ScaleImage pic, d#, d#
        Cls
        DrawImage pic, 0,0
        Flip
        ok=SaveImage (pic,dest$+bmp$)
        WaitEvent(0)
        FreeImage(pic)
    EndIf
Until file$=""

One difference: It crashes more profoundly, now.

Time to do extensive research.

Bye for now :-)


Floyd(Posted 2010) [#5]
You might be running out of resources because of all the previously loaded images.

Try doing FreeImage pic before doing pic=LoadImage(TheNextOne).


VIP3R(Posted 2010) [#6]
I've tried the code with 170 images here and it worked as expected. No memory/resource issues.

Are you using the latest version of BlitzPlus? There was a bug in versions prior to V1.46 that would crash with certain sized images.

Not sure what else to suggest other than to double check the images you're converting.


julianbury(Posted 2010) [#7]
Hello, VIP3R :-)

I recently upgraded my machine.
I reinstalled:
BlitzPlusSetup 1.11.exe
BlitzPlusUpdate 1.47.exe
BPDocsPak141.exe

Unless another bug was introduced, this should be solid.

I have been fiddling with the code and have screwed up grievously.

Global csd
Global file$, bmp$
Global pic, ok, x#, d#
Global sorc$, dest$
Global xres=GadgetWidth(Desktop())
Global yres=GadgetHeight(Desktop())

Graphics xres, yres, 32, 0
SetBuffer BackBuffer()

sorc$="E:\BlitzPlus\Girls\big\"
dest$="E:\BlitzPlus\Girls\small\"
csd=ReadDir(sorc$)

Repeat
	file$=NextFile$(csd)
	If Len(file$)>5
		Flip
		Cls
		bmp$=Left$(file$, Len(file$)-3)+"bmp"
		pic=LoadImage(sorc$+file$)
		x# = ImageWidth(pic)
		d# = 60.0/x#
		ScaleImage pic, d#, d#
		DrawImage pic, 0, 0
		ok=SaveImage (pic, dest$+bmp$)
		FreeImage(pic)
		Delay 200
	EndIf
Until file$=""

Graphics 400, 400
text 190, 190, "DONE"
Delay 1000
End


I am now getting a "Invalid Image Handle" error for x# = ImageWidth(pic)

So.

Would you please quote the code that works for you?

Thank you so much for your interest (^_^)


Matty(Posted 2010) [#8]
Maybe you should check if "pic" is non - zero, if it is zero then it hasn't loaded the image correctly, and then you may be able to check if there is something wrong with that file...


VIP3R(Posted 2010) [#9]

Would you please quote the code that works for you?


It was the code in the top post but with WaitEvent(0) added.

I second the suggestion by Matty...

pic=LoadImage(sorc$+file$)
If pic=0 Then RuntimeError("Load Failed: "+sorc$+file$)



julianbury(Posted 2010) [#10]
Hi VIP3R and Matty :-)

pic is coming up zero! I wish I knew why. In a fit of peek, I have uninstalled BlitzPlus. I shall reinstall it later when my rage subsides, but for now, although I am intimidated by BlitzMax, I have installed it to see if I can make it "work as expected" in the way that BlitzPlus seems unable to.

I shall now drift off the radar while I come to grips with the Max monster. Thank you again, for all your help.

Bye for now (-_-)


Stamm(Posted 2010) [#11]
could it ba that your program just encountered the desktop.ini and then crashe trying to load it as an image?
by tha way why are you replacing the file ending - which must be .bmp unless it won't work to load it as a bmp image - by .bmp? this could actually generate a fatal error like this


Matty(Posted 2010) [#12]
He's replacing the file ending to save it as a bitmap, not to load. So that is not the issue.


The problem is that he is loading a file which is not an image, or which is a corrupted image. What he should do, which has been suggested above, is to check if the loadimage returns a zero, and if so handle the error in some other method, perhaps logging the filename of the attempted load in a text file so he can check the files that cause the system to fall over.

There's nothing wrong with blitzplus' image loading commands, as far as I am aware.


Stamm(Posted 2010) [#13]
and then maybe just implement something that just jumps to the next file when the loaded one isnt a valid image
soemthing like
repeat
path$=nextfile(dir)
pic=loadimage(path$)
if pic then
;Resize your image
endif
until path=""



Stamm(Posted 2010) [#14]
this is called "error tolerance": if the program finds an error it just jumps to the next instruction that is not affected by the error