1

Been searching all over the place, found a number of approaches. However on a per-pixel approach (pull byte data from Raster, bit shift / multiply values for ARGB result) I get a bit stuck figuring out how to apply my Color to that pixel.

Here's the image:

ARGB bitmap

This is my current approach and code but it's foiled by a bug that will be fixed in a subsequent JRE / JDK release but isn't as of yet (8u66):

public BufferedImage applyShader(BufferedImage input) {
    BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);
    float red = new Float(Integer.toString(shader.getRed()));
    float blue = new Float(Integer.toString(shader.getBlue()));
    float green = new Float(Integer.toString(shader.getGreen()));
    if(red > 0.0f) red = 255.0f / red * 100f;
    if(blue > 0.0f) blue = 255.0f / blue * 100f;
    if(green > 0.0f) green = 255.0f / green * 100f;     
    System.out.println(red + ", " + blue + ", " + green);       

    float[] factors = new float[] {
        1.0f, 1.0f, 1.0f, 1.0f
    };

    float[] offsets = new float[] {
        red, blue, green
    };

    RescaleOp op = new RescaleOp(factors, offsets, null);
    output = op.filter(input, null);
    return output;
}

This is my previous approach with the Color values hopefully thrown in correctly (Stack Overflow source), but it hangs indefinitely (well, for several minutes before I killed it). Probably is my code, but performance could also be something to consider:

public BufferedImage applyShader(BufferedImage input) {
    BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);
    float red = new Float(Integer.toString(shader.getRed()));
    float blue = new Float(Integer.toString(shader.getBlue()));
    float green = new Float(Integer.toString(shader.getGreen()));
    int red1 = shader.getRed();
    int blue1 = shader.getBlue();
    int green1 = shader.getGreen();

    final byte[] pixels = ((DataBufferByte) input.getRaster().getDataBuffer()).getData();
    final int width = input.getWidth();
    final int height = input.getHeight();
    final boolean hasAlphaChannel = input.getAlphaRaster() != null;

    int[][] result = new int[height][width];
    if(hasAlphaChannel) {
        final int pixelLength = 4;
        for(int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
            argb += ((int) (pixels[pixel + 1] + blue1) & 0xff); // blue
            argb += (((int) (pixels[pixel + 2] + green1) & 0xff) << 8); // green
            argb += (((int) (pixels[pixel + 3] + red1) & 0xff) << 16); // red
            result[row][col] = argb;
            col++;
            if (col == width) {
                col = 0;
                row++;
            }
        }
    } else {
        final int pixelLength = 3;
        for(int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
            int argb = 0;
            argb += -16777216; // 255 alpha
            argb += ((int) (pixels[pixel] + blue1) & 0xff); // blue
            argb += (((int) (pixels[pixel + 1] + green1) & 0xff) << 8); // green
            argb += (((int) (pixels[pixel + 2] + red1) & 0xff) << 16); // red
            result[row][col] = argb;
            col++;
            if (col == width) {
                col = 0;
                row++;
            }
        }
    }

    System.out.println(input.getRaster().getWidth() + " / " + input.getRaster().getHeight());
    WritableRaster raster = Raster.createWritableRaster(input.getSampleModel(), new Point(0, 0));
    for(int i = 0; i < input.getRaster().getWidth(); i++) {
        for(int j = 0; j < input.getRaster().getHeight(); j++) {
            int k = result[i][j];
            raster.setSample(i, j, 0, k);
        }
    }

    output.setData(raster);
    return output;
}

Any advice or links? Am I missing something simple in the alternate byte to ARGB method?

This is the closest question I've found, but surprisingly doesn't even have a single vote, nevermind answer.

Community
  • 1
  • 1
Gorbles
  • 1,169
  • 12
  • 27
  • 1
    Are you sure it's that bug? You seem to be using 4 factors (ARGB) but only 3 offsets (RGB). Those arrays should probably be the same length. Also, why are you creating a new `BufferedImage` and assigning it to `output`, but later pass `null` as the second argument to `op.filter(input, null)`? This effectively just throws away the image you created. Did you mean to pass `output` as the second argument? – Harald K Oct 29 '15 at 18:18
  • By adding a `0` as the fourth offset, the code above works fine for me. You probably also want to swap the blue and the green (it's *RGB*, not RBG). :-) – Harald K Oct 29 '15 at 18:32
  • 1
    I'm not entirely sure about the effect that you want to achieve. If the goal is really a multiplication, like in a GPU shader, then I can post some example code later. Otherwise, you may just need a few lines of code like the ones in this answer: http://stackoverflow.com/a/21385150/3182664 (Just added a screenshot to show the effect) – Marco13 Oct 29 '15 at 19:17

1 Answers1

1

From my comments above, using 4 factors and 4 offsets, using the first created output and re-arranging the RBG order to RGB, I have the following code and it's working fine:

public class ShaderTest {
    private final Color shader;

    public ShaderTest(final Color shader) {
        this.shader = shader;
    }

    public  BufferedImage applyShader(BufferedImage input) {
        BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_ARGB);
        float red = shader.getRed();
        float green = shader.getGreen();
        float blue = shader.getBlue();
        if(red > 0.0f) red = 255.0f / red * 100f;
        if(green > 0.0f) green = 255.0f / green * 100f;
        if(blue > 0.0f) blue = 255.0f / blue * 100f;
        System.out.println(red + ", " + green + ", " + blue);

        float[] factors = new float[] {
                1.0f, 1.0f, 1.0f, 1.0f
        };

        float[] offsets = new float[] {
                red, green, blue, 0
        };

        RescaleOp op = new RescaleOp(factors, offsets, null);
        return op.filter(input, output);
    }

    // Test code
    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File(args[0]));
        final BufferedImage shaded = new ShaderTest(Color.ORANGE).applyShader(image);

        // Show shaded image
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("ShaderTest");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(new JLabel(new ImageIcon(shaded)));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Hah, I can't believe I missed the RBG / RGB. That's what comes from staring at the same screen for an afternoon. Thanks for the `filter` correction and additional float, that sorted it for me. – Gorbles Oct 29 '15 at 21:04