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 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #include <SDL_image.h> |
---|
26 | |
---|
27 | #include "pipi.h" |
---|
28 | #include "pipi_internals.h" |
---|
29 | |
---|
30 | static int pipi_free_sdl(pipi_image_t *); |
---|
31 | static SDL_Surface *create_32bpp_surface(int w, int h); |
---|
32 | |
---|
33 | pipi_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_RGBA_C].pixels = priv->pixels; |
---|
52 | img->p[PIPI_PIXELS_RGBA_C].w = priv->w; |
---|
53 | img->p[PIPI_PIXELS_RGBA_C].h = priv->h; |
---|
54 | img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch; |
---|
55 | img->p[PIPI_PIXELS_RGBA_C].bpp = 32; |
---|
56 | img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h; |
---|
57 | img->last_modified = PIPI_PIXELS_RGBA_C; |
---|
58 | |
---|
59 | img->codec_priv = (void *)priv; |
---|
60 | img->codec_format = PIPI_PIXELS_RGBA_C; |
---|
61 | img->codec_free = pipi_free_sdl; |
---|
62 | |
---|
63 | img->wrap = 0; |
---|
64 | img->u8 = 1; |
---|
65 | |
---|
66 | return img; |
---|
67 | } |
---|
68 | |
---|
69 | int pipi_save_sdl(pipi_image_t *img, const char *name) |
---|
70 | { |
---|
71 | if(!img->codec_priv) |
---|
72 | { |
---|
73 | SDL_Surface *priv = create_32bpp_surface(img->w, img->h); |
---|
74 | |
---|
75 | /* FIXME: check pitch differences here */ |
---|
76 | if(img->last_modified == PIPI_PIXELS_RGBA_C) |
---|
77 | { |
---|
78 | memcpy(priv->pixels, img->p[PIPI_PIXELS_RGBA_C].pixels, |
---|
79 | priv->pitch * priv->h); |
---|
80 | free(img->p[PIPI_PIXELS_RGBA_C].pixels); |
---|
81 | } |
---|
82 | |
---|
83 | img->p[PIPI_PIXELS_RGBA_C].pixels = priv->pixels; |
---|
84 | img->p[PIPI_PIXELS_RGBA_C].w = priv->w; |
---|
85 | img->p[PIPI_PIXELS_RGBA_C].h = priv->h; |
---|
86 | img->p[PIPI_PIXELS_RGBA_C].pitch = priv->pitch; |
---|
87 | img->p[PIPI_PIXELS_RGBA_C].bpp = 32; |
---|
88 | img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h; |
---|
89 | |
---|
90 | img->codec_priv = (void *)priv; |
---|
91 | img->codec_format = PIPI_PIXELS_RGBA_C; |
---|
92 | img->codec_free = pipi_free_sdl; |
---|
93 | |
---|
94 | img->wrap = 0; |
---|
95 | img->u8 = 1; |
---|
96 | } |
---|
97 | |
---|
98 | pipi_set_colorspace(img, img->codec_format); |
---|
99 | SDL_SaveBMP(img->codec_priv, name); |
---|
100 | |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * XXX: The following functions are local. |
---|
106 | */ |
---|
107 | |
---|
108 | static int pipi_free_sdl(pipi_image_t *img) |
---|
109 | { |
---|
110 | SDL_FreeSurface(img->codec_priv); |
---|
111 | |
---|
112 | return 0; |
---|
113 | } |
---|
114 | |
---|
115 | static SDL_Surface *create_32bpp_surface(int w, int h) |
---|
116 | { |
---|
117 | Uint32 rmask, gmask, bmask, amask; |
---|
118 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
119 | rmask = 0xff000000; |
---|
120 | gmask = 0x00ff0000; |
---|
121 | bmask = 0x0000ff00; |
---|
122 | amask = 0x00000000; |
---|
123 | #else |
---|
124 | rmask = 0x000000ff; |
---|
125 | gmask = 0x0000ff00; |
---|
126 | bmask = 0x00ff0000; |
---|
127 | amask = 0x00000000; |
---|
128 | #endif |
---|
129 | |
---|
130 | return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, |
---|
131 | rmask, gmask, bmask, amask); |
---|
132 | } |
---|
133 | |
---|