Help with GRABIMAGE

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Oct 14, 2014 12:09 pm

Help with GRABIMAGE

Post by loulou2522 »

I'd like to cut a part of an image to recreate one without using a gadget. When I submit the following program, Purebasic takes a very long time to execute it

Code: Select all

UsePNGImageEncoder()
UsePNGImageDecoder()
If  LoadImage(0, "C:\Users\xxx\BIL000006.png")
  hauteur.l = ImageHeight(0)-1200
  largeur.l= ImageWidth(0)-1150
  GrabImage(0,1,1150,1200,largeur,hauteur)
  SaveImage(1,"c:\users\xxx\essai.png",#PB_ImagePlugin_PNG, #PB_Image_FloydSteinberg,4)  
  FreeImage(0)
  FreeImage(1)
EndIf
End   
The program takes a very long time to run. Is this normal or have I misused the instructions?
Thanks in advance
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help with GRABIMAGE

Post by infratec »

First:
We can not test ist ... we don't own the picture, we don't have informations about it.

Next:
You need to know the part which takes the time:

Code: Select all

EnableExplicit

Define.i hauteur, largeur
Define.q StartTime, T1, T2, T3

DisableDebugger

UsePNGImageEncoder()
UsePNGImageDecoder()

StartTime = ElapsedMilliseconds()
If LoadImage(0, "C:\Users\xxx\BIL000006.png")
  T1 = ElapsedMilliseconds() - StartTime
  hauteur = ImageHeight(0) - 1200
  largeur= ImageWidth(0) - 1150
  If GrabImage(0, 1, 1150, 1200, largeur, hauteur)
    T2 = ElapsedMilliseconds() - T1
    SaveImage(1,"c:\users\xxx\essai.png",#PB_ImagePlugin_PNG, #PB_Image_FloydSteinberg,4)
    T3 = ElapsedMilliseconds() - T2
    FreeImage(1)
  EndIf
  FreeImage(0)
EndIf

EnableDebugger

Debug "T1: " + Str(T1)
Debug "T2: " + Str(T2)
Debug "T3: " + Str(T3)
I don't think that it is GrabImage().
loulou2522
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Oct 14, 2014 12:09 pm

Re: Help with GRABIMAGE

Post by loulou2522 »

Here is the result
T1: 585
T2: 74
T3: 33597
It seems it saveimage with takelonger times.
The size of the image is 1605 ko
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Help with GRABIMAGE

Post by TI-994A »

loulou2522 wrote: Sun Dec 01, 2024 11:35 amIt seems it saveimage with takelonger times...
Then it would probably boil down to the speed of your storage device.

I tested Bernd's code with a 100MB, 16000 × 9000 pixel, 600-dpi image, with these results:

Code: Select all

T1: 1829
T2: 700
T3: 20797

Faster on an SSD, perhaps. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help with GRABIMAGE

Post by infratec »

Repeat without dithering flag. It costs time.
Also play with the bits per pixel.
Maybe you can find an acceptable compromise.
loulou2522
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Oct 14, 2014 12:09 pm

Re: Help with GRABIMAGE

Post by loulou2522 »

Hi infratec,
with jpg image
T1: 333
T2: 140
T3: 1194
It seems the problem come from saving image in png format becaus withh jpg image the result is good
Is-it normal or this is possibly a bug to inform Fred ?
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help with GRABIMAGE

Post by infratec »

Have you recalculated the native 24 or 32 bit image to 4 bit for the jpg?
Have used dithering in jpg to achieve this?

These are the settings you used for the png. In jpg this is not possible, so it is faster, because no calculation for this is needed..
Have you tried to save it as png without dithering and with 24 or 32 bits according to your image?
Then saving as png should need the same time as saving as jpg,
PurePilish
User
User
Posts: 10
Joined: Mon Oct 07, 2024 9:15 am

Re: Help with GRABIMAGE

Post by PurePilish »

It is not surprising that both dithering and using 4-bit depth add a lot of time to PNG compression. I doubt that it is a bug, it's just that those operations are expensive. As others have said, you can probably solve it by saving as JPG, or saving as PNG without dithering and using 24 or 32 bit depth.
Post Reply