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

Last change on this file since 2629 was 2629, checked in by Sam Hocevar, 14 years ago
  • pipi.c: reimplement pipi_new() without relying on the underlying codec library, so we can have several or none of them at some time.
  • sdl.c imlib.c: allow to save images that weren't created using the current codec.
File size: 2.4 KB
Line 
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 * 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
31pipi_image_t *pipi_load_imlib2(const char *name)
32{
33    pipi_image_t *img;
34    Imlib_Image priv = imlib_load_image(name);
35
36    if(!priv)
37        return NULL;
38
39    imlib_context_set_image(priv);
40    img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
41
42    img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data();
43    img->p[PIPI_PIXELS_RGBA32].w = img->w;
44    img->p[PIPI_PIXELS_RGBA32].h = img->h;
45    img->p[PIPI_PIXELS_RGBA32].pitch = 4 * img->w;
46    img->last_modified = PIPI_PIXELS_RGBA32;
47
48    img->codec_priv = (void *)priv;
49    img->codec_format = PIPI_PIXELS_RGBA32;
50
51    return img;
52}
53
54void pipi_free_imlib2(pipi_image_t *img)
55{
56    imlib_context_set_image(img->codec_priv);
57    imlib_free_image();
58}
59
60void pipi_save_imlib2(pipi_image_t *img, const char *name)
61{
62    if(!img->codec_priv)
63    {
64        Imlib_Image priv = imlib_create_image(img->w, img->h);
65        void *data;
66
67        imlib_context_set_image(priv);
68        data = imlib_image_get_data();
69
70        /* FIXME: check pitch differences here */
71        if(img->last_modified == PIPI_PIXELS_RGBA32)
72        {
73            memcpy(data, img->p[PIPI_PIXELS_RGBA32].pixels,
74                   4 * img->w * img->h);
75            free(img->p[PIPI_PIXELS_RGBA32].pixels);
76        }
77
78        img->p[PIPI_PIXELS_RGBA32].pixels = data;
79        img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
80        img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
81        img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
82
83        img->codec_priv = (void *)priv;
84        img->codec_format = PIPI_PIXELS_RGBA32;
85    }
86
87    pipi_getpixels(img, img->codec_format);
88    imlib_context_set_image(img->codec_priv);
89    imlib_save_image(name);
90}
91
Note: See TracBrowser for help on using the repository browser.