How to convert 32 bit PNG to RGB565?

旧街凉风 提交于 2019-12-12 09:11:54

问题


How can I accomplish this? A programmatic solution (Objective-c) is great, but even a non-progarmmatic one is good.

I have pixelmator -> But that doesn't give you the option. I can't seem to do it with Preview either.

I have tried googling, but haven't been able to find a solution so far. The only tool I have been able to use to do this is TexturePacker, but that creates a sprite sheet.


回答1:


You can use libpng to convert the PNG image to three-byte (8:8:8) RGB. Then you can downsample to the 5:6:5 16-bit color values of RGB565. If r, g, and b are the respective 8-bit colors (stored in an unsigned char type), then the 16-bit RGB565 value is:

((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)

You can improve a tad on this by rounding instead of chopping, being careful to not overflow the values. You can also force the green value to be equal to the blue and red values when they are all equal in the original 8-bit values. Otherwise it is possible to have colors that were originally gray inadvertently take on color after conversion.




回答2:


Create Bitmap Context with color RGB565 using Quartz, paint your PNG on this context, save this bitmap context to file.




回答3:


PNG does not support a RGB565 packing. You can always apply a posterize to the image (programatically or with ImageMagick or with any image editor), which amounts to discard the lower significant bits in each channel. When saving to PNG, you will still be saving 8 bits per channel (unless you use a palette), but even then you will get an appreciable reduction in size, because of the PNG compression.

A quick example: original:

after a simple posterize with 32 levels (equivalent to a RGB555) applied with XnView

The size goes from 89KB to 47KB, with a small quality loss. In case of synthetic images with gradients, the quality loss could be much more noticiable (banding).




回答4:


I received this answer from the creator of texture packer:

you can do it from command line - see

http://www.texturepacker.com/uncategorized/batch-converting-images-to-pvr-or-pvr-ccz/

Just adjust the opt and set output to .png instead of pvr.ccz
Make sure that you do not overwrite your source images.



回答5:


According to Wikipedia, which is always right, the only 16-bit PNG is a greyscale PNG. http://en.wikipedia.org/wiki/Portable_Network_Graphics

If you just add your 32-bit (alpha) or 24-bit (no alpha) PNG to your project as normal, and then set the texture format in Cocos2D, all should be fine. The code for that is:

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];


来源:https://stackoverflow.com/questions/10118374/how-to-convert-32-bit-png-to-rgb565

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!