Jump to content

GetPixel Color from BITMAP


 Share

Recommended Posts

Hello everyone.

I'm truing to create simple map converter for my game,

but I did't find eny examples, or at least functions, that allow Pixel search from bitmap.

Only this:

PixelSearch ( left, top, right, bottom, color [, shade-variation [, step [, hwnd]]] )

But this isn't what I need. I don't wnat to create eny window.

Simple: load bitmap, search black pixels and write their positions in to the text file.

Maybe somehow with GDI Plus UDF ?...

Eny Ideas how to do this ?

======

And sorry for my English :)

post-22665-12599559867141_thumb.png

[RU] Zone
Link to comment
Share on other sites

Hi,

quick and dirty....

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <GDIPlusConstants.au3>
#include <StructureConstants.au3>
#include <Array.au3>


$bitmapfile = "pic2.bmp" ;some image with black pixels

$t = TimerInit()
$array = _blackpixel2array_file($bitmapfile, "test.txt") ;write pixelcolours from an image-file into an array and black pixel koordinates into a file
$m = TimerDiff($t)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $m = ' & $m & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


Func _blackpixel2array_file($bmpfile, $textfile) ;returns a 2d array with the colours BGR
    _GDIPlus_Startup()
    $hfile = FileOpen($textfile, 2)
    $pBitmap = _GDIPlus_BitmapCreateFromFile($bmpfile)
    If @error Then MsgBox(0, "", "Fehler BitmapCreateFromFile")
    ;_GDIPlus_BitmapLockBits gibt $tagGDIPBITMAPDATA-Struktur zurück
    $BitmapData = _GDIPlus_BitmapLockBits($pBitmap, 0, 0, _GDIPlus_ImageGetWidth($pBitmap), _GDIPlus_ImageGetHeight($pBitmap), $GDIP_ILMREAD, $GDIP_PXF24RGB)
    If @error Then MsgBox(0, "", "Error locking region " & @error)
    $stride = DllStructGetData($BitmapData, "Stride");Stride - Offset, in bytes, between consecutive scan lines of the bitmap. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.
    $Width = DllStructGetData($BitmapData, "Width");Image width - Number of pixels in one scan line of the bitmap.
    $Height = DllStructGetData($BitmapData, "Height");Image height - Number of scan lines in the bitmap.
    ;$pixelFormat = DllStructGetData($BitmapData, "PixelFormat");Pixel format - Integer that specifies the pixel format of the bitmap
    $Scan0 = DllStructGetData($BitmapData, "Scan0");Scan0 - Pointer to the first (index 0) scan line of the bitmap.
    $pixeldata = DllStructCreate("byte[" & (Abs($stride) * ($Height)) & "]", $Scan0)
    $BMPData = StringTrimLeft(DllStructGetData($pixeldata, 1), 2) ;string im Struct-format, d.h. Byte+nulByte, in dem die pixeldaten im BGR-Format stehen

    Dim $array[$Height + 1][$Width + 1]

    For $y = 0 To $Height - 1 ;to 1 step -1   ;bottomup bitmap, first line in image is last line in data
        For $i = 0 To $stride * 2 Step 6 ;every 3 byte are one pixel
            $array[$y + 1][$i / 6] = StringMid($BMPData, ($y * $stride * 2) + $i + 1, 6) ;colour of the pixel into Array[x][y] with x and y are the koordinates
            If $array[$y + 1][$i / 6] = "000000" Then FileWriteLine($hfile, $i / 6+1 & ";" & $y + 1) ;if colour is black, write x and y-coordinates into file
        Next
    Next
    ;    _arraydisplay($array)
    FileClose($hfile)
    $BMPDataStart = ""
    $pixeldata = 0
    _GDIPlus_BitmapUnlockBits($pBitmap, $BitmapData)
    _GDIPlus_ImageDispose($pBitmap)
    _WinAPI_DeleteObject($pBitmap)
    _GDIPlus_Shutdown()
    Return $array
EndFunc   ;==>_blackpixel2array_file

There are very fast solutions in several dll´s. If you need a faster script, i´ll do my very best^^

/*EDIT*/ coordinates in file correct now

Edited by AndyG
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...