i_closepl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. #include "sys-defines.h"
  16. #include "extern.h"
  17. #include "i_rle.h" /* use miGIF RLE (non-LZW) compression */
  18. #include "xmi.h"
  19. /* GIF89a frame disposal methods (a 3-bit field; values 4..7 are reserved) */
  20. #define DISP_UNSPECIFIED 0
  21. #define DISP_NONE 1
  22. #define DISP_RESTORE_TO_BACKGROUND 2
  23. #define DISP_RESTORE_TO_PREVIOUS 3
  24. /* forward references */
  25. static bool same_colormap (plColor cmap1[256], plColor cmap2[256], int num1, int num2);
  26. bool
  27. _pl_i_end_page (S___(Plotter *_plotter))
  28. {
  29. /* Output current frame as a GIF image, preceded by a GIF header if
  30. necessary. This applies only if this is page #1. */
  31. #ifdef LIBPLOTTER
  32. if (_plotter->data->outfp || _plotter->data->outstream)
  33. #else
  34. if (_plotter->data->outfp)
  35. #endif
  36. /* have an output stream */
  37. {
  38. if (_plotter->data->page_number == 1)
  39. {
  40. if (_plotter->i_header_written == false)
  41. {
  42. _pl_i_write_gif_header (S___(_plotter));
  43. _plotter->i_header_written = true;
  44. }
  45. /* emit GIF image of current frame using RLE module (see i_rle.c) */
  46. _pl_i_write_gif_image (S___(_plotter));
  47. _pl_i_write_gif_trailer (S___(_plotter));
  48. }
  49. }
  50. /* delete image: deallocate frame's canvas, reset frame's color table */
  51. _pl_i_delete_image (S___(_plotter));
  52. return true;
  53. }
  54. void
  55. _pl_i_write_gif_header (S___(Plotter *_plotter))
  56. {
  57. int i, packed_bits;
  58. /* determine whether transparency extension is really needed */
  59. if (_plotter->i_transparent)
  60. {
  61. if (_plotter->i_animation)
  62. /* transparent color index will be #0 in each image's color table;
  63. see i_color.c */
  64. {
  65. _plotter->i_transparent = true;
  66. _plotter->i_transparent_index = 0;
  67. }
  68. else /* only 1 image, and 1 color table */
  69. {
  70. bool found = false;
  71. plColor t_color;
  72. t_color = _plotter->i_transparent_color;
  73. /* search for user-specified color */
  74. for (i = 0; i < _plotter->i_num_color_indices; i++)
  75. {
  76. if (_plotter->i_colormap[i].red == t_color.red
  77. && _plotter->i_colormap[i].green == t_color.green
  78. && _plotter->i_colormap[i].blue == t_color.blue)
  79. {
  80. found = true;
  81. break;
  82. }
  83. }
  84. if (found)
  85. {
  86. _plotter->i_transparent = true;
  87. _plotter->i_transparent_index = i;
  88. }
  89. else /* transparency not needed */
  90. _plotter->i_transparent = false;
  91. }
  92. }
  93. /* Header block, including Signature and Version. */
  94. /* To express transparency, a nontrivial number of iterations, or a
  95. nontrivial delay between successive images, need GIF89a format, not
  96. GIF87a. */
  97. if (_plotter->i_transparent
  98. || (_plotter->i_animation && _plotter->i_iterations > 0)
  99. || (_plotter->i_animation && _plotter->i_delay > 0))
  100. _write_string (_plotter->data, "GIF89a");
  101. else
  102. _write_string (_plotter->data, "GIF87a");
  103. /* Logical Screen Descriptor Block */
  104. /* Logical Screen Width and Height (2-byte unsigned ints) */
  105. _pl_i_write_short_int (R___(_plotter) (unsigned int)_plotter->i_xn);
  106. _pl_i_write_short_int (R___(_plotter) (unsigned int)_plotter->i_yn);
  107. /* Global Color Table Flag (1 bit) [1/0 = global table follows / doesn't
  108. follow]. Represented by 0x80 / 0x00 respectively. */
  109. packed_bits = 0x80;
  110. /* Color Resolution, i.e. bitdepth minus 1, with min=0 (3 bits) */
  111. packed_bits |= (IMAX(_plotter->i_bit_depth - 1, 0)) << 4;
  112. /* Sort Flag [0 = unordered] (1 bit) */
  113. /* Size of Global Color Table, i.e. bitdepth minus 1, with min=0 (3 bits) */
  114. packed_bits |= (IMAX(_plotter->i_bit_depth - 1, 0));
  115. /* write 1 byte of packed bits */
  116. _write_byte (_plotter->data, (unsigned char)packed_bits);
  117. /* Background Color Index (if there's no global color table this field
  118. should be set to 0) */
  119. _write_byte (_plotter->data, _plotter->drawstate->i_bg_color_index);
  120. /* Pixel Aspect Ratio (0 = field unused) */
  121. _write_byte (_plotter->data, (unsigned char)0);
  122. /* Global Color Table (expanded to next higher power of 2, with min=2) */
  123. for (i = 0; i < (1 << IMAX(_plotter->i_bit_depth, 1)); ++i)
  124. {
  125. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].red);
  126. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].green);
  127. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].blue);
  128. /* stash table (for comparison with color tables of later frames) */
  129. _plotter->i_global_colormap[i] = _plotter->i_colormap[i];
  130. }
  131. _plotter->i_num_global_color_indices = _plotter->i_num_color_indices;
  132. /* Netscape Loop Extension Block (extension blocks are a GIF89a feature;
  133. this requests `looping' of subsequent images) */
  134. if (_plotter->i_animation && _plotter->i_iterations > 0)
  135. {
  136. /* Extension Introducer */
  137. _write_byte (_plotter->data, (unsigned char)'!');
  138. /* Application Extension Label */
  139. _write_byte (_plotter->data, (unsigned char)0xff);
  140. /* Block Size (fixed at 11) */
  141. _write_byte (_plotter->data, (unsigned char)11);
  142. /* Application Identifier (8 bytes) and Auth. Code (3 bytes) */
  143. _write_string (_plotter->data, "NETSCAPE2.0");
  144. /* Block Size (fixed at 3) */
  145. _write_byte (_plotter->data, (unsigned char)0x03);
  146. /* Block, 3 bytes long */
  147. _write_byte (_plotter->data, (unsigned char)0x01);/* what is this? */
  148. _pl_i_write_short_int (R___(_plotter) (unsigned int)(_plotter->i_iterations));
  149. /* Block Terminator (0-length data block) */
  150. _write_byte (_plotter->data, (unsigned char)0x00);
  151. }
  152. }
  153. /* Write image descriptor, including color table. Also scan image, and
  154. compress and write the resulting stream of color indices. */
  155. void
  156. _pl_i_write_gif_image (S___(Plotter *_plotter))
  157. {
  158. bool write_local_table;
  159. int i, min_code_size, packed_bits;
  160. /* Graphic Control Block (a GIF89a feature; modifies following image
  161. descriptor). Needed to express transparency of each image, or a
  162. non-default delay after each image. */
  163. if (_plotter->i_transparent
  164. || (_plotter->i_animation && _plotter->i_delay > 0))
  165. {
  166. unsigned char packed_byte;
  167. /* Extension Introducer */
  168. _write_byte (_plotter->data, (unsigned char)'!');
  169. /* Graphic Control Label */
  170. _write_byte (_plotter->data, (unsigned char)0xf9);
  171. /* Block Size (fixed at 4) */
  172. _write_byte (_plotter->data, (unsigned char)4);
  173. /* Packed fields: Reserved (3 bits), Disposal Method (3 bits),
  174. User Input Flag (1 bit), Transparency Flag (final 1 bit) */
  175. packed_byte = 0;
  176. if (_plotter->i_transparent)
  177. packed_byte |= 1;
  178. if (_plotter->i_transparent && _plotter->i_animation)
  179. packed_byte |= (DISP_RESTORE_TO_BACKGROUND << 2);
  180. else
  181. packed_byte |= (DISP_UNSPECIFIED << 2);
  182. _write_byte (_plotter->data, packed_byte);
  183. /* Delay time in hundredths of a second [the same for all frames]
  184. (2-byte unsigned int) */
  185. _pl_i_write_short_int (R___(_plotter) (unsigned int)(_plotter->i_delay));
  186. /* Transparent Color Index [the same for all frames] */
  187. _write_byte (_plotter->data, (unsigned char)_plotter->i_transparent_index);
  188. /* Block Terminator (0-length data block) */
  189. _write_byte (_plotter->data, (unsigned char)0);
  190. }
  191. /* Image Descriptor */
  192. /* Image Separator */
  193. _write_byte (_plotter->data, (unsigned char)',');
  194. /* Image Left and Top Positions (w/ respect to logical screen;
  195. 2-byte unsigned ints) */
  196. _pl_i_write_short_int (R___(_plotter) 0);
  197. _pl_i_write_short_int (R___(_plotter) 0);
  198. /* Image Width, Height (2-byte unsigned ints) */
  199. _pl_i_write_short_int (R___(_plotter) (unsigned int)_plotter->i_xn);
  200. _pl_i_write_short_int (R___(_plotter) (unsigned int)_plotter->i_yn);
  201. /* does current frame's color table differ from zeroth frame's color
  202. table (i.e. GIF file's global color table)? */
  203. write_local_table
  204. = same_colormap (_plotter->i_colormap, _plotter->i_global_colormap,
  205. _plotter->i_num_color_indices,
  206. _plotter->i_num_global_color_indices) ? false : true;
  207. /* Packed fields: Local Color Table (1 bit), Interlace Flag (1 bit), Sort
  208. Flag (1 bit), Reserved (2 bits), Local Color Table Size (3 bits) */
  209. packed_bits = 0x00;
  210. if (write_local_table)
  211. {
  212. packed_bits |= 0x80;
  213. packed_bits |= (IMAX(_plotter->i_bit_depth - 1, 0));
  214. }
  215. /* interlace? */
  216. if (_plotter->i_interlace)
  217. packed_bits |= 0x40;
  218. /* write one byte of packed bits */
  219. _write_byte (_plotter->data, (unsigned char)packed_bits);
  220. /* Local Color Table (expanded to next higher power of 2, with min=2) */
  221. if (write_local_table)
  222. {
  223. for (i = 0; i < (1 << IMAX(_plotter->i_bit_depth, 1)); ++i)
  224. {
  225. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].red);
  226. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].green);
  227. _write_byte (_plotter->data, (unsigned char)_plotter->i_colormap[i].blue);
  228. }
  229. }
  230. /* Table-Based Image Data */
  231. /* LZW Minimum Code Size. (Minimum number of bits required to represent
  232. the set of actual pixel values, which will be the same as the bit
  233. depth, since our allocated color indices are contiguous. However,
  234. this has a floor of 2, and, also compression codes must start out one
  235. bit longer than the floored version, "because of some algorithmic
  236. constraints". See i_rle.c.) */
  237. min_code_size = IMAX(_plotter->i_bit_depth, 2);
  238. _write_byte (_plotter->data, (unsigned char)min_code_size);
  239. /* initialize pixel scanner */
  240. _pl_i_start_scan (S___(_plotter));
  241. /* Image Data, consisting of a sequence of sub-blocks of size at most
  242. 255 bytes each, encoded as LZW with variable-length code
  243. (actually, we use miGIF [RLE] rather than LZW; see i_rle.c) */
  244. {
  245. rle_out *rle;
  246. int pixel;
  247. #ifdef LIBPLOTTER
  248. rle = _rle_init (_plotter->data->outfp, _plotter->data->outstream,
  249. _plotter->i_bit_depth);
  250. #else
  251. rle = _rle_init (_plotter->data->outfp,
  252. _plotter->i_bit_depth);
  253. #endif
  254. while ((pixel = _pl_i_scan_pixel (S___(_plotter))) != -1)
  255. _rle_do_pixel (rle, pixel);
  256. _rle_terminate (rle);
  257. }
  258. /* Block Terminator */
  259. _write_byte (_plotter->data, (unsigned char)0);
  260. }
  261. void
  262. _pl_i_write_gif_trailer (S___(Plotter *_plotter))
  263. {
  264. /* Trailer Block */
  265. _write_byte (_plotter->data, (unsigned char)';');
  266. }
  267. /* reset scanner variables (first pixel scanned is (0,0), i.e. upper
  268. left-hand corner) */
  269. void
  270. _pl_i_start_scan (S___(Plotter *_plotter))
  271. {
  272. _plotter->i_pixels_scanned = 0;
  273. _plotter->i_pass = 0;
  274. _plotter->i_hot.x = 0;
  275. _plotter->i_hot.y = 0;
  276. }
  277. /* Return index (in color table) of pixel under the hot spot, and continue
  278. the scan by moving to the next pixel. Return -1 when scan is finished. */
  279. int
  280. _pl_i_scan_pixel (S___(Plotter *_plotter))
  281. {
  282. if (_plotter->i_pixels_scanned < _plotter->i_num_pixels)
  283. {
  284. miCanvas *canvas;
  285. int x, y;
  286. miPixel full_pixel;
  287. int pixel;
  288. /* use a libxmi macro, defined in xmi.h, to extract the miPixel at
  289. the hotspot; extract index field from it */
  290. canvas = (miCanvas *)_plotter->i_canvas;
  291. x = _plotter->i_hot.x;
  292. y = _plotter->i_hot.y;
  293. MI_GET_CANVAS_DRAWABLE_PIXEL(canvas, x, y, full_pixel)
  294. pixel = full_pixel.u.index;
  295. _plotter->i_hot.x++;
  296. if (_plotter->i_hot.x == _plotter->i_xn)
  297. {
  298. _plotter->i_hot.x = 0;
  299. if (_plotter->i_interlace == false)
  300. _plotter->i_hot.y++;
  301. else /* move to next scan line */
  302. {
  303. switch (_plotter->i_pass)
  304. {
  305. case 0:
  306. /* every 8th row, starting with row 0 */
  307. _plotter->i_hot.y += 8;
  308. if (_plotter->i_hot.y >= _plotter->i_yn)
  309. {
  310. _plotter->i_pass++;
  311. _plotter->i_hot.y = 4;
  312. }
  313. break;
  314. case 1:
  315. /* every 8th row, starting with row 4 */
  316. _plotter->i_hot.y += 8;
  317. if (_plotter->i_hot.y >= _plotter->i_yn)
  318. {
  319. _plotter->i_pass++;
  320. _plotter->i_hot.y = 2;
  321. }
  322. break;
  323. case 2:
  324. /* every 4th row, starting with row 2 */
  325. _plotter->i_hot.y += 4;
  326. if (_plotter->i_hot.y >= _plotter->i_yn)
  327. {
  328. _plotter->i_pass++;
  329. _plotter->i_hot.y = 1;
  330. }
  331. break;
  332. case 3:
  333. /* every 2nd row, starting with row 1 */
  334. _plotter->i_hot.y += 2;
  335. break;
  336. }
  337. }
  338. }
  339. _plotter->i_pixels_scanned++;
  340. return pixel;
  341. }
  342. else /* scan is finished */
  343. return -1;
  344. }
  345. /* write out an unsigned short int, in range 0..65535, as 2 bytes in
  346. little-endian order */
  347. void
  348. _pl_i_write_short_int (R___(Plotter *_plotter) unsigned int i)
  349. {
  350. unsigned char bytes[2];
  351. bytes[0] = (unsigned char)(i & 0xff);
  352. bytes[1] = (unsigned char)((i >> 8) & 0xff);
  353. _write_bytes (_plotter->data, 2, bytes);
  354. }
  355. /* tear down image, i.e. deallocate libxmi canvas and reset colormap */
  356. void
  357. _pl_i_delete_image (S___(Plotter *_plotter))
  358. {
  359. /* deallocate libxmi's drawing canvas (and painted set struct too) */
  360. miDeleteCanvas ((miCanvas *)_plotter->i_canvas);
  361. _plotter->i_canvas = (void *)NULL;
  362. miDeletePaintedSet ((miPaintedSet *)_plotter->i_painted_set);
  363. _plotter->i_painted_set = (void *)NULL;
  364. /* reset colormap */
  365. _plotter->i_num_color_indices = 0;
  366. /* flag color indices in drawing state as bogus */
  367. _plotter->drawstate->i_pen_color_status = false;
  368. _plotter->drawstate->i_fill_color_status = false;
  369. _plotter->drawstate->i_bg_color_status = false;
  370. }
  371. /* compare two partially filled size-256 colormaps for equality */
  372. static bool
  373. same_colormap (plColor cmap1[256], plColor cmap2[256], int num1, int num2)
  374. {
  375. int i;
  376. if (num1 != num2)
  377. return false;
  378. for (i = 0; i < num1; i++)
  379. if ((cmap1[i].red != cmap2[i].red)
  380. || (cmap1[i].green != cmap2[i].green)
  381. || (cmap1[i].blue != cmap2[i].blue))
  382. return false;
  383. return true;
  384. }