pixbuf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* $Id: pixbuf.c,v 1.6 2001/04/29 22:07:44 kilobug Exp $ */
  2. #include "client.h"
  3. #include <gdk-pixbuf/gdk-pixbuf.h>
  4. image_t *load_picture(const char *filename, const char *mask)
  5. {
  6. image_t *res;
  7. guchar *data = NULL, *mdata = NULL;
  8. GdkPixbuf *pixbuf, *pbmask = NULL;
  9. int x, y, sx2, bpp;
  10. char *s;
  11. if (getenv("PROLO_DATA_DIR") == NULL)
  12. {
  13. fprintf(stderr, "Variable PROLO_DATA_DIR invalide.\n");
  14. exit(1);
  15. }
  16. res = g_malloc(sizeof(*res));
  17. res->sx = res->sy = 0;
  18. res->data = NULL;
  19. if (!g_path_is_absolute(filename))
  20. res->name = g_strconcat(getenv("PROLO_DATA_DIR"), G_DIR_SEPARATOR_S,
  21. filename, NULL);
  22. else
  23. res->name = g_strdup(filename);
  24. pixbuf = gdk_pixbuf_new_from_file(res->name);
  25. if (pixbuf == NULL)
  26. {
  27. fprintf(stderr, "Erreur lors du chargement de l'image: %s\n", res->name);
  28. g_free(res->name);
  29. g_free(res);
  30. return NULL;
  31. }
  32. if (mask != NULL)
  33. {
  34. if (!g_path_is_absolute(mask))
  35. s = g_strconcat(getenv("PROLO_DATA_DIR"), G_DIR_SEPARATOR_S, mask,
  36. NULL);
  37. else
  38. s = g_strdup(mask);
  39. pbmask = gdk_pixbuf_new_from_file(s);
  40. mdata = gdk_pixbuf_get_pixels(pbmask);
  41. g_free(s);
  42. }
  43. res->sx = gdk_pixbuf_get_width(pixbuf);
  44. res->sy = gdk_pixbuf_get_height(pixbuf);
  45. res->data = g_malloc(res->sx * res->sy * 4);
  46. data = gdk_pixbuf_get_pixels(pixbuf);
  47. sx2 = gdk_pixbuf_get_rowstride(pixbuf);
  48. bpp = gdk_pixbuf_get_has_alpha(pixbuf) ? 4 : 3;
  49. for (x = 0; x < res->sx; x++)
  50. for (y = 0; y < res->sy; y++)
  51. {
  52. memcpy(res->data + (x + y * res->sx) * 4, data + x * bpp + y * sx2, 3);
  53. if (mdata != NULL)
  54. res->data[(x + y * res->sx) * 4 + 3] = mdata[x * bpp + y * sx2];
  55. else
  56. res->data[(x + y * res->sx) * 4 + 3] = 0;
  57. }
  58. gdk_pixbuf_unref(pixbuf);
  59. if (pbmask != NULL)
  60. gdk_pixbuf_unref(pbmask);
  61. return res;
  62. }
  63. void destroy_picture(image_t *pic)
  64. {
  65. if (pic == NULL)
  66. return;
  67. g_free(pic->data);
  68. g_free(pic->name);
  69. g_free(pic);
  70. }
  71. image_t *dup_picture(const image_t *src)
  72. {
  73. image_t *res;
  74. int size;
  75. res = g_malloc(sizeof(*res));
  76. res->sx = src->sx;
  77. res->sy = src->sy;
  78. res->name = g_strdup(src->name);
  79. size = src->sx * src->sy * 4;
  80. res->data = g_malloc(size);
  81. memcpy(res->data, src->data, size);
  82. return res;
  83. }
  84. image_t *tint_picture(const image_t *src, guchar *col)
  85. {
  86. image_t *res;
  87. int temp;
  88. int ofs, size;
  89. res = g_malloc(sizeof(*res));
  90. res->sx = src->sx;
  91. res->sy = src->sy;
  92. res->name = g_strdup(src->name);
  93. size = src->sx * src->sy * 4;
  94. res->data = g_malloc(size);
  95. for (ofs = 0; ofs < size; ofs += 4)
  96. {
  97. if (src->data[ofs + 3] == 0)
  98. {
  99. temp = col[2] / SPRITE_COLOR_DIV + (int)src->data[ofs];
  100. if (temp > 0xFF)
  101. temp = 0xFF;
  102. res->data[ofs] = (guchar) temp;
  103. temp = col[1] / SPRITE_COLOR_DIV + (int)src->data[ofs + 1];
  104. if (temp > 0xFF)
  105. temp = 0xFF;
  106. res->data[ofs + 1] = (guchar) temp;
  107. temp = col[0] / SPRITE_COLOR_DIV + (int)src->data[ofs + 2];
  108. if (temp > 0xFF)
  109. temp = 0xFF;
  110. res->data[ofs + 2] = (guchar) temp;
  111. }
  112. res->data[ofs + 3] = src->data[ofs + 3];
  113. }
  114. return res;
  115. }