| View previous topic :: View next topic |
| Author |
Message |
DeNitro
Joined: 16 Sep 2005 Posts: 4
|
Posted: Thu Sep 29, 2005 11:28 am Post subject: 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 |
|
| Back to top |
|
 |
mrbrown
Joined: 17 Jan 2004 Posts: 1536
|
Posted: Thu Sep 29, 2005 2:29 pm Post subject: |
|
|
| SDL_image? |
|
| Back to top |
|
 |
DeNitro
Joined: 16 Sep 2005 Posts: 4
|
Posted: Thu Sep 29, 2005 9:37 pm Post subject: |
|
|
Awesome got it working. Thanks again mrbrown and great job on the pspsdk. You guys are amazing :)
--DeNitro |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Tue Nov 21, 2006 2:03 pm Post subject: |
|
|
| Any examples of how to use it in code? |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Tue Nov 21, 2006 2:11 pm Post subject: |
|
|
| Code: |
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;
}
|
While I'm at it.. has anyone seen the above example, and knows what is wrong with it?
Cheers, Art. |
|
| Back to top |
|
 |
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Tue Nov 21, 2006 4:45 pm Post subject: |
|
|
Looks like the function from the lastest Lua Player's graphics.cpp.
If it's not then check it out ^^^ |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Wed Nov 22, 2006 6:26 am Post subject: |
|
|
| Art wrote: | While I'm at it.. has anyone seen the above example, and knows what is wrong with it?
|
Yes, I have seen this code, because I wrote it. You need the Image struct from graphics.h in order to compile it. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Thu Nov 23, 2006 6:36 am Post subject: |
|
|
What is an image structure?
Does that function get placed in graphics.h? |
|
| Back to top |
|
 |
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Thu Nov 23, 2006 7:18 am Post subject: |
|
|
Download the Lua Player source from SVN or http://www.luaplayer.org
The graphics.cpp and graphics.h are included. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Thu Nov 23, 2006 7:19 am Post subject: |
|
|
| Art wrote: | What is an image structure?
Does that function get placed in graphics.h? |
"struct": http://www.google.com/search?q=c+struct
"Image": see graphics.h |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Nov 26, 2006 10:15 am Post subject: |
|
|
| Code: |
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;
|
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. 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. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Nov 26, 2006 5:31 pm Post subject: |
|
|
| 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. |
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. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Nov 26, 2006 6:32 pm Post subject: |
|
|
Well I did read the page you linked to, and yes it is rather obvious definition.
I mean If I just include this:
| Code: | 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; |
Without touching anything else, my program that previously compiled,
now comes up with many errors. One of the first is | Quote: |
graphics.h:23: error: previous declaration of 'Image' was here |
Which lead me to believe your function belongs in graphics.h.
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. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Sun Nov 26, 2006 10:46 pm Post subject: |
|
|
| Art wrote: |
Without touching anything else, my program that previously compiled,
now comes up with many errors. One of the first is | Quote: |
graphics.h:23: error: previous declaration of 'Image' was here |
|
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:
| Quote: |
#ifndef GRAPHICS_H
#define GRAPHICS_H
...
#endif
|
| 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.
|
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. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Mon Nov 27, 2006 12:59 pm Post subject: |
|
|
| Thanks, I'll give it a go. |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Mon Nov 27, 2006 5:10 pm Post subject: |
|
|
| Art wrote: |
I think even the source of a HB game that uses it would do as an example. |
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. |
|
| Back to top |
|
 |
Art
Joined: 09 Nov 2005 Posts: 647
|
Posted: Sun Dec 10, 2006 10:45 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
|