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 | |
---|
31 | pipi_image_t *pipi_load_sdl(const char *name) |
---|
32 | { |
---|
33 | pipi_image_t *img; |
---|
34 | SDL_Surface *priv = IMG_Load(name); |
---|
35 | |
---|
36 | if(!priv) |
---|
37 | return NULL; |
---|
38 | |
---|
39 | if(priv->format->BytesPerPixel != 4) |
---|
40 | { |
---|
41 | img = pipi_new(priv->w, priv->h); |
---|
42 | SDL_BlitSurface(priv, NULL, img->codec_priv, NULL); |
---|
43 | SDL_FreeSurface(priv); |
---|
44 | return img; |
---|
45 | } |
---|
46 | |
---|
47 | img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); |
---|
48 | memset(img, 0, sizeof(pipi_image_t)); |
---|
49 | |
---|
50 | img->w = priv->w; |
---|
51 | img->h = priv->h; |
---|
52 | |
---|
53 | img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels; |
---|
54 | img->p[PIPI_PIXELS_RGBA32].w = priv->w; |
---|
55 | img->p[PIPI_PIXELS_RGBA32].h = priv->h; |
---|
56 | img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch; |
---|
57 | img->last_modified = PIPI_PIXELS_RGBA32; |
---|
58 | |
---|
59 | img->codec_priv = (void *)priv; |
---|
60 | img->codec_format = PIPI_PIXELS_RGBA32; |
---|
61 | |
---|
62 | return img; |
---|
63 | } |
---|
64 | |
---|
65 | pipi_image_t *pipi_new_sdl(int width, int height) |
---|
66 | { |
---|
67 | pipi_image_t *img; |
---|
68 | SDL_Surface *priv; |
---|
69 | Uint32 rmask, gmask, bmask, amask; |
---|
70 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
71 | rmask = 0xff000000; |
---|
72 | gmask = 0x00ff0000; |
---|
73 | bmask = 0x0000ff00; |
---|
74 | amask = 0x00000000; |
---|
75 | #else |
---|
76 | rmask = 0x000000ff; |
---|
77 | gmask = 0x0000ff00; |
---|
78 | bmask = 0x00ff0000; |
---|
79 | amask = 0x00000000; |
---|
80 | #endif |
---|
81 | priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, |
---|
82 | rmask, gmask, bmask, amask); |
---|
83 | |
---|
84 | if(!priv) |
---|
85 | return NULL; |
---|
86 | |
---|
87 | img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); |
---|
88 | memset(img, 0, sizeof(pipi_image_t)); |
---|
89 | |
---|
90 | img->w = priv->w; |
---|
91 | img->h = priv->h; |
---|
92 | |
---|
93 | img->p[PIPI_PIXELS_RGBA32].pixels = priv->pixels; |
---|
94 | img->p[PIPI_PIXELS_RGBA32].w = priv->w; |
---|
95 | img->p[PIPI_PIXELS_RGBA32].h = priv->h; |
---|
96 | img->p[PIPI_PIXELS_RGBA32].pitch = priv->pitch; |
---|
97 | img->last_modified = PIPI_PIXELS_RGBA32; |
---|
98 | |
---|
99 | img->codec_priv = (void *)priv; |
---|
100 | img->codec_format = PIPI_PIXELS_RGBA32; |
---|
101 | |
---|
102 | return img; |
---|
103 | } |
---|
104 | |
---|
105 | void pipi_free_sdl(pipi_image_t *img) |
---|
106 | { |
---|
107 | SDL_FreeSurface(img->codec_priv); |
---|
108 | |
---|
109 | free(img); |
---|
110 | } |
---|
111 | |
---|
112 | void pipi_save_sdl(pipi_image_t *img, const char *name) |
---|
113 | { |
---|
114 | pipi_getpixels(img, img->codec_format); |
---|
115 | SDL_SaveBMP(img->codec_priv, name); |
---|
116 | } |
---|
117 | |
---|