Malkey Posted June 5, 2009 Posted June 5, 2009 The CreateASCIIBitmap script sequentially divides up the ASCII equivalent value of each character of a text file into groups of three. The three ASCII numeric values make up the three colour channels of a pixel (RGB).The resulting string of pixels are made into a bitmap.A colour matrix is applied to the bitmap. The purpose of this is mainly for enciphering (encoding) the plain pixels to encipher pixels. This also adds or changes the overall colour of the image. The colour depends on the values used in the colour matrix. The image can be saved as a BMP or PNG image file. A resultant BMP image file is the same size as the TXT file used. A PNG file is not the same size.The BitmapToASCII script loads the previously created image file.An inverse matrix of the colour matrix used to create image, is applied to loaded bitmap. This deciphers (decodes) back to the original plain pixels.The pixel colour channels values are converted back to ASCII text and pasted to NotePad.Enjoy.This PNG is an example encrypted text file.CreateASCIIBitmap expandcollapse popup; #include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Misc.au3> #include <string.au3> #include <WindowsConstants.au3> Opt('MustDeclareVars', 1) Global $hGui, $hImage, $hImage2, $hGraphic1, $hGraphicGUI, $hBMPBuff, $iW, $iH, $sString, $Path Global $aM[5][5] = [[1, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1, 0],[0.3, 0.2, 0.4, 0, 1]] $sString = FileRead("C:\Program Files\Autoio\Examples\matrixCalc\MatrixUDF_Examples\Matrix.txt") $Path = FileOpenDialog("Choose Image File", @DesktopDir & "", _ "Images (*.txt;*.au3;*.ini;*.log)| All (*.*)", 1, "Test.txt") If Not @error Then $sString = FileRead($Path) _CreateASCIIBitmap($sString) Else Exit EndIf Func _CreateASCIIBitmap($sString) Local $Num, $SqrNum, $Extrachars, $sDat, $hBitmap1, $Reslt, $iSize, $hGraphic, $width, $height Local $stride, $format, $Scan0, $v_BufferA, $AllPixels, $strLen, $sString1, $sDat, $tMP Local $strLen = StringLen($sString) $sString &= _StringRepeat(" ", (3 - Mod($strLen, 3)) * (Mod($strLen, 3) <> 0)) $strLen = StringLen($sString) $Num = $strLen / 3;Num of Pixels $SqrNum = Int(Sqrt($Num)) $iW = $SqrNum $iH = $SqrNum Do If $iW <= $iH Then $iW += 1 Else $iH += 1 EndIf If (Mod($iW * $iH, 12) = 0 And $iW * $iH >= $Num) Then $Extrachars = (($iW * $iH) - $Num) * 3 Until (Mod($iW * $iH, 12) = 0 And $iW * $iH >= $Num) $sString &= _StringRepeat(" ", $Extrachars) ConsoleWrite($iW & "x" & $iH & " " & Mod(StringLen($sString), 3) & " srtLen = " & StringLen($sString) & @CRLF) $sString1 = StringToBinary($sString) ; Create a GUI for the original image $hGui = GUICreate("Text as ACSII Image", $iW, $iH, -1, -1);, $WS_CAPTION) GUISetState() GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3) ; Initialize GDI+ library and load image _GDIPlus_Startup() ; Draw original image $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui) $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphicGUI) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff) $hBitmap1 = _GDIPlus_BitmapCloneArea($hBMPBuff, 0, 0, $iW, $iH, $GDIP_PXF24RGB) ; Locks a portion of a bitmap for reading or writing $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iW, $iH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF24RGB);$GDIP_PXF32RGB ) ;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") ConsoleWrite("$width " & $width & @CRLF & _ "$height " & $height & @CRLF & _ "$stride " & $stride & @CRLF & _ "$format " & $format & @CRLF) $v_BufferA = DllStructCreate("byte[" & $width * $height * 3 & "]", $Scan0); Create DLL structure for all pixels $AllPixels = DllStructSetData($v_BufferA, 1, $sString1) _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt); releases the locked region ;_GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap1, 0, 0) _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphic, $hBitmap1, 0, 0) _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0) Do $tMP = _WinAPI_GetMousePos() If _WinAPI_WindowFromPoint($tMP) = WinGetHandle($hGui) Then ToolTip("Press Ctrl + Left mouse click to save image", DllStructGetData($tMP, "X") + 5, DllStructGetData($tMP, "Y") - 20) Else ToolTip("") EndIf If _IsPressed("01") And _IsPressed("11") Then;Left click + Ctrl key to save image Do Until Not _IsPressed("01") Local $PathFile = FileSaveDialog("Choose Image File Name", @DesktopDir & "", _ "Images (*.png;*.bmp)| All (*.*)", 1, "AsciiBitmap.png") If Not @error Then _GDIPlus_ImageSaveToFile($hBMPBuff, $PathFile) ShellExecute($PathFile) EndIf EndIf Sleep(10) Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Release resources _WinAPI_DeleteObject($hBitmap1) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_GraphicsDispose($hGraphicGUI) _WinAPI_DeleteObject($hBMPBuff) _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hImage2) _GDIPlus_Shutdown() EndFunc ;==>_CreateASCIIBitmap Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _ $iDstX = "", $iDstY = "", $iDstWidth = "", $iDstHeight = "", $iUnit = 2) Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage) If $iSrcWidth = 0 Or $iSrcWidth = "" Then $iSrcWidth = $iW If $iSrcHeight = 0 Or $iSrcHeight = "" Then $iSrcHeight = $iH If $iDstX = "" Then $iDstX = $iSrcX If $iDstY = "" Then $iDstY = $iSrcY If $iDstWidth = "" Then $iDstWidth = $iSrcWidth If $iDstHeight = "" Then $iDstHeight = $iSrcHeight If $iUnit = "" Then $iUnit = 2 ;;create color matrix data $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]") $x = DllStructSetData($tColorMatrix, 1, $aM[0][0], 1) * DllStructSetData($tColorMatrix, 1, $aM[0][1], 2) * DllStructSetData($tColorMatrix, 1, $aM[0][2], 3) * _ DllStructSetData($tColorMatrix, 2, $aM[1][0], 1) * DllStructSetData($tColorMatrix, 2, $aM[1][1], 2) * DllStructSetData($tColorMatrix, 2, $aM[1][2], 3) * _ DllStructSetData($tColorMatrix, 3, $aM[2][0], 1) * DllStructSetData($tColorMatrix, 3, $aM[2][1], 2) * DllStructSetData($tColorMatrix, 3, $aM[2][2], 3) * _ DllStructSetData($tColorMatrix, 4, $aM[3][3], 4) * DllStructSetData($tColorMatrix, 5, $aM[4][0], 1) * DllStructSetData($tColorMatrix, 5, $aM[4][1], 2) * _ DllStructSetData($tColorMatrix, 5, $aM[4][2], 3) * DllStructSetData($tColorMatrix, 5, $aM[4][3], 4) * DllStructSetData($tColorMatrix, 5, $aM[4][4], 5) $hImgAttrib = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0) $hImgAttrib = $hImgAttrib[1] DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _ "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0) ;;draw image into graphic object with alpha blend DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _ $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _ $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0) ;;clean up DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib) Return EndFunc ;==>_GDIPlus_GraphicsDrawImageRectRectTrans Func MY_PAINT($hWnd, $msg, $wParam, $lParam) If $hWnd = $hGui Then _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphicGUI, $hBMPBuff, 0, 0);_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0) Return $GUI_RUNDEFMSG EndFunc ;==>MY_PAINT ;BitmapToASCIIexpandcollapse popup; #include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Misc.au3> #include <string.au3> #include <WindowsConstants.au3> #include <Array.au3> Opt('MustDeclareVars', 1) Global $hGui, $hImage, $hImage2, $hGraphic1, $hGraphicGUI, $hBMPBuff, $iW, $iH ;Global $aM[5][5] = [[1, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1, 0],[0.3, 0.2, 0.4, 0, 1]] Global $aInv[5][5] = [[1, 0, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 0, 1 , 0],[-0.3, -0.2, -0.4, 0, 1]] Global $aInv _BitmapToASCII() Func _BitmapToASCII() Local $Num, $SqrNum, $Extrachars, $sDat, $hBitmap1, $Reslt, $iSize, $hGraphic, $width, $height Local $stride, $format, $Scan0, $v_BufferA, $AllPixels, $hbmp Local $Path = FileOpenDialog("Choose Image File", @DesktopDir & "", _ "Images (*.png;*.bmp)| All (*.*)", 1, "AsciiBitmap.png") If Not @error Then _GDIPlus_Startup() Local $hImage = _GDIPlus_ImageLoadFromFile($Path) Else Exit EndIf $iW = _GDIPlus_ImageGetWidth($hImage) $iH = _GDIPlus_ImageGetHeight($hImage) ; Create a GUI for the original image $hGui = GUICreate("Text as ACSII Image", $iW, $iH, -1, -1);, $WS_CAPTION) GUISetState() GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3) ; Initialize GDI+ library and load image _GDIPlus_Startup() ;$hImage = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & "\GDIPlus_Image.png") ; Draw original image $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui) $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphicGUI) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff) ;_GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0) _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphic, $hImage, 0, 0) _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0) $hBitmap1 = _GDIPlus_BitmapCloneArea($hBMPBuff, 0, 0, $iW, $iH, $GDIP_PXF24RGB) ; Locks a portion of a bitmap for reading or writing $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iW, $iH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF24RGB) ;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") ;ConsoleWrite("$width " & $width & @CRLF & _ ;"$height " & $height & @CRLF & _ ;"$stride " & $stride & @CRLF & _ ;"$format " & $format & @CRLF) $v_BufferA = DllStructCreate("byte[" & $width * $height * 3 & "]", $Scan0); Create DLL structure for all pixels Local $sDat = DllStructGetData($v_BufferA, 1) ;ConsoleWrite( $sDat & @CRLF) $sDat = StringRegExpReplace($sDat, "(.{2})", '\1 ') $sDat = StringRegExpReplace($sDat, "(00)", '') $sDat = StringRegExpReplace($sDat, "(20 )*$", '') ;ConsoleWrite( $sDat & @CRLF) $sDat = StringStripWS($sDat, 8) ;ConsoleWrite( $sDat & @CRLF) Local $str1 = BinaryToString($sDat) ;ConsoleWrite(" StrLen = " & StringLen($str1) & @CRLF & $str1 & @CRLF) _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt); releases the locked region ClipPut($str1) Run("notepad.exe") WinActivate("[Class:Notepad]") WinWaitActive("[Class:Notepad]", "", 3) ControlSend("[Class:Notepad]", "", "Edit1", "^v") ; Loop until user exits Do If _IsPressed("01") And _IsPressed("11") Then;Left click + Ctrl key to save image Do Until Not _IsPressed("01") Local $PathFile = FileSaveDialog("Choose Image File Name", @DesktopDir & "", _ "Images (*.png;*.bmp)| All (*.*)", 1, "AsciiBitmap.png") If Not @error Then _GDIPlus_ImageSaveToFile($hBitmap1, $PathFile) ShellExecute($PathFile) EndIf EndIf Sleep(10) Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Release resources _WinAPI_DeleteObject($hBitmap1) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_GraphicsDispose($hGraphicGUI) _WinAPI_DeleteObject($hBMPBuff) _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hImage2) _GDIPlus_Shutdown() EndFunc ;==>_BitmapToASCII Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _ $iDstX = "", $iDstY = "", $iDstWidth = "", $iDstHeight = "", $iUnit = 2) Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage) If $iSrcWidth = 0 Or $iSrcWidth = "" Then $iSrcWidth = $iW If $iSrcHeight = 0 Or $iSrcHeight = "" Then $iSrcHeight = $iH If $iDstX = "" Then $iDstX = $iSrcX If $iDstY = "" Then $iDstY = $iSrcY If $iDstWidth = "" Then $iDstWidth = $iSrcWidth If $iDstHeight = "" Then $iDstHeight = $iSrcHeight If $iUnit = "" Then $iUnit = 2 ;;create color matrix data $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]") $x = DllStructSetData($tColorMatrix, 1, $aInv[0][0], 1) * DllStructSetData($tColorMatrix, 1, $aInv[0][1], 2) * DllStructSetData($tColorMatrix, 1, $aInv[0][2], 3) * _ DllStructSetData($tColorMatrix, 2, $aInv[1][0], 1) * DllStructSetData($tColorMatrix, 2, $aInv[1][1], 2) * DllStructSetData($tColorMatrix, 2, $aInv[1][2], 3) * _ DllStructSetData($tColorMatrix, 3, $aInv[2][0], 1) * DllStructSetData($tColorMatrix, 3, $aInv[2][1], 2) * DllStructSetData($tColorMatrix, 3, $aInv[2][2], 3) * _ DllStructSetData($tColorMatrix, 4, $aInv[3][3], 4) * DllStructSetData($tColorMatrix, 5, $aInv[4][0], 1) * DllStructSetData($tColorMatrix, 5, $aInv[4][1], 2) * _ DllStructSetData($tColorMatrix, 5, $aInv[4][2], 3) * DllStructSetData($tColorMatrix, 5, $aInv[4][3], 4) * DllStructSetData($tColorMatrix, 5, $aInv[4][4], 5) $hImgAttrib = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0) $hImgAttrib = $hImgAttrib[1] DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _ "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0) ;;draw image into graphic object with alpha blend DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _ $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _ $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0) ;;clean up DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib) Return EndFunc ;==>_GDIPlus_GraphicsDrawImageRectRectTrans Func MY_PAINT($hWnd, $msg, $wParam, $lParam) If $hWnd = $hGui Then _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0) Return $GUI_RUNDEFMSG EndFunc ;==>MY_PAINT ;
monoceres Posted June 5, 2009 Posted June 5, 2009 Very clever Malkey. I love these kind of stuff Broken link? PM me and I'll send you the file!
Szhlopp Posted June 6, 2009 Posted June 6, 2009 Very clever Malkey. I love these kind of stuff Indeed. @OPYou did a way better job of this than I did lol... I created something that did the same exact thing (Bah deleted attachment here...). I was just starting to code and didn't no squat about GDI or dll calling to get some of the pixel data. Was really slow and sluggish, but it worked RegEx/RegExRep Tester!Nerd Olympics - Community App!Login UDFMemory UDF - "Game.exe+753EC" - CE pointer to AU3Password Manager W/ SourceDataFiler - Include files in your au3!--- Was I helpful? Click the little green '+'
timgames Posted June 6, 2009 Posted June 6, 2009 (edited) It doesn't work for me, when I convert it back I get something else then what the input was.first I use the "CreateASCIIBitmap" then it creates a png, and then I convert it back with "BitmapToASCII" and then I get the wrong string back.I might do it wrong?Nevermind I fixed it, it really looks nice Edited June 6, 2009 by timgames
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