Sign in to follow this  
Followers 0
rapot

Convert a BufferedImage to a Byte Array as JPEG GrayScale

6 posts in this topic

How can I use a BufferedImage as an array of byte? :)

Local $aImage[$Height][$Width]

Please help me ;)

Thank

Share this post


Link to post
Share on other sites



Hi

BufferedImage in there program is $hImage2

#include <GDIP.au3>

_GDIPlus_Startup()

$FileName = FileOpenDialog("Select an image", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.gif)", 1)

$hImage1 = _GDIPlus_ImageLoadFromFile($FileName)
$hImage2 = _GDIPlus_ImageGreyscale($hImage1)

$Width = _GDIPlus_ImageGetWidth($hImage2)
$Height = _GDIPlus_ImageGetHeight($hImage2)
$hGUI = GUICreate("Convert image to greyscale", $Width, $Height)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $Width, $Height)
_GDIPlus_ImageDispose ($hImage1)
_GDIPlus_ImageDispose ($hImage2)
_GDIPlus_ShutDown ()

While GUIGetMsg() <> -3 * Sleep(50)
WEnd

I want create an array like $aImage[$Height][$Width] from $hImage2

Someone please help me :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Like this >>

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GDI.au3>

_GDIPlus_Startup()

Global $FileName = FileOpenDialog("Select an image", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.gif)", 1)

Global $hImage1 = _GDIPlus_ImageLoadFromFile($FileName)
Global $hImage2 = _GDIPlus_ImageGreyscale($hImage1)

Global $Width = _GDIPlus_ImageGetWidth($hImage2)
Global $Height = _GDIPlus_ImageGetHeight($hImage2)
Global $aDimension[2] = [$Width, $Height]; <<<<< I think this is what you were asking for?!
Global $hGUI = GUICreate("Convert image to greyscale", $aDimension[0], $aDimension[1]) ; <<<<< Changed for the Array variables.
GUISetState()

Global $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $aDimension[0], $aDimension[1]) ; <<<<< Changed for the Array variables.
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_Shutdown()

While GUIGetMsg() <> -3 * Sleep(50)
WEnd
Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Do you mean something like this:

;coded by UEZ 2011
#include <Array.au3>
#include <GDIPlus.au3>

$file = FileOpenDialog("Select an image to convert it to greyscale", "", "Images (*.jpg;*.png;*.bmp;*.gif;*.tif)", 1)
If @error Then Exit

_GDIPlus_Startup ()
$hImage = _GDIPlus_ImageLoadFromFile($file)
$hContext = _GDIPlus_ImageGetGraphicsContext($hImage)
_Greyscale($hImage, $hContext)

;~ $newfile = StringRegExpReplace($file, "(.*)\.(.+)", "$1") & "_Grey.jpg"
;~ _GDIPlus_ImageSaveToFile($hImage, $newfile)

$aDim = _GDIPlus_ImageGetDimension($hImage)
$width = $aDim[0]
$height = $aDim[1]
$hGUI = GUICreate("Convert image to greyscale", $width, $height)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $width, $height)

Dim $aImage[$height][$width]
For $y = 0 To $height - 1
    For $x = 0 To $width - 1
        $aImage[$y][$x] = "0x" & Hex(_GDIPlus_BitmapGetPixel($hImage, $x, $y), 8)
    Next
Next

_ArrayDisplay($aImage, "Array with pixel information")

While GUIGetMsg() <> -3
WEnd

_GDIPlus_GraphicsDispose($hContext)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
GUIDelete($hGUI)

Exit

