help : alpha values not read correctly from bmp?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Stellar
Posts: 48
Joined: Mon Dec 12, 2005 9:13 am

help : alpha values not read correctly from bmp?

Post by Stellar »

:: UPDATE :: ok, i used a hex editor (xvi32, my fav) to check the image in binary, it actually reads BA FE CA AE. the AE at the end is what's important. when you edit in photoshop, it reads BE from the color 'info' toolbox. Perhaps i dont understand how to correctly set the values for the alpha mask. Ill have to look into it.
:: END UPDATE ::

Ok weird problem, i have attached my function, bmp with alpha channel, and results. The image has only one pixel, and the pixel reads in hex (RGBA order) 'CAFEBABE' ( no i dont like java, thats the only think i took out of it :P ) of course in psp image format that would read 'BECAFEBA' since alpha comes first.

link to image

at any rate, when i read in the RGB values, data is ok, but when i read the A value, its modified or does not read correctly? It always reports a lower value that specified. If you open the image in photoshop, you can check the values, they are correct.

Just a quirk i also, noticed. The function sceGuTexMode accepts GU_PSM_8888 as a pixel format in 0xAARRGGBB order, but the function to set the texture sceGuTexImage takes a component flag GU_TCC_RGBA ( not say, GU_TCC_ARGB as the format for the other function ). I doubt it means anything, I just noticed it?

i appreciate any assistance on the matter,
thanks,
-stellar :)

Code: Select all

void ImageLoadBmp32(
	char *filename,
	ImageBmp32 **pImage
) 
{ 
    FILE *file; 
	FILE *fpLog;
	unsigned short nBpp;
	unsigned short nPlanes;

	file  = fopen( filename, "rb");
	fpLog = fopen( "image.txt", "a" );

    if ( file == NULL ) 
	{
		goto FN_EXIT;
	}

	if ( fpLog == NULL )
	{
		goto FN_EXIT;
	}

	fprintf( fpLog, "Loading Texture [%s]\n", filename );

	*pImage = (ImageBmp32*) calloc( sizeof( ImageBmp32 ), 1 );

	if ( *pImage == NULL )
	{
		fprintf( fpLog, "  Failed to allocate [ImageBmp32]\n" );
		goto FN_EXIT;
	}
	
	if ( 0 != fseek( file, 18, SEEK_CUR ) )
	{
		fprintf( fpLog, "  Failed to fseek 18\n" );
		goto FN_EXIT;
	}
	
	if ( 1 != fread( &(*pImage)->nWidth, sizeof( unsigned long ), 1, file ) )
	{
		fprintf( fpLog, "  Failed to read width\n" );
		goto FN_EXIT;
	}

	(*pImage)->nStride = (*pImage)->nWidth;

    if ( 1 != fread(&((*pImage))->nHeight, 4, 1, file) )
	{
		fprintf( fpLog, "  Failed to read height\n" );
		goto FN_EXIT;
	}

	(*pImage)->nSize = (*pImage)->nWidth * (*pImage)->nHeight * 4; // 4 bytes per pixel XRGB

	if ( 1 != fread(&nPlanes, 2, 1, file ) )
	{
		fprintf( fpLog, "  Failed to read planes\n" );
		goto FN_EXIT;
	}

	if ( 1 != fread( &nBpp, 2, 1, file ) )
	{
		fprintf( fpLog, "  Failed to read bpp\n" );
		goto FN_EXIT;
	}

    if ( nBpp != 32 )
	{
		fprintf( fpLog, "  Failed : BPP != 32\n" );
		goto FN_EXIT;
	}

	if ( nPlanes != 1 )
	{
		fprintf( fpLog, "  Failed : Planes != 1\n" );
		goto FN_EXIT;
	}

    if ( 0 != fseek( file, 24, SEEK_CUR ) )
	{
		fprintf( fpLog, "  Failed to FSEEK 24\n" );
		goto FN_EXIT;
	}

	// allocate for image pixels
    (*pImage)->pData = ( unsigned char * ) memalign( 16, (*pImage)->nSize ); // 

    if ( (*pImage)->pData == NULL )
	{
		fprintf( fpLog, "  Failed to allocate (%u) for pixels\n", (*pImage)->nSize );
		goto FN_EXIT;
	}

	// read image
    if ( 1 != fread( (*pImage)->pData, (*pImage)->nSize, 1, file ) )
	{
		fprintf( fpLog, "  Failed to read pixels\n" );
		goto FN_EXIT;
	}

	// reorder the bytes into correct format ///////////
    for &#40; unsigned int i = 0; i < &#40;*pImage&#41;->nSize; i += 4 &#41;
	&#123;	// x=3,r=2,b=0,g=1
		unsigned char xrgb&#91;4&#93;;

		memset&#40; xrgb, 0, sizeof&#40; unsigned char &#41; * 4 &#41;;

		xrgb&#91;0&#93; = &#40;*pImage&#41;->pData&#91;i + 3&#93;; // x
		xrgb&#91;1&#93; = &#40;*pImage&#41;->pData&#91;i + 2&#93;; // r
 		xrgb&#91;2&#93; = &#40;*pImage&#41;->pData&#91;i + 1&#93;; // g
		xrgb&#91;3&#93; = &#40;*pImage&#41;->pData&#91;i + 0&#93;; // b

		if &#40; i == 0 &#41;
		&#123;
			fprintf&#40;
				fpLog,
				"B&#40;%X&#41; G&#40;%X&#41; R&#40;%X&#41; A&#40;%X&#41; -> " 
				"A&#40;%X&#41; R&#40;%X&#41; G&#40;%X&#41; B&#40;%X&#41;\n",  // BE CA FE BA
				&#40;*pImage&#41;->pData&#91;i + 0&#93;,
				&#40;*pImage&#41;->pData&#91;i + 1&#93;,
				&#40;*pImage&#41;->pData&#91;i + 2&#93;,
				&#40;*pImage&#41;->pData&#91;i + 3&#93;,
				xrgb&#91;0&#93;,
				xrgb&#91;1&#93;,
				xrgb&#91;2&#93;,
				xrgb&#91;3&#93;
			&#41;;
		&#125;

		memcpy&#40; &&#40;*pImage&#41;->pData&#91;i&#93;, xrgb, sizeof&#40; char &#41; * 4 &#41;;
    &#125;

	fprintf&#40; fpLog, "  Texture Loaded\n" &#41;;

FN_EXIT &#58;
	if &#40; file != NULL &#41;
	&#123;
		fclose&#40; file &#41;;
	&#125;

	if &#40; fpLog != NULL &#41;
	&#123;
		fclose&#40; fpLog &#41;;
	&#125;
&#125;
results

Code: Select all

Loading Texture &#91;test.bmp&#93;
B&#40;BA&#41; G&#40;FE&#41; R&#40;CA&#41; A&#40;AE&#41; -> A&#40;AE&#41; R&#40;CA&#41; G&#40;FE&#41; B&#40;BA&#41;
  Texture Loaded

  or in decimal

B&#40;186&#41; G&#40;254&#41; R&#40;202&#41; A&#40;174&#41; -> A&#40;174&#41; R&#40;202&#41; G&#40;254&#41; B&#40;186&#41;
:: UPDATE :: ok, i used a hex editor (xvi32, my fav) to check the image in binary, it actually reads BA FE CA AE. the AE at the end is what's important. when you edit in photoshop, it reads BE from the color 'info' toolbox. Perhaps i dont understand how to correctly set the values for the alpha mask. Ill have to look into it.
:: END UPDATE ::
Image
Post Reply