Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. #include <config.h>
  2. #include <setjmp.h>
  3. #include "gdk_imlib.h"
  4. #define id _gdk_imlib_data
  5. #include "gdk_imlib_private.h"
  6.  
  7. unsigned char *
  8. loader_xpm(FILE *file, int *w, int *h, int *t)
  9. {
  10. unsigned char *data, *ptr, *end;
  11. int pc, c, i, j, k, ncolors, cpp, comment, transp, quote,
  12. context, len, done;
  13. char line[65536], s[65536], tok[65536], col[65536];
  14. XColor xcol;
  15. struct _cmap
  16. {
  17. unsigned char str[8];
  18. unsigned char transp;
  19. int r, g, b;
  20. }
  21. *cmap;
  22. int lookup[256][256];
  23.  
  24. transp = 0;
  25. done = 0;
  26.  
  27. if (!file)
  28. return NULL;
  29.  
  30. *w = 10;
  31. *h = 10;
  32.  
  33. ptr = NULL;
  34. end = NULL;
  35. data = NULL;
  36. c = ' ';
  37. comment = 0;
  38. quote = 0;
  39. context = 0;
  40. i = j = 0;
  41. cmap = NULL;
  42.  
  43. while (!done)
  44. {
  45. pc = c;
  46. c = fgetc(file);
  47. if (c == EOF)
  48. break;
  49. if (!quote)
  50. {
  51. if (pc == '/' && c == '*')
  52. comment = 1;
  53. else if (pc == '*' && c == '/' && comment)
  54. comment = 0;
  55. }
  56. if (!comment)
  57. {
  58. if (!quote && c == '"')
  59. {
  60. quote = 1;
  61. i = 0;
  62. }
  63. else if (quote && c == '"')
  64. {
  65. line[i] = 0;
  66. quote = 0;
  67. if (context == 0)
  68. {
  69. /* Header */
  70. sscanf(line, "%i %i %i %i", w, h, &ncolors, &cpp);
  71. if (cpp > 7)
  72. {
  73. fprintf(stderr, "gdk_imlib ERROR: XPM files with characters per pixel > 7 not supported\n");
  74. return NULL;
  75. }
  76. if (*w > 32767)
  77. {
  78. fprintf(stderr, "gdk_imlib ERROR: Image width > 32767 pixels for file\n");
  79. return NULL;
  80. }
  81. if (*h > 32767)
  82. {
  83. fprintf(stderr, "gdk_imlib ERROR: Image height > 32767 pixels for file\n");
  84. return NULL;
  85. }
  86. cmap = malloc(sizeof(struct _cmap) * ncolors);
  87.  
  88. if (!cmap)
  89. return NULL;
  90. data = malloc(*w ** h * 3);
  91. if (!data)
  92. {
  93. free(cmap);
  94. return NULL;
  95. }
  96. ptr = data;
  97. end = ptr + (*w ** h * 3);
  98. j = 0;
  99. context++;
  100. }
  101. else if (context == 1)
  102. {
  103. /* Color Table */
  104. if (j < ncolors)
  105. {
  106. int slen;
  107. int hascolor, iscolor;
  108.  
  109. hascolor = 0;
  110. iscolor = 0;
  111. tok[0] = 0;
  112. col[0] = 0;
  113. s[0] = 0;
  114. len = strlen(line);
  115. strncpy(cmap[j].str, line, cpp);
  116. cmap[j].str[cpp] = 0;
  117. cmap[j].r = -1;
  118. cmap[j].transp = 0;
  119. for (k = cpp; k < len; k++)
  120. {
  121. if (line[k] != ' ')
  122. {
  123. s[0] = 0;
  124. sscanf(&line[k], "%65535s", s);
  125. slen = strlen(s);
  126. k += slen;
  127. if (!strcmp(s, "c"))
  128. iscolor = 1;
  129. if ((!strcmp(s, "m")) || (!strcmp(s, "s")) ||
  130. (!strcmp(s, "g4")) || (!strcmp(s, "g")) ||
  131. (!strcmp(s, "c")) || (k >= len))
  132. {
  133. if (k >= len)
  134. {
  135. if (col[0])
  136. strcat(col, " ");
  137. strcat(col, s);
  138. }
  139. if (col[0])
  140. {
  141. if (!strcasecmp(col, "none"))
  142. {
  143. transp = 1;
  144. cmap[j].transp = 1;
  145. }
  146. else
  147. {
  148. if (((cmap[j].r < 0) ||
  149. (!strcmp(tok, "c"))) &&
  150. (!hascolor))
  151. {
  152. XParseColor(id->x.disp,
  153. id->x.root_cmap,
  154. col, &xcol);
  155. cmap[j].r = xcol.red >> 8;
  156. cmap[j].g = xcol.green >> 8;
  157. cmap[j].b = xcol.blue >> 8;
  158. if ((cmap[j].r == 255) &&
  159. (cmap[j].g == 0) &&
  160. (cmap[j].b == 255))
  161. cmap[j].r = 254;
  162. if (iscolor)
  163. hascolor = 1;
  164. }
  165. }
  166. }
  167. strcpy(tok, s);
  168. col[0] = 0;
  169. }
  170. else
  171. {
  172. if (col[0])
  173. strcat(col, " ");
  174. strcat(col, s);
  175. }
  176. }
  177. }
  178. }
  179. j++;
  180. if (j >= ncolors)
  181. {
  182. if (cpp == 1)
  183. for (i = 0; i < ncolors; i++)
  184. lookup[(int)cmap[i].str[0]][(int)cmap[i].str[1]] = i;
  185. if (cpp == 2)
  186. for (i = 0; i < ncolors; i++)
  187. lookup[(int)cmap[i].str[0]][(int)cmap[i].str[1]] = i;
  188. context++;
  189. }
  190. }
  191. else
  192. {
  193. /* Image Data */
  194. i = 0;
  195. if (cpp == 0)
  196. {
  197. /* Chars per pixel = 0? well u never know */
  198. }
  199. if (cpp == 1)
  200. {
  201. if (transp)
  202. {
  203. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  204. {
  205. col[0] = line[i];
  206. if (cmap[lookup[(int)col[0]][0]].transp)
  207. {
  208. *ptr++ = 255;
  209. *ptr++ = 0;
  210. *ptr++ = 255;
  211. }
  212. else
  213. {
  214. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].r;
  215. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].g;
  216. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].b;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  223. {
  224. col[0] = line[i];
  225. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].r;
  226. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].g;
  227. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][0]].b;
  228. }
  229. }
  230. }
  231. else if (cpp == 2)
  232. {
  233. if (transp)
  234. {
  235. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  236. {
  237. col[0] = line[i++];
  238. col[1] = line[i];
  239. if (cmap[lookup[(int)col[0]][(int)col[1]]].transp)
  240. {
  241. *ptr++ = 255;
  242. *ptr++ = 0;
  243. *ptr++ = 255;
  244. }
  245. else
  246. {
  247. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].r;
  248. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].g;
  249. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].b;
  250. }
  251. }
  252. }
  253. else
  254. {
  255. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  256. {
  257. col[0] = line[i++];
  258. col[1] = line[i];
  259. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].r;
  260. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].g;
  261. *ptr++ = (unsigned char)cmap[lookup[(int)col[0]][(int)col[1]]].b;
  262. }
  263. }
  264. }
  265. else
  266. {
  267. if (transp)
  268. {
  269. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  270. {
  271. for (j = 0; j < cpp; j++, i++)
  272. {
  273. col[j] = line[i];
  274. }
  275. col[j] = 0;
  276. i--;
  277. for (j = 0; j < ncolors; j++)
  278. {
  279. if (!strcmp(col, cmap[j].str))
  280. {
  281. if (cmap[j].transp)
  282. {
  283. *ptr++ = 255;
  284. *ptr++ = 0;
  285. *ptr++ = 255;
  286. }
  287. else
  288. {
  289. *ptr++ = (unsigned char)cmap[j].r;
  290. *ptr++ = (unsigned char)cmap[j].g;
  291. *ptr++ = (unsigned char)cmap[j].b;
  292. }
  293. j = ncolors;
  294. }
  295. }
  296. }
  297. }
  298. else
  299. {
  300. for (i = 0; ((i < 65536) && (ptr < end) && (line[i])); i++)
  301. {
  302. for (j = 0; j < cpp; j++, i++)
  303. {
  304. col[j] = line[i];
  305. }
  306. col[j] = 0;
  307. i--;
  308. for (j = 0; j < ncolors; j++)
  309. {
  310. if (!strcmp(col, cmap[j].str))
  311. {
  312. *ptr++ = (unsigned char)cmap[j].r;
  313. *ptr++ = (unsigned char)cmap[j].g;
  314. *ptr++ = (unsigned char)cmap[j].b;
  315. j = ncolors;
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. /* Scan in line from XPM file (limit line length 65k) */
  325. if (i < 65536)
  326. {
  327. if ((!comment) && (quote) && (c != '"'))
  328. {
  329. line[i++] = c;
  330. }
  331. }
  332. if ((ptr) && ((ptr - data) >= *w ** h * 3))
  333. done = 1;
  334. }
  335. if (transp)
  336. *t = 1;
  337. else
  338. *t = 0;
  339. free(cmap);
  340. return data;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement