Announcement

Collapse
No announcement yet.

Display BMP as small size and other

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Display BMP as small size and other

    Hi All,

    I did a bunch of searching and didn't find what I could use.

    Admittedly, I struggle terribly with GRAPHIC commands. I have some code using graphics for BMPs displays but it is probably hacky and incorrect in some areas.

    Would anybody have a simple example where you'd read in a larger BMP like 2048x1024 and then reduce it to a smaller size and display at a point on a dialog or place in a graphic?

    One thing I also need is the ability to change the image on command.

    Thank you.

  • #2
    See:
    GRAPHIC SET STRETCHMODE

    See also
    GRAPHIC BITMAP LOAD, GRAPHIC COPY, GRAPHIC GET STRETCHMODE, GRAPHIC RENDER, GRAPHIC STRETCH

    Do a search on GRAPHIC STRETCH for examples.
    Rod
    I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

    Comment


    • #3
      Howdy, Dean!

      I've posted dozens of such examples here on the forum. Surprised they were hard to find. Sometimes the search terms just don't work out.

      Here's another example:

      Code:
      'Compilable Example:  (Jose Includes)
      #Compiler PBWin 10
      #Compile Exe
      #Dim All
      %Unicode=1
      #Include "Win32API.inc"
      #Resource Bitmap girl "cowgirl.bmp"
      
      %IDC_Graphic = 600
      Global hDlg,hBMP,wImg,hImg As Dword
      
      Function PBMain() As Long
         Dialog New Pixels, 0, "Image Resize",300,300,300,200, %WS_OverlappedWindow To hDlg
         Control Add Graphic, hDlg, %IDC_Graphic,"",10,10,200,150, %WS_Border
         Graphic Attach hDlg, %IDC_Graphic
         Dialog Show Modal hDlg Call DlgProc
      End Function
      
      CallBack Function DlgProc() As Long
         Local x,y,wNew,hNew,w,h As Long
         Select Case Cb.Msg
            Case %WM_InitDialog
               Graphic Bitmap Load "girl", 0, 0 To hBMP  'put image in memory bitmap
               Graphic Attach hBMP,0
               Graphic Get Canvas To wImg,hImg
               Graphic Attach hDlg, %IDC_Graphic
               Graphic Copy hBMP,0                   'copy from memory bitmap to graphic control, put in upper/left
            Case %WM_Size
               Dialog Get Client hDlg To w,h
               Control Set Size hDlg, %IDC_Graphic, w-20, h-20
               RedrawImage
         End Select
      End Function
      
      Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
         'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
         wNew = wImg / Max(wImg / wCont, hImg / hCont)
         hNew = hImg / Max(wImg / wCont, hImg / hCont)
      End Sub
      
      Sub RedrawImage
         Local w,h,x,y,wNew,hNew As Long
         Graphic Clear
         Graphic Get Client To w,h
         GetNewImageSize(w,h,wImg,hImg,wNew,hNew)     'get resized image dimensions
         x = (w-wNew)/2 : y = (h-hNew)/2               'upper/left position so resized image is centered
         Graphic Stretch hBMP, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1)  'copy (resized) from memory bitmap to visible image
      End Sub
      
      'gbs_01136
      'Date: 03-03-2012

      Comment


      • #4
        Dean,
        Note that the example above loads a *.bmp file. Also, it centers the image and maintains aspect ratio. I've also posted examples, using code from Jose, that will load other image formts.

        Comment


        • #5
          Gary, Dean says he's unfamiliar with the graphic commands so I doubt that he would think to use the terms 'stretch' as it is a bit of a stretch for a command that both stretches or contracts an image. GRAPHIC RESIZE might have been a better term to use. Nice example!
          Rod
          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

          Comment


          • #6
            Hey Guys,

            thank you. Gary, I found and tried this example earlier but it didn't work then. It works now.

            But here is the issue. The reduced size corrupts the image terribly. Here is what I am doing. Loading planet texture maps so they can be displayed in OpenGL. I want to display the texture that is defaulted and give the user the ability to load another.

            So my maps are 2048x1024, e.g. powers of 2^n

            This what the Earth map looks like in this example.

            Click image for larger version

