Jump to content

Inverting an image or producing a negative?


Recommended Posts

Is it possible in Autoit to invert an image to give out the negative of the image. I have been researching the grayscale code given by Saio i think it is and love it. Is there already code in autoit to do this?

Thanks in advance

Link to comment
Share on other sites

It's not exactly obvious how that helps someone trying to produce a result in AutoIt.

The principal behind colour inversion is to get the colour components for each pixel, then change each component to it's inverse which is 255 - colour, and finally add the components together again to get the new colour.

If the RGB colour is $c then the inverted colour is produced like this

$InvertedCol = Inverted($col)
  ConsoleWrite(Hex($InvertedCol) & @CRLF)
  
  Func Inverted($iC)
     Local $r, $g, $b
     $r = BitAND(0xFF, BitShift($iC, 16))
     $g = BitAND(0xFF, BitShift($iC, 8))
     $b = BitAND($iC, 0xFF)
     Return (255 - $b) + 0x100 * (255 - $g) + 0x10000 * (255 - $r)
  EndFunc;==>Inverted

If you applied that to every pixel in an image you would get the colour inverted result. There might be solutions around though.

EDIT: After a post by weaponx I see the above is over-the-top. See here.

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

smcombs,

And here is some working code to do it:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
#Include <Color.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>

; Credit: Malkey for the basic GDI code

Global $iTolerance = 30, $iX1, $iY1, $iX2, $iY2, $fType = ""

; Create GUI
$hMain_GUI = GUICreate("Invert BMP", 240, 150)

$hLabel_1 = GUICtrlCreateLabel("First mark the area to invert or select a BMP", 10, 10, 260, 20)

$hRect_Button   = GUICtrlCreateButton("Mark Area",   10, 40, 80, 30)
$hChoose_Button = GUICtrlCreateButton("Choose BMP", 150, 40, 80, 30)

$hLabel_2 = GUICtrlCreateLabel("", 160, 90, 70, 20)

$hAction_Button = GUICtrlCreateButton("Invert",   150,  110, 80, 30)
GUICtrlSetState(-1, $GUI_DISABLE)
$hCancel_Button = GUICtrlCreateButton("Cancel",   10, 110, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE, $hCancel_Button
            GUIDelete($hMain_GUI)
            Exit
        Case $hRect_Button
            GUISetState(@SW_HIDE, $hMain_GUI)
            Mark_Rect()
            GUISetState(@SW_SHOW, $hMain_GUI)
            GUICtrlSetState($hAction_Button, $GUI_ENABLE)
            GUICtrlSetData($hLabel_1, "")
            GUICtrlSetData($hLabel_2, "Now Invert!")
            $fType = "Rect"
        Case $hChoose_Button
            $sBMP_Path = FileOpenDialog("Select BMP to invert", "C:\", "Bitmaps (*.bmp)", 2)
            If @error Then
                MsgBox(64, "Info", "No BMP selected")
            Else
                GUICtrlSetState($hAction_Button, $GUI_ENABLE)
                GUICtrlSetData($hLabel_1, "")
                GUICtrlSetData($hLabel_2, "Now Invert!")
                $fType = "File"
            EndIf
        Case $hAction_Button
            GUIDelete($hMain_GUI)
            ExitLoop
    EndSwitch

WEnd

; Capture selected area if needed
If $fType = "Rect" Then
    $sBMP_Path = @ScriptDir & "\TestNormal.bmp"
    GUISetState(@SW_HIDE, $hMain_GUI)
    _ScreenCapture_Capture($sBMP_Path, $iX1, $iY1, $iX2, $iY2, False)
EndIf

; Load original image
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile($sBMP_Path)
If @error Then
    MsgBox(16, "Error", "Could not load BMP file")
    _GDIPlus_Shutdown()
    Exit
EndIf

Global $GuiSizeX = _GDIPlus_ImageGetWidth($hImage)
Global $GuiSizeY = _GDIPlus_ImageGetHeight($hImage)

; Display original image
$hBitmap_GUI = GUICreate("Original Bitmap", $GuiSizeX, $GuiSizeY, 100, 100)
GUISetState()

; Create Double Buffer, so the doesn't need to be repainted on PAINT-Event
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hBitmap_GUI)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage, 0, 0, $GuiSizeX, $GuiSizeY)

GUIRegisterMsg(0xF, "MY_PAINT")
GUIRegisterMsg(0x85, "MY_PAINT")
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

; Invert the image
Local $hBitmap = Image_Invert($hBMPBuff, 0, 0, $GuiSizeX, $GuiSizeY)
If _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\TestInverted.bmp") =  False Then MsgBox(16 , "Error", "Inverted image not created")

WinActivate($hBitmap_GUI)

; Display inverted image
$hInverted_GUI = GUICreate("Inverted Image", $GuiSizeX, $GuiSizeY, 500, 200)
$hPic = GUICtrlCreatePic(@ScriptDir & "\TestInverted.bmp", 0, 0, $GuiSizeX, $GuiSizeY)
GUISetState()

While 1
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_Shutdown()
        Exit
    EndIf
WEnd

; -------------

;Func to redraw on PAINT MSG
Func MY_PAINT($hWnd, $msg, $wParam, $lParam)

   ; Check, if the GUI with the Graphic should be repainted
   ; The sequencial order of these two commands is important.
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hBitmap_GUI, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG

EndFunc  ;==>MY_PAINT

; -------------

Func Image_Invert($hImage2, $iStartPosX = 0, $iStartPosY = 0, $GuiSizeX = Default, $GuiSizeY = Default)

    Local $hBitmap1, $Reslt, $width, $height, $stride, $format, $Scan0, $v_Buffer, $v_Value, $iIW, $iIH
    $iIW = _GDIPlus_ImageGetWidth($hImage2)
    $iIH = _GDIPlus_ImageGetHeight($hImage2)
    If $GuiSizeX = Default Or $GuiSizeX > $iIW - $iStartPosX Then $GuiSizeX = $iIW - $iStartPosX
    If $GuiSizeY = Default Or $GuiSizeY > $iIH - $iStartPosY Then $GuiSizeY = $iIH - $iStartPosY
    $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage2, $iStartPosX, $iStartPosY, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB)

    ProgressOn("Inverting Image", "The image is being processed.", "0 percent", -1, -1, 16)

    $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $GuiSizeX, $GuiSizeY, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $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")
    For $i = 0 To $GuiSizeX - 1
        For $j = 0 To $GuiSizeY - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
        ; Get colour value of pixel
            $v_Value = DllStructGetData($v_Buffer, 1)
        ; Invert
            If (Abs(_ColorGetBlue ($v_Value) - 0x80) <= $iTolerance And _ ; Blue
                Abs(_ColorGetGreen($v_Value) - 0x80) <= $iTolerance And _ ; Green
                Abs(_ColorGetRed  ($v_Value) - 0x80) <= $iTolerance) Then ; Red
                DllStructSetData($v_Buffer, 1, BitAND((0x7F7F7F + $v_Value) , 0xFFFFFF))
            Else
                DllStructSetData($v_Buffer, 1, BitXOR($v_Value ,0xFFFFFF))
            EndIf
        Next
        ProgressSet(Int(100 * $i / ($GuiSizeX)), Int(100 * $i / ($GuiSizeX)) & " percent")
    Next
    _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt)

    ProgressOff()
    Return $hBitmap1

EndFunc  ;==>Image_Invert

; -------------

Func Mark_Rect()

    Local $aMouse_Pos, $aMask, $aM_Mask, $iTemp
    Local $UserDLL = DllOpen("user32.dll")

; Wait until mouse button pressed
    While Not _IsPressed("01", $UserDLL)
        Sleep(10)
    WEnd

; Get first mouse position
    $aMouse_Pos = MouseGetPos()
    $iX1 = $aMouse_Pos[0]
    $iY1 = $aMouse_Pos[1]

    Global $hRectangle_GUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    GUISetBkColor(0x000000)
    GUISetState()

; Draw rectangle while mouse button pressed
    While _IsPressed("01", $UserDLL)

        $aMouse_Pos = MouseGetPos()

        $aM_Mask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", 0, "long", 0, "long", 0, "long", 0)
    ; Bottom of rectangle
        $aMask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $iX1, "long", $aMouse_Pos[1], "long", $aMouse_Pos[0], "long", $aMouse_Pos[1] + 1)
        DllCall("gdi32.dll", "long", "CombineRgn", "long", $aM_Mask[0], "long", $aMask[0], "long", $aM_Mask[0], "int", 2)
    ; Left of rectangle
        $aMask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $iX1, "long", $iY1, "long", $iX1 + 1, "long", $aMouse_Pos[1])
        DllCall("gdi32.dll", "long", "CombineRgn", "long", $aM_Mask[0], "long", $aMask[0], "long", $aM_Mask[0], "int", 2)
    ; Top of rectangle
        $aMask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $iX1 + 1, "long", $iY1 + 1, "long", $aMouse_Pos[0], "long", $iY1)
        DllCall("gdi32.dll", "long", "CombineRgn", "long", $aM_Mask[0], "long", $aMask[0], "long", $aM_Mask[0], "int", 2)
    ; Right of rectangle
        $aMask = DllCall("gdi32.dll", "long", "CreateRectRgn", "long", $aMouse_Pos[0], "long", $iY1, "long", $aMouse_Pos[0] + 1, "long", $aMouse_Pos[1])
        DllCall("gdi32.dll", "long", "CombineRgn", "long", $aM_Mask[0], "long", $aMask[0], "long", $aM_Mask[0], "int", 2)
        DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $hRectangle_GUI, "long", $aM_Mask[0], "int", 1)

        Sleep(50)

    WEnd

; Get second mouse position
    $iX2 = $aMouse_Pos[0]
    $iY2 = $aMouse_Pos[1]

; Set in correct order if required
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf

    GUIDelete($hRectangle_GUI)
    DllClose($UserDLL)

EndFunc  ;==>Mark_Rect

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here is a fast way to invert colours.

;
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>

; http://www.autoitscript.com/forum/index.php?s=&showtopic=77799&view=findpost&p=563657
HotKeySet("{Esc}", "_Exit")

Opt("GUIOnEventMode", 1);0=disabled, 1=OnEvent mode enabled

Local $iW = 400, $iH = 300

$hGui = GUICreate("Invert Colours Example", $iW, $iH)
GUISetOnEvent(-3, "_Exit")
GUISetState()

_GDIPlus_Startup()
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
$hdc = _WinAPI_GetDC(0)
$hcdc = _WinAPI_CreateCompatibleDC($hdc)
$hcbmp = _WinAPI_CreateCompatibleBitmap($hdc, $iW, $iH)
_WinAPI_SelectObject($hcdc, $hcbmp)

_WinAPI_BitBlt($hcdc, 0, 0, $iW, $iH, $hdc, 0, 0, $NOTSRCERASE)

$gc = _GDIPlus_GraphicsCreateFromHDC($hdc)
$bmp = _GDIPlus_BitmapCreateFromHBITMAP($hcbmp)
_GDIPlus_GraphicsDrawImage($hGraphic, $bmp, 0, 0)

;$sFileName = @DesktopDir & "\GDIPlus_Image1.jpg"
;_GDIPlus_ImageSaveToFile($bmp, $sFileName)
;ShellExecute(@DesktopDir & "\GDIPlus_Image1.jpg")

_GDIPlus_GraphicsDispose($gc)
_GDIPlus_ImageDispose($bmp)

_WinAPI_DeleteObject($hcbmp)
GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
GUIRegisterMsg(0x85, "MY_PAINT"); $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

While 1
    Sleep(100)
WEnd

Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _WinAPI_ReleaseDC(0, $hdc)
    _WinAPI_DeleteDC($hcdc)
    _WinAPI_RedrawWindow(0, 0, 0, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_ALLCHILDREN))
    _GDIPlus_Shutdown()
    Exit
