z_write.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. /* This file contains a special version of the function
  16. _maybe_output_image, which is called by the BitmapPlotter closepl method
  17. (see b_closepl.c). This version is for the PNGPlotter subclass:
  18. provided that the current page of graphics is the first, it writes out a
  19. PNG file. */
  20. /* Note: this calls gmtime(), which is not reentrant. For thread-safety we
  21. should use gmtime_r() instead, but declaring it portably is a hard
  22. problem (cf. comments at head of p_defplot.c and c_defplot.c). */
  23. /* Other than this egregious problem, this code is thread-safe (the warning
  24. and error handlers which we pass to libpng lock and unlock the `message
  25. mutex' defined in g_error.c). Since libpng uses callbacks, warning and
  26. error messages aren't produced simply by calling the functions
  27. _plotter->warning() and _plotter->error() defined in g_error.c. */
  28. /* Note: we should improve this code to support the use of user-specified
  29. warning and error handlers for libplot; cf. the code in g_error.c. But
  30. their use isn't documented yet. */
  31. #include "sys-defines.h"
  32. #include "extern.h"
  33. #include "xmi.h"
  34. #include <png.h>
  35. /* song and dance to define time_t, and declare both time() and gmtime() */
  36. #ifdef HAVE_SYS_TYPES_H
  37. #include <sys/types.h> /* for time_t on some pre-ANSI Unix systems */
  38. #endif
  39. #ifdef TIME_WITH_SYS_TIME
  40. #include <sys/time.h> /* for time() on some pre-ANSI Unix systems */
  41. #include <time.h> /* for gmtime() */
  42. #else /* not TIME_WITH_SYS_TIME, include only one (prefer <sys/time.h>) */
  43. #ifdef HAVE_SYS_TIME_H
  44. #include <sys/time.h>
  45. #else /* not HAVE_SYS_TIME_H */
  46. #include <time.h>
  47. #endif /* not HAVE_SYS_TIME_H */
  48. #endif /* not TIME_WITH_SYS_TIME */
  49. /* Mutex for locking the warning/error message subsystem. Defined in
  50. g_error.c */
  51. #ifdef PTHREAD_SUPPORT
  52. #ifdef HAVE_PTHREAD_H
  53. extern pthread_mutex_t _message_mutex;
  54. #endif
  55. #endif
  56. static const char _short_months[12][4] =
  57. { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  58. /* forward references */
  59. static int _image_type (miPixel **pixmap, int width, int height);
  60. static void _our_error_fn_stdio (png_struct *png_ptr, const char *data);
  61. static void _our_warn_fn_stdio (png_struct *png_ptr, const char *data);
  62. #ifdef LIBPLOTTER
  63. static void _our_IO_flush_fn (png_struct *png_ptr);
  64. static void _our_error_fn_stream (png_struct *png_ptr, const char *data);
  65. static void _our_warn_fn_stream (png_struct *png_ptr, const char *data);
  66. static void _our_write_fn (png_struct *png_ptr, png_byte *data, png_size_t length);
  67. #endif /* LIBPLOTTER */
  68. int
  69. _pl_z_maybe_output_image (S___(Plotter *_plotter))
  70. {
  71. miPixel **pixmap; /* pixmap in miCanvas */
  72. int width, height;
  73. int image_type, bit_depth, color_type;
  74. png_struct *png_ptr;
  75. png_info *info_ptr;
  76. char time_buf[32], software_buf[64];
  77. png_text text_ptr[10];
  78. time_t clock;
  79. struct tm *tmsp;
  80. FILE *fp = _plotter->data->outfp;
  81. FILE *errorfp = _plotter->data->errfp;
  82. void *error_ptr;
  83. png_error_ptr error_fn_ptr, warn_fn_ptr;
  84. #ifdef LIBPLOTTER
  85. ostream *stream = _plotter->data->outstream;
  86. ostream *errorstream = _plotter->data->errstream;
  87. #endif
  88. #ifdef LIBPLOTTER
  89. if (fp == (FILE *)NULL && stream == (ostream *)NULL)
  90. return 0;
  91. #else
  92. if (fp == (FILE *)NULL)
  93. return 0;
  94. #endif
  95. /* Output the page as a PNG file, but only if it's page #1, since PNG
  96. format supports only a single page of graphics. */
  97. if (_plotter->data->page_number != 1)
  98. return 0;
  99. /* work out libpng error handling (i.e. callback functions and data) */
  100. #ifdef LIBPLOTTER
  101. if (errorstream)
  102. {
  103. error_fn_ptr = _our_error_fn_stream;
  104. warn_fn_ptr = _our_warn_fn_stream;
  105. error_ptr = (void *)errorstream;
  106. }
  107. else if (errorfp)
  108. {
  109. error_fn_ptr = _our_error_fn_stdio;
  110. warn_fn_ptr = _our_warn_fn_stdio;
  111. error_ptr = (void *)errorfp;
  112. }
  113. else
  114. {
  115. error_fn_ptr = NULL;
  116. warn_fn_ptr = NULL;
  117. error_ptr = (void *)NULL;
  118. }
  119. #else /* not LIBPLOTTER */
  120. if (errorfp)
  121. {
  122. error_fn_ptr = _our_error_fn_stdio;
  123. warn_fn_ptr = _our_warn_fn_stdio;
  124. error_ptr = (void *)errorfp;
  125. }
  126. else
  127. {
  128. error_fn_ptr = NULL;
  129. warn_fn_ptr = NULL;
  130. error_ptr = (void *)NULL;
  131. }
  132. #endif /* not LIBPLOTTER */
  133. /* create png_struct, install error/warning handlers */
  134. png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
  135. error_ptr,
  136. error_fn_ptr, warn_fn_ptr);
  137. if (png_ptr == (png_struct *)NULL)
  138. return -1;
  139. /* allocate/initialize image information data */
  140. info_ptr = png_create_info_struct (png_ptr);
  141. if (info_ptr == (png_info *)NULL)
  142. {
  143. png_destroy_write_struct (&png_ptr, (png_info **)NULL);
  144. return -1;
  145. }
  146. /* cleanup after libpng errors (error handler does a longjmp) */
  147. if (setjmp (png_ptr->jmpbuf))
  148. {
  149. png_destroy_write_struct (&png_ptr, (png_info **)NULL);
  150. return -1;
  151. }
  152. #ifdef LIBPLOTTER
  153. if (stream)
  154. {
  155. /* use custom write and flush functions, defined below */
  156. png_set_write_fn (png_ptr,
  157. (void *)stream,
  158. (png_rw_ptr)_our_write_fn,
  159. (png_flush_ptr)_our_IO_flush_fn);
  160. }
  161. else
  162. /* must have fp!=NULL, so use default stdio-based output */
  163. png_init_io (png_ptr, fp);
  164. #else /* not LIBPLOTTER */
  165. /* use default stdio-based output */
  166. png_init_io (png_ptr, fp);
  167. #endif /* not LIBPLOTTER */
  168. /* extract pixmap (2D array of miPixels) from miCanvas */
  169. pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;
  170. /* what is best image type that can be used? 0/1/2 = mono/gray/rgb */
  171. width = _plotter->b_xn;
  172. height = _plotter->b_yn;
  173. image_type = _image_type (pixmap, width, height);
  174. switch (image_type)
  175. {
  176. case 0: /* mono */
  177. bit_depth = 1;
  178. color_type = PNG_COLOR_TYPE_GRAY;
  179. break;
  180. case 1: /* gray */
  181. bit_depth = 8;
  182. color_type = PNG_COLOR_TYPE_GRAY;
  183. break;
  184. case 2: /* rgb */
  185. default:
  186. bit_depth = 8;
  187. color_type = PNG_COLOR_TYPE_RGB;
  188. break;
  189. }
  190. /* Set image information in file header. Width and height are up to
  191. 2^31, bit_depth is one of 1, 2, 4, 8, or 16, but valid values also
  192. depend on the color_type selected. color_type is one of
  193. PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA,
  194. PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, or
  195. PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
  196. PNG_INTERLACE_ADAM7. compression_type and filter_type MUST currently
  197. be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. */
  198. png_set_IHDR (png_ptr, info_ptr,
  199. (unsigned int)width, (unsigned int)height,
  200. bit_depth, color_type,
  201. _plotter->z_interlace ? PNG_INTERLACE_ADAM7
  202. : PNG_INTERLACE_NONE,
  203. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  204. /* set transparent color (if user specified one) */
  205. if (_plotter->z_transparent)
  206. {
  207. plColor transparent_color = _plotter->z_transparent_color;
  208. bool transparent_color_ok = true;
  209. png_color_16 trans_value;
  210. switch (image_type)
  211. {
  212. case 0: /* mono */
  213. if ((transparent_color.red != 0 && transparent_color.red != 0xffff)
  214. ||
  215. (transparent_color.green != 0 && transparent_color.green != 0xffff)
  216. ||
  217. (transparent_color.blue != 0 && transparent_color.blue != 0xffff)
  218. ||
  219. (transparent_color.red != transparent_color.green
  220. || transparent_color.red != transparent_color.blue))
  221. /* user-specified transparent color isn't monochrome */
  222. transparent_color_ok = false;
  223. else
  224. trans_value.gray = (png_uint_16)transparent_color.red;
  225. break;
  226. case 1: /* gray */
  227. if (transparent_color.red != transparent_color.green
  228. || transparent_color.red != transparent_color.blue)
  229. /* user-specified transparent color isn't grayscale */
  230. transparent_color_ok = false;
  231. else
  232. trans_value.gray = (png_uint_16)transparent_color.red;
  233. break;
  234. case 2: /* rgb */
  235. default:
  236. trans_value.red = (png_uint_16)transparent_color.red;
  237. trans_value.green = (png_uint_16)transparent_color.green;
  238. trans_value.blue = (png_uint_16)transparent_color.blue;
  239. break;
  240. }
  241. if (transparent_color_ok)
  242. png_set_tRNS (png_ptr, info_ptr, (png_byte *)NULL, 1, &trans_value);
  243. }
  244. /* add some comments to file header */
  245. text_ptr[0].key = (char *)"Title";
  246. text_ptr[0].text = (char *)"PNG plot";
  247. text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
  248. text_ptr[1].key = (char *)"Creation Time";
  249. time (&clock);
  250. tmsp = gmtime (&clock);
  251. sprintf (time_buf,
  252. "%d %s %d %02d:%02d:%02d +0000", /* RFC 1123 date */
  253. (tmsp->tm_mday) % 31,
  254. _short_months[(tmsp->tm_mon) % 12], 1900 + tmsp->tm_year,
  255. (tmsp->tm_hour) % 24, (tmsp->tm_min) % 60, (tmsp->tm_sec) % 61);
  256. text_ptr[1].text = time_buf;
  257. text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
  258. text_ptr[2].key = (char *)"Software";
  259. sprintf (software_buf, "GNU libplot drawing library %s",
  260. PL_LIBPLOT_VER_STRING);
  261. text_ptr[2].text = software_buf;
  262. text_ptr[2].compression = PNG_TEXT_COMPRESSION_NONE;
  263. png_set_text(png_ptr, info_ptr, text_ptr, 3);
  264. /* write out PNG file header */
  265. png_write_info (png_ptr, info_ptr);
  266. /* Write out image data, a row at a time; support multiple passes over
  267. image if interlacing. We don't simply call png_write_image() because
  268. the image in the miCanvas's pixmap is a 2-D array of miPixels, and
  269. sizeof(miPixel) > 4 is possible. Instead we copy each miPixel in a
  270. row into a local row buffer, and write out the row buffer. */
  271. {
  272. png_byte *rowbuf;
  273. int num_passes, pass;
  274. switch (image_type)
  275. {
  276. case 0: /* mono */
  277. rowbuf = (png_byte *)_pl_xmalloc(((width + 7)/8) * sizeof(png_byte));
  278. break;
  279. case 1: /* gray */
  280. rowbuf = (png_byte *)_pl_xmalloc(width * sizeof(png_byte));
  281. break;
  282. case 2: /* rgb */
  283. default:
  284. rowbuf = (png_byte *)_pl_xmalloc(3 * width * sizeof(png_byte));
  285. break;
  286. }
  287. if (_plotter->z_interlace)
  288. /* turn on interlace handling; if interlacing, need >1 pass over image */
  289. num_passes = png_set_interlace_handling (png_ptr);
  290. else
  291. num_passes = 1;
  292. for (pass = 0; pass < num_passes; pass++)
  293. {
  294. int i, j;
  295. for (j = 0; j < height; j++)
  296. {
  297. png_byte *ptr;
  298. /* fill row buffer with 3 bytes per miPixel (RGB), or 1 byte
  299. (gray), or 1 bit (mono) */
  300. ptr = rowbuf;
  301. for (i = 0; i < width; i++)
  302. {
  303. switch (image_type)
  304. {
  305. case 0: /* mono */
  306. if (i % 8 == 0)
  307. {
  308. if (i != 0)
  309. ptr++;
  310. *ptr = (png_byte)0;
  311. }
  312. if (pixmap[j][i].u.rgb[0]) /* white pixel */
  313. *ptr |= (1 << (7 - (i % 8)));
  314. break;
  315. case 1: /* gray */
  316. *ptr++ = (png_byte)pixmap[j][i].u.rgb[0];
  317. break;
  318. case 2: /* rgb */
  319. default:
  320. *ptr++ = (png_byte)pixmap[j][i].u.rgb[0];
  321. *ptr++ = (png_byte)pixmap[j][i].u.rgb[1];
  322. *ptr++ = (png_byte)pixmap[j][i].u.rgb[2];
  323. break;
  324. }
  325. }
  326. /* write out row buffer */
  327. png_write_rows (png_ptr, &rowbuf, 1);
  328. }
  329. }
  330. free (rowbuf);
  331. }
  332. /* write out PNG file trailer (could add more comments here) */
  333. png_write_end (png_ptr, (png_info *)NULL);
  334. /* tear down */
  335. png_destroy_write_struct (&png_ptr, (png_info **)NULL);
  336. return true;
  337. }
  338. /* return best type for writing an image (0=mono, 1=grey, 2=color) */
  339. static int
  340. _image_type (miPixel **pixmap, int width, int height)
  341. {
  342. int i, j;
  343. int type = 0; /* default is mono */
  344. for (j = 0; j < height; j++)
  345. for (i = 0; i < width; i++)
  346. {
  347. unsigned char red, green, blue;
  348. red = pixmap[j][i].u.rgb[0];
  349. green = pixmap[j][i].u.rgb[1];
  350. blue = pixmap[j][i].u.rgb[2];
  351. if (type == 0) /* up to now, all pixels are black or white */
  352. {
  353. if (! ((red == (unsigned char)0 && green == (unsigned char)0
  354. && blue == (unsigned char)0)
  355. || (red == (unsigned char)255 && green == (unsigned char)255
  356. && blue == (unsigned char)255)))
  357. {
  358. if (red == green && red == blue)
  359. type = 1; /* need grey */
  360. else
  361. {
  362. type = 2; /* need color */
  363. return type;
  364. }
  365. }
  366. }
  367. else if (type == 1)
  368. {
  369. if (red != green || red != blue)
  370. {
  371. type = 2; /* need color */
  372. return type;
  373. }
  374. }
  375. }
  376. return type;
  377. }
  378. /* custom error and warning handlers (for stdio) */
  379. static void
  380. _our_error_fn_stdio (png_struct *png_ptr, const char *data)
  381. {
  382. FILE *errfp;
  383. errfp = (FILE *)png_get_error_ptr (png_ptr);
  384. if (errfp)
  385. {
  386. #ifdef PTHREAD_SUPPORT
  387. #ifdef HAVE_PTHREAD_H
  388. /* lock the message subsystem */
  389. pthread_mutex_lock (&_message_mutex);
  390. #endif
  391. #endif
  392. fprintf (errfp, "libplot: libpng error: %s\n", data);
  393. #ifdef PTHREAD_SUPPORT
  394. #ifdef HAVE_PTHREAD_H
  395. /* unlock the message subsystem */
  396. pthread_mutex_unlock (&_message_mutex);
  397. #endif
  398. #endif
  399. }
  400. longjmp (png_ptr->jmpbuf, 1);
  401. }
  402. static void
  403. _our_warn_fn_stdio (png_struct *png_ptr, const char *data)
  404. {
  405. FILE *errfp;
  406. errfp = (FILE *)png_get_error_ptr (png_ptr);
  407. if (errfp)
  408. {
  409. #ifdef PTHREAD_SUPPORT
  410. #ifdef HAVE_PTHREAD_H
  411. /* lock the message subsystem */
  412. pthread_mutex_lock (&_message_mutex);
  413. #endif
  414. #endif
  415. fprintf (errfp, "libplot: libpng: %s\n", data);
  416. #ifdef PTHREAD_SUPPORT
  417. #ifdef HAVE_PTHREAD_H
  418. /* unlock the message subsystem */
  419. pthread_mutex_unlock (&_message_mutex);
  420. #endif
  421. #endif
  422. }
  423. }
  424. #ifdef LIBPLOTTER
  425. /* custom write and flush functions (for streams) */
  426. static void
  427. _our_write_fn (png_struct *png_ptr, png_byte *data, png_size_t length)
  428. {
  429. ostream *stream;
  430. stream = (ostream *)png_get_io_ptr (png_ptr);
  431. stream->write ((const char *)data, length);
  432. }
  433. static void
  434. _our_IO_flush_fn (png_struct *png_ptr)
  435. {
  436. ostream *stream;
  437. stream = (ostream *)png_get_io_ptr (png_ptr);
  438. stream->flush ();
  439. }
  440. /* custom error and warning handlers (for streams) */
  441. static void
  442. _our_error_fn_stream (png_struct *png_ptr, const char *data)
  443. {
  444. ostream *errstream;
  445. errstream = (ostream *)png_get_error_ptr (png_ptr);
  446. if (errstream)
  447. {
  448. #ifdef PTHREAD_SUPPORT
  449. #ifdef HAVE_PTHREAD_H
  450. /* lock the message subsystem */
  451. pthread_mutex_lock (&_message_mutex);
  452. #endif
  453. #endif
  454. (*errstream) << "libplot: libpng error: " << data << 'n';
  455. #ifdef PTHREAD_SUPPORT
  456. #ifdef HAVE_PTHREAD_H
  457. /* unlock the message subsystem */
  458. pthread_mutex_unlock (&_message_mutex);
  459. #endif
  460. #endif
  461. }
  462. longjmp (png_ptr->jmpbuf, 1);
  463. }
  464. static void
  465. _our_warn_fn_stream (png_struct *png_ptr, const char *data)
  466. {
  467. ostream *errstream;
  468. errstream = (ostream *)png_get_error_ptr (png_ptr);
  469. if (errstream)
  470. {
  471. #ifdef PTHREAD_SUPPORT
  472. #ifdef HAVE_PTHREAD_H
  473. /* lock the message subsystem */
  474. pthread_mutex_lock (&_message_mutex);
  475. #endif
  476. #endif
  477. (*errstream) << "libplot: libpng: " << data << 'n';
  478. #ifdef PTHREAD_SUPPORT
  479. #ifdef HAVE_PTHREAD_H
  480. /* unlock the message subsystem */
  481. pthread_mutex_unlock (&_message_mutex);
  482. #endif
  483. #endif
  484. }
  485. }
  486. #endif /* LIBPLOTTER */