Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  Getting / Setting image pixels  (Read 1769 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2011-11-29 03:30:31 »

Hi, I am trying to dynamically edit an image.
I have put the pixels into a byte buffer and then made a byte array from that.
How can i set and get a single pixel at x,y?

Thanks

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
   public void GetPixelsFromImage()
   {
      int size = m_iWidth*m_iHeight*16;
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID);
      m_bbPixels = ByteBuffer.allocateDirect(size);
      GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_INT, m_bbPixels);
      m_pixels = new byte[size];
      m_bbPixels.get(m_pixels);
   }
   
   public void SetPixels()
   {
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID);
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, m_iWidth, m_iHeight, 0,GL11.GL_RGBA, GL11.GL_INT, m_bbPixels);
   }
   
   public void CopyPixelsToByteBuffer()
   {
      m_bbPixels.put(m_pixels);
   }
   
   public void SetPixel(int x, int y, int rgb)
   {
      ///???????
   }

   public int GetPixel(int x, int y)
   {
      ///???????
   }
Offline theagentd
« Reply #1 - Posted 2011-11-29 04:21:39 »

1  
2  
3  
4  
public void setPixel(int x, int y, int rgba){
    int index = (y * width + x) * 4; //4 is for the 4 components
    //put data at index
}

I'm veeeery suspicious of your use of GL_INT...

Myomyomyo.
Offline gbeebe
« Reply #2 - Posted 2011-11-29 04:30:52 »

edit:   I was wrong.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roland
« Reply #3 - Posted 2011-11-29 04:52:43 »

Thanks theagentd  Smiley

1  
2  
3  
4  
public void setPixel(int x, int y, int rgba){
    int index = (y * width + x) * 4; //4 is for the 4 components
    //put data at index
}

I'm veeeery suspicious of your use of GL_INT...

So pixels[index+0] = red (0 - 255?)
+1 = g
+2 = b
+3 = a?

What's wrong with GL_INT? I stole the code from http://lwjgl.org/wiki/index.php?title=Render_to_Texture_with_Frame_Buffer_Objects_%28FBO%29
Offline theagentd
« Reply #4 - Posted 2011-11-29 05:18:13 »

GL_INT is signed. I'd use GL_UNSIGNED_BYTE. If you use bytes instead, the index + <0-3> is correct. If you want to, you can wrap your ByteBuffer in an IntBuffer, and just set a whole pixel with a single command.

Myomyomyo.
Offline roland
« Reply #5 - Posted 2011-11-29 05:46:39 »

Ok, thanks again.
I tried wrapping the ByteBuffer in an IntBuffer, but when trying to set the pixels back with glTexImage2D using the intbuffer instead of the bytebuffer, it said the intbuffer wasn't big enough Huh
Offline theagentd
« Reply #6 - Posted 2011-11-29 08:33:39 »

You're supposed to set all channels of a pixel in one command -> one set is one pixel. You also shouldn't multiply the index by 4, as an int is already four bytes.

Myomyomyo.
Offline roland
« Reply #7 - Posted 2011-11-30 03:55:30 »

Thanks. Ok it's almost working, but when I get all the pixel colours and write them onto another image (to test that its working), the image turns out blue. Other than that, it's fine. The shading is correct
Image:
http://i41.tinypic.com/13zv9cj.jpg

where it is blue it should be brown(its a ground texture). Do you have any idea why this could be?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      for (int x = 0; x < width; x++)
      {
         for (int y = 0; y < height; y++)
         {
            int colour = m_texture.GetPixelAt(x, y);
            int r = colour % 256;
            int g = (int)(colour / 256) % 256;
            int b = (int)((colour / 256) / 256) % 256;
            int a = (int)(((colour / 256) / 256)/256) % 256; //doesnt get the right alpha either
           
            GL11.glColor3b((byte)r,(byte)g,(byte)b);
            Graphics.DrawSquare(x,y, 1, 1);
         }
      }
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2011-11-30 04:36:59 »

How about a cleaner solution:
1  
2  
3  
4  
byte r = (byte)((colour >>> 24) & 0xff);
byte g = (byte)((colour >>> 16) & 0xff);
byte b = (byte)((colour >>> 8) & 0xff);
byte a = (byte)(colour & 0xff);

Offline roland
« Reply #9 - Posted 2011-11-30 06:12:06 »

Thanks for the reply ra4king Smiley
As well as being cleaner, that fixed most of it. Although the alpha is 0 at every pixel when it should be 255 and the pixels are too bright. It must have something to do with my offscreen rendering code. I just have no idea what Huh
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-11-30 08:09:21 »

That's because byte values only go from -128 to 127. However, I do believe OpenGL correctly translates that to 0 - 255.

EDIT: wait...255 should be -1, how do you get 0?

Offline roland
« Reply #11 - Posted 2011-11-30 09:15:54 »

Im just guessing. Some pixels are very transparent, the rest you can't see at all
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.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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

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
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!