Page 1 of 1

Image resize problem

Posted: Thu Jun 05, 2003 4:59 pm
by LCD
One more question: Im loading a visual file (BMP,PNG,JPG), if I display it without resizing, everything is fine, but if I scale it down, the colours are wrong. Any idea why?

Code: Select all

  CreateImage(2,256,192)
  LoadImage(2,filename$)
  StartDrawing(WindowOutput())
  ResizeImage(2,256,192)
  DrawImage(UseImage(2),0,0)
  StopDrawing()
Edit: Oh, I forgot: Thos happens only if I scale the picture down, if I scale it up, anything is correct. Maybe a bug?

Posted: Thu Jun 05, 2003 5:03 pm
by LCD
And some more newbie questions:
How to change gadget position after it is created?
How to change the Main window's title?
TIA
Edit: Is there a possibility to copy a part of a image() to another image()?

Posted: Thu Jun 05, 2003 5:24 pm
by TerryHough
Depending on the size of your image, the resize to a fixed image size is likely to be severly distorted and have unexpected results.

You probably need to resize the image based on a percentage comparison of the image's width and height to the desired final image size. Usually this means finding the lower percentage of the comparisons and using it.

But, I am not real familiar with the DrawImage process. Just speaking generally.

Here is an example of code I use with and ImageGadget

Code: Select all

If LoadImage(0, Filename$)
      ResizeWidth.f  = GadgetWidth(0)  / ImageWidth()
      ResizeHeight.f = GadgetHeight(0) / ImageHeight()
      ResizePercent.f = ResizeHeight
      If ResizeWidth < ResizeHeight
        ResizePercent = ResizeWidth
      EndIf  
      ResizeImage(0, ImageWidth()*ResizePercent, ImageHeight()*ResizePercent)
      ResizeGadget(0,((WindowWidth()-ImageWidth())/2), ((WindowHeight()-ImageHeight())/2),ImageWidth(), ImageHeight())
      SetGadgetState(0, ImageID())  ; Change the picture in the gadget
...
And I am assuming you have loaded the proper Image Plugins Decoder to read the image properly.

Hope this helps
Terry

Posted: Thu Jun 05, 2003 6:25 pm
by LCD
That's not it. I tried to rescale the image first in Paint shop Pro to 512x384, which should give a scaling factor of 0.5 with my program, but the colours are still wrong, and it looks more likely that this is a 16>32 Bit (or vica versa) conversion problem (I use 32 bit display depth), also the histogram is completly wrong.
And yes, I have these plugins loaded.
The images have wrong colours, some colours that should be grey, are green, other bright colours are dark. Have you tried to load and rescale your images?

*Bug Panic* <- Dont press this unless you are in serious trouble.

Posted: Thu Jun 05, 2003 6:34 pm
by Pupil
Have you tried with the updated Image libary found here:
http://www.purebasic.com/update/
Put it in your "PureLibraries\" directory.

Posted: Thu Jun 05, 2003 6:36 pm
by tinman
LCD wrote:How to change gadget position after it is created?
ResizeGadget() (I think).

Posted: Thu Jun 05, 2003 6:44 pm
by LCD
@Pupil:
Does not help, exactly the same behaviour as before.
@tinman:
Yup, exactly, thank you...

Posted: Thu Jun 05, 2003 7:07 pm
by TerryHough
@LCD
Yes, I have loaded and rescaled many images.

I have been working on a program to display images. It ist set to rescale the image to an ImageGadget. That ImageGadget is initially resized to slightly smaller than the client space of the full screen window resolution for a maximum, then rescaled to a size that will allow displaying the entire rescaled image.

So I now have this working quite successfully with BMP and JPG images. Haven't tried any other formats.

I do have a problem with 8 bit Gray Scale JPG files displaying incorrectly. I reported this in the Bug section and sent code and examples to Fred. See post
viewtopic.php?t=6333

Occasionally I have experienced a problem saving 32 bit Color images as JPG files. So far, this has been intermittent and I haven't discovered the cause.

Terry

Posted: Thu Jun 05, 2003 8:22 pm
by LCD
Yes, the 8-bit Grayscale jpg's... They crash DarkBasic Pro if I try to load. Looks like Im talking with a expert. Do you know how to change the quality level for saving jpg's?
I will retry your code again, maybe there is a different problem...

Posted: Thu Jun 05, 2003 8:51 pm
by TerryHough
LCD wrote:Looks like Im talking with a expert.
:mrgreen: ROFL

Not really, just been trying to learn more about PB. So I built basically a
"Slide Show" image viewer to automatically display the hundreds of
images I have accumulated in my genealogy research. I still have some
work to do on it, mostly due to my level of PB knowledge.

The 8b Gray Scale jpgs load fine, just display incorrectly, with PB. I hope
Fred will find the problem soon. Probably too busy with the new release
to spend any time on it, unless he has already fixed it for the new release
and hasn't had time to reply. In the meantime, I am converting them to
16 bit Color externally so that I can use them successfully.

