showimage.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 02:09:27 +0000
changeset 187 3405ab551300
parent 186 5fbbeabf647f
child 204 dcb38b0e78a6
permissions -rw-r--r--
Date: Fri, 2 Jan 2009 02:01:16 -0800
From: "E. Wing"
Subject: [SDL] Submission: ImageIO backend for SDL_Image on Mac OS X

I have a submission for SDL_image. I have implemented an ImageIO
backend for Mac OS X.

I've uploaded the code here:
http://www.assembla.com/spaces/SDL_Image_ImageIO

A Mercurial repository and a raw tar-ball can be found there.


The existing SDL_image implementation relies on 3rd-party libjpeg,
libpng, libgif, and libtiff which is a pain to build and maintain. In
the Mac release, we currently statically link all these libraries into
the framework. But Mac OS X already comes with these libraries, but
they are hidden away underneath their ImageIO framework. This
submission uses ImageIO to load these image formats.

To further minimize duplicated code, I also moved the bmp and tga
loader to ImageIO. Incidentally, Apple seems to have an implementation
that can detect if a file is a TGA.


ImageIO was introduced in Tiger 10.4. So this submission is intended
to go with the SDL_image that will accompany the SDL 1.3 release.

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