How is the .bin texture generated in wii gxSprite demo ?

Post Reply
vlad2048
Posts: 1
Joined: Sat Aug 16, 2008 4:37 am

How is the .bin texture generated in wii gxSprite demo ?

Post by vlad2048 » Sat Aug 16, 2008 4:41 am

I can see the texture ballsprites.bin is 64x64 and the .bin file is 16384 bytes. So it's 4 bytes per pixel.
How was the .bin file generated ?

I've tried using bmp2bin from the devkitPPC package but it doesn't work. bmp2bin only output 1,2 or 3 bytes per pixel. Not 4.

How can I convert my own images ?

Thanks

kiwimaddog
Posts: 6
Joined: Mon Aug 25, 2008 4:00 pm

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by kiwimaddog » Mon Aug 25, 2008 5:06 pm

bmp2bin can't be the correct app used to create the file as bmp files don't have alpha channels. When you run the program the sprites have alpha channels. Could someone please explain this? Is there any documentation on the mater?

Many Thanks
David

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by WinterMute » Thu Aug 28, 2008 2:47 pm

The tool used to create the binary data was just a quick & dirty hack that I'm reluctant to release as part of the toolchain. Unfortunately these things have a habit of spreading around the community & encouraging bad habits and/or poor workflow in projects. The main objective of the gxsprite demo was to provide an example of using the GX hardware for 2D instead of relying on the usual framebuffer approach.

With that in mind I'll provide the source here but bear in mind that we hope to provide better tools in the future which will allow relatively automatic image data conversion. This code has only been tested on windows so far but should compile on other platforms with little modification - caveat: I can't guarantee I accounted properly for endianess issues. The code requires freeimage.

Code: Select all

#include <stdio.h>
#include <FreeImage.h>
#include <assert.h>
#include <stdlib.h>

#include <getopt.h>

#include <string>

//--------------------------------------------------------------------
static void codecRGBA8(uint32_t width, uint32_t height, void *data, FIBITMAP *bitmap, bool decode) {
//--------------------------------------------------------------------

	BYTE *tex = (BYTE *)data;
	uint32_t stride = FreeImage_GetPitch(bitmap);
	

	for ( uint32_t y = 0; y < height; y+=4 ) {

		BYTE *line = FreeImage_GetScanLine(bitmap, y);

		for ( uint32_t x = 0; x < width; x+= 4 ) {
		
			BYTE *bits = line + (x * 4);

			for ( int ty = 0; ty < 4; ty++ ) {
				for ( int tx = 0; tx < 4; tx++ ) {

					if ( decode ) {
						bits[FI_RGBA_ALPHA] = tex[0];
						bits[FI_RGBA_RED]   = tex[1];
						bits[FI_RGBA_GREEN] = tex[32];
						bits[FI_RGBA_BLUE]  = tex[33];
					} else {
						tex[0]  = bits[FI_RGBA_ALPHA];
						tex[1]  = bits[FI_RGBA_RED];
						tex[32] = bits[FI_RGBA_GREEN];
						tex[33] = bits[FI_RGBA_BLUE];
					}
						tex += 2;
						bits += 4;
				}
				bits += stride - 16;
			}
			tex +=32;
		}
	}
}



