Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

imlib.c 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }