DrawImage blurry

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

DrawImage blurry

Post by wombats »

I am using DrawImage() to draw a zoomed version of an image (the image sizes are multiplied by 'zoom', which is a float). However, the enlarged versions come out blurry. Is there a way to stop this and have it just enlarge it 'raw'?

Alternatively, is there a way to draw sub-pixels on a canvas (where 'zoom' would be 0.25, 0.5, etc.) for when the image is zoomed out? I have all the pixel data stored in an array.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: DrawImage blurry

Post by Dude »

How are you zooming it? With ResizeImage()? If so, it smooths by default, so make sure you use the #PB_Image_Raw flag with it.
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: DrawImage blurry

Post by wombats »

I am using the width and height parameters of DrawImage to define the desired size.

I am attempting to make a small pixel editor, so I don’t think ResizeImage would be fast enough, as I need the image redrawn each time a change is made to the pixel data.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: DrawImage blurry

Post by Dude »

Oops, I forgot that DrawImage() did resizing. Sorry for my pointless reply. :oops:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: DrawImage blurry

Post by wilbert »

wombats wrote:I am using the width and height parameters of DrawImage to define the desired size.

I am attempting to make a small pixel editor, so I don’t think ResizeImage would be fast enough, as I need the image redrawn each time a change is made to the pixel data.
ResizeImage is pretty fast.
You could also access the drawing buffer and create your own procedure.
Windows (x64)
Raspberry Pi OS (Arm64)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4659
Joined: Sun Apr 12, 2009 6:27 am

Re: DrawImage blurry

Post by RASHAD »

Hi
Using MouseWheel
Do not use the original image and do not use cumulative resizing

Code: Select all

UseGIFImageDecoder()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

