How is the .bin texture generated in wii gxSprite demo? October 22, 2010 03:58PM | Registered: 14 years ago Posts: 9 |
Re: How is the .bin texture generated in wii gxSprite demo? October 22, 2010 04:03PM | Registered: 14 years ago Posts: 37 |
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
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 | Registered: 15 years ago Posts: 379 |