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

Last change on this file since 2756 was 2756, checked in by Sam Hocevar, 13 years ago
  • Add a new image flag, u8, which indicates whether the image samples come from an 8-bpp source.
File size: 3.2 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->p[PIPI_PIXELS_RGBA32].bpp = 32;
56    img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
57    img->last_modified = PIPI_PIXELS_RGBA32;
58
59    img->codec_priv = (void *)priv;
60    img->codec_format = PIPI_PIXELS_RGBA32;
61
62    img->wrap = 0;
63    img->u8 = 1;
64
65    return img;
66}
67
68void pipi_free_sdl(pipi_image_t *img)
69{
70    SDL_FreeSurface(img->codec_priv);
71}
72
73void pipi_save_sdl(pipi_image_t *img, const char *name)
74{
75    if(!img->codec_priv)
76    {
77        SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
78
79        /* FIXME: check pitch differences here */
80        if(img->last_modified == PIPI_PIXELS_RGBA32)
81        {
82            memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA32].pixels,
83                   priv->pitch * priv->h);
84            free(img->p[PIPI_PIXELS_RGBA32].pixels);
85        }
86
87        img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
88        img->p[PIPI_PIXELS_RGBA32].w = priv->w;
89        img->p[PIPI_PIXELS_RGBA32].h = priv->h;
90        img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
91        img->p[PIPI_PIXELS_RGBA32].bpp = 32;
92        img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
93
94        img->codec_priv = (void *)priv;
95        img->codec_format = PIPI_PIXELS_RGBA32;
96
97        img->wrap = 0;
98        img->u8 = 1;
99    }
100
101    pipi_getpixels(img, img->codec_format);
102    SDL_SaveBMP(img->codec_priv, name);
103}
104
105/*
106 * The following functions are local.
107 */
108
109static SDL_Surface *create_32bpp_surface(int w, int h)
110{
111    Uint32 rmask, gmask, bmask, amask;
112#if SDL_BYTEORDER == SDL_BIG_ENDIAN
113    rmask = 0xff000000;
114    gmask = 0x00ff0000;
115    bmask = 0x0000ff00;
116    amask = 0x00000000;
117#else
118    rmask = 0x000000ff;
119    gmask = 0x0000ff00;
120    bmask = 0x00ff0000;
121    amask = 0x00000000;
122#endif
123
124    return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
125                                rmask, gmask, bmask, amask);
126}
127
Note: See TracBrowser for help on using the repository browser.