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

Last change on this file since 3341 was 3341, checked in by Sam Hocevar, 14 years ago

Add pipi_release_pixels and pipi_set_colorspace in prevision of
pipi_get_pixels using the tiles system. These functions should
disappear one day, because they're going to leak like crazy.

File size: 3.0 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
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_C].pixels = imlib_image_get_data();
51    img->p[PIPI_PIXELS_RGBA_C].w = img->w;
52    img->p[PIPI_PIXELS_RGBA_C].h = img->h;
53    img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * img->w;
54    img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
55    img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
56    img->last_modified = PIPI_PIXELS_RGBA_C;
57
58    img->codec_priv = (void *)priv;
59    img->codec_format = PIPI_PIXELS_RGBA_C;
60    img->codec_free = pipi_free_imlib2;
61
62    img->wrap = 0;
63    img->u8 = 1;
64
65    return img;
66}
67
68int 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        data = imlib_image_get_data();
77
78        /* FIXME: check pitch differences here */
79        if(img->last_modified == PIPI_PIXELS_RGBA_C)
80        {
81            memcpy(data, img->p[PIPI_PIXELS_RGBA_C].pixels,
82                   4 * img->w * img->h);
83            free(img->p[PIPI_PIXELS_RGBA_C].pixels);
84        }
85
86        img->p[PIPI_PIXELS_RGBA_C].pixels = data;
87        img->p[PIPI_PIXELS_RGBA_C].w = imlib_image_get_width();
88        img->p[PIPI_PIXELS_RGBA_C].h = imlib_image_get_height();
89        img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * imlib_image_get_width();
90        img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
91        img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
92
93        img->codec_priv = (void *)priv;
94        img->codec_format = PIPI_PIXELS_RGBA_C;
95        img->codec_free = pipi_free_imlib2;
96
97        img->wrap = 0;
98        img->u8 = 1;
99    }
100
101    pipi_set_colorspace(img, img->codec_format);
102    imlib_context_set_image(img->codec_priv);
103    imlib_save_image(name);
104
105    return 0;
106}
107
108/*
109 * XXX: The following functions are local.
110 */
111
112static int pipi_free_imlib2(pipi_image_t *img)
113{
114    imlib_context_set_image(img->codec_priv);
115    imlib_free_image();
116
117    return 0;
118}
119
Note: See TracBrowser for help on using the repository browser.