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
|
$NetBSD: patch-ae,v 1.2 2011/01/14 10:55:42 wiz Exp $
Support png screenshots.
--- imgsav.c.orig 1999-09-26 14:35:06.000000000 +0000
+++ imgsav.c
@@ -29,6 +29,8 @@
/* ******************** Include Files ************** */
+#include <png.h>
+#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -254,6 +256,96 @@ void TVIMGSAVDoSaveTIFF( char filename[]
}
+void TVIMGSAVDoSavePNG( char filename[], TV_IMAGE *img )
+{
+ char errmsg[160];
+ png_structp png_ptr;
+ png_infop info_ptr;
+ FILE *out;
+ TV_INT32 linebytes,
+ y,
+ pass,
+ compon;
+ TV_UINT8 *buf;
+
+ buf = NULL;
+ if ( img->pix_geom.type != TV_PIXELTYPE_RGB ) {
+ fprintf( stderr, "Attempt to save non-RGB data as TIFF\n" );
+ exit(1);
+ }
+
+ /* Open output file */
+ if ( (out = fopen( filename, "wb" )) == NULL ) {
+ sprintf( errmsg, "Can't open output file '%s'", filename );
+ XUTILDialogPause( TVTOPLEVEL, "Error", errmsg, TV_DIALOG_TYPE_OK );
+ return;
+ }
+
+ /* Create PNG data structures and initialize */
+ png_ptr = png_create_write_struct
+ (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr) {
+ sprintf( errmsg, "Can't create PNG write structure" );
+ XUTILDialogPause( TVTOPLEVEL, "Error", errmsg, TV_DIALOG_TYPE_OK );
+ return;
+ }
+ info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_write_struct(&png_ptr, NULL);
+ sprintf( errmsg, "Can't create PNG info structure" );
+ XUTILDialogPause( TVTOPLEVEL, "Error", errmsg, TV_DIALOG_TYPE_OK );
+ return;
+ }
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ fprintf( stderr, "PNG write error\n" );
+ XBell( TVDISPLAY, 100 );
+ png_destroy_write_struct( &png_ptr, &info_ptr );
+ free( buf );
+ fclose( out );
+ unlink( filename );
+ return;
+ }
+ png_init_io(png_ptr, out);
+ /* fast compression */
+ png_set_compression_level(png_ptr, Z_BEST_SPEED);
+
+ /* Setup image format info (chunks) */
+ png_set_IHDR(png_ptr, info_ptr, img->geom.w, img->geom.h,
+ TV_BITS_PER_COMP, PNG_COLOR_TYPE_RGB /* XXX: ? */,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+ /* and write it */
+ png_write_info(png_ptr, info_ptr);
+ /* flush image out every 50 rows */
+ png_set_flush(png_ptr, 50);
+
+ linebytes = img->geom.w * TV_BYTES_PER_PIX;
+
+ if ( (buf = malloc( linebytes )) == NULL )
+ TVUTILOutOfMemory();
+
+ /* Convert & write the image data */
+ for ( pass = 0; pass < 1; pass++ ) {
+ compon = DoRed | DoGreen | DoBlue;
+ for ( y = 0; y < img->geom.h; y++ ) {
+
+ /* Format scanline */
+ TVIMGSAVFmtScanline24bpp( img, y, compon, buf );
+
+ /* And write it in PNG */
+ png_write_row(png_ptr, buf);
+ }
+ }
+
+ /* All done. Close up shop and go home */
+ png_write_end(png_ptr, NULL);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ fclose(out);
+ free( buf );
+}
+
+
void TVIMGSAVDoSavePPM( char filename[], TV_IMAGE *img )
{
static char *S_fp_buf = NULL;
@@ -408,6 +500,9 @@ void TVIMGSAVDoSave( char filename[], TV
}
TVIMGSAVDoSaveTIFF( filename, img ); break;
+ case TV_STILL_FMT_PNG :
+ TVIMGSAVDoSavePNG ( filename, img ); break;
+
case TV_STILL_FMT_PPM :
TVIMGSAVDoSavePPM ( filename, img ); break;
|