@@ -38,6 +38,30 @@ uint16_t RGBA8toIA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a){
3838 return output;
3939}
4040
41+ uint16_t RGBA8toRGB5A3 (uint8_t r, uint8_t g, uint8_t b, uint8_t a){
42+ uint16_t color = 0x0000 ;
43+ if (a != 0xFF ){
44+ a >>= 5 ;
45+ r >>= 4 ;
46+ g >>= 4 ;
47+ b >>= 4 ;
48+ color = 0x0000 ;
49+ color |= ((a & 0x7 ) << 12 );
50+ color |= ((r & 0xF ) << 8 );
51+ color |= ((g & 0xF ) << 4 );
52+ color |= ((b & 0xF ) << 0 );
53+ } else {
54+ r >>= 3 ;
55+ g >>= 3 ;
56+ b >>= 3 ;
57+ color = 0x8000 ;
58+ color |= ((r & 0x1F ) << 10 );
59+ color |= ((g & 0x1F ) << 5 );
60+ color |= ((b & 0x1F ) << 0 );
61+ }
62+ return color;
63+ }
64+
4165uint32_t RGB565toRGBA8 (uint16_t data) {
4266 uint8_t r = (data & 0xF100 ) >> 11 ;
4367 uint8_t g = (data & 0x07E0 ) >> 5 ;
@@ -389,9 +413,41 @@ namespace Decode {
389413
390414namespace Encode {
391415 void CMPR (bStream::CStream* stream, uint16_t width, uint16_t height, std::vector<uint8_t >& imageData){}
392- void RGB5A3 (bStream::CStream* stream, uint16_t width, uint16_t height, std::vector<uint8_t >& imageData){}
393416 void RGB565 (bStream::CStream* stream, uint16_t width, uint16_t height, std::vector<uint8_t >& imageData){}
394417
418+ void RGB5A3 (bStream::CStream* stream, uint16_t width, uint16_t height, std::vector<uint8_t >& imageData){
419+ if (imageData.size () == 0 )
420+ return ;
421+
422+ uint32_t numBlocksW = width / 4 ;
423+ uint32_t numBlocksH = height / 4 ;
424+
425+ // Iterate the blocks in the image
426+ for (int blockY = 0 ; blockY < numBlocksH; blockY++) {
427+ for (int blockX = 0 ; blockX < numBlocksW; blockX++) {
428+ // Iterate the pixels in the current block
429+ for (int pixelY = 0 ; pixelY < 4 ; pixelY++) {
430+ for (int pixelX = 0 ; pixelX < 4 ; pixelX++) {
431+ // Bounds check to ensure the pixel is within the image.
432+ if ((blockX * 4 + pixelX >= width) || (blockY * 4 + pixelY >= height))
433+ continue ;
434+
435+ // RGB values for this pixel are stored in a 16-bit integer.
436+ uint32_t destIndex = (width * ((blockY * 4 ) + pixelY) + (blockX * 4 ) + pixelX) * 4 ;
437+ uint8_t r = imageData[destIndex];
438+ uint8_t g = imageData[destIndex + 1 ];
439+ uint8_t b = imageData[destIndex + 2 ];
440+ uint8_t a = imageData[destIndex + 3 ];
441+
442+ uint16_t rgb5a3 = ColorFormat::RGBA8toRGB5A3 (r,g,b,a);
443+ stream->writeUInt16 (rgb5a3);
444+
445+ }
446+ }
447+ }
448+ }
449+ }
450+
395451 void I4 (bStream::CStream* stream, uint16_t width, uint16_t height, std::vector<uint8_t >& imageData){
396452 if (imageData.size () == 0 ) return ;
397453
0 commit comments