March 29, 2024, 03:23:19 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Bitmap examination

Started by RitchieF, January 19, 2009, 09:44:45 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RitchieF

Hi all,

just for interest and maybe a little program : How to examine a b&w bitmap?

For example I load a b&w bitmap (as result from a scan ) and want to know where can I find it iin the memory ?

How can I scan by a program the bitmap to find out if a pixel is black or white. ?

Is there an origin for counting the pixels (from bottom left to top right or similar ) and find out a specific pattern ?

Does anybody know some urls to give help ?

Thanks in advance !

Richard

Quentin

just an idea.
try to use LOADSPRITE and load your graphic in system ram. You'll get a pointer to the sprite information. I think the graphic data is stored in DIB format, but I'm not relly sure ;). Its like BMP format without header information I guess. You can try to find more information about it in MSDN.

other idea:
load your sprite into the backbuffer and read pixel information with the READPIXEL command.

perhaps someone with more knowledge has better ideas though

RitchieF

Quentin,
thanks for your help. I'll better explain what my idea is:

Here at http://www.free-cross-stitch-pattern.com/index.php you find cross stitch patterns . So now if you select one pattern for download you will see a b&w bitmap and a table which floss to use.
This bitmap shows patterns each one representing the color of a floss to use.
I'd like to 'parse' those patterns and count each one to know how many times it appears.

Then I could use instead of floss small pieces of wall tiles (i.e. 10x10mm or 1/2" x 1/2" size) and create a mosaic on a wall or swimming pool floor or something similar.

Just an idea!

Richard

Quentin

January 22, 2009, 02:12:28 PM #3 Last Edit: January 22, 2009, 02:37:25 PM by Quentin
hmm not really sure if I do understand. Parsing the pattern would be not so easy. It's much easier to parse the b&w bitmap directly

here's a small example on how to use it. The program shows the b&w sprite (its saved as 8 bit bmp, see attached file). The color of the pixel at the mouse hot spot is shown. You can use this information to draw your pattern whenever color is 0. Please read carefully the chapter about direct buffer writing in the 2d help :)

(It's not necessary to draw the sprite in this sample as the color information is read directly from the memory, just for better understanding how it works)


AUTODEFINE "OFF"

DEF picture AS POINTER               
DEF pitch AS UINT               
DEF buffer AS POINTER
DEF bufpos AS POINTER

DEF spritewidth AS UINT
DEF spriteheight AS UINT
DEF pcolor AS CHAR
DEF mx AS INT
DEF my AS INT

'open full screen window
IF CREATESCREEN(800, 600, 32) < 0
MESSAGEBOX 0, "Failed to open screen!", "Error"
END
ENDIF

picture = LOADSPRITE("sample.bmp")   'load the sprite
LOCKSPRITE picture                   'we need to lock it for direct memory access
pitch = GETSPRITEPITCH(picture)      'get the bytes per line of the sample picture
buffer = GETSPRITEPOINTER(picture)   'get the start address of the picture in memory
UNLOCKSPRITE picture
spritewidth = GETSPRITEWIDTH(picture) 'we need the width and the height of the sprite for later use
spriteheight = GETSPRITEHEIGHT(picture)


DO
FILLSCREEN 0                     'clear the screen to black
DRAWSPRITEXY picture, 0, 0       'draw the sample picture

mx = MOUSEX()                'get the mouse coordinates
my = MOUSEY()
IF mx < spritewidth AND my < spriteheight
LOCKSPRITE picture
'now we have to read the color of the pixel at the position of the mouse hotspot
'it depends on the bits per pixel we have set with CREATESCREEN
'here we use 32 bpp so mx must be multiplied with 4
bufpos = buffer + my * pitch + mx * 4
pcolor = #<UINT>bufpos
UNLOCKSPRITE picture
ENDIF

WRITETEXT spritewidth + 10, 0, "x position: " + STR$(mx)
WRITETEXT spritewidth + 10, 30, "y position: " + STR$(my)
WRITETEXT spritewidth + 10, 60, "color:      " + STR$(pcolor)

FLIP

UNTIL GETKEY <> ""

FREESPRITE picture
END



Parker

This code uses a very inefficient way of copying a bitmap pixel by pixel to a window. Once you've loaded the bitmap with all the API stuff, you can use APIGetPixel to look at an individual pixel from the bitmap. (I know I didn't use all of the API declares - they're there just in case they're needed).
window win
openwindow win, 0, 0, 400, 400, @CAPTION | @SYSMENU, 0, "Window", &handler

declare import, CreateDC alias CreateDCA( drv:pointer, dev:pointer, out:pointer, pointer initData ),uint
declare import, CreateCompatibleDC( hdc:uint ),uint
declare import, CreateCompatibleBitmap( hdc:uint, width:int, height:int ),uint
declare import, DeleteObject( obj:uint ),int
declare import, APILoadImage alias LoadImageA( inst:uint, name:pointer, utype:uint, cx:int, cy:int, fuload:uint ),uint
declare import, DeleteDC( dc:uint ),int
declare import, SelectObject( hdc:uint, hgdiobj:uint ),uint
declare import, GetObject alias GetObjectA( hgdiobj:uint, cbBuffer:int, lpvObject:pointer ),int
declare import, APIGetPixel alias GetPixel( hdc:uint, xpos:int, ypos:int ),uint

const LR_LOADFROMFILE = 0x10
const LR_MONOCHROME = 0x1
const IMAGE_BITMAP = 0
const CLR_INVALID = 0xFFFFFFFF

type BITMAP
int bmType
int bmWidth
int bmHeight
int bmWidthBytes
word bmPlanes
word bmBitsPixel
pointer bmBits
endtype

uint hdc, hbmp
uint hwindc
hwindc = GetHDC( win )
hdc = CreateCompatibleDC( hwindc ) 'CreateDC( 0, 0, 0, 0 )
ReleaseHDC( win, hwindc )
hbmp = APILoadImage( 0, "test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_MONOCHROME )
SelectObject( hdc, hbmp )

BITMAP bmp
GetObject( hbmp, len(BITMAP), bmp )
for i = 0 to bmp.bmWidth - 1
for j = 0 to bmp.bmHeight - 1
pset( win, i+15, j+15, APIGetPixel( hdc, i, j ) )
next j
next i

DeleteObject( hbmp )
DeleteDC( hdc )

waituntil iswindowclosed(win)
end

sub handler
select @MESSAGE
case @IDCLOSEWINDOW
closewindow *<WINDOW>@HITWINDOW
endselect
endsub

RitchieF

Thanks to both of you!

I'll study the code and see if I can learn from it.
Anyway it's pointing me in the right direction and I can start with it.

Thanks

Richard

RitchieF

Just as addition:

Here's a link showing what I mean:

http://www.catenary.com/howto/findmark.html

Richard