Understanding pixel format in cocos2d v0.7.3
转载自:http://www.cocos2d-iphone.org/archives/61
Since cocos2d v0.7.3, you can specify the texture’spixel formatof your PNG/TIFF/BMP/GIF
images.
The texture’spixel formatis the way the image is stored in GPU memory.
Possible pixel formats:
- RGBA8888 (32-bit)(kTexture2DPixelFormat_RGBA8888)
- RGBA4444 (16-bit)(kTexture2DPixelFormat_RGBA4444)
- RGB5_A1 (16-bit)(kTexture2DPixelFormat_RGB5A1)
- RGB565 (16-bit)(kTexture2DPixelFormat_RGB565)
RGBA8888:
- 8bits are assigned to theredchannel,8bits to thegreenchannel,8bits to thebluechannel and8bits to thealphachannel.
- Use this pixel format when you need themaximum possible qualityfor your image.
- But it will consume the double of memory compared to 16-bit textures. Memory is a precious resource on the iPhone
- Usually it is also slower to render.
- Useful for: background image of your intro scene, and for images with lots of gradient colors
RGBA4444:
- 4bits are assigned to theredchannel,4bits to thegreenchannel,4bits to thebluechannel, and4bits to thealphachannel
- It gives you good quality in all channels, good speed, good memory consumption.
- Useful for:spritesthat have different values of transparency
RGB5A1:
- 5bits are assigned to theredchannel,5bits to thegreenchannel,5bits to thebluechannel, andonly1bit to thealphachannel
- It gives you good quality in RGB channels but poor quality on the A channel. It also gives you good speed and good memory consumption.
- Useful for:spritesthat have transparent parts, but the transparency is either On or Off
RGB565:
- 5bits are assigned to theredchannel,6bits to thegreenchannel, and5bits to thebluechannel. It hasno alphachannel support
- It gives you thebest possible quality for 16-bittextures, butwithout alphachannel support.
- Useful for: background images in your game.
The default pixel format in v0.7.3 is RGBA8888.
How to use it:
FAQ:
-
Q:Can I use this technique for PVRTC images ?
- A:No, PVRTC images have their own format. PVRTC images are faster and they consume less memory since they are either 2-bit or 4-bit textures but the quality is not that great.
- Q:If I use 16-bit textures, will my game load faster ?
-
A:The
texture pixel format has nothing to do with the loading times of your game. The Pixel Format is how the image is stored in the GPU memory. If you want faster loading times you should reduce the size of your .PNG/GIF/TIFF/TMP image file.
- Q:Should I use RGB565 for the images that are compressed by XCode ?
-
A:When
XCode compresses the PNG images, it will create a compressed PNG image that has it’s alpha channel premultiplied in the RGB channels. The PNG format is RGB565, but you should NOT confuse a PNG format with a texture pixel format. A PNG image is understood by
Photoshop and cocos2d, but the GPU knows nothing about PNG images. The PNG images need to be converted into a Texture (the format understood by the GPU). The answer is: it depends on what you want. The Image (PNG,GIF,TIFF,BMP) format is independent of the
Texture format.
- Q:If I create a PNG/BMP/TIFF/GIF image without alpha channel, can I change the texture pixel format ?
- A:If your PNG/BMP/TIFF/GIF image has no alpha channel (premultiplied or not), then the pixel format that will be used for the texture is RGB565. It can’t be changed using the API but you can change by modifying the Texture2D.m file
- Q:Can I change the pixel format once the texture was created.
- A:No. Once the Texture was created you can’t modify the pixel format. But you can create new textures from the same image using different pixel formats.Just remember to remove the image from the TextureMgr!