File: z_write.c

package info (click to toggle)
plotutils 2.6-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 13,184 kB
  • sloc: ansic: 68,670; sh: 20,086; cpp: 12,382; yacc: 2,588; makefile: 838; lex: 137
file content (552 lines) | stat: -rw-r--r-- 16,069 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.

   The GNU plotutils package is free software.  You may redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software foundation; either version 2, or (at your
   option) any later version.

   The GNU plotutils package is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with the GNU plotutils package; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
   Boston, MA 02110-1301, USA. */

/* This file contains a special version of the function
   _maybe_output_image, which is called by the BitmapPlotter closepl method
   (see b_closepl.c).  This version is for the PNGPlotter subclass:
   provided that the current page of graphics is the first, it writes out a
   PNG file. */

/* Note: this calls gmtime(), which is not reentrant.  For thread-safety we
   should use gmtime_r() instead, but declaring it portably is a hard
   problem (cf. comments at head of p_defplot.c and c_defplot.c). */

/* Other than this egregious problem, this code is thread-safe (the warning
   and error handlers which we pass to libpng lock and unlock the `message
   mutex' defined in g_error.c).  Since libpng uses callbacks, warning and
   error messages aren't produced simply by calling the functions
   _plotter->warning() and _plotter->error() defined in g_error.c. */

/* Note: we should improve this code to support the use of user-specified
   warning and error handlers for libplot; cf. the code in g_error.c.  But
   their use isn't documented yet. */

#include "sys-defines.h"
#include "extern.h"
#include "xmi.h"

#include <png.h>

/* song and dance to define time_t, and declare both time() and gmtime() */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>		/* for time_t on some pre-ANSI Unix systems */
#endif
#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>		/* for time() on some pre-ANSI Unix systems */
#include <time.h>		/* for gmtime() */
#else  /* not TIME_WITH_SYS_TIME, include only one (prefer <sys/time.h>) */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else  /* not HAVE_SYS_TIME_H */
#include <time.h>
#endif /* not HAVE_SYS_TIME_H */
#endif /* not TIME_WITH_SYS_TIME */

/* Mutex for locking the warning/error message subsystem.  Defined in
   g_error.c */
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
extern pthread_mutex_t _message_mutex;
#endif
#endif

static const char _short_months[12][4] = 
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

/* forward references */
static int _image_type (miPixel **pixmap, int width, int height);
static void _our_error_fn_stdio (png_struct *png_ptr, const char *data);
static void _our_warn_fn_stdio (png_struct *png_ptr, const char *data);
#ifdef LIBPLOTTER
static void _our_IO_flush_fn (png_struct *png_ptr);
static void _our_error_fn_stream (png_struct *png_ptr, const char *data);
static void _our_warn_fn_stream (png_struct *png_ptr, const char *data);
static void _our_write_fn (png_struct *png_ptr, png_byte *data, png_size_t length);
#endif /* LIBPLOTTER */

int
_pl_z_maybe_output_image (S___(Plotter *_plotter))
{
  miPixel **pixmap;		/* pixmap in miCanvas */
  int width, height;
  int image_type, bit_depth, color_type;
  png_struct *png_ptr;
  png_info *info_ptr;
  char time_buf[32], software_buf[64];
  png_text text_ptr[10];
  time_t clock;
  struct tm *tmsp;
  FILE *fp = _plotter->data->outfp;
  FILE *errorfp = _plotter->data->errfp;
  void *error_ptr;
  png_error_ptr error_fn_ptr, warn_fn_ptr;
#ifdef LIBPLOTTER
  ostream *stream = _plotter->data->outstream;
  ostream *errorstream = _plotter->data->errstream;
#endif

#ifdef LIBPLOTTER
  if (fp == (FILE *)NULL && stream == (ostream *)NULL)
    return 0;
#else
  if (fp == (FILE *)NULL)
    return 0;
#endif
  
  /* Output the page as a PNG file, but only if it's page #1, since PNG
     format supports only a single page of graphics. */
  if (_plotter->data->page_number != 1)
    return 0;

  /* work out libpng error handling (i.e. callback functions and data) */
#ifdef LIBPLOTTER
  if (errorstream)
    {
      error_fn_ptr = _our_error_fn_stream;
      warn_fn_ptr = _our_warn_fn_stream;
      error_ptr = (void *)errorstream;
    }
  else if (errorfp)
    {
      error_fn_ptr = _our_error_fn_stdio;
      warn_fn_ptr = _our_warn_fn_stdio;
      error_ptr = (void *)errorfp;
    }
  else
    {
      error_fn_ptr = NULL;
      warn_fn_ptr = NULL;
      error_ptr = (void *)NULL;
    }
#else  /* not LIBPLOTTER */
  if (errorfp)
    {
      error_fn_ptr = _our_error_fn_stdio;
      warn_fn_ptr = _our_warn_fn_stdio;
      error_ptr = (void *)errorfp;
    }
  else
    {
      error_fn_ptr = NULL;
      warn_fn_ptr = NULL;
      error_ptr = (void *)NULL;
    }
#endif /* not LIBPLOTTER */

  /* create png_struct, install error/warning handlers */
  png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
				     error_ptr, 
				     error_fn_ptr, warn_fn_ptr);
  if (png_ptr == (png_struct *)NULL)
    return -1;

  /* allocate/initialize image information data */
  info_ptr = png_create_info_struct (png_ptr);
  if (info_ptr == (png_info *)NULL)
    {
      png_destroy_write_struct (&png_ptr, (png_info **)NULL);
      return -1;
    }

  /* cleanup after libpng errors (error handler does a longjmp) */
  if (setjmp (png_jmpbuf (png_ptr)))
    {
      png_destroy_write_struct (&png_ptr, (png_info **)NULL);
      return -1;
    }
  
#ifdef LIBPLOTTER
  if (stream)
    {
      /* use custom write and flush functions, defined below */
      png_set_write_fn (png_ptr, 
			(void *)stream,
			(png_rw_ptr)_our_write_fn, 
			(png_flush_ptr)_our_IO_flush_fn);
    }
  else
    /* must have fp!=NULL, so use default stdio-based output */
    png_init_io (png_ptr, fp);

#else  /* not LIBPLOTTER */
    /* use default stdio-based output */
    png_init_io (png_ptr, fp);
#endif /* not LIBPLOTTER */

  /* extract pixmap (2D array of miPixels) from miCanvas */
  pixmap = ((miCanvas *)(_plotter->b_canvas))->drawable->pixmap;

  /* what is best image type that can be used?  0/1/2 = mono/gray/rgb */
  width = _plotter->b_xn;
  height = _plotter->b_yn;
  image_type = _image_type (pixmap, width, height);
  switch (image_type)
    {
    case 0:			/* mono */
      bit_depth = 1;
      color_type = PNG_COLOR_TYPE_GRAY;
      break;
    case 1:			/* gray */
      bit_depth = 8;
      color_type = PNG_COLOR_TYPE_GRAY;
      break;
    case 2:			/* rgb */
    default:
      bit_depth = 8;
      color_type = PNG_COLOR_TYPE_RGB;
      break;
    }

  /* Set image information in file header.  Width and height are up to
     2^31, bit_depth is one of 1, 2, 4, 8, or 16, but valid values also
     depend on the color_type selected. color_type is one of
     PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA,
     PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, or
     PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
     PNG_INTERLACE_ADAM7.  compression_type and filter_type MUST currently
     be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.  */
  png_set_IHDR (png_ptr, info_ptr, 
		(unsigned int)width, (unsigned int)height, 
		bit_depth, color_type,
		_plotter->z_interlace ? PNG_INTERLACE_ADAM7 
		                      : PNG_INTERLACE_NONE,
		PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  
  /* set transparent color (if user specified one) */
  if (_plotter->z_transparent)
    {
      plColor transparent_color = _plotter->z_transparent_color;
      bool transparent_color_ok = true;
      png_color_16 trans_value;
      
      switch (image_type)
	{
	case 0:			/* mono */
	  if ((transparent_color.red != 0 && transparent_color.red != 0xffff)
	      || 
	      (transparent_color.green != 0 && transparent_color.green != 0xffff)
	      || 
	      (transparent_color.blue != 0 && transparent_color.blue != 0xffff)
	      ||
	      (transparent_color.red != transparent_color.green
	       || transparent_color.red != transparent_color.blue))
	    /* user-specified transparent color isn't monochrome */
	    transparent_color_ok = false;
	  else
	    trans_value.gray = (png_uint_16)transparent_color.red;
	  break;
	case 1:			/* gray */
	  if (transparent_color.red != transparent_color.green
	      || transparent_color.red != transparent_color.blue)
	    /* user-specified transparent color isn't grayscale */
	    transparent_color_ok = false;
	  else
	    trans_value.gray = (png_uint_16)transparent_color.red;
	  break;
	case 2:			/* rgb */
	default:
	  trans_value.red = (png_uint_16)transparent_color.red;
	  trans_value.green = (png_uint_16)transparent_color.green;
	  trans_value.blue = (png_uint_16)transparent_color.blue;
	  break;
	}
      if (transparent_color_ok)
	png_set_tRNS (png_ptr, info_ptr, (png_byte *)NULL, 1, &trans_value);
    }

  /* add some comments to file header */
  text_ptr[0].key = (char *)"Title";
  text_ptr[0].text = (char *)"PNG plot";
  text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
  text_ptr[1].key = (char *)"Creation Time";
  time (&clock);
  tmsp = gmtime (&clock);
  sprintf (time_buf, 
	   "%d %s %d %02d:%02d:%02d +0000", /* RFC 1123 date */
	   (tmsp->tm_mday) % 31, 
	   _short_months[(tmsp->tm_mon) % 12], 1900 + tmsp->tm_year, 
	   (tmsp->tm_hour) % 24, (tmsp->tm_min) % 60, (tmsp->tm_sec) % 61);
  text_ptr[1].text = time_buf;
  text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
  text_ptr[2].key = (char *)"Software";
  sprintf (software_buf, "GNU libplot drawing library %s",
	   PL_LIBPLOT_VER_STRING);
  text_ptr[2].text = software_buf;
  text_ptr[2].compression = PNG_TEXT_COMPRESSION_NONE;

  png_set_text(png_ptr, info_ptr, text_ptr, 3);
  
  /* write out PNG file header */
  png_write_info (png_ptr, info_ptr);
  
  /* Write out image data, a row at a time; support multiple passes over
     image if interlacing.  We don't simply call png_write_image() because
     the image in the miCanvas's pixmap is a 2-D array of miPixels, and
     sizeof(miPixel) > 4 is possible.  Instead we copy each miPixel in a
     row into a local row buffer, and write out the row buffer. */
  {
    png_byte *rowbuf;
    int num_passes, pass;

    switch (image_type)
      {
      case 0:			/* mono */
	rowbuf = (png_byte *)_pl_xmalloc(((width + 7)/8) * sizeof(png_byte));
	break;
      case 1:			/* gray */
	rowbuf = (png_byte *)_pl_xmalloc(width * sizeof(png_byte));	
	break;
      case 2:			/* rgb */
      default:
	rowbuf = (png_byte *)_pl_xmalloc(3 * width * sizeof(png_byte));
	break;
      }

    if (_plotter->z_interlace)
      /* turn on interlace handling; if interlacing, need >1 pass over image */
      num_passes = png_set_interlace_handling (png_ptr);
    else
      num_passes = 1;

    for (pass = 0; pass < num_passes; pass++)
      {
	int i, j;

	for (j = 0; j < height; j++)
	  {
	    png_byte *ptr;

	    /* fill row buffer with 3 bytes per miPixel (RGB), or 1 byte
	       (gray), or 1 bit (mono) */
	    ptr = rowbuf;
	    for (i = 0; i < width; i++)
	      {
		switch (image_type)
		  {
		  case 0:	/* mono */
		    if (i % 8 == 0)
		      {
			if (i != 0)
			  ptr++;
			*ptr = (png_byte)0;
		      }
		    if (pixmap[j][i].u.rgb[0]) /* white pixel */
		      *ptr |= (1 << (7 - (i % 8)));
		    break;
		  case 1:	/* gray */
		    *ptr++ = (png_byte)pixmap[j][i].u.rgb[0];
		    break;
		  case 2:	/* rgb */
		  default:
		    *ptr++ = (png_byte)pixmap[j][i].u.rgb[0];
		    *ptr++ = (png_byte)pixmap[j][i].u.rgb[1];
		    *ptr++ = (png_byte)pixmap[j][i].u.rgb[2];
		    break;
		  }
	      }
	    
	    /* write out row buffer */
	    png_write_rows (png_ptr, &rowbuf, 1);
	  }
      }

    free (rowbuf);
  }

  /* write out PNG file trailer (could add more comments here) */
  png_write_end (png_ptr, (png_info *)NULL);

  /* tear down */
  png_destroy_write_struct (&png_ptr, (png_info **)NULL);

  return true;
}