EndFunc  ;==>_Exit

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
; Check, if the GUI with the Graphic should be repainted
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc  ;==>MY_PAINT
;

Link to comment
Share on other sites

If you want to invert .bmp- or .jpg-files you can use FreeImage:

#include <FreeImage.au3>
_FreeImage_LoadDLL(@ScriptDir&"\FreeImage.dll")
_FreeImage_Initialise()

$sFile = "Path\To\image.jpg"

$FIF = _FreeImage_GetFileTypeU($sFile)
If $FIF = $FIF_UNKNOWN Then
    $FIF = _FreeImage_GetFIFFromFilenameU($sFile)
EndIf
$ImageHandle = _FreeImage_LoadU($FIF, $sFile)
_FreeImage_Invert($ImageHandle)
_FreeImage_SaveU($FIF, $ImageHandle, $sFile)
_FreeImage_Unload($ImageHandle)
_FreeImage_DeInitialise()
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

This script:-

1/ Shows the image selected from FileOpenDialog for 2 secs;

2/ Shows black and white version of the above image for 2 secs; and,

3/ Finally, shows the negative of the previous black and white image.

;
#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
;#include <Constants.au3>

; http://www.autoitscript.com/forum/index.php?s=&showtopic=77799&view=findpost&p=563657
; http://www.autoitscript.com/forum/index.php?s=&showtopic=86951&view=findpost&p=623855

Opt("GUIOnEventMode", 1);0=disabled, 1=OnEvent mode enabled

Local $Path = FileOpenDialog("Choose Image File", @ScriptDir & "", _
        "Images (*.gif;*.png;*.jpg;*.bmp)| All (*.*)")
If $Path <> "" Then
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($Path)
Else
    Exit
EndIf
Local $iW, $iH
$iW = _GDIPlus_ImageGetWidth($hImage)
$iH = _GDIPlus_ImageGetHeight($hImage)

$hGui = GUICreate("B & W Negative Example", $iW, $iH)
GUISetOnEvent(-3, "_Exit")
GUISetState()

$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
;$hImage = _GDIPlus_ImageLoadFromFile ($FileLoad )
_GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)

GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
GUIRegisterMsg(0x85, "MY_PAINT"); $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

Sleep(2000)

_GDIPlus_GraphicsDrawImageRectRectTrans($hGraphic, $hBMPBuff, 0, 0)
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

Sleep(2000)

$hdc = _WinAPI_GetDC($hGui)
$hcdc = _WinAPI_CreateCompatibleDC($hdc)
$hcbmp = _WinAPI_CreateCompatibleBitmap($hdc, $iW, $iH)

_WinAPI_SelectObject($hcdc, $hcbmp)

_WinAPI_BitBlt($hcdc, 0, 0, $iW, $iH, $hdc, 0, 0, $NOTSRCERASE)

$gc = _GDIPlus_GraphicsCreateFromHDC($hdc)
$bmp = _GDIPlus_BitmapCreateFromHBITMAP($hcbmp)
_GDIPlus_GraphicsDrawImage($hGraphic, $bmp, 0, 0)

;$sFileName = @DesktopDir & "\GDIPlus_Image1.jpg"
;_GDIPlus_ImageSaveToFile($hBMPBuff, $sFileName)
;ShellExecute(@DesktopDir & "\GDIPlus_Image1.jpg")

_GDIPlus_GraphicsDispose($gc)
_GDIPlus_ImageDispose($bmp)

_WinAPI_DeleteObject($hcbmp)
GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
GUIRegisterMsg(0x85, "MY_PAINT"); $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

While 1
    Sleep(100)
WEnd

Func _Exit()
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _WinAPI_ReleaseDC(0, $hdc)
    _WinAPI_DeleteDC($hcdc)
    _WinAPI_RedrawWindow(0, 0, 0, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_ALLCHILDREN))
    _GDIPlus_Shutdown()
    Exit