int main ( int argc, char **argv ) {


	int c;
	
	int width=0, height=0;
	
	bool decode = false;
	
	while ((c = getopt (argc, argv, "dew:h:")) != -1) {
		switch (c) {
			case 'e':
				decode = false;
				break;
			case 'd':
				decode = true;
				break;
			case 'w':
				width=strtoul(optarg,NULL,0);
				break;
			case 'h':
				height=strtoul(optarg,NULL,0);
				break;
		}
	}

	const char *src_filename = argv[optind];
	if ( NULL == src_filename) {
		printf("No file specified!\n");
		exit(1);
	}

	std::string outfile = src_filename;
	outfile.erase(outfile.rfind('.'));
	if (decode) {
		outfile += ".png";
	} else {
		outfile += ".bin";
	}
		
	const char *dst_filename = outfile.c_str();

	FreeImage_Initialise(false);

	if (decode) {
	
	
		if ( width == 0 || height == 0 ) {
			printf("specify width and height for decoding!\n");
			exit(1);
		}

		FILE *handle = fopen(src_filename,"rb");
		if ( NULL == handle ) {
			printf("File open failed!\n");
			exit(1);
		}
		
		fseek(handle, 0L, SEEK_END);
		
		int size = ftell(handle);
		
		if ( (size & -64) != size || (width * height * 4) != size) {
			printf("Invalid filesize!\n");
			fclose(handle);
			exit(1);
		}
		fseek(handle, 0L, SEEK_SET);
		
		void *buffer = malloc(size);
		fread(buffer,1,size,handle);
		fclose(handle);
		
		FIBITMAP *dst = FreeImage_Allocate(width, height, 32 );
		
		codecRGBA8 ( width, height, buffer, dst, true);
		
		FreeImage_FlipVertical(dst);
		FreeImage_Save(FIF_PNG, dst, dst_filename, PNG_DEFAULT);

		FreeImage_Unload(dst);
		free(buffer);


	} else {


		FREE_IMAGE_FORMAT src_fif = FreeImage_GetFileType(src_filename);
		FIBITMAP *src = FreeImage_Load(src_fif, src_filename, 0); //24bit image 
	
		unsigned src_bpp = FreeImage_GetBPP(src);

		printf("image bpp is %d\n", src_bpp);
	
		if ( 32 != src_bpp ) {

			printf("converting to 32bit ...\n");
			FIBITMAP *tmp = FreeImage_ConvertTo32Bits(src);
			assert(tmp != NULL);
			FreeImage_Unload(src); 
			src = tmp;
		}
	
		unsigned src_stride = FreeImage_GetPitch(src);
	
		printf("image stride is %d\n", src_stride);
	

		unsigned width = FreeImage_GetWidth(src);
		unsigned height = FreeImage_GetHeight(src);
	
		printf("image is %d x %d\n", width, height);

		void *buffer = malloc( width * height * 4 );
	
		FreeImage_FlipVertical(src);

		codecRGBA8 ( width, height, buffer, src, false);
	
		FILE *handle = fopen(dst_filename,"wb");
		fwrite(buffer,1,width*height*4,handle);
		fclose(handle);
		free(buffer);


	}


	return 0;

}

Help keep devkitPro toolchains free, Donate today

Personal Blog

kiwimaddog
Posts: 6
Joined: Mon Aug 25, 2008 4:00 pm

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by kiwimaddog » Sun Aug 31, 2008 11:39 am

Thanks, It works fine!.
David

Jeireff
Posts: 7
Joined: Tue Sep 02, 2008 12:36 am

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by Jeireff » Wed Sep 03, 2008 9:33 pm

I'm sorry for that question, but can anybody compile and upload it for me?
I'm normaly develop in PHP and have just installed Programmer's Notepad
to develop in C/C++ and I don't know how to manipulate the makefile in
the right way. It's just funny, I'm working as a kind of Developer in a
Softwarehouse and I had to speak C++ over two years (before I started,
to develop Internetapplications) and I don't even know how to configurate
a compiler :-D

I hope you can help me with this...

Greetz Jeireff

kiwimaddog
Posts: 6
Joined: Mon Aug 25, 2008 4:00 pm

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by kiwimaddog » Thu Sep 04, 2008 6:04 pm

Here is a compiled version
If you want to encode an image you just type: 'WiiGfxTest.exe nameOfPngToEncode.png' - if you want to decode an existing file then you'll need to do 'WiiGfxTest.exe -w 64 -h 64 -d nameOfBinToDecode.bin'. You'll have to replace -w 64 and -h 64 with the dimensions of the image.
Thanks To Steven Craft!
https://www.yousendit.com/download/bVlD ... b0JFQlE9PQ

Jeireff
Posts: 7
Joined: Tue Sep 02, 2008 12:36 am

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by Jeireff » Thu Sep 04, 2008 10:57 pm

Thanks for your quick help...
I will test it right now :D

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: How is the .bin texture generated in wii gxSprite demo ?

Post by WinterMute » Tue Dec 23, 2008 5:02 am

Just to keep people in the loop.

The converter tool I spoke of has been added to the latest devkitPPC release. The gxSprite example has been updated to work directly from the source graphics.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests