boogieoompa Posted December 19, 2012 Share Posted December 19, 2012 I must have been looking at this too long but I cannot figure out where I am making a mistake. Basically I am making a program that loads a bitmap into a 2d array, transforms the data (this color = this other color) and re writes the whole bitmap as a 2nd file. Basically I am trying to strip out pieces of the bitmap I dont want, so I will be turning certain pixels white if they dont meet a very specific criteria. Anyways when this writes to the 2nd file it only writes the first line. So I open up the 2nd image and only have one line on the top of the screen. I assume the issue lies somewhere in this loop... $i = 1 $j = 1 While $j < $arrayheight While $i < $arraywidth _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, 0x & $aArr[$i][$j]) $i = $i + 1 WEnd $j = $j + 1 WEnd But I cannot find why $j is not advancing. When I tried to debug this it looked like it should be working (I put a msg box in to display the string representation of the array parameters and it seemed to be fine). I'm sure this has to be some dumb easy mistake but its been like 5 hours and I cant find it. Here is the full code... expandcollapse popup#include <Array.au3> #include <GDIPlus.au3> Local $filename, $begin, $aArr, $time Local $filename = FileOpenDialog("Select image", @ScriptDir, "All images (*.jpg;*.png;*.gif;*.bmp;)", 1) If $filename = "" Then Exit ShellExecute($filename) $begin = TimerInit() _FileImageToArray($filename, $aArr) $time = Round(TimerDiff($begin) / 1000, 3) ;_ArrayDisplay($aArr) _ImageDraw() Func _FileImageToArray($sFileName, ByRef $aArray) Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hImage Local $v_Buffer, $width, $height _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($sFileName) $iIW = _GDIPlus_ImageGetWidth($hImage) $iIH = _GDIPlus_ImageGetHeight($hImage) ProgressOn("Progress Bar", "Filling a " & $iIW & " x " & $iIH & " size array.", "0 percent") $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iIW, $iIH, $GDIP_ILMREAD, $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") Dim $aArray[$height][$width] For $j = 0 To $iIH - 1 For $i = 0 To $iIW - 1 $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4)) $aArray[$j][$i] = Hex(DllStructGetData($v_Buffer, 1), 6) Next ProgressSet(Int(100 * $j / ($iIH)), Int(100 * $j / ($iIH)) & " percent") Next _GDIPlus_BitmapUnlockBits($hImage, $Reslt) ProgressOff() _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() Return EndFunc ;==>_FileImageToArray Func _ImageDraw() _GDIPlus_Startup() $arraywidth = Ubound($aArr,2) $arrayheight = Ubound($aArr,1) $hBitmap = _GDIPlus_BitmapCreateFromScan0($arraywidth, $arrayheight) $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsSetSmoothingMode($hContext, 2) _GDIPlus_GraphicsClear($hContext, 0xFFFFFFFF) $i = 1 $j = 1 While $j < $arrayheight While $i < $arraywidth _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, 0x & $aArr[$i][$j]) $i = $i + 1 WEnd $j = $j + 1 WEnd ; _GDIPlus_BitmapSetPixel($hBitmap, 251, 257, 0x7b7b7b) _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Test.BMP") _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_GraphicsDispose($hContext) _GDIPlus_Shutdown() ShellExecute(@ScriptDir & "\Test.BMP") EndFunc Func _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iARGB = 0xFFFFFFFF) Local $aRet $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "dword", $iARGB) Return EndFunc ;==>_GDIPlus_BitmapSetPixel Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0) Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0) If @error Then Return SetError(@error, @extended, 0) Return $aResult[6] EndFunc ;==>_GDIPlus_BitmapCreateFromScan Thanks! Link to comment Share on other sites More sharing options...
Developers Jos Posted December 19, 2012 Developers Share Posted December 19, 2012 (edited) Haven't you swapped the $I and $J in this Array statement? _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, 0x & $aArr[$i][$j]) Jos Edited December 19, 2012 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boogieoompa Posted December 19, 2012 Author Share Posted December 19, 2012 Great eye! But that is actually just a mistake. Before I sent this over I was trying to flip those variables to see if I would get the same result and I did. Just rotated 90 degrees. So yes its a mistake but its an intentional one that didnt effect the loop. Thanks Link to comment Share on other sites More sharing options...
boogieoompa Posted December 19, 2012 Author Share Posted December 19, 2012 So the weird thing is $i = 1 $j = 1 While $j < $arrayheight While $i < $arraywidth ; _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, "0xFF" & $aArr[$i][$j]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 1, "0xFF" & $aArr[$i][1]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 2, "0xFF" & $aArr[$i][2]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 3, "0xFF" & $aArr[$i][3]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 4, "0xFF" & $aArr[$i][4]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 5, "0xFF" & $aArr[$i][5]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 6, "0xFF" & $aArr[$i][6]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 7, "0xFF" & $aArr[$i][7]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 8, "0xFF" & $aArr[$i][8]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 9, "0xFF" & $aArr[$i][9]) _GDIPlus_BitmapSetPixel($hBitmap, $i, 10, "0xFF" & $aArr[$i][10]) $i = $i + 1 WEnd $j = $j + 1 WEnd Works but when I have $j populated by a loop it doesn't. Even when I put in a msgbox to tell me what $j's status is per loop. I also noted that my color pallet need to change for the translation so I added "0xFF" (FF for opaque) to convert the hex to something recognizable. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 19, 2012 Developers Share Posted December 19, 2012 And when you do it this way which simulates your hardcoded syntax? $i = 1 $j = 1 While $i < $arraywidth While $j < $arrayheight _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, "0xFF" & $aArr[$i][$j]) $j = $j + 1 WEnd $i = $i + 1 WEnd SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boogieoompa Posted December 19, 2012 Author Share Posted December 19, 2012 Yes and that is why I am so confused. No matter what i test it looks like it should OK but it seem like the loop refuses to recognize the +1 in the _GDIPlus_BitmapSetPixel call. It will take it if I hardcode it, it will take it if I put it in a msg box. You can test it yourself rather easily just open up any bitmap file when you run it and you will see 1 line drawn, really weird. Incedently the picture is rotated 90 degrees but 1 problem at a time I suppose (that ones easy to fix). I know this has to be something easy I just dont see it. Link to comment Share on other sites More sharing options...
JohnOne Posted December 19, 2012 Share Posted December 19, 2012 (edited) Does not $j need to be reset after it's height loop, ot maybe $i, I'm not sure loops confuse me. $j1Start = 1 $i = 1 $j = 1 $Complete = 0 While $i < $arraywidth While $j < $arrayheight _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, "0xFF" & $aArr[$i][$j]) $j = $j + 1 WEnd $j1Start += 1 $j = $j1Start ;Or Just $j = 1 $i = $i + 1 WEnd Edited December 19, 2012 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 19, 2012 Developers Share Posted December 19, 2012 You are right JohnOne .... something like this should be better $i = 1 While $i < $arraywidth $j=1 While $j < $arrayheight _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, "0xFF" & $aArr[$i][$j]) $j = $j + 1 WEnd $i = $i + 1 WEnd SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
BrewManNH Posted December 20, 2012 Share Posted December 20, 2012 Actually, wouldn't a couple of nested For loops be simpler to troubleshoot? If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
UEZ Posted December 20, 2012 Share Posted December 20, 2012 Here another way to change the color of a bitmap: You can define an array for searching and replacing color ($aRemapTable). Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
boogieoompa Posted December 20, 2012 Author Share Posted December 20, 2012 Thank you all very much for your help, John was right and I needed to reset the X coordinate line to 1. My loop was in fact writing to the next Y line but the X coordinates were out of the matrix. Thank you all again for your help! Link to comment Share on other sites More sharing options...
boogieoompa Posted December 20, 2012 Author Share Posted December 20, 2012 Here is the final working result. Perhaps someone else can find use for it. Basically it opens an image, loads it into a 2d array, does something to it (_Pixel_Translate) and copies it pixel by pixel into a new file. Thanks again everyone! expandcollapse popup#include <Array.au3> #include <GDIPlus.au3> Local $filename, $begin, $aArr, $time Local $filename = FileOpenDialog("Select image", @ScriptDir, "All images (*.jpg;*.png;*.gif;*.bmp;)", 1) If $filename = "" Then Exit ShellExecute($filename) $begin = TimerInit() _FileImageToArray($filename, $aArr) $time = Round(TimerDiff($begin) / 1000, 3) ;_ArrayDisplay($aArr) _ImageDraw() Func _FileImageToArray($sFileName, ByRef $aArray) Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hImage Local $v_Buffer, $width, $height _GDIPlus_Startup() $hImage = _GDIPlus_ImageLoadFromFile($sFileName) $iIW = _GDIPlus_ImageGetWidth($hImage) $iIH = _GDIPlus_ImageGetHeight($hImage) ProgressOn("Progress Bar", "Filling a " & $iIW & " x " & $iIH & " size array.", "0 percent") $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iIW, $iIH, $GDIP_ILMREAD, $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") Dim $aArray[$height][$width] For $j = 0 To $iIH - 1 For $i = 0 To $iIW - 1 $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4)) $aArray[$j][$i] = Hex(DllStructGetData($v_Buffer, 1), 6) Next ProgressSet(Int(100 * $j / ($iIH)), Int(100 * $j / ($iIH)) & " percent") Next _GDIPlus_BitmapUnlockBits($hImage, $Reslt) ProgressOff() _GDIPlus_ImageDispose($hImage) _GDIPlus_Shutdown() Return EndFunc ;==>_FileImageToArray Func _ImageDraw() _GDIPlus_Startup() $arraywidth = Ubound($aArr,2) $arrayheight = Ubound($aArr,1) $hBitmap = _GDIPlus_BitmapCreateFromScan0($arraywidth, $arrayheight) $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsSetSmoothingMode($hContext, 2) _GDIPlus_GraphicsClear($hContext, 0xFFFFFFFF) $i = 0 While $i < $arraywidth $j = 0 While $j < $arrayheight $newpixel = _Pixel_Translate($aArr[$j][$i]) _GDIPlus_BitmapSetPixel($hBitmap, $i, $j, $newpixel) $j = $j + 1 WEnd $i = $i + 1 WEnd _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "Test.BMP") _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_GraphicsDispose($hContext) _GDIPlus_Shutdown() ShellExecute(@ScriptDir & "Test.BMP") EndFunc Func _Pixel_Translate($pixel) ;put whatever translation you want. This one just converts it to a displayable GDI format $pixel = "0xFF" & $pixel Return $pixel EndFunc Func _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iARGB = 0xFFFFFFFF) Local $aRet $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "dword", $iARGB) Return EndFunc ;==>_GDIPlus_BitmapSetPixel Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0) Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0) If @error Then Return SetError(@error, @extended, 0) Return $aResult[6] EndFunc ;==>_GDIPlus_BitmapCreateFromScan0 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now