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 | * imlib.c: ImLib 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 <Imlib2.h> |
---|
26 | |
---|
27 | #include "pipi.h" |
---|
28 | #include "pipi_internals.h" |
---|
29 | |
---|
30 | static int pipi_free_imlib2(pipi_image_t *); |
---|
31 | |
---|
32 | pipi_image_t *pipi_load_imlib2(const char *name) |
---|
33 | { |
---|
34 | pipi_image_t *img; |
---|
35 | Imlib_Image priv = imlib_load_image(name); |
---|
36 | |
---|
37 | if(!priv) |
---|
38 | return NULL; |
---|
39 | |
---|
40 | imlib_context_set_image(priv); |
---|
41 | |
---|
42 | if(!imlib_image_get_data()) |
---|
43 | { |
---|
44 | imlib_free_image(); |
---|
45 | return NULL; |
---|
46 | } |
---|
47 | |
---|
48 | img = pipi_new(imlib_image_get_width(), imlib_image_get_height()); |
---|
49 | |
---|
50 | img->p[PIPI_PIXELS_RGBA_U8].pixels = imlib_image_get_data(); |
---|
51 | img->p[PIPI_PIXELS_RGBA_U8].w = img->w; |
---|
52 | img->p[PIPI_PIXELS_RGBA_U8].h = img->h; |
---|
53 | img->p[PIPI_PIXELS_RGBA_U8].pitch = 4 * img->w; |
---|
54 | img->p[PIPI_PIXELS_RGBA_U8].bpp = 32; |
---|
55 | img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h; |
---|
56 | img->last_modified = PIPI_PIXELS_RGBA_U8; |
---|
57 | |
---|
58 | img->codec_priv = (void *)priv; |
---|
59 | img->codec_format = PIPI_PIXELS_RGBA_U8; |
---|
60 | img->codec_free = pipi_free_imlib2; |
---|
61 | |
---|
62 | img->wrap = 0; |
---|
63 | img->u8 = 1; |
---|
64 | |
---|
65 | return img; |
---|
66 | } |
---|
67 | |
---|
68 | int pipi_save_imlib2(pipi_image_t *img, const char *name) |
---|
69 | { |
---|
70 | if(!img->codec_priv) |
---|
71 | { |
---|
72 | Imlib_Image priv = imlib_create_image(img->w, img->h); |
---|
73 | void *data; |
---|
74 | |
---|
75 | imlib_context_set_image(priv); |
---|
76 | imlib_image_set_has_alpha(1); |
---|
77 | data = imlib_image_get_data(); |
---|
78 | |
---|
79 | /* FIXME: check pitch differences here */ |
---|
80 | if(img->last_modified == PIPI_PIXELS_RGBA_U8) |
---|
81 | { |
---|
82 | memcpy(data, img->p[PIPI_PIXELS_RGBA_U8].pixels, |
---|
83 | 4 * img->w * img->h); |
---|
84 | free(img->p[PIPI_PIXELS_RGBA_U8].pixels); |
---|
85 | } |
---|
86 | |
---|
87 | img->p[PIPI_PIXELS_RGBA_U8].pixels = data; |
---|
88 | img->p[PIPI_PIXELS_RGBA_U8].w = imlib_image_get_width(); |
---|
89 | img->p[PIPI_PIXELS_RGBA_U8].h = imlib_image_get_height(); |
---|
90 | img->p[PIPI_PIXELS_RGBA_U8].pitch = 4 * imlib_image_get_width(); |
---|
91 | img->p[PIPI_PIXELS_RGBA_U8].bpp = 32; |
---|
92 | img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h; |
---|
93 | |
---|
94 | img->codec_priv = (void *)priv; |
---|
95 | img->codec_format = PIPI_PIXELS_RGBA_U8; |
---|
96 | img->codec_free = pipi_free_imlib2; |
---|
97 | |
---|
98 | img->wrap = 0; |
---|
99 | img->u8 = 1; |
---|
100 | } |
---|
101 | |
---|
102 | pipi_set_colorspace(img, img->codec_format); |
---|
103 | imlib_context_set_image(img->codec_priv); |
---|
104 | imlib_save_image(name); |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | /* |
---|
110 | * XXX: The following functions are local. |
---|
111 | */ |
---|
112 | |
---|
113 | static int pipi_free_imlib2(pipi_image_t *img) |
---|
114 | { |
---|
115 | imlib_context_set_image(img->codec_priv); |
---|
116 | imlib_free_image(); |
---|
117 | |
---|
118 | return 0; |
---|
119 | } |
---|
120 | |
---|