Jump to content

StringRegExpReplace all with 0 except FFFFFF


Go to solution Solved by Herb191,

Recommended Posts

I am sure there is a way to do this without using multiple StringRegExpReplaces but I am not very experienced with regular expressions.

Basically I am trying to change any color in a bitmap file that’s not white to black.

$BitmapPath = @ScriptDir & "\test2.bmp"

$File = FileOpen($BitmapPath, 16)
$Content = FileRead($File)
FileClose($File)
$Header = BinaryMid($Content, 1, 54)
$ColorData = Hex(BinaryMid($Content, 55))

;I need a better StringRegExpReplace here.
$sOutputColorData = StringRegExpReplace($ColorData, "[A-E]|[1-9]", "0")

$File = FileOpen(@ScriptDir & "\black and white.bmp", 18)
FileWrite($File, $Header & $sOutputColorData)
FileClose($File)
Edited by Herb191
Link to comment
Share on other sites

Will this work?

Edit: Dont think it does, need to catch the padding and maybe extract the pixel size..

$BitmapPath = @ScriptDir & "\Untitled2.bmp"

$File = FileOpen($BitmapPath, 16)
$Content = FileRead($File)
FileClose($File)
$Header = BinaryMid($Content, 1, 54)
$ColorData = BinaryMid($Content, 55)

$sOutputColorData = ""
For $idx = 1 To BinaryLen($ColorData) Step 3
    $Pixel = Hex(BinaryMid($ColorData, $idx, 3))
    ConsoleWrite("Pixel : " & $Pixel & @crlf)
    IF Hex($Pixel, 3) <> "FFFFFF" Then $Pixel=Hex(0, 3)
    $sOutputColorData = $sOutputColorData & Hex($Pixel, 3)
Next
$File = FileOpen(@ScriptDir & "\black and white.bmp", 16)
FileWrite($File, $Header & $sOutputColorData)
FileClose($File)
Edited by Geir1983
Link to comment
Share on other sites

 

Will this work?

$BitmapPath = @ScriptDir & "\Untitled.bmp"

$File = FileOpen($BitmapPath, 16)
$Content = FileRead($File)
FileClose($File)
$Header = BinaryMid($Content, 1, 54)
$ColorData = Hex(BinaryMid($Content, 55))

$sOutputColorData = ""
For $idx = 0 To StringLen($Content) Step 4
    $Pixel = StringMid($Content, $idx, 4)
    IF Hex($Pixel, 4) <> "FFFFFF" Then $Pixel="0000"
    $sOutputColorData = $sOutputColorData & $Pixel  
Next
$File = FileOpen(@ScriptDir & "\black and white.bmp", 18)
FileWrite($File, $Header & $sOutputColorData)
FileClose($File)

It would probably work but it is really really slow. It would be faster to use String RegExpReplace a few times in a row. I was hoping to do it all with one expression for performance reasons.

Link to comment
Share on other sites

Guys, you're converting the byte values to hex in strings. Use a DllStruct instead.

Local $bOutputColorData = Binary(StringRegExpReplace($bHeader, "([^\x{ffffff}])", "\x{000000}"))

Note the binary hungarian notation!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

This works on 24 bit bmp, but probably not the most optimized :P

$BitmapPath = @ScriptDir & "\Untitled2.bmp"

$File = FileOpen($BitmapPath, 16)
$Content = FileRead($File)
FileClose($File)
$Header = BinaryMid($Content, 1, 54)
$ColorData = BinaryMid($Content, 55)

$sOutputColorData = ""


For $idx = 1 To BinaryLen($ColorData) Step 3
    $Pixel = BinaryMid($ColorData, $idx, 3)
    ;ConsoleWrite("Pixel : " & hex($Pixel) & @crlf)
    IF $Pixel <> 0xFFFFFF Then $Pixel=0
    $sOutputColorData = $sOutputColorData & hex($Pixel, 6)
Next

$File = FileOpen(@ScriptDir & "\black and white.bmp", 26)
FileWrite($File, $Header & $sOutputColorData)
FileClose($File)
Link to comment
Share on other sites

Previously, I have used a regular expression with _GDIPlus_BitmapLockBits to manipulate colors of an image.
This works also.

#include <GDIPlus.au3>

;Local $BitmapPath = @ScriptDir & "\TestGdi.bmp" ; 32 Bpp RGB
Local $BitmapPath = @ScriptDir & "\colormap.bmp" ; 24 Bits per pixel (Bpp) RGB

Local $File = FileOpen($BitmapPath, 16)
Local $Content = FileRead($File)
FileClose($File)
Local $Header = StringMid($Content, 1, 109)
Local $ColorData = StringMid($Content, 110)

#region ; Added Edit2
Local $iBpp
_GDIPlus_Startup()                               ; Initialize GDI+ library
$hImage = _GDIPlus_ImageLoadFromFile($BitmapPath); Load screen capture bitmap from file
$aRet = _GDIPlus_ImageGetPixelFormat($hImage)    ; Show pixel format for saved file
$iBpp = StringLeft($aRet[1], 2) / 4
;ConsoleWrite("Image pixel format of saved file: " & $aRet[1] & @LF)
_GDIPlus_ImageDispose($hImage)                   ; Clean up resources
_GDIPlus_Shutdown()                              ; Shut down GDI+ library
#endregion ; Added Edit2

