Change color in a bmp

Change color in a bmp

Postby Otto » Wed Apr 18, 2018 7:15 am

Hello,
I would like to change some of the colors programmatically. Can you please help me on that.

Thanks in advance

Otto


Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6312
Joined: Fri Oct 07, 2005 7:07 pm

Re: Change color in a bmp

Postby Antonio Linares » Wed Apr 18, 2018 7:34 am

Otto,

If the bitmap is already shown as a control:
Code: Select all  Expand view

local hDC := oBmp:GetDC()

for nRow = 0 to oBmp:nHeigth() - 1
   for nCol = 0 to oBmp:nWidth() - 1
     if GetPixel( hDC, nRow, nCol ) == nClrToReplace
        SetPixel( hDC, nRow, nCol, nClrNew )
     endif
   next
next

oBmp:ReleaseDC()
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42059
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Change color in a bmp

Postby Otto » Wed Apr 18, 2018 8:38 am

Dear Antonio,
thank you for your help.
But it seems that this function is working to slow for my purpose.
Is it possible to print a transparent bmp over the original.
Thank you in advance
Otto
Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6312
Joined: Fri Oct 07, 2005 7:07 pm

Re: Change color in a bmp

Postby Silvio.Falconi » Wed Apr 18, 2018 10:39 am

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby nageswaragunupudi » Wed Apr 18, 2018 11:45 am

Use Windows GDI functions FloodFill() or ExtFloodFill().
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Change color in a bmp

Postby ukoenig » Fri Mar 12, 2021 11:59 am

Otto,

Use Windows GDI functions FloodFill() or ExtFloodFill().


a idea maybe using a colorpicker

I still have to work on the < floodfill function >
I could not' find any working tests inside the forum or sample folder :cry:
I don't know for the moment if we can fill a single cell
inside the image on mouseclick

1. select a color
2. use floodfill inside the working image 1
3. after selections save the result to image 1 and 2
4. delete old selections with floodfill using color white

There are 2 colorpickers
one with all colors and one with a colorselection

Image

regards
Uwe :?:
Last edited by ukoenig on Mon Mar 29, 2021 7:27 am, edited 5 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Change color in a bmp

Postby Silvio.Falconi » Fri Mar 12, 2021 12:23 pm

if I have this figure and I wanted to click on the pencils and recognize the color and then fill the white part of the drawing with the selected color, what can I do?

Image
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby Natter » Fri Mar 12, 2021 2:56 pm

1, Get hDC.
2. Read pixels around the coordinates of the mouse cursor.
3. Determine that it is a white color and change it.

Only it's a long time.

You can specify the polygon with the corresponding color yourself
Natter
 
Posts: 1203
Joined: Mon May 14, 2007 9:49 am

Re: Change color in a bmp

Postby Silvio.Falconi » Fri Mar 12, 2021 5:56 pm

Natter wrote:1, Get hDC.
2. Read pixels around the coordinates of the mouse cursor.
3. Determine that it is a white color and change it.

Only it's a long time.

You can specify the polygon with the corresponding color yourself

In words you are all good, I know what I should do, if I asked how to do it it means that perhaps I have not succeeded,
in fact even if I know how to fill an area in a drawing I do not know the function or the command to take the pixel color only by clicking with the mouse
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby Silvio.Falconi » Mon Mar 15, 2021 12:47 pm

I made a sample test

Code: Select all  Expand view


Function test()
   local cFile:="a.jpg"
   local cColori:="colori.bmp"
   local oDlg,oBmp,oImg
   local nColor:= 1,oColor
   local hdc

DEFINE CURSOR oHand HAND

DEFINE dialog oDlg size 600,600

 @ 4,1 XIMAGE oImg filename cFile  SIZE 200,200 OF oDlg

 @ 220,1 XIMAGE oBmp filename cColori  SIZE 200,20 OF oDlg

 @ 4, 220 get oColor var ncolor size 20,30 pixel  COLOR ncolor,ncolor UPDATE


 oBmp:oCursor = oHand
 oBmp:bLClicked = { | nRow, nCol | (GetColor( nRow, nCol, oBmp,@nColor ), Msginfo(nColor), oColor:refresh()  ) }

