Anonymous avatar Anonymous committed 5461377

Added SDL.image and showimage test

Comments (0)

Files changed (5)

  - Fixed SDL_Surface.pixels size to respect pitch rather than width
  - SDL.dll refactored into a class, so it can be used for any SDL-like
    library (i.e., SDL_image, SDL_ttf, etc)
+ - SDL.dll no longer requires a version function to load, will presume
+   version 0.0.0 if unversioned (needed for SDL_image).
  - Added SDL_ttf and SDL_mixer support
+ - Added as_int16 and as_int32 methods to SDL_array.
 
 SDL-ctypes 0.03
 ---------------
         self._dll = getattr(cdll, library_name)
         
         # Get the version of the DLL we're using
-        version_function = getattr(self._dll, version_function_name)
-        version_function.restype = POINTER(_SDL_version)
-        self._version = _version_parts(version_function().contents)
+        try:
+            version_function = getattr(self._dll, version_function_name)
+            version_function.restype = POINTER(_SDL_version)
+            self._version = _version_parts(version_function().contents)
+        except AttributeError:
+            self._version = (0,0,0)
 
     def version_compatible(self, v):
         '''Returns True iff `v` is equal to or later than the loaded library
             import SDL.error
             raise SDL.error.SDL_NotImplementedError, \
                 '%s requires SDL version %s; currently using version %s' % \
-                (name, _version_string(since), _version_string(_version))
+                (name, _version_string(since), _version_string(self._version))
 
 
 
                 raise SDL.error.SDL_NotImplementedError, \
                       '%s requires %s %s; currently using version %s' % \
                       (name, self.library_name, _version_string(since), 
-                       _version_string(_version))
+                       _version_string(self._version))
             if args:
                 _f._args = args
             _f.__doc__ = doc
+#!/usr/bin/env python
+
+'''Load images of various formats as SDL surfaces.
+
+This module supports BMP, PNM (PPM/PGM/PBM), XPM, LBM, PCX, GIF, JPEG, PNG,
+TGA and TIFF formats.
+
+Typical usage::
+
+    from SDL.image import *
+    surface = IMG_Load('image.png')
+
+:note: Early versions of this library (pre-1.2.5) do not have versioning
+    information; do not count on `IMG_Linked_Version` being available.
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+from ctypes import *
+
+import SDL.dll
+import SDL.rwops
+import SDL.version
+import SDL.video
+
+_dll = SDL.dll.SDL_DLL('SDL_image', 'IMG_Linked_Version')
+
+IMG_Linked_Version = _dll.function('IMG_Linked_Version',
+    '''Get the version of the dynamically linked SDL_image library.
+    ''',
+    args=[],
+    arg_types=[],
+    return_type=POINTER(SDL.version.SDL_version),
+    dereference_return=True,
+    require_return=True,
+    since=(1,2,5))
+
+IMG_LoadTyped_RW = _dll.function('IMG_LoadTyped_RW',
+    '''Load an image from an SDL data source, specifying a type.
+
+    If the image format supports a transparent pixel, SDL will set the
+    colorkey for the surface.  You can enable RLE acceleration on the
+    surface afterwards by calling::
+        
+        SDL_SetColorKey(image, SDL_RLEACCEL, image.format.colorkey)
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+        `freesrc` : int
+            If non-zero, the source will be freed after loading.
+        `type` : string
+            One of "BMP", "GIF", "PNG", etc.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src', 'freesrc', 'type'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops), c_int, c_char_p],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_Load = _dll.function('IMG_Load',
+    '''Load an image from a file.
+
+    If the image format supports a transparent pixel, SDL will set the
+    colorkey for the surface.  You can enable RLE acceleration on the
+    surface afterwards by calling::
+        
+        SDL_SetColorKey(image, SDL_RLEACCEL, image.format.colorkey)
+
+    :Parameters:
+        `file` : string
+            Filename to load.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['file'],
+    arg_types=[c_char_p],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_Load_RW = _dll.function('IMG_Load_RW',
+    '''Load an image from an SDL data source.
+
+    If the image format supports a transparent pixel, SDL will set the
+    colorkey for the surface.  You can enable RLE acceleration on the
+    surface afterwards by calling::
+        
+        SDL_SetColorKey(image, SDL_RLEACCEL, image.format.colorkey)
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+        `freesrc` : int
+            If non-zero, the source will be freed after loading.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src', 'freesrc'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops), c_int],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+# IMG_InvertAlpha is a no-op.
+
+IMG_isBMP = _dll.function('IMG_isBMP',
+    '''Detect if a seekable source is a BMP image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+IMG_isGIF = _dll.function('IMG_isGIF',
+    '''Detect if a seekable source is a GIF image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isJPG = _dll.function('IMG_isJPG',
+    '''Detect if a seekable source is a JPG image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isLBM = _dll.function('IMG_isLBM',
+    '''Detect if a seekable source is a LBM image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isPCX = _dll.function('IMG_isPCX',
+    '''Detect if a seekable source is a PCX image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isPNG = _dll.function('IMG_isPNG',
+    '''Detect if a seekable source is a PNG image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isPNM = _dll.function('IMG_isPNM',
+    '''Detect if a seekable source is a PNM image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isTIF = _dll.function('IMG_isTIF',
+    '''Detect if a seekable source is a TIF image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isXCF = _dll.function('IMG_isXCF',
+    '''Detect if a seekable source is a XCF image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isXPM = _dll.function('IMG_isXPM',
+    '''Detect if a seekable source is a XPM image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int)
+
+
+IMG_isXV = _dll.function('IMG_isXV',
+    '''Detect if a seekable source is a XV image.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to inspect.
+    
+    :rtype: int
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=c_int,
+    since=(1,2,5))
+
+IMG_LoadBMP_RW = _dll.function('IMG_LoadBMP_RW',
+    '''Load a BMP image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadGIF_RW = _dll.function('IMG_LoadGIF_RW',
+    '''Load a GIF image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadJPG_RW = _dll.function('IMG_LoadJPG_RW',
+    '''Load a JPG image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadLBM_RW = _dll.function('IMG_LoadLBM_RW',
+    '''Load a LBM image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadPCX_RW = _dll.function('IMG_LoadPCX_RW',
+    '''Load a PCX image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadPNG_RW = _dll.function('IMG_LoadPNG_RW',
+    '''Load a PNG image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadPNM_RW = _dll.function('IMG_LoadPNM_RW',
+    '''Load a PNM image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadTGA_RW = _dll.function('IMG_LoadTGA_RW',
+    '''Load a TGA image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadTIF_RW = _dll.function('IMG_LoadTIF_RW',
+    '''Load a TIF image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadXCF_RW = _dll.function('IMG_LoadXCF_RW',
+    '''Load a XCF image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadXPM_RW = _dll.function('IMG_LoadXPM_RW',
+    '''Load a XPM image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True)
+
+IMG_LoadXV_RW = _dll.function('IMG_LoadXV_RW',
+    '''Load a XV image from an SDL data source.
+
+    :Parameters:
+        `src` : `SDL_RWops`
+            Source RWops to load from.
+
+    :rtype: `SDL_Surface`
+    ''',
+    args=['src'],
+    arg_types=[POINTER(SDL.rwops.SDL_RWops)],
+    return_type=POINTER(SDL.video.SDL_Surface),
+    dereference_return=True,
+    require_return=True,
+    since=(1,2,5))
+
+# IMG_ReadXPMFromArray cannot be implemented.

support/prep_doc.py

     import SDL
     import SDL.ttf
     import SDL.mixer
+    import SDL.image
     write_module(SDL)
     write_module(SDL.ttf)
     write_module(SDL.mixer)
+    write_module(SDL.image)

test/showimage.py

+#!/usr/bin/env python
+
+'''Test application for the SDL.image module.
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+import sys
+
+from SDL import *
+from SDL.image import *
+
+# Draw a Gimpish background pattern to show transparency in the image.
+def draw_background(screen):
+    dst = screen.pixels
+    bpp = screen.format.BytesPerPixel
+    col = (SDL_MapRGB(screen.format, 0x66, 0x66, 0x66),
+           SDL_MapRGB(screen.format, 0x99, 0x99, 0x99))
+    for y in range(screen.h):
+        for x in range(screen.w):
+            c = col[((x ^ y) >> 3) & 1]
+            dst[y * screen.pitch/bpp + x] = c
+
+if __name__ == '__main__':
+    if len(sys.argv) < 2:
+        print >> sys.stderr, 'Usage: %s <image_file>' % sys.argv[0]
+        sys.exit(1)
+
+    SDL_Init(SDL_INIT_VIDEO)
+
+    i = 1
+    flags = SDL_SWSURFACE
+    if sys.argv[i] == '-fullscreen':
+        SDL_ShowCursor(0)
+        flags |= SDL_FULLSCREEN
+        i += 1
+
+    while i < len(sys.argv):
+        # Open the image file
+        image = IMG_Load(sys.argv[i])
+        SDL_WM_SetCaption(sys.argv[i], 'showimage')
+
+        # Create a display for the image
+        depth = SDL_VideoModeOK(image.w, image.h, 32, flags)
+        # Use the deepest native mode, expcept that we emulate 32bpp
+        # for viewing non-indexed images on 8bpp screens.
+        if depth == 0:
+            if image.format.BytesPerPixel > 1:
+                depth = 32
+            else:
+                depth = 8
+        elif image.format.BytesPerPixel > 1 and depth == 8:
+            depth = 32
+        if depth == 8:
+            flags |= SDL_HWPALETTE
+
+        screen = SDL_SetVideoMode(image.w, image.h, depth, flags)
+        
+        # Set the palette, if one exists
+        if image.format.palette:
+            SDL_SetColors(screen, image.format.palette.colors, 0)
+
+        # 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, None, screen, None)
+        SDL_UpdateRect(screen, 0, 0, 0, 0)
+
+        done = 0
+        while not done:
+            event = SDL_WaitEventAndReturn()
+            if event.type == SDL_KEYUP:
+                if event.keysym.sym == SDLK_LEFT:
+                    if i > 1:
+                        i -= 2
+                        done = 1
+                elif event.keysym.sym == SDLK_RIGHT:
+                    if i < len(sys.argv) - 1:
+                        done = 1
+                elif event.keysym.sym in (SDLK_ESCAPE, SDLK_q):
+                    done = 1
+                    i = len(sys.argv)
+                elif event.keysym.sym in (SDLK_SPACE, SDLK_TAB):
+                    done = 1
+            elif event.type == SDL_MOUSEBUTTONDOWN:
+                done = 1
+            elif event.type == SDL_QUIT:
+                i = len(sys.argv)
+                done = 1
+
+        SDL_FreeSurface(image)
+        i += 1
+
+    SDL_Quit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.