Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36511

Re: BufferedImage and int[] array

From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: BufferedImage and int[] array
Date 2017-04-22 11:28 -0500
Organization A noiseless patient Spider
Message-ID <odg05k$e1f$1@dont-email.me> (permalink)
References <5a1d0bab-5ae7-45a1-9c8f-344183ed48ba@googlegroups.com>

Show all headers | View raw


On 4/21/2017 10:28, Rick C. Hodgin wrote:
> I'm using JDK 1.8 on Linux.
>
> I have a PNG file which is ARGB when I load it in GIMP, and would like
> to obtain an array of int pixels to alter its contents directly from
> within my app.
>
> I am able to obtain byte[], but I don't know how to translate it to
> int[] for direct 4-byte access per pixel.
>
> I read on Stack Overflow that they aren't compatible unless the img
> created is ARGB as well, meaning the ImageIO.read(new File("myfile.png"))
> may have returned only a 24-bit RGB image, and not with the transparency
> layer/channel.  I also see on Stack Overflow that this may be a known
> bug as other people have had this same issue in the past.
>
> If true, how would I force it to read in and create it as an ARGB layer
> even if it doesn't think the alpha channel is there?  And if I cannot,
> do I read it in in whatever format it's in, create a second image of the
> same size but with 32-bit ARGB for the color depth, and then do some kind
> of BitBlt operation to paint the pixel bits into the other image?
>
> I come from a Win32 GDI and C/C++ background where I handle most of the
> low-level drawing through custom functions.  I'm not sure what the
> equivalent operation is in Java.
>
> -----
> I am basically trying to load an image, obtain an array to the raw
> pixel data so I can manipulate each pixel directly in various ways,
> including a colorize() algorithm, invert(), putBorder(), etc.
>
> Thank you,
> Rick C. Hodgin
>

What you want is a RescaleOp.  In test6 I create an image with three 
bars of red, green and blue.  Using test5 I invert the pixel values as 
you specified further down in these posts with a RescaleOp and the 
result is an image with three bars of cyan, magenta and yellow.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class test6 {
     public static void main(String... args) throws Exception {
         BufferedImage img =
          new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
         Graphics2D g = img.createGraphics();
         g.setColor(Color.RED);
         g.fillRect(0,0,213,480);
         g.setColor(Color.GREEN);
         g.fillRect(214,0,214,480);
         g.setColor(Color.BLUE);
         g.fillRect(428,0,213,480);
         g.dispose();
         ImageIO.write(img,"JPEG",new File("bars.jpg"));
     }
}

import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class test5 {
     public static void main(String... args) throws Exception {
         BufferedImage img = ImageIO.read(new File("bars.jpg"));
         RescaleOp op = new RescaleOp(-1.0f,255f,null);
         BufferedImage chg = op.filter(img,null);
         ImageIO.write(chg,"JPEG",new File("bars2.jpg"));
     }
}


-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 08:28 -0700
  Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 08:54 -0700
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 09:03 -0700
      Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 10:41 -0700
        Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 10:46 -0700
        Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 11:13 -0700
          Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 12:12 -0700
          Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:24 -0700
            Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:37 -0700
              Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:40 -0700
              Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:54 -0700
                Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 13:59 -0700
              Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-21 13:58 -0700
                Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-21 14:06 -0700
  Re: BufferedImage and int[] array Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-22 11:28 -0500
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-24 07:33 -0700
      Re: BufferedImage and int[] array Eric Douglas <e.d.programmer@gmail.com> - 2017-04-24 08:04 -0700
    Re: BufferedImage and int[] array "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-04-24 08:17 -0700
      Re: BufferedImage and int[] array Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-04-24 11:25 -0500

csiph-web