Name:	Earth Loaded.png
Views:	87
Size:	39.5 KB
ID:	796371

            Here is the image in paint, shrunk down:
            Click image for larger version

Name:	Earth Basis Texture.png
Views:	75
Size:	77.7 KB
ID:	796372

            I'd like it to look like this.

            So I tried this, HALFTONE, but it won't compile and kicks a function error:

            Code:
            CASE %WM_INITDIALOG
            
            DIM S_FileName AS STRING
            S_FileName = "C:\Odin_local_backup\Work LynxCAT\Software\LYX\LynxCAT\Schedule AST PLA\Texture Maps\Earth.bmp"
            
            ' This fails
            'Graphic Bitmap Load S_FileName, 00, 00, %HALFTONE To hBMP 'put image in memory bitmap
            
            ' This works but is coarse
            GRAPHIC BITMAP LOAD S_FileName, 00, 00 TO hBMP 'put image in memory bitmap
            
            ' Original code
            'Graphic Bitmap Load "girl", 0, 0 To hBMP 'put image in memory bitmap
            GRAPHIC ATTACH hBMP,0
            GRAPHIC GET CANVAS TO wImg,hImg
            GRAPHIC ATTACH hDlg, %IDC_Graphic
            GRAPHIC COPY hBMP,0 'copy from memory bitmap to graphic control, put in upper/left

            Comment


            • #7
              Anyway, thanks for helping.

              Comment


              • #8
                Here is where I am going with all this. This is an interplanetary modeling tool that I have been working on for some years. Here is the OpenGL 3D display. I use the textures to make each planet. Gary, I actually made use of your GL drawlist example.

                Click image for larger version

Name:	Planetary Model.png
Views:	78
Size:	291.9 KB
ID:	796375

                Part of the software displays a cylindrical projection of the planet along with objects in scenario, like satellites and ground stations. Here is where I need to toggle out the old BMP and load a different texture. When the user observes the scenario from a different planet, I need to load that BMP into this graphic. I build this graphic but it is messy and I don't think I am doing this properly. Perhaps I can load the code and you could see if I am messing things up somewhere. It works but is clunky.

                Click image for larger version

