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.
 
 
 
 
 
 

114 lines
2.9 KiB

  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. * 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. static int pipi_free_imlib2(pipi_image_t *);
  26. pipi_image_t *pipi_load_imlib2(const char *name)
  27. {
  28. pipi_image_t *img;
  29. Imlib_Image priv = imlib_load_image(name);
  30. if(!priv)
  31. return NULL;
  32. imlib_context_set_image(priv);
  33. img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
  34. img->p[PIPI_PIXELS_RGBA_C].pixels = imlib_image_get_data();
  35. img->p[PIPI_PIXELS_RGBA_C].w = img->w;
  36. img->p[PIPI_PIXELS_RGBA_C].h = img->h;
  37. img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * img->w;
  38. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  39. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  40. img->last_modified = PIPI_PIXELS_RGBA_C;
  41. img->codec_priv = (void *)priv;
  42. img->codec_format = PIPI_PIXELS_RGBA_C;
  43. img->codec_free = pipi_free_imlib2;
  44. img->wrap = 0;
  45. img->u8 = 1;
  46. return img;
  47. }
  48. int pipi_save_imlib2(pipi_image_t *img, const char *name)
  49. {
  50. if(!img->codec_priv)
  51. {
  52. Imlib_Image priv = imlib_create_image(img->w, img->h);
  53. void *data;
  54. imlib_context_set_image(priv);
  55. data = imlib_image_get_data();
  56. /* FIXME: check pitch differences here */
  57. if(img->last_modified == PIPI_PIXELS_RGBA_C)
  58. {
  59. memcpy(data, img->p[PIPI_PIXELS_RGBA_C].pixels,
  60. 4 * img->w * img->h);
  61. free(img->p[PIPI_PIXELS_RGBA_C].pixels);
  62. }
  63. img->p[PIPI_PIXELS_RGBA_C].pixels = data;
  64. img->p[PIPI_PIXELS_RGBA_C].w = imlib_image_get_width();
  65. img->p[PIPI_PIXELS_RGBA_C].h = imlib_image_get_height();
  66. img->p[PIPI_PIXELS_RGBA_C].pitch = 4 * imlib_image_get_width();
  67. img->p[PIPI_PIXELS_RGBA_C].bpp = 32;
  68. img->p[PIPI_PIXELS_RGBA_C].bytes = 4 * img->w * img->h;
  69. img->codec_priv = (void *)priv;
  70. img->codec_format = PIPI_PIXELS_RGBA_C;
  71. img->codec_free = pipi_free_imlib2;
  72. img->wrap = 0;
  73. img->u8 = 1;
  74. }
  75. pipi_getpixels(img, img->codec_format);
  76. imlib_context_set_image(img->codec_priv);
  77. imlib_save_image(name);
  78. return 0;
  79. }
  80. /*
  81. * XXX: The following functions are local.
  82. */
  83. static int pipi_free_imlib2(pipi_image_t *img)
  84. {
  85. imlib_context_set_image(img->codec_priv);
  86. imlib_free_image();
  87. return 0;
  88. }