Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (768)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  BufferedImage and TYPE_BYTE_GRAY  (Read 6479 times)
0 Members and 1 Guest are viewing this topic.
Offline PeterB

Junior Devvie


Exp: 15 years



« Posted 2005-11-16 00:48:04 »

Folks,
I'm loading an 8-bit grayscale image (RAW) into a one dimensional array representing pixels.
Knowing the width and height of the image I'm using *BufferedImage* to create an *Image* object and then paint this onto the screen.
(code below)

The problem is that the loaded image (being 8bit) contains 255 colors yet the displayed image has only 19 colors:

What it should look like:


How it is displayed:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
Image image;

   // snippet
        int width = 513;
        int height = 513;
        byte[] b = null;
        int[] pixels = new int [(width*height)];
        b = generatePixels (width, height);
       
        for (int i=0; i < b.length; i++)
        {
            pixels[i] = b[i] & 0xFF;
        }

        BufferedImage recoverImg = new BufferedImage (width,height,BufferedImage.TYPE_BYTE_GRAY);
        myImg.setRGB (0,0,width,height,pixels,0,width);
        image = myImg.getScaledInstance (width, height, Image.SCALE_SMOOTH);
       
        Frame frame = new Frame ("Test");
        frame.add (this);
        frame.setSize (600, 600);
        frame.setVisible (true);
        this.repaint ();


I don't have a problem with the RAW loader - outputting the values of the array shows all colours from 0 to 255 are present. It's the displaying that seems to be the issue. Any ideas?

Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #1 - Posted 2005-11-16 21:14:45 »

Quote
1  
image = myImg.getScaledInstance (width, height, Image.SCALE_SMOOTH);


I think this is your problem. Is there a reason you need to use getScaledInstance?
You don't change the size of the image.

Thanks,
  Dmitri
Java2D Team
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2005-11-16 21:27:36 »

myImg.setRGB (0,0,width,height,pixels,0,width);

There is the problem. See api doc.

"Sets an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, into a portion of the image data. Color conversion takes place if the default model does not match the image ColorModel."

That explains why the luminance is only a third of what it should be.

You could either do some bit op voodoo... or you could try MemoryImageSource... or WritableRaster...

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #3 - Posted 2005-11-17 22:50:54 »

I think oNyx is correct. You need to set the pixels to the raster to avoid color conversion:
instead of Img.setRGB use
1  
Img.getRaster().setPixels(0, 0, width, height, pixels);


Thanks,
Dmitri
Offline PeterB

Junior Devvie


Exp: 15 years



« Reply #4 - Posted 2005-11-19 19:09:23 »

Thanks for the assistance guys! Using .getRaster().setPixels (..) seems to have fixed the problem.

Pete


Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1338 views)
2018-06-10 19:43:48

EgonOlsen (1229 views)
2018-06-10 19:43:44

EgonOlsen (939 views)
2018-06-10 19:43:20

DesertCoockie (1380 views)
2018-05-13 18:23:11

nelsongames (1112 views)
2018-04-24 18:15:36

nelsongames (1374 views)
2018-04-24 18:14:32

ivj94 (2119 views)
2018-03-24 14:47:39

ivj94 (1268 views)
2018-03-24 14:46:31

ivj94 (2285 views)
2018-03-24 14:43:53

Solater (807 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!