Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6599375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:29:00+00:00 2026-05-25T18:29:00+00:00

I have two BufferedImage objects, src and dest . Both are grayscale, src is

  • 0

I have two BufferedImage objects, src and dest. Both are grayscale, src is 1bpc (B&W basically), and dest could really be any sort of color space/bpc/etc.

I need to be able to draw some color onto dest using src as a bitmask. Basically, if the pixel in src is black, then dest should be changed to the draw color. But if the pixel in src is white, the dest should be left alone.

If it matters, I am also applying an affine transform during the draw operation.

Graphics2D g = dest.createGraphics();
// do something here???
g.drawImage(src, transform, null);
g.dispose();

In a pure B&W world this would involve a simple | of the pixel values together – but it seems like there’s probably a correct way to do this using image operations.

Gut instinct says that this is a matter of setting the Composite and some sort of alpha – but I’m at a total loss as to what values to use. I have very little experience with the more advanced aspects of graphics 2d – any pointers would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:29:01+00:00Added an answer on May 25, 2026 at 6:29 pm

    I think that I’ve come up with an effective solution using this article

    This is certainly efficient, although I’m not sure if follows best practices or not:

    BufferedImage dest; // input
    BufferedImage src; // input
    
    ...
    
    byte[] r = new byte[]{(byte)0,(byte)255}; // 255=black, we could set it to some other gray component as desired
    byte[] g = new byte[]{(byte)0,(byte)255};
    byte[] b = new byte[]{(byte)0,(byte)255};
    byte[] a = new byte[]{(byte)255,(byte)0};
    IndexColorModel bitmaskColorModel = new IndexColorModel(1, 2, r, g, b, a);
    
    BufferedImage masked = new BufferedImage(bitmaskColorModel, src.getRaster(), false, null);
    
    Graphics2D g = dest.createGraphics();
    g.drawImage(masked, transform, null);
    g.dispose();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Have two fields start and end, both are dates, I want to write a
I have two sorted arrays (can be ArrayLists, Collections, or any other data format)
I have two Generic Lists with the same objects Type T within them. For
I have two arrays of objects which are likely to have the same values,
Have two variables both containing integers: var input; var value; Need to perform arithmetic
I have two convex polygons in 3D. They are both flat on different planes,
I have two pages, Page A and Page B. Both of them have an
I have two BufferedImages I loaded in from pngs. The first contains an image,
Have two folders with approx. 150 java property files. In a shell script, how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.