Welcome! Log In Create A New Profile

Advanced

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

Posted by ShadowSpace 
How is the .bin texture generated in wii gxSprite demo?
October 22, 2010 03:58PM
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
Re: How is the .bin texture generated in wii gxSprite demo?
October 22, 2010 04:03PM
Quote
ShadowSpace
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

The tool used to create the binary data was just a quick & dirty hack that was released 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.

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.

Quote


#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;

}
Re: How is the .bin texture generated in wii gxSprite demo?
October 22, 2010 04:51PM
Note that you are better off with using something like [wiibrew.org] or at a even higher level GRRLIB or libwiisprite
Sorry, only registered users may post in this forum.

Click here to login