source: libpipi/trunk/pipi/codec/imlib.c @ 2902

Last change on this file since 2902 was 2902, checked in by Sam Hocevar, 11 years ago

Support C99 types on Win32 through the same hacks as in libcaca.

File size: 2.9 KB
Line 
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
30static int pipi_free_imlib2(pipi_image_t *);
31
32pipi_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    img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
42
43    img->p[PIPI_PIXELS_RGBA_C].pixels = imlib_image_get_data();
44    img->p[PIPI_PIXELS_RGBA_C].w = img->w;
45    img->p[PIPI_PIXELS_RGBA_C].h = img->h;
46    img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * img->w;
47    img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
48    img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
49    img->last_modified = PIPI_PIXELS_RGBA_C;
50
51    img->codec_priv = (void *)priv;
52    img->codec_format = PIPI_PIXELS_RGBA_C;
53    img->codec_free = pipi_free_imlib2;
54
55    img->wrap = 0;
56    img->u8 = 1;
57
58    return img;
59}
60
61int pipi_save_imlib2(pipi_image_t *img, const char *name)
62{
63    if(!img->codec_priv)
64    {
65        Imlib_Image priv = imlib_create_image(img->w, img->h);
66        void *data;
67
68        imlib_context_set_image(priv);
69        data = imlib_image_get_data();
70
71        /* FIXME: check pitch differences here */
72        if(img->last_modified == PIPI_PIXELS_RGBA_C)
73        {
74            memcpy(data, img->p[PIPI_PIXELS_RGBA_C].pixels,
75                   4 * img->w * img->h);
76            free(img->p[PIPI_PIXELS_RGBA_C].pixels);
77        }
78
79        img->p[PIPI_PIXELS_RGBA_C].pixels = data;
80        img->p[PIPI_PIXELS_RGBA_C].w = imlib_image_get_width();
81        img->p[PIPI_PIXELS_RGBA_C].h = imlib_image_get_height();
82        img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * imlib_image_get_width();
83        img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
84        img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
85
86        img->codec_priv = (void *)priv;
87        img->codec_format = PIPI_PIXELS_RGBA_C;
88        img->codec_free = pipi_free_imlib2;
89
90        img->wrap = 0;
91        img->u8 = 1;
92    }
93
94    pipi_getpixels(img, img->codec_format);
95    imlib_context_set_image(img->codec_priv);
96    imlib_save_image(name);
97
98    return 0;
99}
100
101/*
102 * XXX: The following functions are local.
103 */
104
105static int pipi_free_imlib2(pipi_image_t *img)
106{
107    imlib_context_set_image(img->codec_priv);
108    imlib_free_image();
109
110    return 0;
111}
112
Note: See TracBrowser for help on using the repository browser.