|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--sdl.video.SDLPixelFormat
SDLPixelFormat describes the structure of pixels in SDLSurface.m_Pixels.
The following description is taken from the SDL documentation on SDL_PixelFormat. I have translated into java, and made some modifications to use jsdl.
If you wish to do pixel level modifications on a surface, then understanding how SDL stores its color information is essential.
8-bit pixel formats are the easiest to understand. Since its an 8-bit format, we have 8 m_BitsPerPixel and 1 m_BytesPerPixel. Since m_BytesPerPixel is 1, all pixels are represented by a single byte (TODO: actually, how _is_ this handled, since bytes are signed...) which contains an index into m_Palette.colors. So, to determine the color of a pixel in a 8-bit surface: we read the color index from surface.m_Pixels and we use that index to read the SDLColor structure from surface.m_Format.m_Palette.m_Colors. Like so:
SDLSurface surface; SDLPixelFormat fmt; SDLColor color; int index; . . // Create surface . . fmt=surface.m_Format; // Check the bitdepth of the surface if(fmt.m_BitsPerPixel!=8){ throw new RuntimeException("Not an 8-bit surface.\n"); } //Get the topleft pixel index=surface.m_Pixels[0]; color=fmt.m_Palette.m_Colors[index]; System.out.println("Pixel Color-> Red: " + color.m_Red + ", Green: " + color.m_Green" + ", Blue: " + color.m_Blue + ". Index: " + index); . .
Pixel formats above 8-bit are an entirely different experience. They are considered to be "TrueColor" formats and the color information is stored in the pixels themselves, not in a palette. The mask, shift and loss fields tell us how the color information is encoded. The mask fields allow us to isolate each color component, the shift fields tell us the number of bits to the right of each component in the pixel value and the loss fields tell us the number of bits lost from each component when packing 8-bit color component in a pixel.
// Extracting color components from a 32-bit color value SDLPixelFormat fmt; SDLSurface surface; int temp, pixel; int red, green, blue, alpha; . . . fmt=surface.format; // pixel is a 32bit integer, but m_Pixels are bytes. // (not sure if I need to worry about endian'ism ) pixel=surface.m_Pixels[0] | (surface.m_Pixels[1]<<8) | (surface.m_Pixels[2]<<16); // Get Red component temp=pixel&fmt.m_RMask; // Isolate red component temp=temp>>fmt.m_RShift; // Shift it down to 8-bit temp=temp<<fmt.m_RLoss; // Expand to a full 8-bit number red=temp; // Get Green component temp=pixel&fmt.m_GMask; // Isolate green component temp=temp>>fmt.m_GShift;// Shift it down to 8-bit temp=temp<<fmt.m_GLoss; // Expand to a full 8-bit number green=temp; // Get Blue component temp=pixel&fmt.m_BMask; // Isolate blue component temp=temp>>fmt.m_BShift;// Shift it down to 8-bit temp=temp<<fmt.m_BLoss; // Expand to a full 8-bit number blue=temp; // Get Alpha component temp=pixel&fmt.m_AMask; // Isolate alpha component temp=temp>>fmt.m_AShift;// Shift it down to 8-bit temp=temp<<fmt.m_ALoss; // Expand to a full 8-bit number alpha=temp; System.out.println("Pixel Color -> R: " + red + ", G: " + green + ", B: " + blue + ", A: " + alpha); . . .
Field Summary | |
byte |
m_ALoss
Number of low-order bits ignored by the alpha component. |
byte |
m_Alpha
|
int |
m_AMask
Binary mask to retrieve alpha component. |
byte |
m_AShift
Bit offset of the alpha component. |
byte |
m_BitsPerPixel
Number of bits used to represent each pixel in a surface. |
byte |
m_BLoss
Number of low-order bits ignored by the blue component. |
int |
m_BMask
Binary mask to retrieve blue component. |
byte |
m_BShift
Bit offset of the blue component. |
byte |
m_BytesPerPixel
The number of bytes used to represent each pixel in a surface. |
int |
m_ColorKey
|
byte |
m_GLoss
Number of low-order bits ignored by the green component. |
int |
m_GMask
Binary mask to retrieve green component. |
byte |
m_GShift
Bit offset of the green component. |
SDLPalette |
m_Palette
Palette, if using a paletted format. |
byte |
m_RLoss
Number of low-order bits ignored by the red component. |
int |
m_RMask
Binary mask to retrieve red component. |
byte |
m_RShift
Bit offset of the red component. |
Constructor Summary | |
SDLPixelFormat(int handle)
|
Method Summary | |
void |
freePixelFormat()
|
int |
getHandle()
|
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public SDLPalette m_Palette
public byte m_BitsPerPixel
public byte m_BytesPerPixel
public int m_RMask
public int m_GMask
public int m_BMask
public int m_AMask
public byte m_RShift
public byte m_GShift
public byte m_BShift
public byte m_AShift
public byte m_RLoss
public byte m_GLoss
public byte m_BLoss
public byte m_ALoss
public int m_ColorKey
public byte m_Alpha
Constructor Detail |
public SDLPixelFormat(int handle)
Method Detail |
public int getHandle()
public void freePixelFormat()
public java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |