libpng - Loading a png from memory question
libpng - Loading a png from memory question
The libpng screenshot code compiles and works great. I was trying to modify the code to load a PNG from a buffer in memory compiled in using bin2o rather than a file buffer. I replaced png_init_io() call with png_set_read_fn() and pointed it to a custom read funcion I wrote but I can't seem it get my function to do anything but crash the psp. Anyone know of a sample with a custom PNG read function they could point me to or have any experience with it?
Big thanks in advance if anyone can help, I've been stumped by it for a few days now.
--DeNitro
Big thanks in advance if anyone can help, I've been stumped by it for a few days now.
--DeNitro
Code: Select all
void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
{
}
typedef struct
{
const unsigned char *buffer;
png_uint_32 bufsize;
png_uint_32 current_pos;
} MEMORY_READER_STATE;
static void read_data_memory(png_structp png_ptr, png_bytep data, png_uint_32 length)
{
MEMORY_READER_STATE *f = (MEMORY_READER_STATE *)png_get_io_ptr(png_ptr);
if (length > (f->bufsize - f->current_pos)) png_error(png_ptr, "read error in read_data_memory (loadpng)");
memcpy(data, f->buffer + f->current_pos, length);
f->current_pos += length;
}
Image* loadImageMemory(const void *buffer, int bufsize)
{
png_structp png_ptr;
png_infop info_ptr;
MEMORY_READER_STATE memory_reader_state;
unsigned int sig_read = 0;
png_uint_32 width, height;
int bit_depth, color_type, interlace_type, x, y;
u32* line;
if (!buffer || (bufsize <= 0)) return NULL;
Image* image = (Image*) malloc(sizeof(Image));
if (!image) return NULL;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
free(image);
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);
png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
return NULL;
}
memory_reader_state.buffer = (unsigned char *)buffer;
memory_reader_state.bufsize = bufsize;
memory_reader_state.current_pos = 0;
png_set_read_fn(png_ptr, &memory_reader_state, (png_rw_ptr)read_data_memory);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
image->imageWidth = width;
image->imageHeight = height;
image->textureWidth = getNextPower2(width);
image->textureHeight = getNextPower2(height);
png_set_sig_bytes(png_ptr, sig_read);
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);
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);
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);
return image;
}
Cheers, Art.
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
-
- Posts: 376
- Joined: Wed May 10, 2006 11:31 pm
Download the Lua Player source from SVN or http://www.luaplayer.org
The graphics.cpp and graphics.h are included.
The graphics.cpp and graphics.h are included.
"struct": http://www.google.com/search?q=c+structArt wrote:What is an image structure?
Does that function get placed in graphics.h?
"Image": see graphics.h
Code: Select all
typedef struct
{
int textureWidth; // the real width of data, 2^n with n>=0
int textureHeight; // the real height of data, 2^n with n>=0
int imageWidth; // the image width
int imageHeight;
Color* data;
} Image;
(I think because it's already declared in graphics.h),
and if I include the whole function above into graphics.h, it doesn't compile either. Would it matter that my SDk install is old?
I'd really like to get either this, or fontlib working so I can get graphic
fonts in programs without myself, or it's users having to copy the 44 character png files to MS.
My crystal ball is a bit dull these days. What do you mean with "break" and "doesn't compile" (compiler message)? But please consider that this is no C learning forum before you answer. Maybe you should buy some book like "learning C in 21 days" or something like this.Art wrote: If I include this into my working program, it breaks it
(I think because it's already declared in graphics.h),
and if I include the whole function above into graphics.h, it doesn't compile either.
Well I did read the page you linked to, and yes it is rather obvious definition.
I mean If I just include this:
Without touching anything else, my program that previously compiled,
now comes up with many errors. One of the first is
There is also a complaint about every occurance of blitImageToScreen as well.
And If I put your function posted above in either main.c or graphics.h
without touching anything else, there are errors as well.
There's no need for programming books, once I get a graphic font working one
way or another (preferably this way, I'll be fine).
I think even the source of a HB game that uses it would do as an example.
I mean If I just include this:
Code: Select all
typedef struct
{
int textureWidth; // the real width of data, 2^n with n>=0
int textureHeight; // the real height of data, 2^n with n>=0
int imageWidth; // the image width
int imageHeight;
Color* data;
} Image;
now comes up with many errors. One of the first is
Which lead me to believe your function belongs in graphics.h.graphics.h:23: error: previous declaration of 'Image' was here
There is also a complaint about every occurance of blitImageToScreen as well.
And If I put your function posted above in either main.c or graphics.h
without touching anything else, there are errors as well.
There's no need for programming books, once I get a graphic font working one
way or another (preferably this way, I'll be fine).
I think even the source of a HB game that uses it would do as an example.
The text after "was here" could be important. The compiler has already parsed the Image structure. Maybe you included it from another file as well? Usually in C you write something like this in header files to avoid this problem:Art wrote: Without touching anything else, my program that previously compiled,
now comes up with many errors. One of the first isgraphics.h:23: error: previous declaration of 'Image' was here
#ifndef GRAPHICS_H
#define GRAPHICS_H
...
#endif
Usually in C you don't put a function in a header file, but the declaration, only. So if you use the original graphics.h and graphics.cpp from Lua Player and if you include just the graphics.h in your main.c (which you need to convert to main.cpp, or you should fix all errors when compiling graphics.cpp as graphics.c), then it should work. Don't forget to add graphics.cpp and main.cpp both to your Makefile.Art wrote: And If I put your function posted above in either main.c or graphics.h
without touching anything else, there are errors as well.
This is a good idea. You could use Lua Player 0.16 from http://www.luaplayer.org/old/index.html (this is the last version without module support, so building and deploying is easier and it should work on firmware >1.5 with loaders). Then delete anything you don't need (but maybe you want script support, too, in your program) and add your code. The licence is BSD, which means that you can do whatever you want with the source, as long as you keep the copyright notice.Art wrote: I think even the source of a HB game that uses it would do as an example.
I got this sorted with a bare minimum program required to display one image to the screen, thanks to a fellow called will1234.
http://www.megaupload.com/?d=OMJT4D5N
I think it's a template for a PSP comic book.
Cheers,
Art.
http://www.megaupload.com/?d=OMJT4D5N
I think it's a template for a PSP comic book.
Cheers,
Art.