activate dialog oDlg
return nil
//--------------------------------------------------------------//
function GetColor( nRow, nCol, oBmp,nColor )
   local hDC  := oBmp:GetDC()

   nColor := GetPixel( hDC, nCol, nRow )
   DeleteDC( hDC )
   oBmp:ReleaseDC()
return nil
//----------------------------------------------------//


for the first time it takes the color but does not refresh the get, the second time and after the color is always -1 why?


a.jpg
Image

color.bmp
Image
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby ukoenig » Tue Mar 16, 2021 10:24 am

Silvio

my changes and tests
I tested < Floodfill and ExtFloodFill > but got NO result :(

Code: Select all  Expand view

FUNCTION MAIN()
local cFile:="a.bmp"
local cColori:="colori.png"
local oDlg,oBmp,oImg
local nColor := 0, oColor
local hdc, nPickRow := 0, nPickCol := 0
local oSay1, oSay2, oSay3, oFont

DEFINE CURSOR oHand HAND
DEFINE FONT oFont NAME "Verdanal" SIZE 0, -12 BOLD

DEFINE dialog oDlg size 520, 520

@ 4,1 XIMAGE oImg filename cFile  SIZE 200,200 OF oDlg
oImg:oCursor := oHand
oImg:bPainted := < |hDC|
        FLOODFILL( nPickRow, nPickCol, nColor )
//  FILLCOLOR( hDC, oImg, nPickRow, nPickCol, nColor )
    oSay1:Refresh()
    oSay2:Refresh()
    oSay3:Refresh()
RETURN NIL
>
oImg:bLClicked := { | nRow, nCol | nPickRow := nRow, nPickCol := nCol, oImg:refresh() }

@ 220,1 XIMAGE oBmp filename cColori  SIZE 180,20 OF oDlg
@ 220, 183 get oColor var ncolor size 20, 20 pixel  COLOR nColor, nColor UPDATE

@ 190, 210 say oSay0 PROMPT "Top, Left, color" size 60, 30 pixel  COLOR 0 FONT oFont UPDATE
@ 200, 210 say oSay1 var nPickRow size 20, 20 pixel  COLOR 255 FONT oFont UPDATE
@ 210, 210 say oSay2 var nPickCol size 20, 20 pixel  COLOR 255 FONT oFont UPDATE
@ 220, 210 say oSay3 var nColor size 20, 20 pixel  COLOR 255 FONT oFont UPDATE

oBmp:oCursor := oHand
oBmp:bLClicked := { | nRow, nCol | ( ncolor := GETCOLOR( oBmp, nRow, nCol ), ;
                                                            oColor:SetColor(nColor, nColor), oColor:refresh() ) }

ACTIVATE DIALOG oDlg CENTERED

oFont:End()

RETURN NIL

//-----------------------

FUNCTION GETCOLOR( oBmp, nRow, nCol )

hDC := oBmp:GetDC()
nColor := GetPixel( hDC, nCol, nRow )
oBmp:ReleaseDC()

RETURN nColor

//-----------------------

FUNCTION FILLCOLOR( hDC, oImg, nRow, nCol, nColor )

//MsgAlert( nRow, "Row" )
//MsgAlert( nCol, "Column" )
//MsgAlert( nColor, "Color" )

//ExtFloodFill( hDC, nRow, nCol, ncolor, 1 )
FloodFill( nRow, nCol, nColor )

RETURN NIL

 


On imageclick I show the cursor-position and color-value

Image

the cleaned image with deleted dirty areas :!:

Image

Floodfill is done with PIXELFORMER :!:
Reduce the image to 4 colors to clean the image from dirty areas

The original image shows the result ( floodfill ) without cleaning

Image

next save the image with full colors to fill the wanted sections close to clear borders
See the result after cleaning :D

Image

regards
Uwe :D
Last edited by ukoenig on Tue Mar 23, 2021 12:06 pm, edited 2 times in total.
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Change color in a bmp

Postby Silvio.Falconi » Tue Mar 16, 2021 12:50 pm

Sorry
I selet the colors
then click on Image not fill the color into area why ?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby ukoenig » Tue Mar 16, 2021 1:20 pm

Silvio,

showing a black and white image I expected that You want to repaint it
because from Rao
Use Windows GDI functions FloodFill() or ExtFloodFill().

I used the extended test for maybe getting a working FLOODFILL-solution

a answer of Your question is included
for the first time it takes the color but does not refresh the get,
the second time and after the color is always -1 why?


Image

regards
Uwe
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: Change color in a bmp

Postby Silvio.Falconi » Wed Mar 17, 2021 11:43 am

image A is black and white
the handicapped child must take a color on image B and click on part of image A and color the part delimited by the lines.
In your example I am able to take the color from image B but then when I click on image A it does not color the selected area

in an old program done at least 15 years ago I was able to color a specific area (region) in an image of a map of Italy
when I was scrolling through a record in an xbrowse as you can see on this picture

Image
on this application I used FloodFill function
Code: Select all  Expand view

 25, 10 BITMAP oBmp RESOURCE "CARTINA" OF oDlg UPDATE PIXEL SIZE 127, 179 NOBORDER TRANSPARENT
oBmp:bPainted := { |hDC| If( Empty( oBrw:oTreeItem:Cargo[ 5 ] ), nil, ;
   FloodFill( hDC, oBrw:oTreeItem:Cargo[ 5 ], oBrw:oTreeItem:Cargo[ 6 ], nil, CLR_RED ) ) }
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Re: Change color in a bmp

Postby Silvio.Falconi » Tue Mar 23, 2021 8:40 am

ukoenig wrote:Silvio,
regards
Uwe


Uwe, Now the test colorize all the image and not the area
please try the test



Code: Select all  Expand view



#include "fivewin.ch"


    FUNCTION MAIN()
    local cFile:="a.bmp"
    local cColori:="colori.png"
    local oDlg,oBmp,oImg
    local nColor := 0, oColor
    local hdc, nPickRow := 0, nPickCol := 0
    local oSay1, oSay2, oSay3, oFont
    local nColorSet:= 1
    DEFINE CURSOR oHand HAND
    DEFINE FONT oFont NAME "Verdanal" SIZE 0, -12 BOLD

    DEFINE dialog oDlg size 520, 520

    @ 4,1 XIMAGE oImg filename cFile  SIZE 200,200 OF oDlg
    oImg:oCursor := oHand
    oImg:bPainted := < |hDC|
        oSay1:Refresh()
        oSay2:Refresh()
        oSay3:Refresh()
    RETURN NIL
    >




    @ 220,1 XIMAGE oBmp filename cColori  SIZE 180,20 OF oDlg
    @ 220, 183 get oColor var ncolor size 20, 20 pixel  COLOR nColor, nColor UPDATE

    @ 190, 210 say oSay0 PROMPT "Top, Left, color" size 60, 30 pixel  COLOR 0 FONT oFont UPDATE
    @ 200, 210 say oSay1 var nPickRow size 20, 20 pixel  COLOR 255 FONT oFont UPDATE
    @ 210, 210 say oSay2 var nPickCol size 20, 20 pixel  COLOR 255 FONT oFont UPDATE
    @ 220, 210 say oSay3 var nColor size 20, 20 pixel  COLOR 255 FONT oFont UPDATE

    oBmp:oCursor := oHand
    oBmp:bLClicked := { | nRow, nCol | ( ncolor := GETCOLOR( oBmp, nRow, nCol ), ;
                                                                oColor:SetColor(nColor, nColor), oColor:refresh(),nColorSet:=ncolor ) }

    oImg:blClicked := { | nRow, nCol | nPickRow := nRow, nPickCol := nCol,;
                                                FILLCOLOR(oImg, nPickRow, nPickCol, nColorSet ) }



    ACTIVATE DIALOG oDlg CENTERED

    oFont:End()

    RETURN NIL

    //-----------------------

    FUNCTION GETCOLOR( oBmp, nRow, nCol )
    hDC := oBmp:GetDC()
    nColor := GetPixel( hDC, nCol, nRow )
    oBmp:ReleaseDC()
    RETURN nColor

    //-----------------------

    FUNCTION FILLCOLOR( oImg, nRow, nCol, nColor )
         local hDC:=oImg:getDc()
            FloodFill( hDC, nRow, nCol, nil,nColor )
           oImg:ReleaseDC()
    RETURN NIL

   //----------------------

 
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7033
Joined: Thu Oct 18, 2012 7:17 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 33 guests