/* return best type for writing an image (0=mono, 1=grey, 2=color) */
static int
_image_type (miPixel **pixmap, int width, int height)
{
  int i, j;
  int type = 0;			/* default is mono */
  
  for (j = 0; j < height; j++)
    for (i = 0; i < width; i++)
      {
	unsigned char red, green, blue;
	
	red = pixmap[j][i].u.rgb[0];
	green = pixmap[j][i].u.rgb[1];
	blue = pixmap[j][i].u.rgb[2];
	if (type == 0)		/* up to now, all pixels are black or white */
	  {
	    if (! ((red == (unsigned char)0 && green == (unsigned char)0
		    && blue == (unsigned char)0)
		   || (red == (unsigned char)255 && green == (unsigned char)255
		    && blue == (unsigned char)255)))
	      {
		if (red == green && red == blue)
		  type = 1;	/* need grey */
		else
		  {
		    type = 2;	/* need color */
		    return type;
		  }
	      }
	  }
	else if (type == 1)
	  {
	    if (red != green || red != blue)
	      {
		type = 2;	/* need color */
		return type;
	      }
	  }
      }
  return type;
}

/* custom error and warning handlers (for stdio) */
static void 
_our_error_fn_stdio (png_struct *png_ptr, const char *data)
{
  FILE *errfp;

  errfp = (FILE *)png_get_error_ptr (png_ptr);
  if (errfp)
    {
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* lock the message subsystem */
      pthread_mutex_lock (&_message_mutex);
#endif
#endif
      fprintf (errfp, "libplot: libpng error: %s\n", data);
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* unlock the message subsystem */
      pthread_mutex_unlock (&_message_mutex);
#endif
#endif
    }

#if (PNG_LIBPNG_VER < 10500)
  longjmp (png_ptr->jmpbuf, 1);
#else
  png_longjmp (png_ptr, 1);
#endif
}

