GIF.C 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /****************************************************************************
  2. * gif.c
  3. *
  4. * Gif-format file reader.
  5. *
  6. * NOTE: Portions of this module were written by Steve Bennett and are used
  7. * here with his permission.
  8. *
  9. * from Persistence of Vision(tm) Ray Tracer
  10. * Copyright 1996,1999 Persistence of Vision Team
  11. *---------------------------------------------------------------------------
  12. * NOTICE: This source code file is provided so that users may experiment
  13. * with enhancements to POV-Ray and to port the software to platforms other
  14. * than those supported by the POV-Ray Team. There are strict rules under
  15. * which you are permitted to use this file. The rules are in the file
  16. * named POVLEGAL.DOC which should be distributed with this file.
  17. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  18. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  19. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  20. *
  21. * This program is based on the popular DKB raytracer version 2.12.
  22. * DKBTrace was originally written by David K. Buck.
  23. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  24. *
  25. * Modification by Hans-Deltev Fink, January 1999, used with permission
  26. *
  27. *****************************************************************************/
  28. /*
  29. * The following routines were borrowed freely from FRACTINT, and represent
  30. * a generalized GIF file decoder. This once seemed the best, most universal
  31. * format for reading in Bitmapped images, until Unisys began enforcing
  32. * its patent on the LZ compression that GIF uses. POV-Ray, as freeware, is
  33. * exempt from GIF licensing fees. GIF is a Copyright of Compuserve, Inc.
  34. *
  35. * Swiped and converted to entirely "C" coded routines by AAC for the most
  36. * in future portability!
  37. */
  38. #include "frame.h"
  39. #include "povproto.h"
  40. #include "gif.h"
  41. #include "gifdecod.h"
  42. #include "povray.h"
  43. /*****************************************************************************
  44. * Local preprocessor defines
  45. ******************************************************************************/
  46. /*****************************************************************************
  47. * Local typedefs
  48. ******************************************************************************/
  49. /*****************************************************************************
  50. * Local variables
  51. ******************************************************************************/
  52. static IMAGE *Current_Image;
  53. static int Bitmap_Line;
  54. static FILE *Bit_File;
  55. unsigned char *decoderline /* [2049] */ ; /* write-line routines use this */
  56. static IMAGE_COLOUR *gif_colour_map;
  57. static int colourmap_size;
  58. /*****************************************************************************
  59. * Static functions
  60. ******************************************************************************/
  61. /*****************************************************************************
  62. *
  63. * FUNCTION
  64. *
  65. * out_line
  66. *
  67. * INPUT
  68. *
  69. * OUTPUT
  70. *
  71. * RETURNS
  72. *
  73. * AUTHOR
  74. *
  75. * POV-Ray Team
  76. *
  77. * DESCRIPTION
  78. *
  79. * -
  80. *
  81. * CHANGES
  82. *
  83. * -
  84. *
  85. ******************************************************************************/
  86. int out_line (unsigned char *pixels, int linelen)
  87. {
  88. register int x;
  89. register unsigned char *line;
  90. if (Bitmap_Line == Current_Image->iheight)
  91. {
  92. Warning (0.0, "Extra data at end of GIF image.\n");
  93. return (0) ;
  94. }
  95. line = Current_Image->data.map_lines[Bitmap_Line++];
  96. for (x = 0; x < linelen; x++)
  97. {
  98. if ((int)(*pixels) > Current_Image->Colour_Map_Size)
  99. {
  100. Error ("Error - GIF image map color out of range.\n");
  101. }
  102. line[x] = *pixels;
  103. pixels++;
  104. }
  105. return (0);
  106. }
  107. /*****************************************************************************
  108. *
  109. * FUNCTION
  110. *
  111. * gif_get_byte
  112. *
  113. * INPUT
  114. *
  115. * OUTPUT
  116. *
  117. * RETURNS
  118. *
  119. * AUTHOR
  120. *
  121. * POV-Ray Team
  122. *
  123. * DESCRIPTION
  124. *
  125. * Get byte from file, return the next byte or an error.
  126. *
  127. * CHANGES
  128. *
  129. * -
  130. *
  131. ******************************************************************************/
  132. int gif_get_byte()
  133. {
  134. register int byte;
  135. if ((byte = getc(Bit_File)) != EOF)
  136. {
  137. return (byte);
  138. }
  139. else
  140. {
  141. Error ("Error reading data from GIF image.\n");
  142. }
  143. /* Keep the compiler happy. */
  144. return(0);
  145. }
  146. /*****************************************************************************
  147. *
  148. * FUNCTION
  149. *
  150. * Read_Gif_Image
  151. *
  152. * INPUT
  153. *
  154. * OUTPUT
  155. *
  156. * RETURNS
  157. *
  158. * AUTHOR
  159. *
  160. * POV-Ray Team
  161. *
  162. * DESCRIPTION
  163. *
  164. * Main GIF file decoder.
  165. *
  166. * CHANGES
  167. *
  168. * -
  169. *
  170. ******************************************************************************/
  171. void Read_Gif_Image(IMAGE *Image, char *filename)
  172. {
  173. register int i, j, status;
  174. unsigned finished, planes;
  175. unsigned char buffer[16];
  176. status = 0;
  177. Current_Image = Image;
  178. if ((Bit_File = Locate_File(filename, READ_BINFILE_STRING, ".gif", ".GIF",NULL,TRUE)) == NULL)
  179. {
  180. Error ("Error opening GIF image.\n");
  181. }
  182. /* Get the screen description. */
  183. for (i = 0; i < 13; i++)
  184. {
  185. buffer[i] = (unsigned char)gif_get_byte();
  186. }
  187. /* Use updated GIF specs. */
  188. if (strncmp((char *) buffer,"GIF",3) == 0) /* Allow only GIF87 and GIF89 */
  189. {
  190. if ((buffer[3] != '8') || ((buffer[4] != '7') && (buffer[4]) != '9') ||
  191. (buffer[5] < 'A') || (buffer[5]) > 'z')
  192. {
  193. Error ("Unsupported GIF version %c%c%c.\n",
  194. buffer[3], buffer[4], buffer[5]);
  195. }
  196. }
  197. else
  198. {
  199. Error ("File is not in GIF format.\n");
  200. }
  201. planes = ((unsigned)buffer[10] & 0x0F) + 1;
  202. colourmap_size = (int)(1 << planes);
  203. gif_colour_map = (IMAGE_COLOUR *)POV_CALLOC((size_t)colourmap_size, sizeof(IMAGE_COLOUR), "GIF color map");
  204. /* Color map (better be!) */
  205. if ((buffer[10] & 0x80) == 0)
  206. {
  207. Error ("Error in GIF color map.\n");
  208. }
  209. for (i = 0; i < colourmap_size ; i++)
  210. {
  211. gif_colour_map[i].Red = (unsigned char)gif_get_byte();
  212. gif_colour_map[i].Green = (unsigned char)gif_get_byte();
  213. gif_colour_map[i].Blue = (unsigned char)gif_get_byte();
  214. gif_colour_map[i].Filter = 0;
  215. gif_colour_map[i].Transmit = 0;
  216. }
  217. /* Now display one or more GIF objects. */
  218. finished = FALSE;
  219. while (!finished)
  220. {
  221. switch (gif_get_byte())
  222. {
  223. /* End of the GIF dataset. */
  224. case ';':
  225. finished = TRUE;
  226. status = 0;
  227. break;
  228. /* GIF Extension Block. */
  229. case '!':
  230. /* Read (and ignore) the ID. */
  231. gif_get_byte();
  232. /* Get data len. */
  233. while ((i = gif_get_byte()) > 0)
  234. {
  235. for (j = 0; j < i; j++)
  236. {
  237. /* Flush data. */
  238. gif_get_byte();
  239. }
  240. }
  241. break;
  242. /* Start of image object. Get description. */
  243. case ',':
  244. for (i = 0; i < 9; i++)
  245. {
  246. /* EOF test (?).*/
  247. if ((j = gif_get_byte()) < 0)
  248. {
  249. status = -1;
  250. break;
  251. }
  252. buffer[i] = (unsigned char) j;
  253. }
  254. /* Check "interlaced" bit. */
  255. if (j & 0x40)
  256. {
  257. Error ("Interlacing in GIF image unsupported.\n");
  258. }
  259. if (status < 0)
  260. {
  261. finished = TRUE;
  262. break;
  263. }
  264. Image->iwidth = buffer[4] | (buffer[5] << 8);
  265. Image->iheight = buffer[6] | (buffer[7] << 8);
  266. Image->width = (DBL) Image->iwidth;
  267. Image->height = (DBL) Image->iheight;
  268. Bitmap_Line = 0;
  269. Image->Colour_Map_Size = colourmap_size;
  270. Image->Colour_Map = gif_colour_map;
  271. Image->data.map_lines = (unsigned char **)POV_MALLOC(Image->iheight * sizeof(unsigned char *), "GIF image");
  272. for (i = 0 ; i < Image->iheight ; i++)
  273. {
  274. Image->data.map_lines[i] = (unsigned char *)POV_CALLOC((size_t)Image->iwidth, sizeof(unsigned char), "GIF image line");
  275. }
  276. /* Setup the color palette for the image. */
  277. decoderline = (unsigned char *)POV_CALLOC(Image->iwidth, sizeof(unsigned char), "GIF decoder line");
  278. /* Put bytes in Buf. */
  279. status = decoder(Image->iwidth);
  280. POV_FREE (decoderline);
  281. decoderline = NULL;
  282. finished = TRUE;
  283. break;
  284. default:
  285. status = -1;
  286. finished = TRUE;
  287. break;
  288. }
  289. }
  290. if (Bit_File != NULL) /* -hdf99- */
  291. {
  292. fclose(Bit_File);
  293. }
  294. }