Skip to content

Latest commit

 

History

History
217 lines (196 loc) · 5.37 KB

showimage.c

File metadata and controls

217 lines (196 loc) · 5.37 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
showimage: A test application for the SDL image loading library.
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
Dec 14, 2001
Dec 14, 2001
5
This library is free software; you can redistribute it and/or
Feb 4, 2006
Feb 4, 2006
6
modify it under the terms of the GNU Lesser General Public
Dec 14, 2001
Dec 14, 2001
7
License as published by the Free Software Foundation; either
Feb 4, 2006
Feb 4, 2006
8
version 2.1 of the License, or (at your option) any later version.
Aug 10, 2000
Aug 10, 2000
9
Dec 14, 2001
Dec 14, 2001
10
This library is distributed in the hope that it will be useful,
Aug 10, 2000
Aug 10, 2000
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
Dec 14, 2001
Dec 14, 2001
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 4, 2006
Feb 4, 2006
13
Lesser General Public License for more details.
Aug 10, 2000
Aug 10, 2000
14
Feb 4, 2006
Feb 4, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Aug 10, 2000
Aug 10, 2000
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
23
24
*/
#include <stdlib.h>
#include <stdio.h>
Apr 5, 2001
Apr 5, 2001
25
#include <string.h>
Aug 10, 2000
Aug 10, 2000
26
27
28
29
#include "SDL.h"
#include "SDL_image.h"
Apr 28, 2001
Apr 28, 2001
30
31
32
33
34
35
/* #define XPM_INCLUDED and supply picture.xpm to test the XPM inclusion
feature */
#ifdef XPM_INCLUDED
#include "picture.xpm"
#endif
Aug 10, 2000
Aug 10, 2000
36
37
/* Draw a Gimpish background pattern to show transparency in the image */
Sep 26, 2009
Sep 26, 2009
38
static void draw_background(SDL_Surface *screen)
Aug 10, 2000
Aug 10, 2000
39
40
41
42
43
44
45
46
47
48
49
50
51
{
Uint8 *dst = screen->pixels;
int x, y;
int bpp = screen->format->BytesPerPixel;
Uint32 col[2];
col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
for(y = 0; y < screen->h; y++) {
for(x = 0; x < screen->w; x++) {
/* use an 8x8 checkerboard pattern */
Uint32 c = col[((x ^ y) >> 3) & 1];
switch(bpp) {
case 1:
May 12, 2006
May 12, 2006
52
dst[x] = (Uint8)c;
Aug 10, 2000
Aug 10, 2000
53
54
break;
case 2:
May 12, 2006
May 12, 2006
55
((Uint16 *)dst)[x] = (Uint16)c;
Aug 10, 2000
Aug 10, 2000
56
57
break;
case 3:
May 12, 2006
May 12, 2006
58
59
60
61
62
63
64
65
66
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
dst[x * 3] = (Uint8)(c);
dst[x * 3 + 1] = (Uint8)(c >> 8);
dst[x * 3 + 2] = (Uint8)(c >> 16);
#else
dst[x * 3] = (Uint8)(c >> 16);
dst[x * 3 + 1] = (Uint8)(c >> 8);
dst[x * 3 + 2] = (Uint8)(c);
#endif
Aug 10, 2000
Aug 10, 2000
67
68
69
70
71
72
73
74
75
76
break;
case 4:
((Uint32 *)dst)[x] = c;
break;
}
}
dst += screen->pitch;
}
}
Sep 1, 2000
Sep 1, 2000
77
int main(int argc, char *argv[])
Aug 10, 2000
Aug 10, 2000
78
{
Jan 30, 2001
Jan 30, 2001
79
Uint32 flags;
Aug 10, 2000
Aug 10, 2000
80
SDL_Surface *screen, *image;
Jan 30, 2001
Jan 30, 2001
81
82
int i, depth, done;
SDL_Event event;
Sep 30, 2009
Sep 30, 2009
83
#if 0
Jan 4, 2009
Jan 4, 2009
84
SDL_RWops* rw_ops;
Sep 30, 2009
Sep 30, 2009
85
#endif
Aug 10, 2000
Aug 10, 2000
86
87
88
89
/* Check command line usage */
if ( ! argv[1] ) {
fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
Nov 30, 2000
Nov 30, 2000
90
return(1);
Aug 10, 2000
Aug 10, 2000
91
92
93
94
95
}
/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
Nov 30, 2000
Nov 30, 2000
96
return(255);
Aug 10, 2000
Aug 10, 2000
97
98
}
Jan 30, 2001
Jan 30, 2001
99
100
101
102
103
104
105
flags = SDL_SWSURFACE;
for ( i=1; argv[i]; ++i ) {
if ( strcmp(argv[i], "-fullscreen") == 0 ) {
SDL_ShowCursor(0);
flags |= SDL_FULLSCREEN;
continue;
}
Jan 4, 2009
Jan 4, 2009
106
107
108
109
110
111
112
113
114
115
116
117
#if 0
rw_ops = SDL_RWFromFile(argv[1], "r");
fprintf(stderr, "BMP:\t%d\n", IMG_isBMP(rw_ops));
fprintf(stderr, "GIF:\t%d\n", IMG_isGIF(rw_ops));
fprintf(stderr, "JPG:\t%d\n", IMG_isJPG(rw_ops));
fprintf(stderr, "PNG:\t%d\n", IMG_isPNG(rw_ops));
fprintf(stderr, "TIF:\t%d\n", IMG_isTIF(rw_ops));
/* fprintf(stderr, "TGA:\t%d\n", IMG_isTGA(rw_ops)); */
fprintf(stderr, "PCX:\t%d\n", IMG_isPCX(rw_ops));
#endif
Jan 30, 2001
Jan 30, 2001
118
/* Open the image file */
Apr 28, 2001
Apr 28, 2001
119
120
121
#ifdef XPM_INCLUDED
image = IMG_ReadXPMFromArray(picture_xpm);
#else
Jan 30, 2001
Jan 30, 2001
122
image = IMG_Load(argv[i]);
Apr 28, 2001
Apr 28, 2001
123
#endif
Jan 30, 2001
Jan 30, 2001
124
125
126
if ( image == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n",
argv[i], SDL_GetError());
Jan 31, 2001
Jan 31, 2001
127
continue;
Jan 30, 2001
Jan 30, 2001
128
129
130
131
132
133
134
}
SDL_WM_SetCaption(argv[i], "showimage");
/* Create a display for the image */
depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
/* Use the deepest native mode, except that we emulate 32bpp
for viewing non-indexed images on 8bpp screens */
Jan 31, 2001
Jan 31, 2001
135
136
137
138
139
140
141
if ( depth == 0 ) {
if ( image->format->BytesPerPixel > 1 ) {
depth = 32;
} else {
depth = 8;
}
} else
Jan 30, 2001
Jan 30, 2001
142
143
144
if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
depth = 32;
}
Feb 27, 2001
Feb 27, 2001
145
146
if(depth == 8)
flags |= SDL_HWPALETTE;
Jan 30, 2001
Jan 30, 2001
147
148
149
screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
if ( screen == NULL ) {
fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
Aug 10, 2000
Aug 10, 2000
150
image->w, image->h, depth, SDL_GetError());
Jan 31, 2001
Jan 31, 2001
151
continue;
Jan 30, 2001
Jan 30, 2001
152
}
Aug 10, 2000
Aug 10, 2000
153
Jan 30, 2001
Jan 30, 2001
154
155
156
157
158
/* Set the palette, if one exists */
if ( image->format->palette ) {
SDL_SetColors(screen, image->format->palette->colors,
0, image->format->palette->ncolors);
}
Aug 10, 2000
Aug 10, 2000
159
Jan 30, 2001
Jan 30, 2001
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Draw a background pattern if the surface has transparency */
if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY))
draw_background(screen);
/* Display the image */
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
done = 0;
while ( ! done ) {
if ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_LEFT:
if ( i > 1 ) {
i -= 2;
done = 1;
}
break;
case SDLK_RIGHT:
if ( argv[i+1] ) {
done = 1;
}
break;
case SDLK_ESCAPE:
case SDLK_q:
argv[i+1] = NULL;
/* Drop through to done */
case SDLK_SPACE:
case SDLK_TAB:
done = 1;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
done = 1;
break;
case SDL_QUIT:
argv[i+1] = NULL;
done = 1;
break;
default:
break;
}
} else {
SDL_Delay(10);
}
Aug 10, 2000
Aug 10, 2000
210
}
Jan 30, 2001
Jan 30, 2001
211
SDL_FreeSurface(image);
Aug 10, 2000
Aug 10, 2000
212
213
214
}
/* We're done! */
Nov 29, 2000
Nov 29, 2000
215
SDL_Quit();
Nov 30, 2000
Nov 30, 2000
216
return(0);
Aug 10, 2000
Aug 10, 2000
217
}