Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/src/plugins/savetiff.c
3203 views
1
// savetiff.c
2
//
3
// Module for compressing and saving ElmerPost-pictures in TIFF-format
4
//
5
// Compile e.g. as follows:
6
//
7
// MinGW: gcc -Wall -shared -O -o savetiff.dll savetiff.c -lopengl32 -ltcl84 -ltiff
8
//
9
// Copy the shared library into $ELMER_POST_HOME/modules and ruin ElmerPost
10
//
11
// Usage:
12
//
13
// Elmer-Post: savetiff file
14
//
15
// Defaults:
16
//
17
// file = elmerpost.tif
18
//
19
// Modified from screensave.c
20
//
21
// Written by: ML, 08. Jan. 2008
22
23
#if defined(WIN32) || defined(win32)
24
#include <windows.h>
25
#endif
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <GL/gl.h>
31
#include <tcl.h>
32
#include <tiffio.h>
33
34
#if !(defined(WIN32) || defined(win32))
35
#include <errno.h>
36
extern int errno;
37
#endif
38
39
static int SaveTIFF( ClientData cl,Tcl_Interp *interp,int argc,char **argv ) {
40
unsigned char *buffer;
41
char fname[256];
42
int nx, ny, ox, oy, viewp[4];
43
TIFF *image;
44
int y, stride;
45
46
// Determine file name:
47
//---------------------
48
if( argc < 2 ) {
49
strcpy( fname, "elmerpost.tif" );
50
} else {
51
strncpy( fname,argv[1], 256 );
52
}
53
54
image = TIFFOpen( fname, "w" );
55
if( image==NULL ) {
56
#if defined(WIN32) || defined(win32)
57
sprintf( interp->result, "savetiff: can't open [%s] for writing!\n",fname );
58
#else
59
sprintf( interp->result, "savetiff: can't open [%s] for writing:\n%s\n",
60
fname, strerror(errno) );
61
#endif
62
return TCL_ERROR;
63
}
64
65
// Determine picture size:
66
//------------------------
67
glGetIntegerv( GL_VIEWPORT, viewp );
68
ox = viewp[0];
69
oy = viewp[1];
70
nx = viewp[2]+1;
71
ny = viewp[3]+1;
72
73
// Allocate buffer:
74
//------------------
75
buffer = (unsigned char *) malloc( nx*ny*3 );
76
if ( buffer==NULL ) {
77
TIFFClose( image );
78
#if defined(WIN32) || defined(win32)
79
sprintf( interp->result, "savetiff: can't allocate memory!\n" );
80
#else
81
sprintf( interp->result, "savetiff: can't allocate memory:\n%s\n",
82
strerror(errno) );
83
#endif
84
return TCL_ERROR;
85
}
86
87
fprintf( stdout, "Saving %s ... ", fname );
88
fflush( stdout );
89
90
// Copy RGB-data into buffer:
91
//----------------------------
92
glReadBuffer( GL_FRONT );
93
glReadPixels( ox, oy, nx, ny, GL_RGB, GL_UNSIGNED_BYTE, buffer );
94
95
// Flip the picture:
96
//------------------
97
stride = 3*nx;
98
for( y=0; y<ny/2; y++ ) {
99
unsigned char *r1 = buffer + stride*y;
100
unsigned char *r2 = buffer + stride*(ny-1-y);
101
memcpy( buffer, r1, stride );
102
memcpy( r1, r2, stride );
103
memcpy( r2, buffer, stride );
104
}
105
106
TIFFSetField( image, TIFFTAG_IMAGEWIDTH, nx );
107
TIFFSetField( image, TIFFTAG_IMAGELENGTH, ny );
108
TIFFSetField( image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE );
109
TIFFSetField( image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
110
TIFFSetField( image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB );
111
TIFFSetField( image, TIFFTAG_BITSPERSAMPLE, 8 );
112
TIFFSetField( image, TIFFTAG_SAMPLESPERPIXEL, 3 );
113
114
if( TIFFWriteEncodedStrip( image, 0, buffer, nx*ny*3) == 0 ) {
115
TIFFClose( image );
116
#if defined(WIN32) || defined(win32)
117
sprintf( interp->result, "savetiff: unable to encode picture\n" );
118
#else
119
sprintf( interp->result, "savetiff: unable to encode picture\n%s\n",
120
strerror(errno) );
121
#endif
122
free( buffer );
123
return TCL_ERROR;
124
}
125
126
TIFFClose( image );
127
free( buffer );
128
129
fprintf( stdout, "done\n");
130
fflush( stdout );
131
132
return TCL_OK;
133
}
134
135
#if defined(WIN32) || defined(win32)
136
__declspec(dllexport)
137
#endif
138
139
int Savetiff_Init( Tcl_Interp *interp ) {
140
Tcl_CreateCommand( interp, "savetiff", (Tcl_CmdProc *)SaveTIFF,
141
(ClientData)NULL, (Tcl_CmdDeleteProc *)NULL );
142
return TCL_OK;
143
}
144
145