You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

96 regels
2.6 KiB

  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. * imlib.c: ImLib I/O functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <Imlib2.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. pipi_image_t *pipi_load_imlib2(const char *name)
  26. {
  27. pipi_image_t *img;
  28. Imlib_Image priv = imlib_load_image(name);
  29. if(!priv)
  30. return NULL;
  31. imlib_context_set_image(priv);
  32. img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
  33. img->p[PIPI_PIXELS_RGBA32].pixels = imlib_image_get_data();
  34. img->p[PIPI_PIXELS_RGBA32].w = img->w;
  35. img->p[PIPI_PIXELS_RGBA32].h = img->h;
  36. img->p[PIPI_PIXELS_RGBA32].pitch = 4 * img->w;
  37. img->p[PIPI_PIXELS_RGBA32].bpp = 32;
  38. img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
  39. img->last_modified = PIPI_PIXELS_RGBA32;
  40. img->codec_priv = (void *)priv;
  41. img->codec_format = PIPI_PIXELS_RGBA32;
  42. return img;
  43. }
  44. void pipi_free_imlib2(pipi_image_t *img)
  45. {
  46. imlib_context_set_image(img->codec_priv);
  47. imlib_free_image();
  48. }
  49. void pipi_save_imlib2(pipi_image_t *img, const char *name)
  50. {
  51. if(!img->codec_priv)
  52. {
  53. Imlib_Image priv = imlib_create_image(img->w, img->h);
  54. void *data;
  55. imlib_context_set_image(priv);
  56. data = imlib_image_get_data();
  57. /* FIXME: check pitch differences here */
  58. if(img->last_modified == PIPI_PIXELS_RGBA32)
  59. {
  60. memcpy(data, img->p[PIPI_PIXELS_RGBA32].pixels,
  61. 4 * img->w * img->h);
  62. free(img->p[PIPI_PIXELS_RGBA32].pixels);
  63. }
  64. img->p[PIPI_PIXELS_RGBA32].pixels = data;
  65. img->p[PIPI_PIXELS_RGBA32].w = imlib_image_get_width();
  66. img->p[PIPI_PIXELS_RGBA32].h = imlib_image_get_height();
  67. img->p[PIPI_PIXELS_RGBA32].pitch = 4 * imlib_image_get_width();
  68. img->p[PIPI_PIXELS_RGBA32].bpp = 32;
  69. img->p[PIPI_PIXELS_RGBA32].bytes = 4 * img->w * img->h;
  70. img->codec_priv = (void *)priv;
  71. img->codec_format = PIPI_PIXELS_RGBA32;
  72. }
  73. pipi_getpixels(img, img->codec_format);
  74. imlib_context_set_image(img->codec_priv);
  75. imlib_save_image(name);
  76. }