gdxpm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. add ability to load xpm files to gd, requires the xpm
  3. library.
  4. Caolan.McNamara@ul.ie
  5. http://www.csn.ul.ie/~caolan
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "gd.h"
  11. #include "gdhelpers.h"
  12. #ifdef HAVE_XPM
  13. #include <X11/xpm.h>
  14. gdImagePtr gdImageCreateFromXpm (char *filename)
  15. {
  16. XpmInfo info = {0};
  17. XpmImage image;
  18. int i, j, k, number;
  19. char buf[5];
  20. gdImagePtr im = 0;
  21. int *pointer;
  22. int red = 0, green = 0, blue = 0;
  23. int *colors;
  24. int ret;
  25. ret = XpmReadFileToXpmImage(filename, &image, &info);
  26. if (ret != XpmSuccess) {
  27. return 0;
  28. }
  29. number = image.ncolors;
  30. for(i = 0; i < number; i++) {
  31. if (!image.colorTable[i].c_color) {
  32. goto done;
  33. }
  34. }
  35. if (!(im = gdImageCreate(image.width, image.height))) {
  36. goto done;
  37. }
  38. colors = (int *) safe_emalloc(number, sizeof(int), 0);
  39. for (i = 0; i < number; i++) {
  40. switch (strlen (image.colorTable[i].c_color)) {
  41. case 4:
  42. buf[1] = '\0';
  43. buf[0] = image.colorTable[i].c_color[1];
  44. red = strtol(buf, NULL, 16);
  45. buf[0] = image.colorTable[i].c_color[2];
  46. green = strtol(buf, NULL, 16);
  47. buf[0] = image.colorTable[i].c_color[3];
  48. blue = strtol(buf, NULL, 16);
  49. break;
  50. case 7:
  51. buf[2] = '\0';
  52. buf[0] = image.colorTable[i].c_color[1];
  53. buf[1] = image.colorTable[i].c_color[2];
  54. red = strtol(buf, NULL, 16);
  55. buf[0] = image.colorTable[i].c_color[3];
  56. buf[1] = image.colorTable[i].c_color[4];
  57. green = strtol(buf, NULL, 16);
  58. buf[0] = image.colorTable[i].c_color[5];
  59. buf[1] = image.colorTable[i].c_color[6];
  60. blue = strtol(buf, NULL, 16);
  61. break;
  62. case 10:
  63. buf[3] = '\0';
  64. buf[0] = image.colorTable[i].c_color[1];
  65. buf[1] = image.colorTable[i].c_color[2];
  66. buf[2] = image.colorTable[i].c_color[3];
  67. red = strtol(buf, NULL, 16);
  68. red /= 64;
  69. buf[0] = image.colorTable[i].c_color[4];
  70. buf[1] = image.colorTable[i].c_color[5];
  71. buf[2] = image.colorTable[i].c_color[6];
  72. green = strtol(buf, NULL, 16);
  73. green /= 64;
  74. buf[0] = image.colorTable[i].c_color[7];
  75. buf[1] = image.colorTable[i].c_color[8];
  76. buf[2] = image.colorTable[i].c_color[9];
  77. blue = strtol(buf, NULL, 16);
  78. blue /= 64;
  79. break;
  80. case 13:
  81. buf[4] = '\0';
  82. buf[0] = image.colorTable[i].c_color[1];
  83. buf[1] = image.colorTable[i].c_color[2];
  84. buf[2] = image.colorTable[i].c_color[3];
  85. buf[3] = image.colorTable[i].c_color[4];
  86. red = strtol(buf, NULL, 16);
  87. red /= 256;
  88. buf[0] = image.colorTable[i].c_color[5];
  89. buf[1] = image.colorTable[i].c_color[6];
  90. buf[2] = image.colorTable[i].c_color[7];
  91. buf[3] = image.colorTable[i].c_color[8];
  92. green = strtol(buf, NULL, 16);
  93. green /= 256;
  94. buf[0] = image.colorTable[i].c_color[9];
  95. buf[1] = image.colorTable[i].c_color[10];
  96. buf[2] = image.colorTable[i].c_color[11];
  97. buf[3] = image.colorTable[i].c_color[12];
  98. blue = strtol(buf, NULL, 16);
  99. blue /= 256;
  100. break;
  101. }
  102. colors[i] = gdImageColorResolve(im, red, green, blue);
  103. }
  104. pointer = (int *) image.data;
  105. for (i = 0; i < image.height; i++) {
  106. for (j = 0; j < image.width; j++) {
  107. k = *pointer++;
  108. gdImageSetPixel(im, j, i, colors[k]);
  109. }
  110. }
  111. gdFree(colors);
  112. done:
  113. XpmFreeXpmImage(&image);
  114. XpmFreeXpmInfo(&info);
  115. return im;
  116. }
  117. #endif