|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- /*
- * libpipi Proper image processing implementation library
- * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
- * All Rights Reserved
- *
- * $Id$
- *
- * This library is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details.
- */
-
- /*
- * sdl.c: SDL_image I/O functions
- */
-
- #include "config.h"
- #include "common.h"
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include <SDL_image.h>
-
- #include "pipi.h"
- #include "pipi_internals.h"
-
- static SDL_Surface *create_32bpp_surface(int w, int h);
-
- pipi_image_t *pipi_load_sdl(const char *name)
- {
- pipi_image_t *img;
- SDL_Surface *priv = IMG_Load(name);
-
- if(!priv)
- return NULL;
-
- if(priv->format->BytesPerPixel != 4)
- {
- SDL_Surface *tmp = create_32bpp_surface(priv->w, priv->h);
- SDL_BlitSurface(priv, NULL, tmp, NULL);
- SDL_FreeSurface(priv);
- priv = tmp;
- }
-
- img = pipi_new(priv->w, priv->h);
-
- img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
- img->p[PIPI_PIXELS_RGBA32].w = priv->w;
- img->p[PIPI_PIXELS_RGBA32].h = priv->h;
- img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
- img->last_modified = PIPI_PIXELS_RGBA32;
-
- img->codec_priv = (void *)priv;
- img->codec_format = PIPI_PIXELS_RGBA32;
-
- return img;
- }
-
- void pipi_free_sdl(pipi_image_t *img)
- {
- SDL_FreeSurface(img->codec_priv);
- }
-
- void pipi_save_sdl(pipi_image_t *img, const char *name)
- {
- if(!img->codec_priv)
- {
- SDL_Surface *priv = create_32bpp_surface(img->w, img->h);
-
- /* FIXME: check pitch differences here */
- if(img->last_modified == PIPI_PIXELS_RGBA32)
- {
- memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA32].pixels,
- priv->pitch * priv->h);
- free(img->p[PIPI_PIXELS_RGBA32].pixels);
- }
-
- img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels;
- img->p[PIPI_PIXELS_RGBA32].w = priv->w;
- img->p[PIPI_PIXELS_RGBA32].h = priv->h;
- img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch;
-
- img->codec_priv = (void *)priv;
- img->codec_format = PIPI_PIXELS_RGBA32;
- }
-
- pipi_getpixels(img, img->codec_format);
- SDL_SaveBMP(img->codec_priv, name);
- }
-
- /*
- * The following functions are local.
- */
-
- static SDL_Surface *create_32bpp_surface(int w, int h)
- {
- Uint32 rmask, gmask, bmask, amask;
- #if SDL_BYTEORDER == SDL_BIG_ENDIAN
- rmask = 0xff000000;
- gmask = 0x00ff0000;
- bmask = 0x0000ff00;
- amask = 0x00000000;
- #else
- rmask = 0x000000ff;
- gmask = 0x0000ff00;
- bmask = 0x00ff0000;
- amask = 0x00000000;
- #endif
-
- return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
- rmask, gmask, bmask, amask);
- }
-
|