EndFunc  ;==>_Exit

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
; Check, if the GUI with the Graphic should be repainted
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc  ;==>MY_PAINT

Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _
        $iDstX = "", $iDstY = "", $iDstWidth = "", $iDstHeight = "", $iUnit = 2, $nTrans = 1)
    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]")

; Grey shading
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 1, 1, 2) * DllStructSetData($tColorMatrix, 1, 1, 3) * _
            DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 0.1, 1) * _
            DllStructSetData($tColorMatrix, 5, 0.1, 2) * DllStructSetData($tColorMatrix, 5, 0.1, 3) * DllStructSetData($tColorMatrix, 5, 1, 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
;
Link to comment
Share on other sites

@Malkey, you have missed out colour inversion, which I think would be like this?

;invert colours
  $x = DllStructSetData($tColorMatrix, 1, -1, 1) * DllStructSetData($tColorMatrix, 2, -1, 2) *    DllStructSetData($tColorMatrix, 3, -1, 3) * _
              DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 1) * _
              DllStructSetData($tColorMatrix, 5, 1, 2) * DllStructSetData($tColorMatrix, 5, 1, 3) * DllStructSetData($tColorMatrix, 5, 1, 4)* DllStructSetData($tColorMatrix, 5, 1, 5)

mgrefcolourmatrix1

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

@Malkey, you have missed out colour inversion, which I think would be like this?

;invert colours
  $x = DllStructSetData($tColorMatrix, 1, -1, 1) * DllStructSetData($tColorMatrix, 2, -1, 2) *    DllStructSetData($tColorMatrix, 3, -1, 3) * _
              DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 1) * _
              DllStructSetData($tColorMatrix, 5, 1, 2) * DllStructSetData($tColorMatrix, 5, 1, 3) * DllStructSetData($tColorMatrix, 5, 1, 4)* DllStructSetData($tColorMatrix, 5, 1, 5)

mgrefcolourmatrix1

martin

The script works correctly on my XP.

The colour image to black and white is performed via the color matrix.

The inversion (negative) is carried out with the BitBlt() with the $iROP (raster operation) as $NOTSRCERASE.

I am guessing there is an operating system conflict.

Malkey

Edit:

Got this to work

$x = DllStructSetData($tColorMatrix, 1, -1, 1) * DllStructSetData($tColorMatrix, 1, -1, 2) *      DllStructSetData($tColorMatrix, 1, -1, 3) * _
              DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 1) * _
              DllStructSetData($tColorMatrix, 5, 1, 2) * DllStructSetData($tColorMatrix, 5, 1, 3) * DllStructSetData($tColorMatrix, 5, 1, 5)

This goes straight from the colour image to the inverted black and white.

The BitBlt() is not needed.

Edited by Malkey
Link to comment
Share on other sites

martin

The script works correctly on my XP.

Yes I didn't say it didn't work, unless I misunderstand it, it's just that it gives a grey scale version and then inverts it. I understood that the OP wanted to invert the colours, ie a colour negative which is what my reply was meant to produce, not a black and white negative.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

martin

"Inverting an image or producing a negative?"

My post #5 covered the first part "Inverting an image".

"producing a negative" I pictured as black and white.

Producing a colour negative would be the same as inverting an image.

I had fun doing it. Thanks to your colour matrix example, I learnt something that I didn't think possible.

Edited by Malkey
Link to comment
Share on other sites

Guys this is amazing thank you so much you have definitely helped me solve my problem. I could not find any command line program to do this and automating the gui's i had just werent working thanks very much all these work wonderful

Link to comment
Share on other sites

martin

"Inverting an image or producing a negative?"

My post #5 covered the first part "Inverting an image".

"producing a negative" I pictured as black and white.

Producing a colour negative would be the same as inverting an image.

I had fun doing it. Thanks to your colour matrix example, I learnt something that I didn't think possible.

Ooh, sorry Malkey, I somehow missed post 5, I didn't know it could be done like that. If I had seen that I wouldn't have tried to work out how to do it with a colormatrix, so it we learned by mistake!

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...