Name:	Cylindrical Projection.png
Views:	68
Size:	846.0 KB
ID:	796376

                Comment


                • #9
                  FYI, our system uses Crowdstrike. I think it is deleting the code so some of the problems I reported may not be real. I am working with IT to get a change to the settings. Stand by ...

                  Comment


                  • #10
                    Code:
                    'Graphic Bitmap Load S_FileName, 00, 00, %HALFTONE To hBMP 'put image in memory bitmap
                    Stretch: Stretch mode if the bitmap s to be resized
                    ...

                    The parameters nWidth& and nHeight& specify the width and height of the bitmap, in pixels. If either of the size parameters are zero (0), the bitmap is loaded at its natural size. If either of the size parameters is different from the natural size, the bitmap is stretched or condensed to the requested size.


                    You are using Stretch with size parameters of zero.

                    Try
                    '
                    Code:
                    GRAPHIC ATTACH hBMP,0
                    GRAPHIC GET CANVAS TO wImg,hImg
                    GRAPHIC BITMAP LOAD S_FileName, wImg, hImg,%HALFTONE TO hBMP 'put image in memory bitmap
                    GRAPHIC ATTACH hDlg, %IDC_Graphic
                    GRAPHIC COPY hBMP,0 'copy from memory bitmap to graphic control, put in upper/left
                    '

                    Comment


                    • #11
                      Howdy, Dean,
                      Can you provide a link to the image, so I see first hand what is happening? Is there more code that you're using than the code above, something you can post for us to see what results we get? The distorted image is not starting at 0,0, so there must be more to this story than you've posted so far?

                      Comment


                      • #12
                        Hi Dean,

                        Try using the GRAPHIC SET STRETCHMODE %HALFTONE Statement prior to any COPY, RENDER or STRETCH operations

                        Comment


                        • #13
                          Interesting stuff, Dean!
                          I'm with Gary on the 'more code', preferably compilable.
                          Rod
                          I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                          Comment


                          • #14
                            Howdy, Manuel!

                            As best I know, %HALFTONE has no effect on a COPY, which is the statement Dean is using.

                            Dean,
                            If you use Graphic Stretch you can put the %HALFTONE in the command to get better results - as Manuel suggests.

                            Comment


                            • #15
                              Hi Gary,
                              Sets the default bitmap stretching mode for the current DC.
                              Syntax GRAPHIC SET STRETCHMODE ModeExpr
                              Remarks There are several operations in PowerBASIC which involve stretching or condensing images on bitmaps, most notably GRAPHIC STRETCH. As individual pixels must be added or removed, there is a good chance that the quality of the image will be degraded. However, if you describe the nature of the image by defining a StretchMode, you can substantially enhance the appearance.

                              The default StretchMode is maintained individually for each DC. You can set the default mode with this statement, or retrieve it with GRAPHIC GET STRETCHMODE. Of course, you can also override the default StretchMode when you execute one of the affected statements.

                              The 4 stretch mode equates are predefined in PowerBASIC.
                              Equate Value Description
                              %BLACKONWHITE 1 This is the default Windows stretch mode, and is most appropriate for monochrome bitmaps, or those with blocks of color. Performs a boolean OR of eliminated and existing pixels. It preserves black pixels at the expense of white pixels.
                              %WHITEONBLACK 2 Performs a boolean OR of eliminated and existing pixels. It preserves white pixels at the expense of black pixels.
                              %COLORONCOLOR 3 Deletes eliminated lines of pixels without trying to preserve their information.
                              %HALFTONE 4 This provides the highest quality for complex color bitmaps. The average color of the destination pixel block is kept approximately the same as the source pixel block.
                              See also GRAPHIC BITMAP LOAD, GRAPHIC COPY, GRAPHIC GET STRETCHMODE, GRAPHIC RENDER, GRAPHIC STRETCH

                              Comment


                              • #16
                                FWIW Graphic Copy has 16 styles :

                                %mix_MaskNotSrc
                                Pixel is a combination of the colors common to both the pixel and the inverse of the source.

                                %mix_NotCopySrc
                                Pixel is the inverse of the pen color.

                                %mix_MaskSrcNot
                                Pixel is a combination of the colors common to both the source and the inverse of the pixel.

                                %mix_Not
                                Pixel is the inverse of the pixel color.

                                %mix_XorSrc
                                Pixel is a combination of the colors in the source and in the pixel, but not in both.

                                %mix_NotMaskSrc
                                Pixel is the inverse of the MaskSrc color.

                                %mix_MaskSrc
                                Pixel is a combination of the colors common to both the source and the pixel.

                                %mix_NotXorSrc
                                Pixel is the inverse of the XorSrc color.

                                %mix_Nop
                                Pixel remains unchanged.

                                %mix_MergeNotSrc
                                Pixel is a combination of the source color and the inverse of the pixel color.

                                %mix_CopySrc
                                Pixel is the source color (default).

                                %mix_MergeSrcNot
                                Pixel is a combination of the source color and the inverse of the pixel color.

                                %mix_MergeSrc
                                Pixel is a combination of the source color and the pixel color.

                                %mix_Whiteness
                                Pixel is always 1 (white).
                                Rod
                                I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                                Comment


                                • #17
                                  Howdy, Manuel!

                                  Sorry, but I'm not sure of your point. That setting does not apply to Graphic Copy, which does no stretching, What am I missing?

                                  Comment


                                  • #18
                                    Hi Gary,

                                    I suggested to use the "GRAPHIC SET STRETCHMODE %HALFTONE" Statement prior to any COPY, RENDER or STRETCH operations, meaning to put that statement in a separate line before the Copying, Rendering or Stretching lines of code. Maybe that statement does not have effect on Copying, but certainly do have effect on Rendering and Stretching.

                                    Comment


                                    • #19
                                      From the docs on GRAPHIC SET STRETCHMODE:
                                      There are several operations in PowerBASIC which involve stretching or condensing images on bitmaps, most notably GRAPHIC STRETCH. As individual pixels must be added or removed, there is a good chance that the quality of the image will be degraded. However, if you describe the nature of the image by defining a StretchMode, you can substantially enhance the appearance.
                                      Rod
                                      I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

                                      Comment


                                      • #20
                                        Here you go..
                                        '
                                        Code:
                                        #Compiler PBWin 10
                                        #Compile Exe
                                        #Dim All
                                        %Unicode=1
                                        #Include "Win32API.inc"
                                        
                                        %IDC_Graphic = 600
                                        Global hDlg,hBMP,wImg,hImg As Dword
                                        
                                        Function PBMain() As Long
                                           Dialog New Pixels, 0, "Image Resize",300,300,300,200, %WS_OverlappedWindow To hDlg
                                           Control Add Graphic, hDlg, %IDC_Graphic,"",10,10,200,150, %WS_Border
                                           Graphic Attach hDlg, %IDC_Graphic
                                           Dialog Show Modal hDlg Call DlgProc
                                        End Function
                                        
                                        CallBack Function DlgProc() As Long
                                           Local x,y,wNew,hNew,w,h As Long
                                           Select Case Cb.Msg
                                              Case %WM_InitDialog
                                        
                                                DIM S_FileName AS STRING
                                                S_FileName = "image_11038.bmp" '(Image copied from post#8)
                                        
                                                ' This fails
                                                'Graphic Bitmap Load S_FileName, 00, 00, %HALFTONE To hBMP 'put image in memory bitmap
                                        
                                                ' This works but is coarse (when subsequently stretched / compressed with default stretchmode)
                                                GRAPHIC BITMAP LOAD S_FileName, 00, 00 TO hBMP 'put image in memory bitmap - image size
                                        
                                                 Graphic Attach hBMP,0
                                                 Graphic Get Canvas To wImg,hImg
                                                 Graphic Attach hDlg, %IDC_Graphic
                                                 Graphic Copy hBMP,0                    ' COPY from memory bitmap to graphic control
                                              Case %WM_Size
                                                 Dialog Get Client hDlg To w,h
                                                 Control Set Size hDlg, %IDC_Graphic, w-20, h-20
                                                 RedrawImage                            ' (stretch / compress as reqd)
                                           End Select
                                        End Function
                                        
                                        Sub GetNewImageSize(wCont As Long, hCont As Long, wImg As Long, hImg As Long, wNew As Long, hNew As Long)
                                           'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
                                           wNew = wImg / Max(wImg / wCont, hImg / hCont)
                                           hNew = hImg / Max(wImg / wCont, hImg / hCont)
                                        End Sub
                                        
                                        Sub RedrawImage
                                           Local w,h,x,y,wNew,hNew As Long
                                           Graphic Clear
                                           Graphic Get Client To w,h
                                           GetNewImageSize(w,h,wImg,hImg,wNew,hNew)     ' get resized image dimensions
                                           x = (w-wNew)/2 : y = (h-hNew)/2              ' upper/left position so resized image is centered
                                           GRAPHIC SET STRETCHMODE %HALFTONE            ' << Default windows mode is %BLACKONWHITE
                                           Graphic Stretch hBMP, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1)  'copy (resized) from memory bitmap to visible image
                                        End Sub
                                        
                                        ' based on gbs_01136
                                        Click image for larger version  Name:	Capture_image_11038.PNG Views:	0 Size:	88.1 KB ID:	796427
                                        Rgds, Dave

                                        Comment

                                        Working...
                                        X