Date:2010-05-30 17:11:43 (10 years 4 months ago)
Author:Xiangfu Liu
Commit:f4ff8931231afa35765fbd0c149c6b7ba9ecffb4
Message:update to 0.2.1

Signed-off-by: Xiangfu Liu <xiangfu@sharism.cc>
Files: sdl-imageviewer.c (11 diffs)

Change Details

sdl-imageviewer.c
11// imgv, a simple SDL-based image viewer for the Ben Nanonote
2// Version 0.2.0
3// Last edited by Fernando Carello <fcarello@libero.it> 2010-05-21
2// Version 0.2.1
3// Last edited by Fernando Carello <fcarello@libero.it> 2010-05-24
44//
55#include <stdlib.h>
66#include <unistd.h>
...... 
4343                upPressed,
4444                downPressed;
4545    char sFilename[255] = "\0",
46                sVersion[] = "0.2.0";
46                sVersion[] = "0.2.1";
4747    double scale_x = 1.0,
4848                scale_y = 1.0,
4949                scale = 1.0;
...... 
6767    }
6868
6969    // Load Picture
70    picture = IMG_Load(sFilename);
70    temp_img = IMG_Load(sFilename);
71    if (temp_img == (SDL_Surface *) (NULL))
72    {
73        fprintf(stderr, "\n Couldn't load image file %s: %s\n\n", sFilename, SDL_GetError());
74        exit (1);
75    }
7176
77    // Set video mode
78    screen = SDL_SetVideoMode
79        (SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL);
80    if (screen == (SDL_Surface *) (NULL))
81    {
82        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n\n", SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_GetError());
83        exit(1);
84    }
85
86    // Can't stand the useless arrow... Ben has no pointing device
87    SDL_ShowCursor(SDL_DISABLE);
88
89    // Convert picture in same format as video framebuffer, to optimize blit performances
90    picture = SDL_DisplayFormat (temp_img);
7291    if (picture == (SDL_Surface *) (NULL))
7392    {
74        fprintf(stderr, "\n Couldn't load image file %s: %s\n\n", sFilename, SDL_GetError());
93        fprintf(stderr, "\n Internal error from DisplayFormat\n\n");
7594        exit (1);
7695    }
96    SDL_FreeSurface (temp_img);
97
7798    imgWidth = picture->w;
7899    imgHeight = picture->h;
79100
...... 
90111        fprintf (stderr, "\n Error from zoomSurface()\n\n");
91112        exit (1);
92113    }
93
94    screen = SDL_SetVideoMode
95        (SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_HWACCEL);
96    if (screen == (SDL_Surface *) (NULL))
97    {
98        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n\n", SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_GetError());
99        exit(1);
100    }
101
102    SDL_ShowCursor(SDL_DISABLE); // Can't stand the useless arrow... Ben has no pointing device
103114
104115    screenPortion.x = 0; // destination coordinates: origin
105116    screenPortion.y = 0;
...... 
128139
129140    while(1)
130141    {
131
132142        if (SDL_WaitEvent( &event ))
133143        {
134144            // We only process SDL_KEYDOWN and SDL_KEYUP events
...... 
250260                pixelFit = TRUE;
251261                picturePortion.w = (Uint16) SCREENWIDTH;
252262                picturePortion.h = (Uint16) SCREENHEIGHT;
263                scale = 1.0;
253264                scaled_img = SDL_ConvertSurface (picture, picture->format, picture->flags);
265                if (scaled_img == (SDL_Surface *) (NULL))
266                {
267                    fprintf (stderr, "\n Error from ConvertSurface()\n\n");
268                    exit (1);
269                }
254270            }
255271            if (iPressed)
256272            { // Zoom in
...... 
260276                pixelFit = FALSE;
261277                scale += ZOOMSTEP;
262278                picturePortion.w = (Uint16) ((double) (imgWidth) * scale);
279                if ( (picturePortion.w - picturePortion.x) >= imgWidth )
280                    picturePortion.w = imgWidth - picturePortion.x;
281                if (picturePortion.w < 1)
282                    picturePortion.w = 1;
263283                picturePortion.h = (Uint16) ((double) (imgHeight) * scale);
284                if ( (picturePortion.h - picturePortion.y) >= imgHeight )
285                    picturePortion.h = imgHeight - picturePortion.y;
286                if (picturePortion.h < 1)
287                    picturePortion.h = 1;
264288                scaled_img = zoomSurface (picture, scale, scale, smoothing);
289                //scaled_img = rotozoomSurface (picture, 0, scale, SMOOTHING_ON);
290
265291                if (scaled_img == (SDL_Surface *) (NULL))
266292                {
267293                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
...... 
275301                alreadyFit = FALSE;
276302                pixelFit = FALSE;
277303                scale -= ZOOMSTEP;
278                if (scale < 0)
279                    scale = 0;
304                if (scale <= 0.0)
305                    scale = 0.01;
280306                picturePortion.w = (Uint16) ((double) (imgWidth) * scale);
307                if ( (picturePortion.w - picturePortion.x) >= imgWidth)
308                    picturePortion.w = imgWidth - picturePortion.x;
309                if (picturePortion.w < 1)
310                    picturePortion.w = 1;
281311                picturePortion.h = (Uint16) ((double) (imgHeight) * scale);
312                if ( (picturePortion.h - picturePortion.y) >= imgHeight )
313                    picturePortion.h = imgHeight - picturePortion.y;
314                if (picturePortion.h < 1)
315                    picturePortion.h = 1;
316
282317                scaled_img = zoomSurface (picture, scale, scale, smoothing);
318                //scaled_img = rotozoomSurface (picture, 0, scale, SMOOTHING_ON);
283319                if (scaled_img == (SDL_Surface *) (NULL))
284320                {
285321                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
...... 
292328                if (scaled_img != (SDL_Surface *) (NULL))
293329                    temp_img = scaled_img;
294330                else
295                    temp_img = picture;
331                {
332                    fprintf (stderr, "\n Error: NULL scaled_img\n\n");
333                    exit (1);
334                }
296335                alreadyFit = FALSE;
297336                scaled_img = rotozoomSurface (temp_img, -90, 1.0, SMOOTHING_OFF);
298                if (temp_img != picture)
299                    SDL_FreeSurface (temp_img);
337                SDL_FreeSurface (temp_img);
300338                if (scaled_img == (SDL_Surface *) (NULL))
301339                {
302                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
340                    fprintf (stderr, "\n Error from rotozoomSurface()\n\n");
303341                    exit (1);
304342                }
305343            }
...... 
308346                if (scaled_img != (SDL_Surface *) (NULL))
309347                    temp_img = scaled_img;
310348                else
311                    temp_img = picture;
349                {
350                    fprintf (stderr, "\n Error: NULL scaled_img\n\n");
351                    exit (1);
352                }
312353                alreadyFit = FALSE;
313354                scaled_img = rotozoomSurface (temp_img, 90, 1.0, SMOOTHING_OFF);
314                if (temp_img != picture)
315                    SDL_FreeSurface (temp_img);
355                SDL_FreeSurface (temp_img);
316356                if (scaled_img == (SDL_Surface *) (NULL))
317357                {
318                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
358                    fprintf (stderr, "\n Error from rotozoomSurface()\n\n");
319359                    exit (1);
320360                }
321361            }
...... 
349389                alreadyFit = FALSE;
350390            }
351391
352            SDL_FillRect(screen, NULL, 0); // draw background color (black)
392            SDL_FillRect(screen, (SDL_Rect *) NULL, 0); // draw background color (black)
353393            SDL_BlitSurface(scaled_img, &picturePortion, screen, &screenPortion);
354394            SDL_Flip(screen);
355395        } // end of if(SDL_WaitEvent())

Archive Download the corresponding diff file

Branches:
master



interactive