Date:2010-05-30 16:46:36 (10 years 4 months ago)
Author:Xiangfu Liu
Commit:3cf62be38dbb14ce2dc08abdf714480c99533811
Message:init, version: 0.2.0

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

Change Details

Makefile
1####### openwrt-xburst path
2
3BASEPATH = /home/fcarello/Projects/openwrt-xburst
4TARGETPATH = /staging_dir/target-mipsel_uClibc-0.9.30.1
5
6####### Compiler, tools and options
7
8CC = mipsel-openwrt-linux-uclibc-gcc
9CXX = mipsel-openwrt-linux-uclibc-g++
10STRIP = mipsel-openwrt-linux-uclibc-strip
11DEFINES =
12CFLAGS = -pipe -O2 -fomit-frame-pointer -mips32 -mtune=mips32 -funit-at-a-time -fhonour-copts -msoft-float -Wall -W -D_REENTRANT $(DEFINES)
13CXXFLAGS = -pipe -O2 -fomit-frame-pointer -mips32 -mtune=mips32 -funit-at-a-time -fhonour-copts -msoft-float -Wall -W -D_REENTRANT $(DEFINES)
14INCPATH = -I$(BASEPATH)$(TARGETPATH)/usr/include/ -I$(BASEPATH)$(TARGETPATH)/usr/include/SDL -I.
15LINK = g++
16LFLAGS = -Wl,-O1
17LIBS = $(SUBLIBS) -L$(BASEPATH)$(TARGETPATH)/usr/lib/ -lSDL -lSDL_image -lSDL_gfx -ldirectfb -ldirect -lfusion -lz
18
19SOURCE1 = sdl-imageviewer.c
20TARGET1 = imgv
21
22####### Build rules
23
24all: Clean Viewer
25
26Clean:
27    rm -f $(TARGET1)
28Viewer:
29    $(CC) $(CFLAGS) $(INCPATH) $(SOURCE1) -o $(TARGET1) $(LIBS)
30    $(STRIP) $(TARGET1)
sdl-imageviewer.c
1// 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
4//
5#include <stdlib.h>
6#include <unistd.h>
7#include <SDL/SDL.h>
8#include <SDL/SDL_image.h>
9#include <SDL/SDL_rotozoom.h>
10
11#define TRUE 1
12#define FALSE 0
13#define SCREENWIDTH 320
14#define SCREENHEIGHT 240
15#define SCREENBPP 32
16#define SMOOTHING_OFF 0
17#define SMOOTHING_ON 1
18#define PANSTEP 40
19#define ZOOMSTEP 0.05
20
21int main(int argc, char *argv[])
22{
23    SDL_Surface *screen = NULL,
24                *scaled_img = NULL,
25                *temp_img = NULL,
26                 *picture = NULL;
27    SDL_Event event;
28    SDL_Rect picturePortion,
29                screenPortion;
30    int imgWidth,
31                imgHeight,
32                smoothing = SMOOTHING_ON,
33                fPressed,
34                zPressed,
35                iPressed,
36                oPressed,
37                alreadyFit,
38                pixelFit,
39                lPressed,
40                rPressed,
41                leftPressed,
42                rightPressed,
43                upPressed,
44                downPressed;
45    char sFilename[255] = "\0",
46                sVersion[] = "0.2.0";
47    double scale_x = 1.0,
48                scale_y = 1.0,
49                scale = 1.0;
50
51
52    atexit(SDL_Quit);
53
54    // Process command line
55    if (argc != 2)
56    {
57        fprintf (stderr, "\n imgv v%s. Syntax: imgv <image file>\n\n Hotkeys:\n 'f' fit to screen\n 'z' zoom at pixel level\n 'i' zoom in 'o' zoom out\n 'l' rotate left 'r' rotate right\n 'arrows' pan 'ESC' quit\n\n", sVersion);
58        exit (1);
59    }
60    strncpy (sFilename, argv[1], 254);
61
62    // Initialize the SDL library
63    if (SDL_Init(SDL_INIT_VIDEO) < 0)
64    {
65        fprintf(stderr, "\n Couldn't initialize SDL: %s\n\n", SDL_GetError());
66        exit(1);
67    }
68
69    // Load Picture
70    picture = IMG_Load(sFilename);
71
72    if (picture == (SDL_Surface *) (NULL))
73    {
74        fprintf(stderr, "\n Couldn't load image file %s: %s\n\n", sFilename, SDL_GetError());
75        exit (1);
76    }
77    imgWidth = picture->w;
78    imgHeight = picture->h;
79
80    // Starting point (default view): uniform scaling to best screen fit, keep aspect ratio
81    scale_x = (double) (SCREENWIDTH) / (double) (imgWidth);
82    scale_y = (double) (SCREENHEIGHT) / (double) (imgHeight);
83    if (scale_y < scale_x)
84        scale = scale_y;
85    else
86        scale = scale_x;
87    scaled_img = zoomSurface (picture, scale, scale, SMOOTHING_ON);
88    if (scaled_img == (SDL_Surface *) (NULL))
89    {
90        fprintf (stderr, "\n Error from zoomSurface()\n\n");
91        exit (1);
92    }
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
103
104    screenPortion.x = 0; // destination coordinates: origin
105    screenPortion.y = 0;
106    picturePortion.x = 0;
107    picturePortion.y = 0;
108    picturePortion.w = (Uint16) imgWidth;
109    picturePortion.h = (Uint16) imgHeight;
110
111    // off-cycle: first image drawing
112    SDL_FillRect(screen, NULL, 0); // draw background color (black)
113    SDL_BlitSurface(scaled_img, &picturePortion, screen, &screenPortion);
114    SDL_Flip(screen);
115
116    leftPressed = FALSE;
117    rightPressed = FALSE;
118    upPressed = FALSE;
119    downPressed = FALSE;
120    fPressed = FALSE;
121    zPressed = FALSE;
122    lPressed = FALSE;
123    rPressed = FALSE;
124    iPressed = FALSE;
125    oPressed = FALSE;
126    pixelFit = FALSE;
127    alreadyFit = TRUE;
128
129    while(1)
130    {
131
132        if (SDL_WaitEvent( &event ))
133        {
134            // We only process SDL_KEYDOWN and SDL_KEYUP events
135            switch (event.type)
136            {
137                  case SDL_KEYDOWN:
138                      switch (event.key.keysym.sym)
139                    {
140                          case SDLK_LEFT:
141                              leftPressed = TRUE;
142                          break;
143                          case SDLK_RIGHT:
144                              rightPressed = TRUE;
145                          break;
146                          case SDLK_DOWN:
147                              downPressed = TRUE;
148                          break;
149                          case SDLK_f:
150                              fPressed = TRUE;
151                          break;
152                          case SDLK_z:
153                              zPressed = TRUE;
154                          break;
155                          case SDLK_i:
156                              iPressed = TRUE;
157                          break;
158                          case SDLK_o:
159                              oPressed = TRUE;
160                          break;
161                          case SDLK_l:
162                              lPressed = TRUE;
163                          break;
164                          case SDLK_r:
165                              rPressed = TRUE;
166                          break;
167                          case SDLK_UP:
168                              upPressed = TRUE;
169                          break;
170                          case SDLK_ESCAPE:
171                            if (picture)
172                                SDL_FreeSurface (picture);
173                            if (scaled_img)
174                                SDL_FreeSurface (scaled_img);
175                            if (screen)
176                                SDL_FreeSurface (screen);
177                              exit(0);
178                          default:
179                          break;
180                      }
181                  break;
182                 case SDL_KEYUP:
183                      switch (event.key.keysym.sym)
184                    {
185                          case SDLK_LEFT:
186                              leftPressed = FALSE;
187                          break;
188                          case SDLK_RIGHT:
189                              rightPressed = FALSE;
190                          break;
191                          case SDLK_DOWN:
192                              downPressed = FALSE;
193                          break;
194                          case SDLK_UP:
195                              upPressed = FALSE;
196                          break;
197                          case SDLK_f:
198                              fPressed = FALSE;
199                          break;
200                          case SDLK_z:
201                              zPressed = FALSE;
202                          break;
203                          case SDLK_i:
204                              iPressed = FALSE;
205                          break;
206                          case SDLK_o:
207                              oPressed = FALSE;
208                          break;
209                          case SDLK_l:
210                              lPressed = FALSE;
211                          break;
212                          case SDLK_r:
213                              rPressed = FALSE;
214                          break;
215                          default:
216                          break;
217                      }
218                  break;
219            } // end of switch (event.type)
220
221            // Process commands
222            if (fPressed && !alreadyFit)
223            { // Fit image to screen
224                if (scaled_img != (SDL_Surface *) (NULL))
225                    SDL_FreeSurface (scaled_img); // since zoomSurface() creates a new surface every time
226                alreadyFit = TRUE;
227                pixelFit = FALSE;
228                scale_x = (double) (SCREENWIDTH) / (double) (imgWidth);
229                scale_y = (double) (SCREENHEIGHT) / (double) (imgHeight);
230                if (scale_y < scale_x)
231                    scale = scale_y;
232                else
233                    scale = scale_x;
234                picturePortion.x = 0;
235                picturePortion.y = 0;
236                picturePortion.w = (Uint16) imgWidth;
237                picturePortion.h = (Uint16) imgHeight;
238                scaled_img = zoomSurface (picture, scale, scale, smoothing);
239                if (scaled_img == (SDL_Surface *) (NULL))
240                {
241                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
242                    exit (1);
243                }
244            }
245            if (zPressed && !pixelFit)
246            { // Zoom at 1:1 (100% zoom / actual pixels)
247                if (scaled_img != (SDL_Surface *) (NULL))
248                    SDL_FreeSurface (scaled_img);
249                alreadyFit = FALSE;
250                pixelFit = TRUE;
251                picturePortion.w = (Uint16) SCREENWIDTH;
252                picturePortion.h = (Uint16) SCREENHEIGHT;
253                scaled_img = SDL_ConvertSurface (picture, picture->format, picture->flags);
254            }
255            if (iPressed)
256            { // Zoom in
257                if (scaled_img != (SDL_Surface *) (NULL))
258                    SDL_FreeSurface (scaled_img);
259                alreadyFit = FALSE;
260                pixelFit = FALSE;
261                scale += ZOOMSTEP;
262                picturePortion.w = (Uint16) ((double) (imgWidth) * scale);
263                picturePortion.h = (Uint16) ((double) (imgHeight) * scale);
264                scaled_img = zoomSurface (picture, scale, scale, smoothing);
265                if (scaled_img == (SDL_Surface *) (NULL))
266                {
267                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
268                    exit (1);
269                }
270            }
271            if (oPressed)
272            { // Zoom out
273                if (scaled_img != (SDL_Surface *) (NULL))
274                    SDL_FreeSurface (scaled_img);
275                alreadyFit = FALSE;
276                pixelFit = FALSE;
277                scale -= ZOOMSTEP;
278                if (scale < 0)
279                    scale = 0;
280                picturePortion.w = (Uint16) ((double) (imgWidth) * scale);
281                picturePortion.h = (Uint16) ((double) (imgHeight) * scale);
282                scaled_img = zoomSurface (picture, scale, scale, smoothing);
283                if (scaled_img == (SDL_Surface *) (NULL))
284                {
285                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
286                    exit (1);
287                }
288            }
289
290            if (lPressed)
291            { // Rotate left 90°
292                if (scaled_img != (SDL_Surface *) (NULL))
293                    temp_img = scaled_img;
294                else
295                    temp_img = picture;
296                alreadyFit = FALSE;
297                scaled_img = rotozoomSurface (temp_img, -90, 1.0, SMOOTHING_OFF);
298                if (temp_img != picture)
299                    SDL_FreeSurface (temp_img);
300                if (scaled_img == (SDL_Surface *) (NULL))
301                {
302                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
303                    exit (1);
304                }
305            }
306            if (rPressed)
307            { // Rotate right 90°
308                if (scaled_img != (SDL_Surface *) (NULL))
309                    temp_img = scaled_img;
310                else
311                    temp_img = picture;
312                alreadyFit = FALSE;
313                scaled_img = rotozoomSurface (temp_img, 90, 1.0, SMOOTHING_OFF);
314                if (temp_img != picture)
315                    SDL_FreeSurface (temp_img);
316                if (scaled_img == (SDL_Surface *) (NULL))
317                {
318                    fprintf (stderr, "\n Error from zoomSurface()\n\n");
319                    exit (1);
320                }
321            }
322
323            if (leftPressed)
324            {
325                picturePortion.x -= PANSTEP;
326                if (picturePortion.x < 0)
327                    picturePortion.x = 0;
328                alreadyFit = FALSE;
329            }
330            if (rightPressed)
331            {
332                picturePortion.x += PANSTEP;
333                if (picturePortion.x >= imgWidth)
334                    picturePortion.x = (Sint16) (imgWidth - 1);
335                alreadyFit = FALSE;
336            }
337            if (upPressed)
338            {
339                picturePortion.y -= PANSTEP;
340                if (picturePortion.y < 0)
341                    picturePortion.y = 0;
342                alreadyFit = FALSE;
343            }
344            if (downPressed)
345            {
346                picturePortion.y += PANSTEP;
347                if (picturePortion.y >= imgHeight)
348                    picturePortion.y = (Sint16) (imgHeight - 1);
349                alreadyFit = FALSE;
350            }
351
352            SDL_FillRect(screen, NULL, 0); // draw background color (black)
353            SDL_BlitSurface(scaled_img, &picturePortion, screen, &screenPortion);
354            SDL_Flip(screen);
355        } // end of if(SDL_WaitEvent())
356    } // end of while(1)
357
358    return 0;
359}
360

Archive Download the corresponding diff file

Branches:
master



interactive