PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Attrib.c

#
C | 308 lines | 216 code | 25 blank | 67 comment | 45 complexity | 35fd2d29d0cc977d74b287315eb530a6 MD5 | raw file
  1. /*
  2. * Copyright (C) 1989-95 GROUPE BULL
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * Except as contained in this notice, the name of GROUPE BULL shall not be
  22. * used in advertising or otherwise to promote the sale, use or other dealings
  23. * in this Software without prior written authorization from GROUPE BULL.
  24. */
  25. /*****************************************************************************\
  26. * Attrib.c: *
  27. * *
  28. * XPM library *
  29. * Functions related to the XpmAttributes structure *
  30. * *
  31. * Developed by Arnaud Le Hors *
  32. \*****************************************************************************/
  33. /* October 2004, source code review by Thomas Biege <thomas@suse.de> */
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37. #include "XpmI.h"
  38. /* 3.2 backward compatibility code */
  39. LFUNC(CreateOldColorTable, int, (XpmColor *ct, unsigned int ncolors,
  40. XpmColor ***oldct));
  41. LFUNC(FreeOldColorTable, void, (XpmColor **colorTable, unsigned int ncolors));
  42. /*
  43. * Create a colortable compatible with the old style colortable
  44. */
  45. static int
  46. CreateOldColorTable(
  47. XpmColor *ct,
  48. unsigned int ncolors,
  49. XpmColor ***oldct)
  50. {
  51. XpmColor **colorTable, **color;
  52. unsigned int a;
  53. if (ncolors >= UINT_MAX / sizeof(XpmColor *))
  54. return XpmNoMemory;
  55. colorTable = (XpmColor **) XpmMalloc(ncolors * sizeof(XpmColor *));
  56. if (!colorTable) {
  57. *oldct = NULL;
  58. return (XpmNoMemory);
  59. }
  60. for (a = 0, color = colorTable; a < ncolors; a++, color++, ct++)
  61. *color = ct;
  62. *oldct = colorTable;
  63. return (XpmSuccess);
  64. }
  65. static void
  66. FreeOldColorTable(
  67. XpmColor **colorTable,
  68. unsigned int ncolors)
  69. {
  70. unsigned int a, b;
  71. XpmColor **color;
  72. char **sptr;
  73. if (colorTable) {
  74. for (a = 0, color = colorTable; a < ncolors; a++, color++) {
  75. for (b = 0, sptr = (char **) *color; b <= NKEYS; b++, sptr++)
  76. if (*sptr)
  77. XpmFree(*sptr);
  78. }
  79. XpmFree(*colorTable);
  80. XpmFree(colorTable);
  81. }
  82. }
  83. /* end 3.2 bc */
  84. /*
  85. * Free the computed color table
  86. */
  87. void
  88. xpmFreeColorTable(
  89. XpmColor *colorTable,
  90. int ncolors)
  91. {
  92. int a, b;
  93. XpmColor *color;
  94. char **sptr;
  95. if (colorTable) {
  96. for (a = 0, color = colorTable; a < ncolors; a++, color++) {
  97. for (b = 0, sptr = (char **) color; b <= NKEYS; b++, sptr++)
  98. if (*sptr)
  99. XpmFree(*sptr);
  100. }
  101. XpmFree(colorTable);
  102. }
  103. }
  104. /*
  105. * Free array of extensions
  106. */
  107. void
  108. XpmFreeExtensions(
  109. XpmExtension *extensions,
  110. int nextensions)
  111. {
  112. unsigned int i, j, nlines;
  113. XpmExtension *ext;
  114. char **sptr;
  115. if (extensions && nextensions > 0) {
  116. for (i = 0, ext = extensions; i < nextensions; i++, ext++) {
  117. if (ext->name)
  118. XpmFree(ext->name);
  119. nlines = ext->nlines;
  120. for (j = 0, sptr = ext->lines; j < nlines; j++, sptr++)
  121. if (sptr && *sptr)
  122. XpmFree(*sptr);
  123. if (ext->lines)
  124. XpmFree(ext->lines);
  125. }
  126. XpmFree(extensions);
  127. }
  128. }
  129. /*
  130. * Return the XpmAttributes structure size
  131. */
  132. int
  133. XpmAttributesSize(void)
  134. {
  135. return sizeof(XpmAttributes);
  136. }
  137. /*
  138. * Init returned data to free safely later on
  139. */
  140. void
  141. xpmInitAttributes(XpmAttributes *attributes)
  142. {
  143. if (attributes) {
  144. attributes->pixels = NULL;
  145. attributes->npixels = 0;
  146. attributes->colorTable = NULL;
  147. attributes->ncolors = 0;
  148. /* 3.2 backward compatibility code */
  149. attributes->hints_cmt = NULL;
  150. attributes->colors_cmt = NULL;
  151. attributes->pixels_cmt = NULL;
  152. /* end 3.2 bc */
  153. if (attributes->valuemask & XpmReturnExtensions) {
  154. attributes->extensions = NULL;
  155. attributes->nextensions = 0;
  156. }
  157. if (attributes->valuemask & XpmReturnAllocPixels) {
  158. attributes->alloc_pixels = NULL;
  159. attributes->nalloc_pixels = 0;
  160. }
  161. }
  162. }
  163. /*
  164. * Fill in the XpmAttributes with the XpmImage and the XpmInfo
  165. */
  166. void
  167. xpmSetAttributes(
  168. XpmAttributes *attributes,
  169. XpmImage *image,
  170. XpmInfo *info)
  171. {
  172. if (attributes->valuemask & XpmReturnColorTable) {
  173. attributes->colorTable = image->colorTable;
  174. attributes->ncolors = image->ncolors;
  175. /* avoid deletion of copied data */
  176. image->ncolors = 0;
  177. image->colorTable = NULL;
  178. }
  179. /* 3.2 backward compatibility code */
  180. else if (attributes->valuemask & XpmReturnInfos) {
  181. int ErrorStatus;
  182. ErrorStatus = CreateOldColorTable(image->colorTable, image->ncolors,
  183. (XpmColor ***)
  184. &attributes->colorTable);
  185. /* if error just say we can't return requested data */
  186. if (ErrorStatus != XpmSuccess) {
  187. attributes->valuemask &= ~XpmReturnInfos;
  188. if (!(attributes->valuemask & XpmReturnPixels)) {
  189. XpmFree(attributes->pixels);
  190. attributes->pixels = NULL;
  191. attributes->npixels = 0;
  192. }
  193. attributes->ncolors = 0;
  194. } else {
  195. attributes->ncolors = image->ncolors;
  196. attributes->hints_cmt = info->hints_cmt;
  197. attributes->colors_cmt = info->colors_cmt;
  198. attributes->pixels_cmt = info->pixels_cmt;
  199. /* avoid deletion of copied data */
  200. image->ncolors = 0;
  201. image->colorTable = NULL;
  202. info->hints_cmt = NULL;
  203. info->colors_cmt = NULL;
  204. info->pixels_cmt = NULL;
  205. }
  206. }
  207. /* end 3.2 bc */
  208. if (attributes->valuemask & XpmReturnExtensions) {
  209. attributes->extensions = info->extensions;
  210. attributes->nextensions = info->nextensions;
  211. /* avoid deletion of copied data */
  212. info->extensions = NULL;
  213. info->nextensions = 0;
  214. }
  215. if (info->valuemask & XpmHotspot) {
  216. attributes->valuemask |= XpmHotspot;
  217. attributes->x_hotspot = info->x_hotspot;
  218. attributes->y_hotspot = info->y_hotspot;
  219. }
  220. attributes->valuemask |= XpmCharsPerPixel;
  221. attributes->cpp = image->cpp;
  222. attributes->valuemask |= XpmSize;
  223. attributes->width = image->width;
  224. attributes->height = image->height;
  225. }
  226. /*
  227. * Free the XpmAttributes structure members
  228. * but the structure itself
  229. */
  230. void
  231. XpmFreeAttributes(XpmAttributes *attributes)
  232. {
  233. if (attributes->valuemask & XpmReturnPixels && attributes->npixels) {
  234. XpmFree(attributes->pixels);
  235. attributes->pixels = NULL;
  236. attributes->npixels = 0;
  237. }
  238. if (attributes->valuemask & XpmReturnColorTable) {
  239. xpmFreeColorTable(attributes->colorTable, attributes->ncolors);
  240. attributes->colorTable = NULL;
  241. attributes->ncolors = 0;
  242. }
  243. /* 3.2 backward compatibility code */
  244. else if (attributes->valuemask & XpmInfos) {
  245. if (attributes->colorTable) {
  246. FreeOldColorTable((XpmColor **) attributes->colorTable,
  247. attributes->ncolors);
  248. attributes->colorTable = NULL;
  249. attributes->ncolors = 0;
  250. }
  251. if (attributes->hints_cmt) {
  252. XpmFree(attributes->hints_cmt);
  253. attributes->hints_cmt = NULL;
  254. }
  255. if (attributes->colors_cmt) {
  256. XpmFree(attributes->colors_cmt);
  257. attributes->colors_cmt = NULL;
  258. }
  259. if (attributes->pixels_cmt) {
  260. XpmFree(attributes->pixels_cmt);
  261. attributes->pixels_cmt = NULL;
  262. }
  263. if (attributes->pixels) {
  264. XpmFree(attributes->pixels);
  265. attributes->pixels = NULL;
  266. attributes->npixels = 0;
  267. }
  268. }
  269. /* end 3.2 bc */
  270. if (attributes->valuemask & XpmReturnExtensions
  271. && attributes->nextensions) {
  272. XpmFreeExtensions(attributes->extensions, attributes->nextensions);
  273. attributes->extensions = NULL;
  274. attributes->nextensions = 0;
  275. }
  276. if (attributes->valuemask & XpmReturnAllocPixels
  277. && attributes->nalloc_pixels) {
  278. XpmFree(attributes->alloc_pixels);
  279. attributes->alloc_pixels = NULL;
  280. attributes->nalloc_pixels = 0;
  281. }
  282. attributes->valuemask = 0;
  283. }