SDL Image viewer... another problems! :(

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

Moderators: cheriff, TyRaNiD

Post Reply
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

SDL Image viewer... another problems! :(

Post by darkness »

Excuse me for the many post, but I've ever strange problem!! :(
I've writed a Image viewer using SDL, but it crash when I try to resize the image!!!!
Here is the code:

Code: Select all

int Image_viewer(char* target, char* est)
{
    SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_ASYNCBLIT);
    
    SDL_ShowCursor(0);
    
    SDL_Surface* back = NULL;
    
    SDL_RWops *rwfile = NULL;
    
    SDL_Surface* background = NULL;
    
    FILE *fp = NULL;
    
    Uint32 black;

    black = SDL_MapRGB(screen->format, 0, 0, 0);
    
    SDL_Rect offset;
    
    SceCtrlLatch pad1;
    SceCtrlData pad2;
    
    sceCtrlSetSamplingCycle(0);
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
    
    fp = fopen(target, "rb");
            
    rwfile = SDL_RWFromFP(fp, 1);

    if(rwfile==NULL)
    {
        Init();
        printf("\nErrore nella lettura del file!\n%s", SDL_GetError());
        sleep(3000000);
        SDL_FreeSurface(screen);
        SDL_FreeRW(rwfile);
        SDL_Quit();
        return 1;
    }

    //Load the image

    if(strcmp(est, "BMP")==0) back = SDL_LoadBMP_RW(rwfile, 1);

    if((strcmp(est, "PNM")==0)
            || (strcmp(est, "PPM")==0)
            || (strcmp(est, "PGM")==0)
            || (strcmp(est, "PBM")==0)) back = IMG_LoadPNM_RW(rwfile);

    if(strcmp(est, "XPM")==0) back = IMG_LoadXPM_RW(rwfile);

    if(strcmp(est, "XCF")==0) back = IMG_LoadXCF_RW(rwfile);

    if(strcmp(est, "PCX")==0) back = IMG_LoadPCX_RW(rwfile);

    if(strcmp(est, "GIF")==0) back = IMG_LoadGIF_RW(rwfile);

    if((strcmp(est, "JPG")==0) || (strcmp(est, "JPEG")==0)
            || (strcmp(est, "JPE")==0)) back = IMG_LoadJPG_RW(rwfile);

    if((strcmp(est, "TIF")==0) || (strcmp(est, "TIFF")==0)) back = IMG_LoadTIF_RW(rwfile);

    if(strcmp(est, "PNG")==0) back = IMG_LoadPNG_RW(rwfile);

    if(strcmp(est, "TGA")==0) back = IMG_LoadTGA_RW(rwfile);

    if(strcmp(est, "LBM")==0) back = IMG_LoadLBM_RW(rwfile);

    if(back == NULL )
    {
        Init();
        printf("\nErrore durante il caricamento dell'immagine\n o formato non supportato\n\n%s", SDL_GetError());
        sleep(3000000);
        SDL_FreeSurface(back);
        SDL_FreeSurface(screen);
        SDL_RWclose(rwfile);
        SDL_FreeRW(rwfile);
        SDL_Quit();
        return 1;
    }
    SDL_RWclose(rwfile);
    SDL_FreeRW(rwfile);
    
    int off_x=0;
    
    int off_y=0;
    
/*
    char* new_target = (char*)malloc(256);
    
    char* new_est = (char*)malloc(8);
    
    strcpy(new_target, target);
    strcpy(new_est, est);
    
    int immagine = 0;
*/
    
    double angle=0.000000;
    
    double scale_x=1.000000;
    
    double scale_y=1.000000;
    
    int custom_param=0;
    
    int custom_pos_x=0;
    
    int custom_pos_y=0;
    
    int custom_angle=0;
    
    int rotate_param=0;
    
    int start_scale_x=0;
    
    int start_scale_y=0;
    
    int first_boot=1;
    
    int moving=0;
    
    int allow_custom_pos_x=0;
    
    int allow_custom_pos_y=0;

    int refresh=1;
    
    // int newimage=1;
    
    while(running())
    {
        #include "Image_viewer/pressione_tasto.c"
        
        SDL_FillRect(screen, NULL, black);

        if(!moving)
        {
            if(custom_param!=1)
            {
                if(rotate_param==0)
                {
                    scale_x=(272/scale_y);
                    scale_y=(272/scale_y);
                }
                else
                {
                    scale_x=(272/scale_x);
                    scale_y=(272/scale_x);
                }
            }

            if(first_boot)
            {
                start_scale_x = scale_x;
                start_scale_y = scale_y;
            }

            if(custom_angle!=1)
            {
                angle = 0.000000;
            }
            
            background = rotozoomSurfaceXY&#40;back, angle, scale_x, scale_y, 0&#41;; // <---- Here the programm crash!!!!!!!!!!!!!
            
            if&#40;custom_pos_x==0&#41;
            &#123;
                off_x = &#40; screen->w - background->w &#41; / 2;
            &#125;
            if&#40;custom_pos_y==0&#41;
            &#123;
                off_y = &#40; screen->h - background->h &#41; / 2;
            &#125;
        &#125;
        if&#40;background->w > screen->w&#41;
        &#123;
            allow_custom_pos_x=1;
        &#125;
        else
        &#123;
            allow_custom_pos_x=0;
        &#125;
        
        if&#40;background->h > screen->h&#41;
        &#123;
            allow_custom_pos_y=1;
        &#125;
        else 
        &#123;
            allow_custom_pos_y=0;
        &#125;
        
        offset.x = off_x;
        offset.y = off_y;
        
        //Blit the surface
        SDL_BlitSurface&#40; background, NULL, screen, &offset &#41;;

        SDL_Flip&#40;screen&#41;;
        
        SDL_FreeSurface&#40;background&#41;;

        first_boot=0;
    &#125;
    SDL_FreeSurface&#40;screen&#41;;
    SDL_FreeSurface&#40;back&#41;;
    
    //free&#40;new_target&#41;;
    //free&#40;new_est&#41;;
    
    SDL_Quit&#40;&#41;;
    return 0;
&#125;
And here is the code included ( pressione_tasto.c )

Code: Select all

sceCtrlReadLatch&#40;&pad1&#41;;
        sceCtrlReadBufferPositive&#40;&pad2, 1&#41;;
        
        if&#40;pad1.uiMake==PSP_CTRL_CIRCLE&#41;
        &#123;
            SDL_FreeSurface&#40;background&#41;;
            printf&#40;"sdkofjhdskfh"&#41;;
            break;
        &#125;
        if&#40;pad1.uiMake==PSP_CTRL_CROSS&#41;
        &#123;
            moving=0;
            custom_param=0;
            custom_pos_x=0;
            custom_pos_y=0;
            refresh=1;
        &#125;
        if&#40;pad1.uiMake==PSP_CTRL_UP&#41;
        &#123;
            custom_angle=1;
            if&#40;rotate_param==0&#41; rotate_param=1;
            else rotate_param=0;
            angle+=90;
            refresh=1;
            moving=0;
        &#125;
        if&#40;pad1.uiMake==PSP_CTRL_DOWN&#41;
        &#123;
            custom_angle=1;
            if&#40;rotate_param==0&#41; rotate_param=1;
            else rotate_param=0;
            angle-=90;
            refresh=1;
            moving=0;
        &#125;
        if&#40;pad1.uiMake==PSP_CTRL_LTRIGGER&#41;
        &#123;
            if&#40;scale_x>=0.2&#41;
            &#123;
                custom_param=1;
                scale_x-=0.2;
                scale_y-=0.2;
                refresh=1;
                moving=0;
            &#125;
        &#125;
        if&#40;pad1.uiMake==PSP_CTRL_RTRIGGER&#41;
        &#123;
            if&#40;scale_x<start_scale_x+1.500000&#41;
            &#123;
                custom_param=1;
                scale_x+=0.2;
                scale_y+=0.2;
                refresh=1;
                moving=0;
            &#125;
        &#125;
        
        if&#40;pad2.Lx > 128 + 80&#41;
        &#123;
            if&#40;allow_custom_pos_x==1&#41;
            &#123;
                custom_pos_x=1;
                if&#40;off_x>&#40;background->w - screen->w&#41;-&#40;&#40;background->w - screen->w&#41;*2&#41;&#41; off_x-=10;
                refresh=1;
                moving=1;
            &#125;
        &#125;
        else if&#40;pad2.Ly > 128 + 80&#41;
        &#123;
            if&#40;allow_custom_pos_y==1&#41;
            &#123;
                custom_pos_y=1;
                if&#40;off_y>&#40;background->h - screen->h&#41;-&#40;&#40;background->h - screen->h&#41;*2&#41;&#41; off_y-=10;
                refresh=1;
                moving=1;
            &#125;
        &#125;
        else if&#40;pad2.Lx < 128 - 80&#41;
        &#123;
            if&#40;allow_custom_pos_x==1&#41;
            &#123;
                custom_pos_x=1;
                if&#40;off_x<0&#41; off_x+=10;
                refresh=1;
                moving=1;
            &#125;
        &#125;
        else if&#40;pad2.Ly < 128 - 80&#41;
        &#123;
            if&#40;allow_custom_pos_y==1&#41;
            &#123;
                custom_pos_y=1;
                if&#40;off_y<0&#41; off_y+=10;
                refresh=1;
                moving=1;
            &#125;
        &#125;
        else moving = 0;
I've haven't found a error!
I've to call this function everytime that I want to use it!
( yesterday this code works great, but now it crash!!! Maybe I've modified somethings... I don't remember! and I don't know why now I get a error! )
Please help... :(

Thanks
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

The problem is the rotozoomSurfaceXY funct! The PSP crash in this funct!
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

I'd say the scale factor is borked. A quick look at the code shows the scale is probably not calculated right. I'd print the arguments, wait a few seconds, then do the rotowhatzits so you can see what is being passed to it when it explodes.
darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

I've try to scale the image with a fixed value, but it still crash!!

Code: Select all

int Image_viewer&#40;char* target, char* est&#41;
&#123;
    SDL_Surface* screen = SDL_SetVideoMode&#40; SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_ASYNCBLIT&#41;;
    
    SDL_ShowCursor&#40;0&#41;;
    
    SDL_Surface* back = NULL;
    
    SDL_RWops *rwfile = NULL;
    
    SDL_Surface* background = NULL;
    
    FILE *fp = NULL;
    
    Uint32 black;

    black = SDL_MapRGB&#40;screen->format, 0, 0, 0&#41;;
    
    SDL_Rect offset;
    
    SceCtrlLatch pad1;
    SceCtrlData pad2;
    
    sceCtrlSetSamplingCycle&#40;0&#41;;
    sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
    
    fp = fopen&#40;target, "rb"&#41;;
            
    rwfile = SDL_RWFromFP&#40;fp, 1&#41;;

    if&#40;rwfile==NULL&#41;
    &#123;
        Init&#40;&#41;;
        printf&#40;"\nErrore nella lettura del file!\n%s", SDL_GetError&#40;&#41;&#41;;
        sleep&#40;3000000&#41;;
        SDL_FreeSurface&#40;screen&#41;;
        SDL_FreeRW&#40;rwfile&#41;;
        SDL_Quit&#40;&#41;;
        return 1;
    &#125;

    //Load the image

    if&#40;strcmp&#40;est, "BMP"&#41;==0&#41; back = SDL_LoadBMP_RW&#40;rwfile, 1&#41;;

    if&#40;&#40;strcmp&#40;est, "PNM"&#41;==0&#41;
            || &#40;strcmp&#40;est, "PPM"&#41;==0&#41;
            || &#40;strcmp&#40;est, "PGM"&#41;==0&#41;
            || &#40;strcmp&#40;est, "PBM"&#41;==0&#41;&#41; back = IMG_LoadPNM_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "XPM"&#41;==0&#41; back = IMG_LoadXPM_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "XCF"&#41;==0&#41; back = IMG_LoadXCF_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "PCX"&#41;==0&#41; back = IMG_LoadPCX_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "GIF"&#41;==0&#41; back = IMG_LoadGIF_RW&#40;rwfile&#41;;

    if&#40;&#40;strcmp&#40;est, "JPG"&#41;==0&#41; || &#40;strcmp&#40;est, "JPEG"&#41;==0&#41;
            || &#40;strcmp&#40;est, "JPE"&#41;==0&#41;&#41; back = IMG_LoadJPG_RW&#40;rwfile&#41;;

    if&#40;&#40;strcmp&#40;est, "TIF"&#41;==0&#41; || &#40;strcmp&#40;est, "TIFF"&#41;==0&#41;&#41; back = IMG_LoadTIF_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "PNG"&#41;==0&#41; back = IMG_LoadPNG_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "TGA"&#41;==0&#41; back = IMG_LoadTGA_RW&#40;rwfile&#41;;

    if&#40;strcmp&#40;est, "LBM"&#41;==0&#41; back = IMG_LoadLBM_RW&#40;rwfile&#41;;

    if&#40;back == NULL &#41;
    &#123;
        Init&#40;&#41;;
        printf&#40;"\nErrore durante il caricamento dell'immagine\n o formato non supportato\n\n%s", SDL_GetError&#40;&#41;&#41;;
        sleep&#40;3000000&#41;;
        SDL_FreeSurface&#40;back&#41;;
        SDL_FreeSurface&#40;screen&#41;;
        SDL_RWclose&#40;rwfile&#41;;
        SDL_FreeRW&#40;rwfile&#41;;
        SDL_Quit&#40;&#41;;
        return 1;
    &#125;
    SDL_RWclose&#40;rwfile&#41;;
    SDL_FreeRW&#40;rwfile&#41;;
    
    int off_x=0;
    
    int off_y=0;
    
/*
    char* new_target = &#40;char*&#41;malloc&#40;256&#41;;
    
    char* new_est = &#40;char*&#41;malloc&#40;8&#41;;
    
    strcpy&#40;new_target, target&#41;;
    strcpy&#40;new_est, est&#41;;
    
    int immagine = 0;
*/
    
    double angle=0.000000;
    
    double scale_x=1.000000;
    
    double scale_y=1.000000;
    
    int custom_param=0;
    
    int custom_pos_x=0;
    
    int custom_pos_y=0;
    
    int custom_angle=0;
    
    int rotate_param=0;
    
    int start_scale_x=0;
    
    int start_scale_y=0;
    
    int first_boot=1;
    
    int moving=0;
    
    int allow_custom_pos_x=0;
    
    int allow_custom_pos_y=0;

    int refresh=1;
    
    // int newimage=1;
    
    while&#40;running&#40;&#41;&#41;
    &#123;
        #include "Image_viewer/pressione_tasto.c"
        
        SDL_FillRect&#40;screen, NULL, black&#41;;

        if&#40;!moving&#41;
        &#123;
            if&#40;custom_param!=1&#41;
            &#123;
                if&#40;rotate_param==0&#41;
                &#123;
                    scale_x=&#40;272/scale_y&#41;;
                    scale_y=&#40;272/scale_y&#41;;
                &#125;
                else
                &#123;
                    scale_x=&#40;272/scale_x&#41;;
                    scale_y=&#40;272/scale_x&#41;;
                &#125;
            &#125;

            if&#40;first_boot&#41;
            &#123;
                start_scale_x = scale_x;
                start_scale_y = scale_y;
            &#125;

            if&#40;custom_angle!=1&#41;
            &#123;
                angle = 0.000000;
            &#125;
            Init&#40;&#41;;
            printf&#40;"Before scale"&#41;;
            
            background = rotozoomSurfaceXY&#40;back, 0, 1.00, 1.00, 1&#41;; // <---- Here the programm crash!!!!!!!!!!!!!
            
            printf&#40;"After scale"&#41;;
            sleep&#40;1000000&#41;;
            
            if&#40;custom_pos_x==0&#41;
            &#123;
                off_x = &#40; screen->w - background->w &#41; / 2;
            &#125;
            if&#40;custom_pos_y==0&#41;
            &#123;
                off_y = &#40; screen->h - background->h &#41; / 2;
            &#125;
        &#125;
        if&#40;background->w > screen->w&#41;
        &#123;
            allow_custom_pos_x=1;
        &#125;
        else
        &#123;
            allow_custom_pos_x=0;
        &#125;
        
        if&#40;background->h > screen->h&#41;
        &#123;
            allow_custom_pos_y=1;
        &#125;
        else 
        &#123;
            allow_custom_pos_y=0;
        &#125;
        
        offset.x = off_x;
        offset.y = off_y;
        
        //Blit the surface
        SDL_BlitSurface&#40; background, NULL, screen, &offset &#41;;

        SDL_Flip&#40;screen&#41;;
        
        SDL_FreeSurface&#40;background&#41;;

        first_boot=0;
    &#125;
    SDL_FreeSurface&#40;screen&#41;;
    SDL_FreeSurface&#40;back&#41;;
    
    //free&#40;new_target&#41;;
    //free&#40;new_est&#41;;
    
    SDL_Quit&#40;&#41;;
    return 0;
&#125;
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

darkness
Posts: 121
Joined: Sun Jun 15, 2008 8:42 pm

Post by darkness »

?? I haven't found a solution! And I'm not use a freemem funct! It crash the first time that the function is called!
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

I'm trying to point out you're both using the same scale function and both having the same bug. Looks a bit like the library itself needs fixing. I think J.F alluded to that too. It's worth grabbing the code from svn and seeing what's wrong.

Jim
Post Reply