1
0

gifalloc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*****************************************************************************
  2. GIF construction tools
  3. ****************************************************************************/
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "gif_lib.h"
  8. #define MAX(x, y) (((x) > (y)) ? (x) : (y))
  9. /******************************************************************************
  10. Miscellaneous utility functions
  11. ******************************************************************************/
  12. /* return smallest bitfield size n will fit in */
  13. int
  14. GifBitSize(int n)
  15. {
  16. register int i;
  17. for (i = 1; i <= 8; i++)
  18. if ((1 << i) >= n)
  19. break;
  20. return (i);
  21. }
  22. /******************************************************************************
  23. Color map object functions
  24. ******************************************************************************/
  25. /*
  26. * Allocate a color map of given size; initialize with contents of
  27. * ColorMap if that pointer is non-NULL.
  28. */
  29. ColorMapObject *
  30. GifMakeMapObject(int ColorCount, const GifColorType *ColorMap)
  31. {
  32. ColorMapObject *Object;
  33. /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
  34. * make the user know that or should we automatically round up instead? */
  35. if (ColorCount != (1 << GifBitSize(ColorCount))) {
  36. return ((ColorMapObject *) NULL);
  37. }
  38. Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
  39. if (Object == (ColorMapObject *) NULL) {
  40. return ((ColorMapObject *) NULL);
  41. }
  42. Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
  43. if (Object->Colors == (GifColorType *) NULL) {
  44. free(Object);
  45. return ((ColorMapObject *) NULL);
  46. }
  47. Object->ColorCount = ColorCount;
  48. Object->BitsPerPixel = GifBitSize(ColorCount);
  49. Object->SortFlag = false;
  50. if (ColorMap != NULL) {
  51. memcpy((char *)Object->Colors,
  52. (char *)ColorMap, ColorCount * sizeof(GifColorType));
  53. }
  54. return (Object);
  55. }
  56. /*******************************************************************************
  57. Free a color map object
  58. *******************************************************************************/
  59. void
  60. GifFreeMapObject(ColorMapObject *Object)
  61. {
  62. if (Object != NULL) {
  63. (void)free(Object->Colors);
  64. (void)free(Object);
  65. }
  66. }
  67. #ifdef DEBUG
  68. void
  69. DumpColorMap(ColorMapObject *Object,
  70. FILE * fp)
  71. {
  72. if (Object != NULL) {
  73. int i, j, Len = Object->ColorCount;
  74. for (i = 0; i < Len; i += 4) {
  75. for (j = 0; j < 4 && j < Len; j++) {
  76. (void)fprintf(fp, "%3d: %02x %02x %02x ", i + j,
  77. Object->Colors[i + j].Red,
  78. Object->Colors[i + j].Green,
  79. Object->Colors[i + j].Blue);
  80. }
  81. (void)fprintf(fp, "\n");
  82. }
  83. }
  84. }
  85. #endif /* DEBUG */
  86. /*******************************************************************************
  87. Compute the union of two given color maps and return it. If result can't
  88. fit into 256 colors, NULL is returned, the allocated union otherwise.
  89. ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are
  90. copied iff they didn't exist before. ColorTransIn2 maps the old
  91. ColorIn2 into the ColorUnion color map table./
  92. *******************************************************************************/
  93. ColorMapObject *
  94. GifUnionColorMap(const ColorMapObject *ColorIn1,
  95. const ColorMapObject *ColorIn2,
  96. GifPixelType ColorTransIn2[])
  97. {
  98. int i, j, CrntSlot, RoundUpTo, NewGifBitSize;
  99. ColorMapObject *ColorUnion;
  100. /*
  101. * We don't worry about duplicates within either color map; if
  102. * the caller wants to resolve those, he can perform unions
  103. * with an empty color map.
  104. */
  105. /* Allocate table which will hold the result for sure. */
  106. ColorUnion = GifMakeMapObject(MAX(ColorIn1->ColorCount,
  107. ColorIn2->ColorCount) * 2, NULL);
  108. if (ColorUnion == NULL)
  109. return (NULL);
  110. /*
  111. * Copy ColorIn1 to ColorUnion.
  112. */
  113. for (i = 0; i < ColorIn1->ColorCount; i++)
  114. ColorUnion->Colors[i] = ColorIn1->Colors[i];
  115. CrntSlot = ColorIn1->ColorCount;
  116. /*
  117. * Potentially obnoxious hack:
  118. *
  119. * Back CrntSlot down past all contiguous {0, 0, 0} slots at the end
  120. * of table 1. This is very useful if your display is limited to
  121. * 16 colors.
  122. */
  123. while (ColorIn1->Colors[CrntSlot - 1].Red == 0
  124. && ColorIn1->Colors[CrntSlot - 1].Green == 0
  125. && ColorIn1->Colors[CrntSlot - 1].Blue == 0)
  126. CrntSlot--;
  127. /* Copy ColorIn2 to ColorUnion (use old colors if they exist): */
  128. for (i = 0; i < ColorIn2->ColorCount && CrntSlot <= 256; i++) {
  129. /* Let's see if this color already exists: */
  130. for (j = 0; j < ColorIn1->ColorCount; j++)
  131. if (memcmp (&ColorIn1->Colors[j], &ColorIn2->Colors[i],
  132. sizeof(GifColorType)) == 0)
  133. break;
  134. if (j < ColorIn1->ColorCount)
  135. ColorTransIn2[i] = j; /* color exists in Color1 */
  136. else {
  137. /* Color is new - copy it to a new slot: */
  138. ColorUnion->Colors[CrntSlot] = ColorIn2->Colors[i];
  139. ColorTransIn2[i] = CrntSlot++;
  140. }
  141. }
  142. if (CrntSlot > 256) {
  143. GifFreeMapObject(ColorUnion);
  144. return ((ColorMapObject *) NULL);
  145. }
  146. NewGifBitSize = GifBitSize(CrntSlot);
  147. RoundUpTo = (1 << NewGifBitSize);
  148. if (RoundUpTo != ColorUnion->ColorCount) {
  149. register GifColorType *Map = ColorUnion->Colors;
  150. /*
  151. * Zero out slots up to next power of 2.
  152. * We know these slots exist because of the way ColorUnion's
  153. * start dimension was computed.
  154. */
  155. for (j = CrntSlot; j < RoundUpTo; j++)
  156. Map[j].Red = Map[j].Green = Map[j].Blue = 0;
  157. /* perhaps we can shrink the map? */
  158. if (RoundUpTo < ColorUnion->ColorCount) {
  159. GifColorType *new_map = (GifColorType *)reallocarray(Map,
  160. RoundUpTo, sizeof(GifColorType));
  161. if( new_map == NULL ) {
  162. GifFreeMapObject(ColorUnion);
  163. return ((ColorMapObject *) NULL);
  164. }
  165. ColorUnion->Colors = new_map;
  166. }
  167. }
  168. ColorUnion->ColorCount = RoundUpTo;
  169. ColorUnion->BitsPerPixel = NewGifBitSize;
  170. return (ColorUnion);
  171. }
  172. /*******************************************************************************
  173. Apply a given color translation to the raster bits of an image
  174. *******************************************************************************/
  175. void
  176. GifApplyTranslation(SavedImage *Image, GifPixelType Translation[])
  177. {
  178. register int i;
  179. register int RasterSize = Image->ImageDesc.Height * Image->ImageDesc.Width;
  180. for (i = 0; i < RasterSize; i++)
  181. Image->RasterBits[i] = Translation[Image->RasterBits[i]];
  182. }
  183. /******************************************************************************
  184. Extension record functions
  185. ******************************************************************************/
  186. int
  187. GifAddExtensionBlock(int *ExtensionBlockCount,
  188. ExtensionBlock **ExtensionBlocks,
  189. int Function,
  190. unsigned int Len,
  191. unsigned char ExtData[])
  192. {
  193. ExtensionBlock *ep;
  194. if (*ExtensionBlocks == NULL)
  195. *ExtensionBlocks=(ExtensionBlock *)malloc(sizeof(ExtensionBlock));
  196. else {
  197. ExtensionBlock* ep_new = (ExtensionBlock *)reallocarray
  198. (*ExtensionBlocks, (*ExtensionBlockCount + 1),
  199. sizeof(ExtensionBlock));
  200. if( ep_new == NULL )
  201. return (GIF_ERROR);
  202. *ExtensionBlocks = ep_new;
  203. }
  204. if (*ExtensionBlocks == NULL)
  205. return (GIF_ERROR);
  206. ep = &(*ExtensionBlocks)[(*ExtensionBlockCount)++];
  207. ep->Function = Function;
  208. ep->ByteCount=Len;
  209. ep->Bytes = (GifByteType *)malloc(ep->ByteCount);
  210. if (ep->Bytes == NULL)
  211. return (GIF_ERROR);
  212. if (ExtData != NULL) {
  213. memcpy(ep->Bytes, ExtData, Len);
  214. }
  215. return (GIF_OK);
  216. }
  217. void
  218. GifFreeExtensions(int *ExtensionBlockCount,
  219. ExtensionBlock **ExtensionBlocks)
  220. {
  221. ExtensionBlock *ep;
  222. if (*ExtensionBlocks == NULL)
  223. return;
  224. for (ep = *ExtensionBlocks;
  225. ep < (*ExtensionBlocks + *ExtensionBlockCount);
  226. ep++)
  227. (void)free((char *)ep->Bytes);
  228. (void)free((char *)*ExtensionBlocks);
  229. *ExtensionBlocks = NULL;
  230. *ExtensionBlockCount = 0;
  231. }
  232. /******************************************************************************
  233. Image block allocation functions
  234. ******************************************************************************/
  235. /* Private Function:
  236. * Frees the last image in the GifFile->SavedImages array
  237. */
  238. void
  239. FreeLastSavedImage(GifFileType *GifFile)
  240. {
  241. SavedImage *sp;
  242. if ((GifFile == NULL) || (GifFile->SavedImages == NULL))
  243. return;
  244. /* Remove one SavedImage from the GifFile */
  245. GifFile->ImageCount--;
  246. sp = &GifFile->SavedImages[GifFile->ImageCount];
  247. /* Deallocate its Colormap */
  248. if (sp->ImageDesc.ColorMap != NULL) {
  249. GifFreeMapObject(sp->ImageDesc.ColorMap);
  250. sp->ImageDesc.ColorMap = NULL;
  251. }
  252. /* Deallocate the image data */
  253. if (sp->RasterBits != NULL)
  254. free((char *)sp->RasterBits);
  255. /* Deallocate any extensions */
  256. GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks);
  257. /*** FIXME: We could realloc the GifFile->SavedImages structure but is
  258. * there a point to it? Saves some memory but we'd have to do it every
  259. * time. If this is used in GifFreeSavedImages then it would be inefficient
  260. * (The whole array is going to be deallocated.) If we just use it when
  261. * we want to free the last Image it's convenient to do it here.
  262. */
  263. }
  264. /*
  265. * Append an image block to the SavedImages array
  266. */
  267. SavedImage *
  268. GifMakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom)
  269. {
  270. if (GifFile->SavedImages == NULL)
  271. GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
  272. else
  273. GifFile->SavedImages = (SavedImage *)reallocarray(GifFile->SavedImages,
  274. (GifFile->ImageCount + 1), sizeof(SavedImage));
  275. if (GifFile->SavedImages == NULL)
  276. return ((SavedImage *)NULL);
  277. else {
  278. SavedImage *sp = &GifFile->SavedImages[GifFile->ImageCount++];
  279. memset((char *)sp, '\0', sizeof(SavedImage));
  280. if (CopyFrom != NULL) {
  281. memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
  282. /*
  283. * Make our own allocated copies of the heap fields in the
  284. * copied record. This guards against potential aliasing
  285. * problems.
  286. */
  287. /* first, the local color map */
  288. if (sp->ImageDesc.ColorMap != NULL) {
  289. sp->ImageDesc.ColorMap = GifMakeMapObject(
  290. CopyFrom->ImageDesc.ColorMap->ColorCount,
  291. CopyFrom->ImageDesc.ColorMap->Colors);
  292. if (sp->ImageDesc.ColorMap == NULL) {
  293. FreeLastSavedImage(GifFile);
  294. return (SavedImage *)(NULL);
  295. }
  296. }
  297. /* next, the raster */
  298. sp->RasterBits = (unsigned char *)reallocarray(NULL,
  299. (CopyFrom->ImageDesc.Height *
  300. CopyFrom->ImageDesc.Width),
  301. sizeof(GifPixelType));
  302. if (sp->RasterBits == NULL) {
  303. FreeLastSavedImage(GifFile);
  304. return (SavedImage *)(NULL);
  305. }
  306. memcpy(sp->RasterBits, CopyFrom->RasterBits,
  307. sizeof(GifPixelType) * CopyFrom->ImageDesc.Height *
  308. CopyFrom->ImageDesc.Width);
  309. /* finally, the extension blocks */
  310. if (sp->ExtensionBlocks != NULL) {
  311. sp->ExtensionBlocks = (ExtensionBlock *)reallocarray(NULL,
  312. CopyFrom->ExtensionBlockCount,
  313. sizeof(ExtensionBlock));
  314. if (sp->ExtensionBlocks == NULL) {
  315. FreeLastSavedImage(GifFile);
  316. return (SavedImage *)(NULL);
  317. }
  318. memcpy(sp->ExtensionBlocks, CopyFrom->ExtensionBlocks,
  319. sizeof(ExtensionBlock) * CopyFrom->ExtensionBlockCount);
  320. }
  321. }
  322. return (sp);
  323. }
  324. }
  325. void
  326. GifFreeSavedImages(GifFileType *GifFile)
  327. {
  328. SavedImage *sp;
  329. if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
  330. return;
  331. }
  332. for (sp = GifFile->SavedImages;
  333. sp < GifFile->SavedImages + GifFile->ImageCount; sp++) {
  334. if (sp->ImageDesc.ColorMap != NULL) {
  335. GifFreeMapObject(sp->ImageDesc.ColorMap);
  336. sp->ImageDesc.ColorMap = NULL;
  337. }
  338. if (sp->RasterBits != NULL)
  339. free((char *)sp->RasterBits);
  340. GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks);
  341. }
  342. free((char *)GifFile->SavedImages);
  343. GifFile->SavedImages = NULL;
  344. }
  345. /* end */