簡體   English   中英

opengl c ++:如何將32位png文件轉換為16位565RGB文件

[英]opengl c++: How to convert a 32bit png file to a 16 bit 565RGB file

我正在嘗試將32位png文件轉換為16位文件格式,我了解如何在彼此之間轉換16位文件格式(例如RGB565 RGBA4444),但是我不確定如何從32位轉換為16位。

我的主要問題是:如何找到32位png的存儲方式(8位分別分配給R,B,G和A值)?

我如何失去精度但仍保持大致相同的值?

提前致謝

使用libpng會比手動實現更好。

我不熟悉32位png像素的確切布局,但是假設它與其他格式相對一致,則可能需要執行以下操作:

// Get the pixel from the png:
unsigned int pngPixel = getPngPixel();

unsigned char r = (pngPixel & 0xFF000000) >> 24;
unsigned char g = (pngPixel & 0x00FF0000) >> 16;
unsigned char b = (pngPixel & 0x0000FF00) >> 8;
unsigned char a = (pngPixel & 0x000000FF);

// you can collapse this to one line, but for clarity...
// masking off the least significant bits.
unsigned short rgb565Pixel = (r & 0xF8) << 11; 
rgb565Pixel |= (g & 0xFC) << 5;
rgb565Pixel |= (b & 0xF8);

// Again you could collapse this down to one line, but for clarity...
// masking off the least significant bits.
unsigned short rgba4Pixel = (r & 0xF0) << 12;
rgba4Pixel |= (g & 0xF0) << 8;
rgba4Pixel |= (b & 0xF0) << 4;
rgba4Pixel |= (a & 0xF0);  

考慮這個偽代碼。

有人可能會說,掩蓋最低有效位,特別是當從8位轉換為4位時,不是在兩者之間轉換的一種很好的方法,它們是正確的。 您可以改用轉換函數:

unsigned int convertColor(unsigned char c, unsigned int oldMax, unsigned int newMax) {
   double oldColor = c;
   double percentOfMax = oldColor / oldMax;
   return ((unsigned int)(newMax * percentOfMax)) & newMax;
}

// now we can do this
unsigned short rgba4Pixel = convertColor(r, 0xFF, 0x0F) << 12;
rgba4Pixel |= convertColor(g, 0xFF, 0x0F) << 8;
rgba4Pixel |= convertColor(b, 0xFF, 0x0F) << 4;
rgba4Pixel |= convertColor(a, 0xFF, 0x0F); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM