| 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 | |
|---|
| 31 | pipi_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 | return img; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void pipi_free_imlib2(pipi_image_t *img) |
|---|
| 57 | { |
|---|
| 58 | imlib_context_set_image(img->codec_priv); |
|---|
| 59 | imlib_free_image(); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void 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_RGBA32) |
|---|
| 74 | { |
|---|
| 75 | memcpy(data, img->p[PIPI_PIXELS_RGBA32].pixels, |
|---|
| 76 | 4 * img->w * img->h); |
|---|
| 77 | free(img->p[PIPI_PIXELS_RGBA32].pixels); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | img->p[PIPI_PIXELS_RGBA32].pixels = data; |
|---|
| 81 | img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width(); |
|---|
| 82 | img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height(); |
|---|
| 83 | img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width(); |
|---|
| 84 | img->p[PIPI_PIXELS_RGBA32].bpp = 32; |
|---|
| 85 | img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h; |
|---|
| 86 | |
|---|
| 87 | img->codec_priv = (void *)priv; |
|---|
| 88 | img->codec_format = PIPI_PIXELS_RGBA32; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | pipi_getpixels(img, img->codec_format); |
|---|
| 92 | imlib_context_set_image(img->codec_priv); |
|---|
| 93 | imlib_save_image(name); |
|---|
| 94 | } |
|---|
| 95 | |
|---|