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 == 1) |
---|
40 | { |
---|
41 | img = pipi_new(priv->w, priv->h); |
---|
42 | SDL_BlitSurface(priv, NULL, img->priv, NULL); |
---|
43 | SDL_FreeSurface(priv); |
---|
44 | return img; |
---|
45 | } |
---|
46 | |
---|
47 | img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); |
---|
48 | img->width = priv->w; |
---|
49 | img->height = priv->h; |
---|
50 | img->pitch = priv->pitch; |
---|
51 | img->channels = priv->format->BytesPerPixel; |
---|
52 | img->pixels = priv->pixels; |
---|
53 | img->priv = (void *)priv; |
---|
54 | |
---|
55 | return img; |
---|
56 | } |
---|
57 | |
---|
58 | pipi_image_t *pipi_new_sdl(int width, int height) |
---|
59 | { |
---|
60 | pipi_image_t *img; |
---|
61 | SDL_Surface *priv; |
---|
62 | Uint32 rmask, gmask, bmask, amask; |
---|
63 | #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
64 | rmask = 0xff000000; |
---|
65 | gmask = 0x00ff0000; |
---|
66 | bmask = 0x0000ff00; |
---|
67 | amask = 0x00000000; |
---|
68 | #else |
---|
69 | rmask = 0x000000ff; |
---|
70 | gmask = 0x0000ff00; |
---|
71 | bmask = 0x00ff0000; |
---|
72 | amask = 0x00000000; |
---|
73 | #endif |
---|
74 | priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, |
---|
75 | rmask, gmask, bmask, amask); |
---|
76 | |
---|
77 | if(!priv) |
---|
78 | return NULL; |
---|
79 | |
---|
80 | img = (pipi_image_t *)malloc(sizeof(pipi_image_t)); |
---|
81 | img->width = priv->w; |
---|
82 | img->height = priv->h; |
---|
83 | img->pitch = priv->pitch; |
---|
84 | img->channels = priv->format->BytesPerPixel; |
---|
85 | img->pixels = priv->pixels; |
---|
86 | img->priv = (void *)priv; |
---|
87 | |
---|
88 | return img; |
---|
89 | } |
---|
90 | |
---|
91 | void pipi_free_sdl(pipi_image_t *img) |
---|
92 | { |
---|
93 | SDL_FreeSurface(img->priv); |
---|
94 | |
---|
95 | free(img); |
---|
96 | } |
---|
97 | |
---|
98 | void pipi_save_sdl(pipi_image_t *img, const char *name) |
---|
99 | { |
---|
100 | SDL_SaveBMP(img->priv, name); |
---|
101 | } |
---|
102 | |
---|