Local $sOutputColorData = StringRegExpReplace($ColorData, "(.{" & $iBpp & "})", "\1 ")

Local $sRegExpPat = "([0-9A-E]{1}[0-9A-F]{5}|[0-9A-F]{1}[0-9A-E]{1}[0-9A-F]{4}|[0-9A-F]{2}[0-9A-E]{1}[0-9A-F]{3}|" & _
        "[0-9A-F]{3}[0-9A-E]{1}[0-9A-F]{2}|[0-9A-F]{4}[0-9A-E]{1}[0-9A-F]{1}|[0-9A-F]{5}[0-9A-E]{1})"

$sOutputColorData = StringRegExpReplace($sOutputColorData, $sRegExpPat, "000000 ")

Local $hFile = FileOpen(@ScriptDir & "\black and white.bmp", 26)
FileWrite($hFile, $Header & StringStripWS($sOutputColorData, 8))
FileClose($hFile)
ShellExecute(@ScriptDir & "\black and white.bmp")

Strings, numbers, binary, they are all the same variant data type. What matters is how you treat them. When treating binary as a string, the string still has to be able to be recognized as valid binary when finished.

Edit: Changed :-
Local $sRegExpPat = "(?i)([0-9A-E]{6}|[0-9A-E]{4}FF|FF[0-9A-E]{4}|[0-9A-E]{2}FF[0-9A-E]{2}|[0-9A-E]{2}FFFF|FFFF[0-9A-E]{2}|FF[0-9A-E]{2}FF)"

Edit2: Allowing for varying bits per pixel (Bpp) of an image.

Edited by Malkey
Link to comment
Share on other sites

What happens to bytes of the form 0xFn or 0xnF with n in [0..E]?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

This?

Local $bHeader = Binary("0xFFFFFFFFFFFF1234561234ffFFFF56000000ffffff123456")
Local $sHeader = StringTrimLeft($bHeader, 2)
Local $bOutputColorData = Binary('0x' & StringRegExpReplace($sHeader, "(?i)\G((?:FFFFFF)*)((?:.{6})(?<!FFFFFF))", "${1}000000"))

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

What happens to bytes of the form 0xFn or 0xnF with n in [0..E]?

Yes, you are correct.

This reg. exp. pattern:-

'"([0-9A-E]{1}[0-9A-F]{5}|[0-9A-F]{1}[0-9A-E]{1}[0-9A-F]{4}|[0-9A-F]{2}[0-9A-E]{1}[0-9A-F]{3}|[0-9A-F]{3}[0-9A-E]{1}[0-9A-F]{2}|[0-9A-F]{4}[0-9A-E]{1}[0-9A-F]{1}|[0-9A-F]{5}[0-9A-E]{1})" & Chr(0)',

should match all colors except "FFFFFF".

I have edited post #8

 

I believe the pixels returned from "FileOpen($BitmapPath, 16)" after the header section is in the format of BBGGRR& Chr(0). Where "BB" is Blue color channel, and, Green and Red color channels, followed by a null byte.

Edit: Altered RE Pattern.

Edited by Malkey
Link to comment
Share on other sites

should match all colors except "FFFFFF".

 

It still does not seem to match all colors when I tested it with the attached file. Maybe there is a simpler solution? All I really want to do is turn all colors except white to black (at a reasonable speed).

colormap.bmp

EDIT: added file

Edited by Herb191
Link to comment
Share on other sites

It still does not seem to match all colors when I tested it with the attached file. Maybe there is a simpler solution? All I really want to do is turn all colors except white to black (at a reasonable speed).

It appears you posted and I edited at the same time. See my new RE pattern.

And what "attached file"?

Link to comment
Share on other sites

  • Solution

Thanks everyone for the help. I ended up using the code bellow. Its faster and works better. I got the code from this post:

#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Opt('MustDeclareVars', 1)

_Main()

ShellExecute(@ScriptDir & "\black and white.bmp")

Func _Main()
    Local $hBitmap, $hClone, $hImage, $iX, $iY

    ; Initialize GDI+ library
    _GDIPlus_StartUp ()

    ; Capture 32 bit bitmap
    $hBitmap = _ScreenCapture_Capture ("")
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap)

    ; Create 24 bit bitmap clone
    $iX = _GDIPlus_ImageGetWidth ($hImage)
    $iY = _GDIPlus_ImageGetHeight ($hImage)
    $hClone = _GDIPlus_BitmapCloneArea ($hImage, 0, 0, $iX, $iY, $GDIP_PXF01INDEXED)

    ; Save bitmap to file
    _GDIPlus_ImageSaveToFile ($hClone, @ScriptDir & "\black and white.bmp")

    ; Clean up resources
    _GDIPlus_ImageDispose ($hClone)
    _GDIPlus_ImageDispose ($hImage)
    _WinAPI_DeleteObject ($hBitmap)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()
EndFunc   ;==>_Main
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...