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

Last change on this file since 2629 was 2629, checked in by Sam Hocevar, 14 years ago
  • pipi.c: reimplement pipi_new() without relying on the underlying codec library, so we can have several or none of them at some time.
  • sdl.c imlib.c: allow to save images that weren't created using the current codec.
File size: 2.9 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation 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 SDL_Surface *create_32bpp_surface(int w, int h);
32
33pipi_image_t *pipi_load_sdl(const char *name)
34{
35    pipi_image_t *img;
36    SDL_Surface *priv = IMG_Load(name);
37
38    if(!priv)
39        return NULL;
40
41    if(priv->format->BytesPerPixel != 4)
42    {
43        SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
44        SDL_BlitSurface(priv, NULL, tmp, NULL);
45        SDL_FreeSurface(priv);
46        priv = tmp;
47    }
48
49    img = pipi_new(priv->w, priv->h);
50
51    img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
52    img->p[PIPI_PIXELS_RGBA32].w = priv->w;
53    img->p[PIPI_PIXELS_RGBA32].h = priv->h;
54    img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
55    img->last_modified = PIPI_PIXELS_RGBA32;
56
57    img->codec_priv = (void *)priv;
58    img->codec_format = PIPI_PIXELS_RGBA32;
59
60    return img;
61}
62
63void pipi_free_sdl(pipi_image_t *img)
64{
65    SDL_FreeSurface(img->codec_priv);
66}
67
68void pipi_save_sdl(pipi_image_t *img, const char *name)
69{
70    if(!img->codec_priv)
71    {
72        SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
73
74        /* FIXME: check pitch differences here */
75        if(img->last_modified == PIPI_PIXELS_RGBA32)
76        {
77            memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA32].pixels,
78                   priv->pitch * priv->h);
79            free(img->p[PIPI_PIXELS_RGBA32].pixels);
80        }
81
82        img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
83        img->p[PIPI_PIXELS_RGBA32].w = priv->w;
84        img->p[PIPI_PIXELS_RGBA32].h = priv->h;
85        img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
86
87        img->codec_priv = (void *)priv;
88        img->codec_format = PIPI_PIXELS_RGBA32;
89    }
90
91    pipi_getpixels(img, img->codec_format);
92    SDL_SaveBMP(img->codec_priv, name);
93}
94
95/*
96 * The following functions are local.
97 */
98
99static SDL_Surface *create_32bpp_surface(int w, int h)
100{
101    Uint32 rmask, gmask, bmask, amask;
102#if SDL_BYTEORDER == SDL_BIG_ENDIAN
103    rmask = 0xff000000;
104    gmask = 0x00ff0000;
105    bmask = 0x0000ff00;
106    amask = 0x00000000;
107#else
108    rmask = 0x000000ff;
109    gmask = 0x0000ff00;
110    bmask = 0x00ff0000;
111    amask = 0x00000000;
112#endif
113
114    return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
115                                rmask, gmask, bmask, amask);
116}
117
Note: See TracBrowser for help on using the repository browser.