static void 
_our_warn_fn_stdio (png_struct *png_ptr, const char *data)
{
  FILE *errfp;

  errfp = (FILE *)png_get_error_ptr (png_ptr);
  if (errfp)
    {
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* lock the message subsystem */
      pthread_mutex_lock (&_message_mutex);
#endif
#endif
      fprintf (errfp, "libplot: libpng: %s\n", data);
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* unlock the message subsystem */
      pthread_mutex_unlock (&_message_mutex);
#endif
#endif
    }
}

#ifdef LIBPLOTTER
/* custom write and flush functions (for streams) */
static void 
_our_write_fn (png_struct *png_ptr, png_byte *data, png_size_t length)
{
  ostream *stream;

  stream = (ostream *)png_get_io_ptr (png_ptr);
  stream->write ((const char *)data, length);
}

static void 
_our_IO_flush_fn (png_struct *png_ptr)
{
  ostream *stream;

  stream = (ostream *)png_get_io_ptr (png_ptr);
  stream->flush ();
}

/* custom error and warning handlers (for streams) */
static void 
_our_error_fn_stream (png_struct *png_ptr, const char *data)
{
  ostream *errstream;

  errstream = (ostream *)png_get_error_ptr (png_ptr);
  if (errstream)
    {
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* lock the message subsystem */
      pthread_mutex_lock (&_message_mutex);
#endif
#endif
      (*errstream) << "libplot: libpng error: " << data << 'n';
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* unlock the message subsystem */
      pthread_mutex_unlock (&_message_mutex);
#endif
#endif
    }

#if (PNG_LIBPNG_VER < 10500)
  longjmp (png_ptr->jmpbuf, 1);
#else
  png_longjmp (png_ptr, 1);
#endif
}

static void 
_our_warn_fn_stream (png_struct *png_ptr, const char *data)
{
  ostream *errstream;

  errstream = (ostream *)png_get_error_ptr (png_ptr);
  if (errstream)
    {
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* lock the message subsystem */
      pthread_mutex_lock (&_message_mutex);
#endif
#endif
      (*errstream) << "libplot: libpng: " << data << 'n';
#ifdef PTHREAD_SUPPORT
#ifdef HAVE_PTHREAD_H
      /* unlock the message subsystem */
      pthread_mutex_unlock (&_message_mutex);
#endif
#endif
    }
}
#endif /* LIBPLOTTER */