No, I don't know how to change the quality level for saving jpgs in PB.
From the Help file:
The JPEG format uses destructive compression (lossy) which means that the picture loses more or less of its original information when it's compressed. This is actually one of the best lossy compression algorithms available, which can dramatically decrease the image size without lots of visible artifacts.
Actually, when I use PB to save jpgs, I usually have much smaller files
than the oginal size created in a commercial scan program using its best
compression. So far I haven't seen a lot of degradation of the
image when reloaded. I'm :D

Regards,
Terry

Posted: Thu Jun 05, 2003 9:39 pm
by LCD
I see, my program is a image converter, so asking the user to preprocess the image manually, is not the right way. I tested your code, but it crashed, maybe I forgot something.
The DarkBasicPro version of my program had no such problems with image resizing, maybe because i misused sprites for this job ;).
I'm looking forward for the 3.70 version, mabe the resizing work better...
Setup of JPG saving quality is important for utility coding, I hope, the setting will be added soon.
Is there a possibility to copy a part of a image() to another image()?
No need to answer this question anymore, as I found the GrabImage() function. :)

Posted: Thu Jun 05, 2003 9:53 pm
by Pupil
LCD wrote:Do you know how to change the quality level for saving jpg's?
There's no way presently to change the quality level of the saved jpg, but it has been requested before and it is on Fred's TODO list. I don't know if it'll be there for the next version(PB3.70) or for a more distant version..

Posted: Thu Jun 05, 2003 10:36 pm
by TerryHough
LCD wrote:I see, my program is a image converter... snip....
I have image conversion included too, but at the moment I have not
included the Image Plugin decoder for anything by JPG and the native
BMP.

I don't have files readily available in the other formats, and since I'm
really writing this for my own use, I chose to not use the other decoders.
Adding UseTGAImageDecoder(), UsePNGImageDecoder(),
UseTIFFImageDecoder() and the UsePNGImageEncoder() causes
the program to be 4 times as large.

I have not worked with the GrabImage() function at all. Maybe later.

Here is my display procedure which uses an ImageGadget. Hope it helps.

Code: Select all

; --------------------------------------------------------------------------
; Subroutine to display selected images
; --------------------------------------------------------------------------
DisplayImage:
  HideGadget(0,1)                             ; Hide the Image Gadget
  If ClickNext.w                              ; Is it in manual mode?
    ResizeGadget(0, 10, 30, WindowWidth()-20, WindowHeight()-92)
    HideGadget(10,0)                          ; Show Next Button
  Else
    ResizeGadget(0, 10, 30, WindowWidth()-20, WindowHeight()-62)
  EndIf  
  If Filename$                                ; Must have a file name
    If LoadImage(0, Filename$)                ; Load the image file
      ResizeWidth.f  = GadgetWidth(0)  / ImageWidth() ; Calc resize width %
      ResizeHeight.f = GadgetHeight(0) / ImageHeight(); Calc resize height %
      ResizePercent.f = ResizeHeight                  ; Then choose smaller
      If ResizeWidth < ResizeHeight
        ResizePercent = ResizeWidth
      EndIf  
      ResizeImage(0, ImageWidth()*ResizePercent, ImageHeight()*ResizePercent)
      ResizeGadget(0,((WindowWidth()-ImageWidth())/2), ((WindowHeight()-ImageHeight())/2),ImageWidth(), ImageHeight())
      SetGadgetState(0, ImageID())  ; Change the picture in the gadget
      HideGadget(0,0)               ; Unhide the Image Gadget
      ; Ability to save the file may be disabled by
      ; uncommenting the Next two lines
;      DisableToolBarButton(3, 1)    ; Disable the save button
;      DisableMenuItem(3, 1)         ; Disable the save menu
    EndIf
    If ClickNext.w                            ; If in manual mode  
      HideGadget(10,0)                          ; Show Next Button
    EndIf 
  EndIf

Return 
; --------------------------------------------------------------------------
; End of the Display Image routine
; --------------------------------------------------------------------------
I hope this will help.

Terry

Posted: Fri Jun 06, 2003 12:13 am
by LCD
Yes, it helped me a bit to understand the problem...

The problem is still here, but I know now that resizing produces worse results with higher downscaling factor.
If I resize a 1024x768 photo to 800x600, it does not look too much wrong. Resizing from 1024x768 to 512x384: a lot of pixels are incorrectly, and at 256x192 it looks completly wrong, even if I use your method.
I can't help myself, this looks still like a bug for me.

Edit:
Now I know why your code did not work. Sorry, my mistake: Gadget 0 was already defined 8O :? I did not noticed this and wondered why my other gadget was allways in wrong place.

Sh... I´m too late again. Just noticed that this bug was reported in bug section (PureBasic or DX9 bug?)

Posted: Fri Jul 11, 2003 12:34 pm
by geoff
I've tried loading JPG images and displaying them at various sizes and I find this works fine. Have I missed a post saying this problem has been fixed?

Of course, very small images are created by sampling pixels from within the large image. The pixels are not the average of pixel blocks in the larger image, so you would expect the small image to look a little noisy. Is this the problem others were referring to?

Fred, you suggested adding variable image quality in SaveImage() for JPG.

I hope this can be added soon, please.