0

I would like to get the result of coloring the image. I'm just trying to paint in a slightly different way than the fill color.

Multiplies two different colors.

That is, given two colors, returns the multiplied value, and transparent color returns itself.

The code below fills the screen with the same color.

 public static BufferedImage color(BufferedImage image, Color color) {

        Graphics2D g = image.createGraphics();
        g.setPaint(color);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.dispose();

        return image;
    }

Is there a fastest way to compute the operation below for every pixel? ('c2' variable is guaranteed to always be the same color for the same object) If I simply repeated that, my program would be absurdly slow.

 public static BufferedImage color(Color c1, Color c2) {

        return new Color(
                         c1.getRed() * c2.getRed(),
                         c1.getGreen() * c2.getGreen(),
                         c1.getBlue() * c2.getBlue(),
                         c1.getAlpha() * c2.getAlpha()
        );
    }

The result I want is this:

example

LW001
  • 2,452
  • 6
  • 27
  • 36
noen
  • 1
  • 2
  • Something [like this](https://stackoverflow.com/a/221990/740553), or something else? – Mike 'Pomax' Kamermans Aug 04 '23 at 15:45
  • right. Similar to filtering image color – noen Aug 04 '23 at 15:48
  • But I'm looking for a faster way than that. This operation must be performed thousands or tens of thousands of times – noen Aug 04 '23 at 15:48
  • 1
    ...I don't mean that _question_, I mean the _answer_ I linked you to, which shows how to do this faster by extracting pixel ranges to run through much faster than if you're accessing one pixel at a time. (and there's nothing stopping you from extracting the entire image instead of only `width` pixels at a time) – Mike 'Pomax' Kamermans Aug 04 '23 at 15:49
  • understand. Doing it that way will make it faster than usual, but it will still be slow. Is faster access possible using Graphics2D? Added setting that the second image is solid color. – noen Aug 04 '23 at 15:59
  • 2
    no, it won't be. Running through an array is _insanely_ fast, so implement it, benchmark it, and hold off on any "it's not fast enough" until you've checked the timings yourself. – Mike 'Pomax' Kamermans Aug 04 '23 at 16:10
  • Thanks for the help, I'll try this – noen Aug 04 '23 at 16:13
  • [`RescaleOp`](https://docs.oracle.com/javase/8/docs/api/java/awt/image/RescaleOp.html) with 3 scale factors and offset 0? Might use native code, but will effectively also just iterate over the pixel array and do the multiplications, so I would not expect huge differences. Cleaner/less code though. – Harald K Aug 07 '23 at 15:49

0 Answers0