[SOLVED]Problem with Bin2o

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

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

[SOLVED]Problem with Bin2o

Post by ne0h »

I will load a image writted in the Eboot with Bin2o:
"Makefile":

logo.o: logo.png
bin2o -i logo.png logo.o logo

This is the function:

extern unsigned char logo_start[];
ourImage = loadImage(logo_start);


Function for loadImage:

Image* loadImage(const char* filename)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, x, y;
u32* line;
FILE *fp;
Image* image = (Image*) malloc(sizeof(Image));
if (!image) return NULL;

if ((fp = fopen(filename, "rb")) == NULL) return NULL;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
free(image);
fclose(fp);
return NULL;;
}
png_set_error_fn(png_ptr, (png_voidp) NULL, (png_error_ptr) NULL, user_warning_fn);
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, sig_read);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
if (width > 512 || height > 512) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
image->imageWidth = width;
image->imageHeight = height;
image->textureWidth = getNextPower2(width);
image->textureHeight = getNextPower2(height);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
image->data = (Color*) memalign(16, image->textureWidth * image->textureHeight * sizeof(Color));
if (!image->data) {
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
line = (u32*) malloc(width * 4);
if (!line) {
free(image->data);
free(image);
fclose(fp);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
for (y = 0; y < height; y++) {
png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
for (x = 0; x < width; x++) {
u32 color = line[x];
image->data[x + y * image->textureWidth] = color;
}
}
free(line);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
fclose(fp);
return image;
}


The compiler give me the error:

[Warning] pointer targets in passing argument 1 of 'loadImage' differ in signedness

and when the image will load, the PSP Crash!

Have someone a or another solution?
Last edited by ne0h on Mon Mar 17, 2008 3:19 am, edited 1 time in total.
CpuWhiz
Posts: 42
Joined: Mon Jun 04, 2007 1:30 am

Post by CpuWhiz »

loadImage() expects a filename instead of a pointer to the actual data. I have made a simple loadMemoryImage() for you based off the code from loadImage(). First add the following to graphics.c (I added it right above the loadImage() function):

Code: Select all

typedef struct _memory_png
&#123;
	char *file;
	png_size_t pos;
	png_size_t size;
&#125;memory_png;

static void user_read_fn&#40;png_structp png_ptr, png_bytep buffer, png_size_t size&#41;
&#123;
	memory_png *data = &#40;memory_png*&#41;png_ptr->io_ptr;
	if&#40; data->size && &#40;data->pos + size&#41; > data->size &#41;
	&#123;
		png_error&#40;png_ptr, "Error reading data from PNG file."&#41;;
		return;
	&#125;

	memcpy&#40;buffer, data->file + data->pos, size&#41;;
	data->pos += size;
&#125;;

Image* loadMemoryImage&#40;const unsigned char* data, int size&#41;
&#123;
	memory_png file;
	png_structp png_ptr;
	png_infop info_ptr;
	unsigned int sig_read = 0;
	png_uint_32 width, height;
	int bit_depth, color_type, interlace_type, x, y;
	u32* line;
	Image* image = &#40;Image*&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
	if &#40;!image&#41; return NULL;

	file.file = &#40;char*&#41;data;
	file.pos = 0;
	file.size = size;

	png_ptr = png_create_read_struct&#40;PNG_LIBPNG_VER_STRING, NULL, NULL, NULL&#41;;
	if &#40;png_ptr == NULL&#41; &#123;
		free&#40;image&#41;;
		return NULL;;
	&#125;
	png_set_error_fn&#40;png_ptr, &#40;png_voidp&#41; NULL, &#40;png_error_ptr&#41; NULL, user_warning_fn&#41;;
	png_set_read_fn&#40;png_ptr, &#40;png_voidp&#41;&file, user_read_fn&#41;;
	info_ptr = png_create_info_struct&#40;png_ptr&#41;;
	if &#40;info_ptr == NULL&#41; &#123;
		free&#40;image&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return NULL;
	&#125;
	png_init_io&#40;png_ptr, &#40;FILE*&#41;&file&#41;;
	png_set_sig_bytes&#40;png_ptr, sig_read&#41;;
	png_read_info&#40;png_ptr, info_ptr&#41;;
	png_get_IHDR&#40;png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL&#41;;
	if &#40;width > 512 || height > 512&#41; &#123;
		free&#40;image&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return NULL;
	&#125;
	image->imageWidth = width;
	image->imageHeight = height;
	image->textureWidth = getNextPower2&#40;width&#41;;
	image->textureHeight = getNextPower2&#40;height&#41;;
	png_set_strip_16&#40;png_ptr&#41;;
	png_set_packing&#40;png_ptr&#41;;
	if &#40;color_type == PNG_COLOR_TYPE_PALETTE&#41; png_set_palette_to_rgb&#40;png_ptr&#41;;
	if &#40;color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8&#41; png_set_gray_1_2_4_to_8&#40;png_ptr&#41;;
	if &#40;png_get_valid&#40;png_ptr, info_ptr, PNG_INFO_tRNS&#41;&#41; png_set_tRNS_to_alpha&#40;png_ptr&#41;;
	png_set_filler&#40;png_ptr, 0xff, PNG_FILLER_AFTER&#41;;
	image->data = &#40;Color*&#41; memalign&#40;16, image->textureWidth * image->textureHeight * sizeof&#40;Color&#41;&#41;;
	if &#40;!image->data&#41; &#123;
		free&#40;image&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return NULL;
	&#125;
	line = &#40;u32*&#41; malloc&#40;width * 4&#41;;
	if &#40;!line&#41; &#123;
		free&#40;image->data&#41;;
		free&#40;image&#41;;
		png_destroy_read_struct&#40;&png_ptr, png_infopp_NULL, png_infopp_NULL&#41;;
		return NULL;
	&#125;
	for &#40;y = 0; y < height; y++&#41; &#123;
		png_read_row&#40;png_ptr, &#40;u8*&#41; line, png_bytep_NULL&#41;;
		for &#40;x = 0; x < width; x++&#41; &#123;
			u32 color = line&#91;x&#93;;
			image->data&#91;x + y * image->textureWidth&#93; =  color;
		&#125;
	&#125;
	free&#40;line&#41;;
	png_read_end&#40;png_ptr, info_ptr&#41;;
	png_destroy_read_struct&#40;&png_ptr, &info_ptr, png_infopp_NULL&#41;;
	return image;
&#125;
Now add the following line to grahpics.h:

Code: Select all

extern Image* loadMemoryImage&#40;const unsigned char* data, int size&#41;;
Finally, to use it (Don't forget to define logo_size as extern. You could use 0 for the size, but that's not very safe now is it):

Code: Select all

ourImage = loadMemoryImage&#40;logo_start, logo_size&#41;;
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Post by ne0h »

Tanks, tanks and tanks....
It work perfectly, also with size 0!
Excuse for my english, i'm italian!
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

We get people from all over the world here, so language is not a problem. As long as you're programming, that's all that matters.

As to passing the size as 0, that's probably fine for what you're doing since you're embedding your own pngs into the program, so you KNOW they are good. It would be more of a problem if you were just using random pngs of unknown quality.
Post Reply