Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 640 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2013-12-28 02:32:44 »

Instructions:

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   public static void main(String[] arg) {
      int pixels[] = new int[64];
      try {
         BufferedImage img = ImageIO.read(Art.class.getResourceAsStream("/ship.png"));
         pixels = img.getRGB(0, 0, 8, 8, pixels, 0, 0);
      }

      catch (IOException e) {
         e.printStackTrace();
      }
   }

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?

EDIT:

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 Knight


Medals: 12
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:

1  
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:

Quote
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
1  
img.getRaster().getDataBuffer()

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:
1  
pixels = ((DataBufferInt) newImg.getRaster().getDataBuffer()).getData();


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online 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.

1  
2  
3  
4  
5  
6  
7  
8  
      try {
         BufferedImage image = ImageIO.read(Game.class.getResource("/image.png"));
         int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
         
         System.out.println(pixels[0]);
      } catch (IOException e) {
         e.printStackTrace();
      }


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

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!