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.

137 lines
3.2KB

  1. //
  2. // "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Fl_XPM_Image routines.
  5. //
  6. // Copyright 1997-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // Contents:
  28. //
  29. //
  30. //
  31. // Include necessary header files...
  32. //
  33. #include <FL/Fl.H>
  34. #include <FL/Fl_XPM_Image.H>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <FL/fl_utf8.h>
  38. #include "flstring.h"
  39. //
  40. // 'hexdigit()' - Convert a hex digit to an integer.
  41. //
  42. static int hexdigit(int x) { // I - Hex digit...
  43. if (isdigit(x)) return x-'0';
  44. if (isupper(x)) return x-'A'+10;
  45. if (islower(x)) return x-'a'+10;
  46. return 20;
  47. }
  48. #define MAXSIZE 2048
  49. #define INITIALLINES 256
  50. /**
  51. The constructor loads the XPM image from the name filename.
  52. <P>The destructor free all memory and server resources that are used by
  53. the image.
  54. */
  55. Fl_XPM_Image::Fl_XPM_Image(const char *name) : Fl_Pixmap((char *const*)0) {
  56. FILE *f;
  57. if ((f = fl_fopen(name, "rb")) == NULL) return;
  58. // read all the c-strings out of the file:
  59. char** new_data = new char *[INITIALLINES];
  60. char** temp_data;
  61. int malloc_size = INITIALLINES;
  62. char buffer[MAXSIZE+20];
  63. int i = 0;
  64. while (fgets(buffer,MAXSIZE+20,f)) {
  65. if (buffer[0] != '\"') continue;
  66. char *myp = buffer;
  67. char *q = buffer+1;
  68. while (*q != '\"' && myp < buffer+MAXSIZE) {
  69. if (*q == '\\') switch (*++q) {
  70. case '\r':
  71. case '\n':
  72. if (!fgets(q,(buffer+MAXSIZE+20)-q,f)) { /* no problem if we hit EOF */ } break;
  73. case 0:
  74. break;
  75. case 'x': {
  76. q++;
  77. int n = 0;
  78. for (int x = 0; x < 3; x++) {
  79. int xd = hexdigit(*q);
  80. if (xd > 15) break;
  81. n = (n<<4)+xd;
  82. q++;
  83. }
  84. *myp++ = n;
  85. } break;
  86. default: {
  87. int c = *q++;
  88. if (c>='0' && c<='7') {
  89. c -= '0';
  90. for (int x=0; x<2; x++) {
  91. int xd = hexdigit(*q);
  92. if (xd>7) break;
  93. c = (c<<3)+xd;
  94. q++;
  95. }
  96. }
  97. *myp++ = c;
  98. } break;
  99. } else {
  100. *myp++ = *q++;
  101. }
  102. }
  103. *myp++ = 0;
  104. if (i >= malloc_size) {
  105. temp_data = new char *[malloc_size + INITIALLINES];
  106. memcpy(temp_data, new_data, sizeof(char *) * malloc_size);
  107. delete[] new_data;
  108. new_data = temp_data;
  109. malloc_size += INITIALLINES;
  110. }
  111. new_data[i] = new char[myp-buffer+1];
  112. memcpy(new_data[i], buffer,myp-buffer);
  113. new_data[i][myp-buffer] = 0;
  114. i++;
  115. }
  116. fclose(f);
  117. data((const char **)new_data, i);
  118. alloc_data = 1;
  119. measure();
  120. }
  121. //
  122. // End of "$Id: Fl_XPM_Image.cxx 7903 2010-11-28 21:06:39Z matt $".
  123. //