ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Color Replacement limitation to Selection area?
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

PeterPanino

982 Posts

Posted - Dec 11 2022 :  09:29:35  Show Profile  Reply
I replace a specific color in the image with this code:

procedure TformMain.ButtonDoReplaceColorClick(Sender: TObject);
begin
  ImageEnViewPixelEditor.Proc.CastColorRange(
  TColor2TRGB(PanelReplaceColorsPickedColor.Color), // OldColor
  TColor2TRGB(PanelReplacementColor.Color),         // NewColor
  TrackBarColorReplacementTolerance.Position);      // Tolerance
end;


Now I would like to limit the replacement to the area inside the current selection (if any). How could this be done?

PeterPanino

982 Posts

Posted - Dec 11 2022 :  10:28:06  Show Profile  Reply
I have created a solution to solve the problem:

if ImageEnView1.Selected then // if a selection exists
begin
  var ThisIEBitmap: TIEBitmap;
  // Create the TIEBitmap to hold the selection:
  ThisIEBitmap := TIEBitmap.Create;
  try
    // Copy the selection to the ThisIEBitmap:
    ImageEnView1.CopySelectionToIEBitmap(ThisIEBitmap);
    // Copy the ThisIEBitmap to another ImageEnView:
    ImageEnViewDummy.IEBitmap.Assign(ThisIEBitmap);
  finally
    ThisIEBitmap.Free;
  end;
  // Replace the color in the other ImageEnView:
  ImageEnViewDummy.Proc.CastColorRange(
    TColor2TRGB(PanelReplaceColorsPickedColor.Color), // OldColor
    TColor2TRGB(PanelReplacementColor.Color),         // NewColor
    cxTrackBarColorReplacementTolerance.Position);    // Tolerance
  // Copy the IEBitmap from the other ImageEnView to the selection in the main ImageEnview:
  ImageEnView1.ApplyBitmapToSelection(ImageEnViewDummy.IEBitmap);
end;


Here is an example result:

https://app.screencast.com/GRz84SmkdhZkw

Can this solution be simplified and improved?
Go to Top of Page

PeterPanino

982 Posts

Posted - Dec 11 2022 :  16:21:55  Show Profile  Reply
UPDATE: This is an improved and simplified version of the code above:

// Create the TIEBitmap to hold the selection:
var ThisIEBitmap := TIEBitmap.Create;
try
  // Copy the selection to the ThisIEBitmap:
  ImageEnView1.CopySelectionToIEBitmap(ThisIEBitmap);

  var proc := TImageEnProc.CreateFromBitmap(ThisIEBitmap);
  try
    proc.CastColorRange(
      TColor2TRGB(PanelReplaceColorsPickedColor.Color), // OldColor
      TColor2TRGB(PanelReplacementColor.Color),         // NewColor
      cxTrackBarColorReplacementTolerance.Position);    // Tolerance

    // Copy the ThisIEBitmap to the main ImageEnView:
    ImageEnView1.ApplyBitmapToSelection(ThisIEBitmap);
  finally
    proc.Free;
  end;
finally
  ThisIEBitmap.Free;
end;


This code eliminates the unnecessary step of copying the TIEBitmap to another ImageEnView just to replace the color, and then copying it back to the main ImageEnView. It instead directly replaces the color in the TIEBitmap, and then applies it to the selection in the main ImageEnView. This reduces the amount of memory and processing needed, and makes the code more efficient.

REMARKS: This shows how I have done it in my app:

My UI: https://app.screencast.com/3ec9XA218baui

Image without selection: https://app.screencast.com/89ulS63WG8dr2

Image WITH selection: https://app.screencast.com/pUGCsd9wYLHNm

Go to Top of Page

xequte

39053 Posts

Posted - Dec 11 2022 :  20:23:21  Show Profile  Reply
Hi

CastColorRange() should already respect the selection (see the demo: \ImageEding\EveryMethod\EveryMethod.dpr).

Can you give me more detail of when this is not occurring?

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

PeterPanino

982 Posts

Posted - Dec 12 2022 :  01:15:09  Show Profile  Reply
Unfortunately, the ImageEn.chm documentation does not explicitly mention the selection restriction of CastColorRange.
Go to Top of Page

xequte

39053 Posts

Posted - Dec 12 2022 :  18:43:45  Show Profile  Reply
Hi Peter

All TImageEnProc methods are limited to the selection. If there is not selection they apply to the whole image. I'll add more detail on this to the documentation.

Nigel
Xequte Software
www.imageen.com
Go to Top of Page

PeterPanino

982 Posts

Posted - Dec 13 2022 :  10:52:40  Show Profile  Reply
Hi Nigel,

Thank you for the information and your support.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: