Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <Imlib2.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. static int pipi_free_imlib2(pipi_image_t *);
  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. if(!imlib_image_get_data())
  33. {
  34. imlib_free_image();
  35. return NULL;
  36. }
  37. img = pipi_new(imlib_image_get_width(), imlib_image_get_height());
  38. img->p[PIPI_PIXELS_RGBA_U8].pixels = imlib_image_get_data();
  39. img->p[PIPI_PIXELS_RGBA_U8].w = img->w;
  40. img->p[PIPI_PIXELS_RGBA_U8].h = img->h;
  41. img->p[PIPI_PIXELS_RGBA_U8].pitch = 4 * img->w;
  42. img->p[PIPI_PIXELS_RGBA_U8].bpp = 32;
  43. img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h;
  44. img->last_modified = PIPI_PIXELS_RGBA_U8;
  45. img->codec_priv = (void *)priv;
  46. img->codec_format = PIPI_PIXELS_RGBA_U8;
  47. img->codec_free = pipi_free_imlib2;
  48. img->wrap = 0;
  49. img->u8 = 1;
  50. return img;
  51. }
  52. int pipi_save_imlib2(pipi_image_t *img, const char *name)
  53. {
  54. if(!img->codec_priv)
  55. {
  56. Imlib_Image priv = imlib_create_image(img->w, img->h);
  57. void *data;
  58. imlib_context_set_image(priv);
  59. imlib_image_set_has_alpha(1);
  60. data = imlib_image_get_data();
  61. /* FIXME: check pitch differences here */
  62. if(img->last_modified == PIPI_PIXELS_RGBA_U8)
  63. {
  64. memcpy(data, img->p[PIPI_PIXELS_RGBA_U8].pixels,
  65. 4 * img->w * img->h);
  66. free(img->p[PIPI_PIXELS_RGBA_U8].pixels);
  67. }
  68. img->p[PIPI_PIXELS_RGBA_U8].pixels = data;
  69. img->p[PIPI_PIXELS_RGBA_U8].w = imlib_image_get_width();
  70. img->p[PIPI_PIXELS_RGBA_U8].h = imlib_image_get_height();
  71. img->p[PIPI_PIXELS_RGBA_U8].pitch = 4 * imlib_image_get_width();
  72. img->p[PIPI_PIXELS_RGBA_U8].bpp = 32;
  73. img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h;
  74. img->codec_priv = (void *)priv;
  75. img->codec_format = PIPI_PIXELS_RGBA_U8;
  76. img->codec_free = pipi_free_imlib2;
  77. img->wrap = 0;
  78. img->u8 = 1;
  79. }
  80. pipi_set_colorspace(img, img->codec_format);
  81. imlib_context_set_image(img->codec_priv);
  82. imlib_save_image(name);
  83. return 0;
  84. }
  85. /*
  86. * XXX: The following functions are local.
  87. */
  88. static int pipi_free_imlib2(pipi_image_t *img)
  89. {
  90. imlib_context_set_image(img->codec_priv);
  91. imlib_free_image();
  92. return 0;
  93. }