Func _Greyscale($hImage, $hImageContext, $rl = 0.3086, $gl = 0.6094, $bl = 0.0820)
    Local $tNegMatrix, $pNegMatrix, $hIA
    $hIA = _GDIPlus_ImageAttributesCreate()
    $aImageSize = _GDIPlus_ImageGetDimension($hImage)
    If $hImage Then
        $tNegMatrix = _GDIPlus_ColorMatrixCreateGrayScale($rl, $gl, $bl)
        $pNegMatrix = DllStructGetPtr($tNegMatrix)
        _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $pNegMatrix)
        _GDIPlus_GraphicsDrawImageRectRectIA($hImageContext, $hImage, 0, 0, $aImageSize[0], $aImageSize[1], 0, 0, $aImageSize[0], $aImageSize[1], $hIA)
        _GDIPlus_ImageAttributesDispose($hIA)
    EndIf
EndFunc

Func _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "uint*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[4]
EndFunc   ;==>_GDIPlus_BitmapGetPixel

Func _GDIPlus_ImageGetDimension($hImage)
    Local $aSize[2], $aResult
    $aResult = DllCall($ghGDIPDll, "uint", "GdipGetImageDimension", "hwnd", $hImage, "float*", 0, "float*", 0)
    If @error Then Return SetError(@error, @extended, -1)
    $GDIP_STATUS = $aResult[0]
    If $GDIP_STATUS Then Return -1
    $aSize[0] = $aResult[2]
    $aSize[1] = $aResult[3]
    Return $aSize
EndFunc   ;==>_GDIPlus_ImageGetDimension

Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateImageAttributes", "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[1]
EndFunc   ;==>_GDIPlus_ImageAttributesCreate

Func _GDIPlus_ColorMatrixCreateGrayScale($GDIP_RLUM = 0.3086, $GDIP_GLUM = 0.6094, $GDIP_BLUM = 0.0820)
    Local Const $tagGDIPCOLORMATRIX = "float m[25];"
    Local $iI, $iJ, $tCM, $aLums[4] = [$GDIP_RLUM, $GDIP_GLUM, $GDIP_BLUM, 0]
    $tCM = DllStructCreate($tagGDIPCOLORMATRIX)
    For $iI = 0 To 3
        For $iJ = 1 To 3
            DllStructSetData($tCM, "m", $aLums[$iI], $iI * 5 + $iJ)
        Next
    Next
    DllStructSetData($tCM, "m", 1, 19)
    DllStructSetData($tCM, "m", 1, 25)
    Return $tCM
EndFunc   ;==>_GDIPlus_ColorMatrixCreateGrayScale

Func _GDIPlus_ImageAttributesSetColorMatrix($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $pClrMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesColorMatrix", "hwnd", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "ptr", $pClrMatrix, "ptr", $pGrayMatrix, "int", $iColorMatrixFlags)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetColorMatrix

Func _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $hImage, $nSrcX, $nSrcY, $nSrcWidth, $nSrcHeight, $nDstX, $nDstY, $nDstWidth, $nDstHeight, $hImageAttributes = 0, $iUnit = 2)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRect", "hwnd", $hGraphics, "hwnd", $hImage, "float", $nDstX, "float", _
            $nDstY, "float", $nDstWidth, "float", $nDstHeight, "float", $nSrcX, "float", $nSrcY, "float", $nSrcWidth, "float", _
            $nSrcHeight, "int", $iUnit, "hwnd", $hImageAttributes, "int", 0, "int", 0)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectIA

Func _GDIPlus_ImageAttributesDispose($hImageAttributes)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipDisposeImageAttributes", "hwnd", $hImageAttributes)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesDispose

Use a small picture first to see the result in the array.

Br,

UEZ

Edited by 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

:) Thank for your help

I think it has solved the main problem. I have been doing so already. I known programs run

slower when using _GDIPlus_BitmapGetPixel function too many. I thought about the create $aImage[$y][$x] at the same time with the transfer to grayscale but i can't do it ;)

PixelRGB[$i][$j] -> PixelGray[$i][$j]

aImage[$i][$j]=PixelGray[$i][$j]

I want a solution as above

Please help me.

GDIP.rar

Share this post


Link to post
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
Sign in to follow this  
Followers 0