JB/LB BMPSAVE make 32 bit BMP, LBB 24 bit

Discussions relevant to the differences between LBB and LB4 which could affect code compatibility
tsh73
Posts: 44
Joined: Fri Apr 06, 2018 7:58 pm

JB/LB BMPSAVE make 32 bit BMP, LBB 24 bit

Post by tsh73 »

When trying to run "Laser cut" program I hit this.
Program was initially written for JB
so it saved single pixel BMP -read back that BMP - read that pixel data, returned that as color.
So JB/LB write 32-bit BMP (or 16-bit, if desktop is set to 16 bit)
(may be not always but in my case it was so)
But LBB writes 24-bit BMP. So procedure had to be modified to handle this.
Someone may stumble on this, too - so I post it here.
(later I redid that to use "getpixel" API call, anyway).

Code: Select all

'*****************************************************
'GetPixelValue$ returns a string with the RGB values of the pixel
'in coordinates x and y in window/graphicbox names handle$ (e.g, "#main.graph")
function GetPixelValue$(x, y, handle$)

'Grab a 1*1 bitmap
    #handle$, "getbmp gpv "; x; " "; y; " "; 1; " "; 1

'Save in a bmp file
    bmpsave "gpv", "getpvaluetemp.bmp"

'Open the file for string input and get it's full contents
    open "getpvaluetemp.bmp" for input as #gpv
    s$ = input$(#gpv, lof(#gpv))
    close #gpv

'Check if user's display is 32-bit, and read the red-green-blue values
'If display 16 bit, then colors are masked. So some last (3 for red, 2 for green, 3 for blue) bits always 0
'That means that you did not get 255 255 255 for white - (248 252 248) instead. You have to experiment
'otherwise function returns nothing (support for other display types could be added (?))
    bpp =  asc(mid$(s$, 29, 1))
    select case bpp
    case 32
        red = asc(mid$(s$, 69, 1))
        green = asc(mid$(s$, 68, 1))
        blue = asc(mid$(s$, 67, 1))
    case 24        'LBB: 24 bit, no palette
        red = asc(mid$(s$, 57, 1))
        green = asc(mid$(s$, 56, 1))
        blue = asc(mid$(s$, 55, 1))
        'print red,green, blue
    case 16
        bytes = asc(mid$( s$, 67, 1)) + 256*asc(mid$( s$, 68, 1))
        red =  (bytes AND 63488) /256       '0xF800
        green =  (bytes AND 2016) / 32 * 4  '0x7E0
        blue =  (bytes AND 31) * 8          '0x1F
    end select


'concatenate the return value, delete temporary file and free memory
    GetPixelValue$ = str$(red)+" "+str$(green)+" "+str$(blue)
    on error goto [skip]
    kill "getpvaluetemp.bmp"
[skip]
    unloadbmp "gpv"
end function