-
Notifications
You must be signed in to change notification settings - Fork 1
/
png.c
268 lines (192 loc) · 5.85 KB
/
png.c
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
/*
-------------------------------------------------------------------------
OBJECT NAME: png.c
FULL NAME: PNG callback
ENTRY POINTS: SavePNG()
savePNG()
confirmPNG()
STATIC FNS: _SavePNG()
DESCRIPTION:
REFERENCES:
REFERENCED BY: XtAppMainLoop()
COPYRIGHT: University Corporation for Atmospheric Research, 2000-2001
-------------------------------------------------------------------------
*/
#ifdef PNG
#include "define.h"
#include <unistd.h>
#include <png.h>
#include <X11/Xlib.h>
int NumberOfColors(), GetColorIndex(uint32_t);
void CheckByteSwap(XImage *image);
unsigned short *GetColorRGB_X(int);
static void _SavePNG(char file_name[], XImage *image);
static char pngDirectory[256] = "*.png";
static int saving = MAIN_CANVAS;
/* -------------------------------------------------------------------- */
void savePNG(Widget w, XtPointer client, XtPointer call)
{
XImage *image;
PLOT_INFO *plot = 0;
char *p;
strcpy(pngDirectory, outFile);
if ((p = strrchr(pngDirectory, '/')))
strcpy(p+1, "*.png");
switch (saving)
{
case SPEC_CANVAS:
plot = &specPlot;
image = XGetImage(plot->dpy, plot->win, 0, 0,
plot->x.windowWidth, plot->x.windowHeight,
(unsigned long)AllPlanes, ZPixmap);
break;
case DIFF_CANVAS:
plot = &diffPlot;
image = XGetImage(plot->dpy, plot->win, 0, 0,
plot->x.windowWidth, plot->x.windowHeight,
(unsigned long)AllPlanes, ZPixmap);
break;
default:
switch (PlotType)
{
case TIME_SERIES:
plot = &mainPlot[0];
break;
case XY_PLOT:
plot = &xyyPlot[0];
break;
case XYZ_PLOT:
plot = &xyzPlot;
break;
}
image = XGetImage(plot->dpy, plot->win, 0, 20,
plot->x.windowWidth, plot->x.windowHeight-20,
(unsigned long)AllPlanes, ZPixmap);
}
CheckByteSwap(image);
_SavePNG(outFile, image);
XDestroyImage(image);
/* To save PNG via Imlib.
idata = Imlib_init(plot->dpy);
image = Imlib_create_image_from_drawable(idata, plot->win, 0, 0, 0,
plot->x.windowWidth, plot->x.windowHeight);
Imlib_save_image(idata, image, outFile, (ImlibSaveInfo *)NULL);
Imlib_kill_image(idata, image);
*/
}
/* -------------------------------------------------------------------- */
void confirmPNG(Widget w, XtPointer client, XtPointer call)
{
FileCancel((Widget)NULL, (XtPointer)NULL, (XtPointer)NULL);
ExtractFileName(((XmFileSelectionBoxCallbackStruct *)call)->value, &outFile);
if (strstr(outFile, ".png") == NULL)
strcat(outFile, ".png");
if (access(outFile, F_OK) == 0)
{
snprintf(buffer, BUFFSIZE, "Overwrite file %s", outFile);
WarnUser(buffer, savePNG, NULL);
}
else
savePNG((Widget)NULL, (XtPointer)NULL, (XtPointer)NULL);
}
/* -------------------------------------------------------------------- */
void SavePNG(Widget w, XtPointer client, XtPointer call)
{
saving = MAIN_CANVAS;
QueryFile("Enter filename to write PNG to:", pngDirectory, confirmPNG);
}
/* -------------------------------------------------------------------- */
void SavePNGspec(Widget w, XtPointer client, XtPointer call)
{
saving = SPEC_CANVAS;
QueryFile("Enter filename to write PNG to:", pngDirectory, confirmPNG);
}
/* -------------------------------------------------------------------- */
void SavePNGdiff(Widget w, XtPointer client, XtPointer call)
{
saving = DIFF_CANVAS;
QueryFile("Enter filename to write PNG to:", pngDirectory, confirmPNG);
}
/* -------------------------------------------------------------------- */
static void _SavePNG(char file_name[], XImage *image)
{
int i, j;
FILE *outFP;
unsigned short *s;
png_structp png_ptr;
png_infop info_ptr;
png_bytep row_pointers[2000];
png_color *palette;
if ((outFP = fopen(file_name, "wb")) == NULL)
return;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(png_voidp)NULL, NULL, NULL);
if (!png_ptr) {
fclose(outFP);
return;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(outFP);
return;
}
#if PNG_LIBPNG_VER >= 10500
if (setjmp(png_jmpbuf(png_ptr))) {
#else
if (setjmp(png_ptr->jmpbuf)) {
#endif
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(outFP);
return;
}
png_init_io(png_ptr, outFP);
png_set_IHDR(png_ptr, info_ptr,
image->width, image->height, 8,
PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if ((palette = new png_color[NumberOfColors()]) == NULL)
return;
for (i = 0; i < NumberOfColors(); i++) {
s = GetColorRGB_X(i);
palette[i].red = s[0];
palette[i].green = s[1];
palette[i].blue = s[2];
}
/* Force background to white.
*/
palette[i-1].red = palette[i-1].green = palette[i-1].blue = 0xff;
png_set_PLTE(png_ptr, info_ptr, palette, NumberOfColors());
/* Write header info to PNG file. */
png_write_info(png_ptr, info_ptr);
for (i = 0; i < image->height; ++i)
{
char *p = &(image->data[i * image->bytes_per_line]);
row_pointers[i] = new png_byte[image->width];
switch (image->depth)
{
case 8:
for (j = 0; j < image->width; ++j)
row_pointers[i][j] = (png_byte)GetColorIndex(((char *)p)[j]);
break;
case 16:
for (j = 0; j < image->width; ++j)
row_pointers[i][j] = (png_byte)GetColorIndex(((uint16_t *)p)[j]);
break;
case 24: case 32:
for (j = 0; j < image->width; ++j)
row_pointers[i][j] = (png_byte)GetColorIndex(((uint32_t *)p)[j]);
break;
}
}
/* Write the data! */
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
for (i = 0; i < image->height; ++i)
delete [] row_pointers[i];
delete [] palette;
fclose(outFP);
}
#endif
/* END PNG.C */