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

Last change on this file since 2756 was 2756, checked in by Sam Hocevar, 13 years ago
  • Add a new image flag, u8, which indicates whether the image samples come from an 8-bpp source.
File size: 2.7 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->p[PIPI_PIXELS_RGBA32].bpp = 32;
47    img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
48    img->last_modified = PIPI_PIXELS_RGBA32;
49
50    img->codec_priv = (void *)priv;
51    img->codec_format = PIPI_PIXELS_RGBA32;
52
53    img->wrap = 0;
54    img->u8 = 1;
55
56    return img;
57}
58
59void pipi_free_imlib2(pipi_image_t *img)
60{
61    imlib_context_set_image(img->codec_priv);
62    imlib_free_image();
63}
64
65void pipi_save_imlib2(pipi_image_t *img, const char *name)
66{
67    if(!img->codec_priv)
68    {
69        Imlib_Image priv = imlib_create_image(img->w, img->h);
70        void *data;
71
72        imlib_context_set_image(priv);
73        data = imlib_image_get_data();
74
75        /* FIXME: check pitch differences here */
76        if(img->last_modified == PIPI_PIXELS_RGBA32)
77        {
78            memcpy(data, img->p[PIPI_PIXELS_RGBA32].pixels,
79                   4 * img->w * img->h);
80            free(img->p[PIPI_PIXELS_RGBA32].pixels);
81        }
82
83        img->p[PIPI_PIXELS_RGBA32].pixels = data;
84        img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
85        img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
86        img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
87        img->p[PIPI_PIXELS_RGBA32].bpp = 32;
88        img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
89
90        img->codec_priv = (void *)priv;
91        img->codec_format = PIPI_PIXELS_RGBA32;
92
93        img->wrap = 0;
94        img->u8 = 1;
95    }
96
97    pipi_getpixels(img, img->codec_format);
98    imlib_context_set_image(img->codec_priv);
99    imlib_save_image(name);
100}
101
Note: See TracBrowser for help on using the repository browser.