Image display question

Discussions related to graphics (2D and 3D), animation and games programming
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Image display question

Post by mikeg »

This is only part of the program, but it should show enough for the question.
I tried to display a .png image in my BBC Basic program, and I have been unsuccessful. Anyone know how to do this?
( being able to display the file will help me with my character extraction)

Code: Select all

  
      PROCgraphics(1280,1024)

      *DISPLAY "2019-12-11.png" 0,0
      END
      DEF PROCgraphics(x,y)
      VDU 23,22,x;y;8,15,16,1
      OFF
      VDU 5
      ENDPROC
Focus is on code subject. Feel free to judge the quality of my work.
David Williams

Re: Image display question

Post by David Williams »

mikeg wrote: Fri 13 Dec 2019, 00:28 This is only part of the program, but it should show enough for the question.
I tried to display a .png image in my BBC Basic program, and I have been unsuccessful. Anyone know how to do this?
( being able to display the file will help me with my character extraction)

Code: Select all

  
      PROCgraphics(1280,1024)

      *DISPLAY "2019-12-11.png" 0,0
      END
      DEF PROCgraphics(x,y)
      VDU 23,22,x;y;8,15,16,1
      OFF
      VDU 5
      ENDPROC
*DISPLAY can only display Windows BMP images, so you could convert the PNG image to BMP format first?

In any case, check out this article from the BB4W Programmer's Reference: "Displaying a PNG or TIFF image":

http://www.bbcbasic.co.uk/wiki/doku.php ... ff_20image

I don't off-hand know how to display PNG images under BBCSDL, but I'm pretty sure it's possible. In fact it's possible that at least one of my games which Richard ported to BBCSDL loads PNG images, but I'm too lazy to check at the moment... it's Election Night on the BBC! :D


David.
--
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Image display question

Post by mikeg »

Very awesome! I got what I needed. Now I can work on character extraction tool.
Thankyou.
Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Image display question

Post by RichardRussell »

David Williams wrote: Fri 13 Dec 2019, 02:10*DISPLAY can only display Windows BMP images, so you could convert the PNG image to BMP format first?
In BBC BASIC for Windows *DISPLAY (OSCLI "DISPLAY...") can only display BMP images, in BBC BASIC for SDL 2.0 (all editions) *DISPLAY can display BMP, JPG, GIF and PNG images (actually it may be able to display several more formats, but that is untested). This is covered in the Differences between BB4W and BBCSDL document (section 4e) and is one of the major advantages of BBCSDL over BB4W.

When porting programs from BB4W to BBCSDL I quite often convert images from BMP to GIF or PNG, and similarly I convert sound files from WAV to MP3 (because BBCSDL can play the latter just as easily as the former). It's not essential - BBCSDL can handle the older file types perfectly well - but it can save a substantial amount of disk space.
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Image display question

Post by mikeg »

DISPLAY can only display Windows BMP images, so you could convert the PNG image to BMP format first?
Unfortunately, when you capture a windows screen using the Windows capture tools, the image is captured in a .png image. I looked to see if I could change the save image options and I couldn't find it. The snipping tool must be downloaded from Microsoft. Once that is done you can either use the tool for custom cutting of images or press FN +printscr button to capture the entire screen.

(of course now I wont need to capture the screen image, as I can use the color capture system tools in a DLL to extract the area I am needing to extract)

After some thought while trading, I remembered I have code for extracting color from the windows screen. It accesses windows colors and should give me the colors I need.
I only will need to scan a live section area and use templates to compare each row of a number until I get a non black pattern that matches to a specific number that also shares the same pattern.

( the templates will be scanned images of known characters and then the layout data will be matched with numbers that need identifying)

(Character recognition should be easy for a type of character no matter the color, as long as the color is not background color I identify to avoid.)

As time passes, I will make it more efficient so it doesnt need to find all positive colored locations of a number.
Focus is on code subject. Feel free to judge the quality of my work.
RichardRussell

Re: Image display question

Post by RichardRussell »

mikeg wrote: Sat 14 Dec 2019, 00:41Unfortunately, when you capture a windows screen using the Windows capture tools, the image is captured in a .png image.
To reiterate: "in BBC BASIC for SDL 2.0 (all editions) *DISPLAY can display BMP, JPG, GIF and PNG images".
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Image display question

Post by mikeg »

To reiterate: "in BBC BASIC for SDL 2.0 (all editions) *DISPLAY can display BMP, JPG, GIF and PNG images".
Thanks Richard. I did see that earlier. And it is noted for future use on the pi3 and on my MAC.
My old MacBook Pro, does need a task like this and it can display Investing.com and uses an older version of BBCSDL.

If say, I could put a transparent window overtop of the screen and then use the TINT or POINT to extract the colors, that could make
short work of the need for DLLs. I have found the program I had that used the system calls for the color extraction but it


needs convert to BBC BASIC
(I am having problems posting text for some reason. some of my text in this region was omitted after submitting)

(I nearly have the whole thing extracted from BMPtoCode and if I could understand how it is done differently in BBC Basic, I can put LB to rest and also, if I can understand more about DLLs in BBC Basic I could finally after many years understand a bigger dimension.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

(this program is in the process of being modified to use the mouse location to locate the color location and to help me identify the colors that exist
at the part of the screen I need to extract. the number length would be like around this big: 10.98 maximum size and the decimal is crucial.)

This subject would not be appropriate for LBB or LB or BBC Basic as this is a LB program and I want to convert to BBC BASIC, but I want to understand the differences.
I dont want a finished program provided. (so thats not what I am asking for.)
Here is where I have been looking, but there is more than what is here:
http://www.bbcbasic.co.uk/wiki/doku.php ... put-output

* UPDATE: I think I have found the information in: Introduction to the Windows API
Here is what I found:
Functions in other DLLs must be explicitly loaded into memory, and must be called by address rather than by name. For example to call the function OleUIChangeIconA which is in OLEDLG.DLL you must perform the following steps:
SYS "LoadLibrary", "OLEDLG.DLL" TO oledlg%
SYS "GetProcAddress", oledlg%, "OleUIChangeIconA" TO chicon%
SYS chicon%, ci% TO uint%

Once you have finished with them, it is important to 'release' any DLLs which you have loaded:
SYS "FreeLibrary", oledlg%

To ensure that the DLLs are released however your program exits, include code similar to the following in a 'cleanup' routine called from your ON CLOSE and ON ERROR handlers:
oledlg% += 0 : IF oledlg% THEN SYS "FreeLibrary", oledlg%

I have an idea of how some of the information would be passed to the above example pieces. But what about the long statements?
I know that i and j would be screen locations and pColor is the color that is extracted, but is this layout used in any way to represent the same
extraction method? Is there something missing in the above example?

Code: Select all

'  NOTE : this program will not run as it is.
open "user32" for dll as #user
  Open "gdi32"for DLL as #gdi
  CallDll #user, "GetDC",_
    hWnd as long,_
    hDC as long

      CallDll #gdi, "GetPixel",_
        hDC as long,_
        i as long,_
        j as long,_
        pColor as long
 call getRGB pColor,r,g,b
       nst$=""
       if r=0 and g=0 and b=0 then nst$="0" 'black
       if r=192 and g=128 and b=64 then nst$="1" ' brown
       if r=64 and g=64 and b=192 then nst$="2" 'dark blue
       if r=128 and g=128 and b=128 then nst$="3" ' dark grey
       if r=128 and g=0 and b=0 then nst$="4" 'dark red
       if r=224 and g=192 and b=0 then nst$="5" 'dark yellow
       if r=128 and g=160 and b=192 then nst$="6" 'flat blue
       if r=32 and g=192 and b=64 then nst$="7" 'green
       if r=166 and g=202 and b=240 then nst$="8" 'light blue
       if r=192 and g=192 and b=192 then nst$="9" 'light gray
       if r=192 and g=224 and b=0 then nst$="a" 'light green
       if r=224 and g=32 and b=64 then nst$="b" 'light red
       if r=0 and g=160 and b=192 then nst$="c" 'medblue
       if r=224 and g=128 and b=64 then nst$="d" 'orange
       if r=224 and g=160 and b=192 then nst$="e" 'pink
       if r=160 and g=64 and b=192 then nst$="f" 'purple
       if r=192 and g=220 and b=192 then nst$="g" 'tan
       if r=255 and g=255 and b=255 then nst$="h" 'white
       if r=255 and g=255 and b=0 then nst$="i" 'yellow
       if nst$="" then nst$="0" 'prevent misses
       print #1, nst$
    
  #1 "vertscrollbar on 0 "; DisplayHeight
  #1 "horizscrollbar on 0 "; DisplayWidth
  #1 "down"
  #1 "size 2"
  #1 "when leftButtonMove [draw]"

  wait

[draw]
  #draw, "set "; MouseX; " "; MouseY
  wait

#1 "when rightButtonMove [quit
  CallDll #user, "ReleaseDC",_
    hWnd as long,_
    hDC as long,_
    r as long
  close #user
  close #gdi
  cursor normal
end
sub getRGB pixcol, byref r, byref g, byref b
   b = int(pixcol / (256*256))  '*
   g = int((pixcol - b *256*256) / 256) '*
   r = int(pixcol - b*256*256 - g*256) '*
end sub

[quit]
  close #1
  end

Focus is on code subject. Feel free to judge the quality of my work.
KenDown
Posts: 327
Joined: Wed 04 Apr 2018, 06:36

Re: Image display question

Post by KenDown »

This code is more or less taken straight from the help file for BB4W, which probably means that it originated with Richard. It works for me. It does require the gdiplib library to be installed.

Code: Select all

      DEFPROCpng(pic$,xpos%,ypos%,xsize%,ysize%)
      LOCALtSI{},bmi{},pic%,image%,ix%,iy%,gfx%,lGDIP%
      DIMpic%LOCAL513
      IFxsize%=1280ysize%=840
      SYS"MultiByteToWideChar",0,0,pic$,-1,pic%,256
      DIMtSI{GdiplusVersion%,DebugEventCallback%,\
      \   SuppressBackgroundThread%,SuppressExternalCodecs%}
      DIMbmi{biSize%,biWidth%,biHeight%,biPlanes{l&,h&},biBitCount{l&,h&},\
      \   biCompression%,biSizeImage%,biXPelsPerMeter%,biYPelsPerMeter%,\
      \   biClrUsed%,biClrImportant%}
      tSI.GdiplusVersion%=1
      SYS`GdiplusStartup`,^lGDIP%,tSI{},0
      SYS`GdipLoadImageFromFile`,pic%,^image%
      IFimage%=0THEN
        SYS`GdiplusShutdown`,lGDIP%
        ERROR 90,"Couldn't load "+pic$
      ENDIF
      SYS`GdipGetImageWidth`,image%,^ix%
      SYS`GdipGetImageHeight`,image%,^iy%
      SYS`GdipCreateFromHDC`,@memhdc%,^gfx%
      IFgfx%=0 ERROR 90,"GdipCreateFromHDC failed"
      SYS`GdipSetSmoothingMode`,gfx%,2
      SYS`GdipDrawImageRectRectI`,gfx%,image%,xpos%,ypos%,xsize%,ysize%,0,0,ix%,iy%,2,0,0,0
      SYS`GdipDeleteGraphics`,gfx%
      SYS`GdipDisposeImage`,image%
      SYS`GdiplusShutdown`,lGDIP%
      ENDPROC
mikeg
Posts: 101
Joined: Sat 23 Jun 2018, 19:52

Re: Image display question

Post by mikeg »

This code is more or less taken straight from the help file for BB4W, which probably means that it originated with Richard. It works for me. It does require the gdiplib library to be installed.
Thanks. I appreciate the assistance. I have decided to use BBCSDL for creating the animations I am doing.
(except for the ones I posted recently of course.)

Using BBCSDL I can resize the images and not need to convert them, so that will save me a lot of time.
Focus is on code subject. Feel free to judge the quality of my work.