Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

bmp color

aeremin
Beginner
686 Views
How to get a color (R,G,B) of a given pixel in a bitmap using VF6.6?
0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
686 Views
In what format is the bitmap presented to you? A bmp file? A bitmap handle? Something other?

Using Windows APIs, you can load bitmap from a .bmp into a memory DC and operate on it using:
INTEGER:: hBitmap, hScreenDC, hMemoryDC

hBitmap = LoadImage(...LR_LOADFROMFILE)
hScreenDC = GetDC(NULL)  !Get screen DC
hMemoryDC = CreateCompatibleDC(hScreenDC)
iret = ReleaseDC(hScreenDC)
iret = SelectObject(hMemoryDC, hBitmap)
rgbColor = GetPixel(hMemoryDC, iX, iY)
iRed = iand(rgbColor, Z'FF')
iGreen = iand(ishl(rgbColor,-8), Z'FF')
iBlue = iand(ishl(rgbColor,-16), Z'FF')
Note that GetPixel/SetPixel are in general slow -- if you need huge raster operations, another method (operating on a DIB section) would be more suitable.

Jugoslav
0 Kudos
aeremin
Beginner
686 Views
Thanks alot! Yes, I am trying to load a bitmap from a file and extract the colors (R,G,B) as a matrix.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
686 Views
Using DIB Sections could be way faster than Get/SetPixel, but more complicated. Didn't try it myself, but you could
do something like:

- add LR_CREATEDIBSECTION flag to LoadImage
- Call either (with inputs properly filled):
* GetDIBits
* GetObject(hBmp, SIZEOF(DS), LOC(DS)) where DS is of TYPE(T_DIBSECTION)
- lpvBits argument to GetDIBits (in 1st case) or DS%dsBm%bmBits will point to an array containing bitmap pixels.

It's best to use 32-bit format, because you'll get an array of INTEGER(4) containing bitmap pixels. I don't recall the format -- I think it goes (n,1),(n,2),...(n,m),(n-1,1)...(1,m), where n is height and m width. You can't use it as a matrix, but you could index a 1-d array appropriately.

Jugoslav
0 Kudos
Reply