|
Post by Walt Decker on Mar 23, 2021 12:00:49 GMT -5
Is there an LB method to obtain the pixel color at point x, y?
|
|
|
Post by Rod on Mar 23, 2021 14:00:45 GMT -5
No native method. The standard workround uses GetPixel() API function. There are examples posted. But it is SLOW! The other technique is to open the .bmp directly and just access the color quads or triplets. That can be a lot faster. Again there are examples posted.
|
|
|
Post by Walt Decker on Mar 24, 2021 8:38:43 GMT -5
That is what I thought. I do not see a flood-fill method either. Looks like I will have to float my own for both. Thank you
PS:
print #handle, "getbmp bmpName x y width height"
Does this mean that the bitmap is loaded into a memory bitmap from a file or that it creates a memory bitmap from the graphic control beginning at X, Y to width, height and gives it the name of bmpName?
|
|
|
Post by Rod on Mar 24, 2021 9:22:04 GMT -5
Yes the command gets a copy of the screen area named and creates a new graphic in memory. Can be saved with savebmp.
|
|
|
Post by Rod on Mar 24, 2021 10:12:00 GMT -5
For what it is worth this is file based getpixel / setpixel Can be a lot faster than API if trawling through lots of pixels. My getpixel function is a bit wierd because it fills three globals for Red Green and Blue. It could easily return one long value but Liberty needs three values for most of its color commands. It will trash the image you give it so take care.
WindowWidth = 700 WindowHeight = 400 UpperLeftX = (DisplayWidth-WindowWidth)/2 UpperLeftY = (DisplayHeight-WindowHeight)/2 graphicbox #1.gb1,10,10,300,300 graphicbox #1.gb2,330,10,300,300 open "SET GET Pixel" for graphics_nf_nsb as #1 #1 "trapclose [quit]" global bmpw,bmph,padding,offset,bytes,Blue,Green,Red
[loadbmp] filedialog "Choose an image","*.bmp",file$ if file$<>"" then 'display the original bmp in gb1 loadbmp "original",file$ #1.gb1 "down ; drawbmp original 0 0"
'now open the same bmp file for binary read/write access open file$ for binary as #bmp
'analyse the file header bytes seek #bmp,10 'picture data offset, where the color data starts 4 bytes of data offset=asc(input$(#bmp,1))+asc(input$(#bmp,1))*256+asc(input$(#bmp,1))*65536+asc(input$(#bmp,1))*16777216 seek #bmp,18 'width 4 bytes of data bmpw=asc(input$(#bmp,1))+asc(input$(#bmp,1))*256+asc(input$(#bmp,1))*65536+asc(input$(#bmp,1))*16777216 seek #bmp,22 'height 4 bytes of data bmph=asc(input$(#bmp,1))+asc(input$(#bmp,1))*256+asc(input$(#bmp,1))*65536+asc(input$(#bmp,1))*16777216 seek #bmp,28 'bits per pixel, ie color depth 2 bytes of data bits=asc(input$(#bmp,1))+asc(input$(#bmp,1))*256
'work out start of picture data and how to move through file pointer=offset
'work out how many bytes in the bits per pixel 24=3 bgr 32=4 abgr bytes=bits/8
'work out padding each raster line must be a 4byte multiple mult=bits/8*bmpw/4 padding = 4*(1-(mult-int(mult))) mod 4 close #bmp
end if
[setall] 'this demo updates every pixel of the image open file$ for binary as #bmp
for y=1 to bmph for x=1 to bmpw
'run through binary file data moving the file pointer as we go seek #bmp, pointer
'get pixel color stored as BGR Liberty needs RGB b=asc(input$(#bmp,1)) g=asc(input$(#bmp,1)) r=asc(input$(#bmp,1))
'change the color r=r-150 if r<0 then r=0
'set pixel color 'because we read three bytes we need to set the file pointer back seek #bmp, pointer #bmp chr$(b);chr$(g);chr$(r);
'move on pointer=pointer+bytes next
'add padding at end of raster line pointer=pointer+padding next
'note we need to close the file to have Windows 'write the last update close #bmp gosub [show]
[setpixel] 'this demo sets individual pixels via function setpixel() 'open temporary file for read and write access open file$ for binary as #bmp for n=0 to bmpw-1 'zero index null=setpixel(n,n,0,0,0) 'x,y,r,g,b next close #bmp gosub [show]
[getpixel] 'this demo retrieves the RBG of a pixel via function getpixel() 'open temporary file for read and write access open file$ for binary as #bmp 'run along top line for n=0 to bmpw-1 'zero index null=getpixel(n,0) 'x,y 'the global RGB variables are filled print Red,Green,Blue next close #bmp
wait
[show] 'show what we did, load file as bmp and display loadbmp "newfile",file$ #1.gb2 "down ; drawbmp newfile 0 0" return
[quit] close #1 end
function setpixel(x,y,r,g,b) 'raster lines are stored bottom up so we invert y y=(bmph-1)-y pointer=offset+x*bytes+y*(bmpw*bytes+padding) 'set pixel color seek #bmp, pointer #bmp chr$(b);chr$(g);chr$(r); end function
function getpixel(x,y) 'raster lines are stored bottom up so we invert y y=(bmph-1)-y pointer=offset+x*bytes+y*(bmpw*bytes+padding) 'get pixel color stored as BGR Liberty needs RGB seek #bmp, pointer Blue=asc(input$(#bmp,1)) Green=asc(input$(#bmp,1)) Red=asc(input$(#bmp,1)) end function
|
|
|
Post by Walt Decker on Mar 27, 2021 8:44:06 GMT -5
Thank you fellows. Sorry for the late reply. I was involved in a small home remodel for the last few days.
I never work directly from an image file simply because it is faster to load the data into memory and work from there.
|
|