source: libpipi/trunk/pipi/codec/sdl.c @ 2844

Last change on this file since 2844 was 2844, checked in by Sam Hocevar, 12 years ago

Fix headers.

File size: 3.3 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * sdl.c: SDL_image I/O functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <SDL_image.h>
27
28#include "pipi.h"
29#include "pipi_internals.h"
30
31static int pipi_free_sdl(pipi_image_t *);
32static SDL_Surface *create_32bpp_surface(int w, int h);
33
34pipi_image_t *pipi_load_sdl(const char *name)
35{
36    pipi_image_t *img;
37    SDL_Surface *priv = IMG_Load(name);
38
39    if(!priv)
40        return NULL;
41
42    if(priv->format->BytesPerPixel != 4)
43    {
44        SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
45        SDL_BlitSurface(priv, NULL, tmp, NULL);
46        SDL_FreeSurface(priv);
47        priv = tmp;
48    }
49
50    img = pipi_new(priv->w, priv->h);
51
52    img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels;
53    img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
54    img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
55    img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
56    img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
57    img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
58    img->last_modified = PIPI_PIXELS_RGBA_C;
59
60    img->codec_priv = (void *)priv;
61    img->codec_format = PIPI_PIXELS_RGBA_C;
62    img->codec_free = pipi_free_sdl;
63
64    img->wrap = 0;
65    img->u8 = 1;
66
67    return img;
68}
69
70int pipi_save_sdl(pipi_image_t *img, const char *name)
71{
72    if(!img->codec_priv)
73    {
74        SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
75
76        /* FIXME: check pitch differences here */
77        if(img->last_modified == PIPI_PIXELS_RGBA_C)
78        {
79            memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA_C].pixels,
80                   priv->pitch * priv->h);
81            free(img->p[PIPI_PIXELS_RGBA_C].pixels);
82        }
83
84        img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels;
85        img->p[PIPI_PIXELS_RGBA_C].w = priv->w;
86        img->p[PIPI_PIXELS_RGBA_C].h = priv->h;
87        img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch;
88        img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
89        img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
90
91        img->codec_priv = (void *)priv;
92        img->codec_format = PIPI_PIXELS_RGBA_C;
93        img->codec_free = pipi_free_sdl;
94
95        img->wrap = 0;
96        img->u8 = 1;
97    }
98
99    pipi_getpixels(img, img->codec_format);
100    SDL_SaveBMP(img->codec_priv, name);
101
102    return 0;
103}
104
105/*
106 * XXX: The following functions are local.
107 */
108
109static int pipi_free_sdl(pipi_image_t *img)
110{
111    SDL_FreeSurface(img->codec_priv);
112
113    return 0;
114}
115
116static SDL_Surface *create_32bpp_surface(int w, int h)
117{
118    Uint32 rmask, gmask, bmask, amask;
119#if SDL_BYTEORDER == SDL_BIG_ENDIAN
120    rmask = 0xff000000;
121    gmask = 0x00ff0000;
122    bmask = 0x0000ff00;
123    amask = 0x00000000;
124#else
125    rmask = 0x000000ff;
126    gmask = 0x0000ff00;
127    bmask = 0x00ff0000;
128    amask = 0x00000000;
129#endif
130
131    return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
132                                rmask, gmask, bmask, amask);
133}
134
Note: See TracBrowser for help on using the repository browser.