OpenWindow(0, 0, 0, 800, 500, "PictureViewer", #PB_Window_SystemMenu|#PB_Window_MaximizeGadget| #PB_Window_ScreenCentered)

ExplorerListGadget(0, 10, 10, 250, 470, "*.bmp;*.jpg;*.png;*.tga;*.tif;*.gif",#PB_Explorer_GridLines)
SetGadgetColor(0, #PB_Gadget_BackColor, $DDFFFF)
SetGadgetColor(0, #PB_Gadget_FrontColor, $FF0000)

CanvasGadget(1,270,10,515,470,#PB_Canvas_Border)
oldDelta.f = 1
Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End       
    
    Case #PB_Event_Gadget   
      Select EventGadget()     
        Case 0
          Select EventType()
             Case #PB_EventType_LeftDoubleClick                
                file$ = GetGadgetText(0) + GetGadgetItemText(0, GetGadgetState(0))
                If FileSize(file$) > 0
                  LoadImage(0, file$)
                  If IsImage(0)            
                    CopyImage(0,1)
                    If IsImage(1)
                      StartDrawing(CanvasOutput(1))
                        Box(0,0,OutputWidth(),OutputHeight(),$FFFFFF)
                        DrawImage(ImageID(1),OutputWidth()/2-ImageWidth(1)/2,OutputHeight()/2-ImageHeight(1)/2)
                      StopDrawing()
                    EndIf
                  EndIf
                EndIf
          EndSelect 
                
        Case 1
          Select EventType()
            Case #PB_EventType_MouseWheel
              Delta.f = GetGadgetAttribute(1, #PB_Canvas_WheelDelta )/100 + oldDelta.f
              If Delta.f < 0.01
                Delta.f = 0.01
              EndIf
              If IsImage(0)            
                CopyImage(0,1)
                ResizeImage(1,ImageWidth(0)*Delta,ImageHeight(0)*Delta)
                If IsImage(1)
                  StartDrawing(CanvasOutput(1))
                    Box(0,0,OutputWidth(),OutputHeight(),$FFFFFF)
                    DrawImage(ImageID(1),OutputWidth()/2-ImageWidth(1)/2,OutputHeight()/2-ImageHeight(1)/2)
                  StopDrawing()
                EndIf
                oldDelta.f = Delta.f
              EndIf
          EndSelect 
      EndSelect        
  EndSelect
ForEver
Egypt my love
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: DrawImage blurry

Post by wombats »

Thank you for the example code, RASHAD!
wilbert wrote:
wombats wrote:I am using the width and height parameters of DrawImage to define the desired size.

I am attempting to make a small pixel editor, so I don’t think ResizeImage would be fast enough, as I need the image redrawn each time a change is made to the pixel data.
ResizeImage is pretty fast.
You could also access the drawing buffer and create your own procedure.
The drawing buffer confuses me to no end. I can't seem to find a simple explanation on how to change a pixel.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: DrawImage blurry

Post by Dude »

Rashad, your example doesn't do anything for me when I scroll the mousewheel after selecting an image?
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: DrawImage blurry

Post by Lord »

Change line 13 to:

Code: Select all

CanvasGadget(1,270,10,515,470,#PB_Canvas_Border|#PB_Canvas_Keyboard )
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4659
Joined: Sun Apr 12, 2009 6:27 am

Re: DrawImage blurry

Post by RASHAD »

@wombats
How do you want to change pixels?.Before or after zooming
Post any simple example

New zoom snippet
Supported :
-Arrow Up & Down
-PgUp & PgDn
-MouseWheel

Code: Select all

UseGIFImageDecoder()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

OpenWindow(0, 0, 0, 800, 500, "PictureViewer", #PB_Window_SystemMenu|#PB_Window_MaximizeGadget| #PB_Window_ScreenCentered)

ExplorerListGadget(0, 10, 10, 210, 480, "*.bmp;*.jpg;*.png;*.tga;*.tif;*.gif",#PB_Explorer_GridLines)
SetGadgetColor(0, #PB_Gadget_BackColor, $DDFFFF)
SetGadgetColor(0, #PB_Gadget_FrontColor, $FF0000)
SetGadgetItemAttribute(0, 0,#PB_Explorer_ColumnWidth,60,1)
SetGadgetItemAttribute(0, 0,#PB_Explorer_ColumnWidth,40,2)
RemoveGadgetColumn(0,3)

CanvasGadget(1,230,10,560,480,#PB_Canvas_Border|#PB_Canvas_Keyboard)

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End       
    
    Case #PB_Event_Gadget   
      Select EventGadget()     
        Case 0
          Select EventType()
             Case #PB_EventType_LeftDoubleClick
                SetActiveGadget(1)
                oldDelta.f = 1                
                file$ = GetGadgetText(0) + GetGadgetItemText(0, GetGadgetState(0))
                If FileSize(file$) > 0
                  LoadImage(0, file$)
                  If IsImage(0)            
                    CopyImage(0,1)
                    If IsImage(1)
                      StartDrawing(CanvasOutput(1))
                        Box(0,0,OutputWidth(),OutputHeight(),$FFFFFF)
                        DrawImage(ImageID(1),OutputWidth()/2-ImageWidth(1)/2,OutputHeight()/2-ImageHeight(1)/2)
                      StopDrawing()
                    EndIf
                  EndIf
                EndIf
          EndSelect 
                
        Case 1
          Select EventType()
            Case #PB_EventType_KeyDown
              Status = GetGadgetAttribute(1, #PB_Canvas_Key)
              If Status = #PB_Shortcut_Up
                Delta.f = oldDelta.f + 1/100
              ElseIf Status = #PB_Shortcut_PageUp
                Delta.f = oldDelta.f + 10/100
              ElseIf Status = #PB_Shortcut_Down
                Delta.f = oldDelta.f - 1/100
              ElseIf Status = #PB_Shortcut_PageDown
                Delta.f = oldDelta.f - 10/100
              EndIf
              If Delta.f > 10.0
                Delta.f = 10
              ElseIf Delta.f < 0.01
                Delta.f = 0.01
              EndIf
              If IsImage(0)            
                CopyImage(0,1)
                ResizeImage(1,ImageWidth(0)*Delta,ImageHeight(0)*Delta)
                If IsImage(1)
                  StartDrawing(CanvasOutput(1))
                    Box(0,0,OutputWidth(),OutputHeight(),$FFFFFF)
                    DrawImage(ImageID(1),OutputWidth()/2-ImageWidth(1)/2,OutputHeight()/2-ImageHeight(1)/2)
                  StopDrawing()
                EndIf
                oldDelta.f = Delta.f
              EndIf
              
            Case #PB_EventType_MouseWheel
              Delta.f = GetGadgetAttribute(1, #PB_Canvas_WheelDelta )/100 + oldDelta.f
              If Delta.f < 0.01
                Delta.f = 0.01
              EndIf
              If IsImage(0)            
                CopyImage(0,1)
                ResizeImage(1,ImageWidth(0)*Delta,ImageHeight(0)*Delta)
                If IsImage(1)
                  StartDrawing(CanvasOutput(1))
                    Box(0,0,OutputWidth(),OutputHeight(),$FFFFFF)
                    DrawImage(ImageID(1),OutputWidth()/2-ImageWidth(1)/2,OutputHeight()/2-ImageHeight(1)/2)
                  StopDrawing()
                EndIf
                oldDelta.f = Delta.f
              EndIf
          EndSelect 
      EndSelect        
  EndSelect
ForEver
Egypt my love
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: DrawImage blurry

Post by blueb »

Rashad, that is a great example.

I was pleasantly surprised that all the commands used were available in PureBasic.
I was expecting all kinds of Win API commands. :mrgreen:
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Post Reply