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

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

Fix headers.

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