Skip to content

Commit

Permalink
all: implement new BR_PMT_BGR_565 texture format
Browse files Browse the repository at this point in the history
Mainly for PSP.
  • Loading branch information
vs49688 committed Aug 20, 2024
1 parent 7a46122 commit 67ef0a2
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/inc/pixelmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ enum {
*/
BR_PMT_RGBA_8888_ARR,

/**
* \brief 16-bit 'true colour' BGR, 5 bits red and blue, 6 bits green.
*
* \note 32-bit encoding:
* \code 0000000000000000bbbbbggggggrrrrr \endcode
*
* \note First Four Bytes of Left Hand Pixel:
* \code gggrrrrr bbbbbggg ........ ........ \endcode
*
* \remark The left hand byte is the byte at br_pixelmap::pixels.
*/
BR_PMT_BGR_565,

BR_PMT_MAX,

BR_PMT_AINDEX_44 = BR_PMT_INDEXA_44,
Expand Down
31 changes: 31 additions & 0 deletions core/pixelmap/pmclone.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ void rgb_565_write(br_uint_8 *pixels, br_colour colour)
*((br_uint_16 *)pixels) = BR_COLOUR_RGB_565(r, g, b);
}

/*
* BR_PMT_BGR_565
*/

br_colour bgr_565_read(const br_uint_8 *pixels, const br_pixelmap *pm)
{
br_uint_8 r, g, b;

r = BR_BLU_565(*((br_uint_16 *)pixels));
g = BR_GRN_565(*((br_uint_16 *)pixels));
b = BR_RED_565(*((br_uint_16 *)pixels));

r = (r << 3) | (r >> 2);
g = (g << 2) | (g >> 4);
b = (b << 3) | (b >> 2);

return BR_COLOUR_RGBA(r, g, b, 255);
}

void bgr_565_write(br_uint_8 *pixels, br_colour colour)
{
br_uint_8 r, g, b;

r = BR_RED(colour);
g = BR_GRN(colour);
b = BR_BLU(colour);

*((br_uint_16 *)pixels) = BR_COLOUR_RGB_565(b, g, r);
}

/*
* BR_PMT_RGB_888
*/
Expand Down Expand Up @@ -332,6 +362,7 @@ br_pixelmap_converter br_pixelmap_converters[] = {
CONVERTER(NULL, NULL, BR_PMT_ARGB_1555, "BR_PMT_ARGB_1555"),
CONVERTER(argb_4444_read, argb_4444_write, BR_PMT_ARGB_4444, "BR_PMT_ARGB_4444"),
CONVERTER(rgba_8888_arr_read, rgba_8888_arr_write, BR_PMT_RGBA_8888_ARR, "BR_PMT_RGBA_8888_ARR"),
CONVERTER(bgr_565_read, bgr_565_write, BR_PMT_BGR_565, "BR_PMT_BGR_565"),
};
#undef CONVERTER
// clang-format on
Expand Down
1 change: 1 addition & 0 deletions core/pixelmap/pmfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static br_file_enum_member pixelmap_type_FM[] = {
_ENUM_MEMBER(BR_PMT_ARGB_1555),
_ENUM_MEMBER(BR_PMT_ARGB_4444),
_ENUM_MEMBER(BR_PMT_RGBA_8888_ARR),
_ENUM_MEMBER(BR_PMT_BGR_565),
};
// clang-format on

Expand Down
1 change: 1 addition & 0 deletions core/pixelmap/pmmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct pm_type_info pmTypeInfo[] = {
[BR_PMT_ARGB_4444] = {.bits = 16, .file_size = 2, .align = 2, .channels = BR_PMCHAN_INDEX | BR_PMCHAN_ALPHA},

[BR_PMT_RGBA_8888_ARR] = {.bits = 32, .file_size = 4, .align = 1, .channels = BR_PMCHAN_RGB | BR_PMCHAN_ALPHA },
[BR_PMT_BGR_565] = {.bits = 16, .file_size = 2, .align = 2, .channels = BR_PMCHAN_RGB },
};
// clang-format on

