Results 1 to 1 of 1
  1. #1
    Goseph is offline Member
    Join Date
    Dec 2008
    Rep Power

    Default manual BMP rendering colour issues

    I am reading in a .BMP file with a datainputstream and trying to create a bufferedImage
    containing the data which I then draw on a JPanel.
    It basically works but the colours are messed up as you can see:

    Gray scale Lena.bmp (8-bit, indexed):
    How I render her:
    Coloured Lena.bmp (24 bit, non-indexed):
    how I render her:

    Here are the relevant bits of code:

    Java Code:
    	//here my BufferedImage "img" is created,
    	//occassionally reading relevant image parameters from the next class
    	if (bmp.bitsPerPixel == 8)
    		img = new BufferedImage(bmp.width, bmp.height, BufferedImage.TYPE_BYTE_INDEXED, bmp.getPalette());
    	else if (bmp.bitsPerPixel == 24)
    		img = new BufferedImage(bmp.width, bmp.height, BufferedImage.TYPE_INT_RGB);
    	img.setRGB(0, 0, bmp.width, bmp.height, bmp.getImageData(), 0, bmp.width);
    	/******************************** CLASS FOR EXTRACTING IMAGE INFO *********************************** **/
    	//the relevant extractor methods/variables from the class
    	//that processes the image's dataInputStream "data"
    	private int[] imageData;	//array of image pixels
    								//with usual coordinate system (origin at top left hand corner of image)
    	private IndexColorModel palette;
    	public int[] getImageData() { return imageData; } //Getters for pixel data and palette
    	public IndexColorModel getPalette() { return palette; }
    	private IndexColorModel readPalette() throws IOException	//extracts palette as a ColorModel
    	{															//assumes that pointer to data
    		byte[] r = new byte[noColours];							//is at start of palette
    		byte[] g = new byte[noColours];
    		byte[] b = new byte[noColours];
    		for (int i = 0; i < noColours; ++i) {
    			b[i] = data.readByte();
    			g[i] = data.readByte();
    			r[i] = data.readByte();
    		return new IndexColorModel(8, 256, r, g, b);
    	private void readImageData() throws IOException		//-extracts image data assuming
    	{													//-that pointer is at start of image data
    		imageData = new int[height*width];
    		int padding = (32-((width*bitsPerPixel)%32))%32;//length of row padding in bits
    		for (int y = height - 1; y >= 0; --y) {
    			for (int x = 0; x < width; ++x) {
    				byte b = data.readByte();
    				switch (bitsPerPixel) {
    					case 8: imageData[(y * width) + x] = (b > 0) ? b : b + 256; break;
    					case 24:byte g = data.readByte();
    							byte r = data.readByte();
    							imageData[(y * width) + x] = b | (g << 8) | (r << 16);
    			data.skipBytes(padding/8);	//skip end of row padding
    Any BufferedImage ninjas out there who understand whats going on?

    PS. My post count isn't high enough to insert images, but if you need to see whats going on then you can just copy those links at the top into a browser
    Last edited by Goseph; 02-24-2009 at 08:28 PM. Reason: adding inconvenience apology note

Similar Threads

  1. Help with JTable rendering
    By daniel2008 in forum AWT / Swing
    Replies: 8
    Last Post: 01-18-2009, 03:51 AM
  2. JLabel Rendering
    By random4534 in forum New To Java
    Replies: 3
    Last Post: 12-16-2008, 08:55 AM
  3. figuring out the current colour of my appelet
    By vortexprogramming in forum New To Java
    Replies: 2
    Last Post: 07-15-2008, 04:16 AM
  4. Manual, good tutorial
    By sin in forum JavaServer Faces (JSF)
    Replies: 5
    Last Post: 05-25-2008, 06:16 AM
  5. Manual of websphere
    By Marcus in forum Web Frameworks
    Replies: 1
    Last Post: 06-27-2007, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts