Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (582)
Games in Android Showcase (163)
games submitted by our members
Games in WIP (633)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ImageIO reading problem: Always get DataBufferByte and not DataBufferInt  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2013-12-28 02:32:44 »


1. Create any PNG image file of size 8x8. Name it whatever you want. (This example will use "ship.png" as the file name.) The first row must be entirely filled with transparent color.
2. Create an empty Java project.
3. Create a class folder in your project.
4. Put your PNG image in the class folder, and set the build path of your project to reference the class folder.
5. Copy the following code:

   public static void main(String[] arg) {
      int pixels[] = new int[64];
      try {
         BufferedImage img ="/ship.png"));
         pixels = img.getRGB(0, 0, 8, 8, pixels, 0, 0);

      catch (IOException e) {

6. Check the pixels array. You can see that it fills up only the first 8 elements. The rest of the array is NULL.

I don't get exactly why this line of code:
pixels = img.getRGB(0, 0, 8, 8, pixels, 0, 0);

returns BufferedImage.TYPE_3BYTE_RGB, and not what the documentation says BufferedImage.TYPE_INT_ARGB. If I were to cast the data buffer that I obtain from:
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();

It would always return DataBufferByte and not DataBufferInt, because of the above problem.

Can anyone solve this mystery?


Image proof:

The ship.png:
Offline nerb
« Reply #1 - Posted 2013-12-28 08:19:27 »

Not certain, but was the image saved as 24bit png? Try re-saving it as 32bit, then it should load up as int_argb.
Offline moogie

JGO Ninja

Medals: 16
Projects: 6
Exp: 10 years

Java games rock!

« Reply #2 - Posted 2013-12-28 08:32:21 »

Well I think you have confused the issue somewhat:

pixels = img.getRGB(0, 0, 8, 8, pixels, 0, 0);

Obviously does not return a DataBuffer... It returns exactly what it states in the API:

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.

Obviously a translation from the image instance's underlying ColourModel to the default ColourModel is performed automatically. The API also states that the only RGB (sRGB) components are returned... i.e. no alpha.

When you use

you get the ColourModel that ImageIO has used when reading the image file. No guarantees on what model it uses. In this case it uses DataBufferByte.

To get ARGB pixel data you need to create a separate BufferedImage of BufferedImage.TYPE_INT_ARGB, paint the original loaded image into this new image and then you can do:
pixels = ((DataBufferInt) newImg.getRaster().getDataBuffer()).getData();

Java4k RIP 2014
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2013-12-28 09:29:42 »

If you're trying to put image data into int[] pixels array, then you should try to put image.getWidth() for scansize. Since you're putting in 0 as scansize, you're not scanning anything, which might be causing the problem.

      try {
         BufferedImage image ="/image.png"));
         int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
      } catch (IOException e) {

That might fix it.. Idk for sure.
Offline tom_mai78101
« Reply #4 - Posted 2013-12-29 01:24:26 »

If you're trying to put image data into int[] pixels array, then you should try to put image.getWidth() for scansize. Since you're putting in 0 as scansize, you're not scanning anything, which might be causing the problem.

That might fix it.. Idk for sure.

Oh God, why...? That's the problem. Thanks for pointing that out for me. Programming alone does have its "quirks."
Offline opiop65
« Reply #5 - Posted 2013-12-29 01:33:04 »

Its not a quirk, its the way the function works. Read the docs.
Offline tom_mai78101
« Reply #6 - Posted 2013-12-29 11:51:11 »

I was referring to myself not being able to spot the obvious error I had, even when I'm reading the documentation and checking the code numerous times.  Tongue
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Ralphanese (20 views)
2015-05-29 13:07:55

Gibbo3771 (17 views)
2015-05-29 06:54:17

Ralphanese (23 views)
2015-05-28 16:52:52

theagentd (31 views)
2015-05-27 22:21:17

theagentd (37 views)
2015-05-27 22:21:08

CopyableCougar4 (27 views)
2015-05-27 19:24:50

MrMapcom (24 views)
2015-05-23 20:26:16

MrMapcom (33 views)
2015-05-23 20:23:34

Waterwolf (38 views)
2015-05-20 15:01:45

chrislo27 (44 views)
2015-05-20 03:42:21
Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!