Expand Down
6 changes: 6 additions & 0 deletions drivers/glrend/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ br_error VIDEOI_BrPixelmapGetTypeDetails(br_uint_8 pmType, GLint *internalFormat
*type = GL_UNSIGNED_SHORT_5_6_5;
*elemBytes = 2;
break;
case BR_PMT_BGR_565:
*internalFormat = GL_RGB;
*format = GL_BGR;
*type = GL_UNSIGNED_SHORT_5_6_5;
*elemBytes = 2;
break;
case BR_PMT_RGB_888:
*internalFormat = GL_RGB;
#if BR_ENDIAN_LITTLE
Expand Down
1 change: 1 addition & 0 deletions drivers/sdl2dev/outfcty.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const static struct {
{.format = SDL_PIXELFORMAT_RGB555, .bpp = 15, .type = BR_PMT_RGB_555 },
{.format = SDL_PIXELFORMAT_BGR555, .bpp = 15, .type = BR_PMT_BGR_555 },
{.format = SDL_PIXELFORMAT_RGB565, .bpp = 16, .type = BR_PMT_RGB_565 },
{.format = SDL_PIXELFORMAT_BGR565, .bpp = 16, .type = BR_PMT_BGR_565 },
#if BR_ENDIAN_LITTLE
{.format = SDL_PIXELFORMAT_BGR24, .bpp = 24, .type = BR_PMT_RGB_888 },
#else
Expand Down
Binary file added examples/dat/smpte_type34_bgr_565.pix
Binary file not shown.
6 changes: 6 additions & 0 deletions examples/devpmtest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ static void draw_pixelbypixel(br_pixelmap *dest, br_pixelmap *pm, br_int_32 base
src_bpp = 8;
break;
case BR_PMT_RGB_565:
case BR_PMT_BGR_565:
src_bpp = 16;
break;

Expand Down Expand Up @@ -717,6 +718,10 @@ static void draw_pixelbypixel(br_pixelmap *dest, br_pixelmap *pm, br_int_32 base
r = BR_RED_565(col) << 3;
g = BR_GRN_565(col) << 2;
b = BR_BLU_565(col) << 3;
} else if(pm->type == BR_PMT_BGR_565) {
b = BR_RED_565(col) << 3;
g = BR_GRN_565(col) << 2;
r = BR_BLU_565(col) << 3;
} else {
r = BR_RED(col);
g = BR_GRN(col);
Expand Down Expand Up @@ -849,6 +854,7 @@ static br_drawtest tests[] = {
MAKE_SMPTE_TEST("smpte_type03_index_8.pix", BR_PMT_INDEX_8, ", embedded palette"),
MAKE_SMPTE_TEST("smpte_type04_rgb_555.pix", BR_PMT_RGB_555, ""),
MAKE_SMPTE_TEST("smpte_type05_rgb_565.pix", BR_PMT_RGB_565, ""),
MAKE_SMPTE_TEST("smpte_type34_bgr_565.pix", BR_PMT_BGR_565, ""),
MAKE_SMPTE_TEST("smpte_type06_rgb_888.pix", BR_PMT_RGB_888, ""),
MAKE_SMPTE_TEST("smpte_type07_rgbx_888.pix", BR_PMT_RGBX_888, ""),
MAKE_SMPTE_TEST("smpte_type08_rgba_8888.pix", BR_PMT_RGBA_8888, ""),
Expand Down
1 change: 1 addition & 0 deletions examples/mkres/mkres.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int main(int argc, char **argv)
{.type = BR_PMT_INDEX_8, .name = "index_8", .width = 672},
{.type = BR_PMT_RGB_555, .name = "rgb_555", .width = 672},
{.type = BR_PMT_RGB_565, .name = "rgb_565", .width = 672},
{.type = BR_PMT_BGR_565, .name = "bgr_565", .width = 672},
{.type = BR_PMT_RGB_888, .name = "rgb_888", .width = 672},
{.type = BR_PMT_RGBA_8888, .name = "rgba_8888", .width = 672},
{.type = BR_PMT_RGBX_888, .name = "rgbx_888", .width = 672},
Expand Down
19 changes: 19 additions & 0 deletions examples/mkres/smpte.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ const static br_smpte_colours smpte_rgb_565 = {
.plugeR = BR_COLOUR_565(29, 29, 29),
};

const static br_smpte_colours smpte_bgr_565 = {
.grey40 = BR_COLOUR_565(104, 104, 104),
.white75 = BR_COLOUR_565(180, 180, 180),
.yellow75 = BR_COLOUR_565(16, 180, 180),
.cyan75 = BR_COLOUR_565(180, 180, 16),
.green75 = BR_COLOUR_565(16, 180, 16),
.magenta75 = BR_COLOUR_565(180, 16, 180),
.red75 = BR_COLOUR_565(16, 16, 180),
.blue75 = BR_COLOUR_565(180, 16, 16),
.black75 = BR_COLOUR_565(16, 16, 16),
.white100 = BR_COLOUR_565(235, 235, 235),
.plusQ = BR_COLOUR_565(118, 16, 72),
.plusI = BR_COLOUR_565(16, 52, 106),
.minusI = BR_COLOUR_565(106, 70, 16),
.plugeL = BR_COLOUR_565(9, 9, 9),
.plugeR = BR_COLOUR_565(29, 29, 29),
};

const static br_smpte_colours smpte_rgba_8888 = {
.grey40 = BR_COLOUR_RGBA(104, 104, 104, 255),
.white75 = BR_COLOUR_RGBA(180, 180, 180, 255),
Expand Down Expand Up @@ -313,6 +331,7 @@ const static struct {
{.type = BR_PMT_INDEX_8, .colours = &smpte_index_8 },
{.type = BR_PMT_RGB_555, .colours = &smpte_rgb_555 },
{.type = BR_PMT_RGB_565, .colours = &smpte_rgb_565 },
{.type = BR_PMT_BGR_565, .colours = &smpte_bgr_565 },
{.type = BR_PMT_RGB_888, .colours = &smpte_rgba_8888}, /* Ignores the alpha part. */
{.type = BR_PMT_RGBA_8888, .colours = &smpte_rgba_8888},
{.type = BR_PMT_RGBX_888, .colours = &smpte_rgba_8888}, /* Ignores the alpha part. */
Expand Down
29 changes: 29 additions & 0 deletions tools/texconv/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,35 @@ void T_Write_RGB_565(char *pixels, br_float fract, br_colour new_pixel)
*((br_uint_16 *)pixels) = temp_colour;
}

br_colour T_Read_BGR_565(char *pixels, br_float fract)
{
/* colour format B G R B G R B G R - 2 bytes per pixel */

br_colour colour;
br_uint_16 temp_colour;
br_uint_8 r, g, b;

temp_colour = *((br_uint_16 *)pixels);

b = (temp_colour >> 8) & 0xf8;
g = (temp_colour >> 3) & 0xf8;
r = (temp_colour << 3) & 0xf8;

colour = BR_COLOUR_ARGB(0, r, g, b);

return colour;
}

void T_Write_BGR_565(char *pixels, br_float fract, br_colour new_pixel)
{
/* colour format B G R B G R B G R - 2 bytes per pixel */
br_uint_16 temp_colour;

temp_colour = ((BR_BLU(new_pixel) >> 3) << 11) | ((BR_GRN(new_pixel) >> 2) << 5) | ((BR_RED(new_pixel) >> 3));

*((br_uint_16 *)pixels) = temp_colour;
}

br_colour T_Read_BGR_555(char *pixels, br_float fract)
{
/* colour format B G R B G R B G R - 2 bytes per pixel */
Expand Down
2 changes: 2 additions & 0 deletions tools/texconv/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ br_colour T_Read_RGBX_888(char *pixels, br_float fract);
br_colour T_Read_RGB_555(char *pixels, br_float fract);
br_colour T_Read_RGB_565(char *pixels, br_float fract);
br_colour T_Read_BGR_555(char *pixels, br_float fract);
br_colour T_Read_BGR_565(char *pixels, br_float fract);
br_colour T_Read_RGBA_4444(char *pixels, br_float fract);
br_colour T_Read_ARGB_4444(char *pixels, br_float fract);

Expand All @@ -28,6 +29,7 @@ void T_Write_RGBX_888(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_RGB_555(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_RGB_565(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_BGR_555(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_BGR_565(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_ARGB_4444(char *pixels, br_float fract, br_colour new_pixel);
void T_Write_RGBA_4444(char *pixels, br_float fract, br_colour new_pixel);

Expand Down
1 change: 1 addition & 0 deletions tools/texconv/pmaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ t_pixelmap_type_info PixelmapTypes[] = {

{T_Read_RGBA_4444, T_Write_RGBA_4444, T_Read_RGBA_4444, 0, BR_PMT_RGBA_4444, "BR_PMT_RGBA_4444", "RGBA 16 bit 4 bits per colour component" },
{NULL, NULL, NULL, 0, BR_PMT_RGBA_8888_ARR, "BR_PMT_RGBA_8888_ARR", "Not supported" },
{T_Read_BGR_565, T_Write_BGR_565, T_Read_BGR_565, 0, BR_PMT_BGR_565, "BR_PMT_BGR_565", "BGR 16 bit 5,6,5 bit colour components" },
};
// clang-format on

Expand Down

0 comments on commit 67ef0a2

Please sign in to comment.