forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Faster PNG loader

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
elevationsnow



Joined: 10 Sep 2005
Posts: 22

PostPosted: Fri Oct 14, 2005 12:08 pm    Post subject: Faster PNG loader Reply with quote

is there any faster png loader than:
Code:
void user_warning_fn(png_structp png_ptr, png_const_charp warning_msg)
{
        // ignore PNG warnings
}
void loadImage(const char* filename,int xx,int yy)
{
        u32* vram32;
        u16* vram16;
        int bufferwidth;
        int pixelformat;
        int unknown;
        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;

        if ((fp = fopen(filename, "rb")) == NULL) return;
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
        if (png_ptr == NULL) {
                fclose(fp);
                return;
        }
        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) {
                fclose(fp);
                png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                return;
        }
        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);
        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);
        line = (u32*) malloc(width * 4);
        if (!line) {
                fclose(fp);
                png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
                return;
        }
        sceDisplayWaitVblankStart();  // if framebuf was set with PSP_DISPLAY_SETBUF_NEXTFRAME, wait until it is changed
        sceDisplayGetFrameBuf((void**)&vram32, &bufferwidth, &pixelformat, &unknown);
        vram16 = (u16*) vram32;
        for (y = 0; y < height; y++) {         
                png_read_row(png_ptr, (u8*) line, png_bytep_NULL);
                for (x = 0; x < width; x++) {             
                        u32 color32 = line[x];
                        u16 color16;
                        int r = color32 & 0xff;
                        int g = (color32 >> 8) & 0xff;
                        int b = (color32 >> 16) & 0xff;
                        switch (pixelformat) {
                                case PSP_DISPLAY_PIXEL_FORMAT_565:
                                        color16 = (r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11);
                                        PlotPixel(x+xx,y+yy,r,g,b);
                                        break;
                                case PSP_DISPLAY_PIXEL_FORMAT_5551:
                                        color16 = (r >> 3) | ((g >> 3) << 5) | ((b >> 3) << 10);
                                        PlotPixel(x+xx,y+yy,r,g,b);
                                        break;
                                case PSP_DISPLAY_PIXEL_FORMAT_4444:
                                        color16 = (r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8);
                                        PlotPixel(x+xx,y+yy,r,g,b);
                                        break;
                                case PSP_DISPLAY_PIXEL_FORMAT_8888:
                                        color32 = r | (g << 8) | (b << 16);
                                        PlotPixel(x+xx,y+yy,r,g,b);
                                        break;
                        }
                }
        }
        free(line);
        png_read_end(png_ptr, info_ptr);
        png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
        fclose(fp);
}
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Fri Oct 14, 2005 2:27 pm    Post subject: Reply with quote

Yes, get the switch() outside the for loops (write it out longhand), and don't use PlotPixel, try to poke the addresses directly - as you go across the scanline the address simply increment.
Having said that, check first that it's not the memory stick bandwidth that's the limiting factor.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group