File: sarscreenshot.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (355 lines) | stat: -rw-r--r-- 8,003 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#ifdef __MSW__
# include <windows.h>
#else
# include <unistd.h>
#endif
#include "../include/disk.h"
#include "../include/tga.h"
#include "gw.h"
#include "sound.h"
#include "sar.h"
#include "sarscreenshot.h"
#include "config.h"


static char *SARScreenShotFindName(const char *dir);
static int SARScreenShotWriteTGA(
	const char *path,
	int width, int height, int bpl, int bpp,
	const u_int8_t *buf
);
static void SARScreenShotDraw(
        sar_core_struct *core_ptr, int detail_level
);

void SARScreenShotRect(
	sar_core_struct *core_ptr, const char *dir, int detail_level,
        int x, int y, int width, int height
);
void SARScreenShot(
	sar_core_struct *core_ptr, const char *dir, int detail_level
);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? (float)atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)        (((a) > (b)) ? (a) : (b))
#define MIN(a,b)        (((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define STRLEN(s)       (((s) != NULL) ? ((int)strlen(s)) : 0)

#define RADTODEG(r)     ((r) * 180.0 / PI)
#define DEGTORAD(d)     ((d) * PI / 180.0)

#define PLAY_SOUND(p)						\
{ if((opt->event_sounds) && (recorder != NULL)) {		\
 char *full_path = (ISPATHABSOLUTE(p)) ?			\
  STRDUP(p) :							\
  STRDUP(PrefixPaths(dname.global_data, (p))			\
 );								\
 SoundStartPlayVoid(						\
  recorder, full_path, 1.0, 1.0, 0, 0				\
 );								\
 free(full_path);						\
} }

/*
 *	Generates a path to a file that does not exist, suitable as
 *	the path to the new screen shot image file.
 *
 *	The returned path must be free'ed by the calling function.
 */
static char *SARScreenShotFindName(const char *dir)
{
	int i;
	char *path = (char *)malloc((PATH_MAX + NAME_MAX + 1) * sizeof(char));
	char cwd[PATH_MAX];
	struct stat stat_buf;

	if(path == NULL)
	    return(NULL);

	/* Use current working directory if no directory was given. */
	if(dir == NULL)
	{
	    if(getcwd(cwd, PATH_MAX) != NULL)
		cwd[PATH_MAX - 1] = '\0';
	    else
		strcpy(cwd, "/");
	    dir = cwd;
	}

/*
	if(STRLEN(dir) >= PATH_MAX)
	    dir[PATH_MAX] = '\0';
 */

	for(i = 1; i < 1000000; i++)
	{
	    sprintf(
		path, "%s%c%s%i%s",
		dir,
		DIR_DELIMINATOR,
		"sar",
		i,
		".tga"
	    );
#ifdef __MSW__
	    if(stat(path, &stat_buf))
#else
	    if(lstat(path, &stat_buf))
#endif
		break;
	}

	return(path);
}

/*
 *	Writes the given buffer to a TGA image.
 *
 *	Returns non-zero on error.
 */
static int SARScreenShotWriteTGA(
        const char *path,
        int width, int height, int bpl, int bpp,
	const u_int8_t *buf
)
{
	int status, tar_depth;
	u_int8_t *tar_buf;
	tga_data_struct td_data, *td = &td_data;

	if((path == NULL) || (buf == NULL))
	    return(-1);

	if((width < 0) || (height < 0) || (bpp < 1))
	    return(-1);

	memset(td, 0x00, sizeof(tga_data_struct));

	/* Calculate bytes per line as needed */
	if(bpl <= 0)
	    bpl = width * bpp;

	/* Get target depth based on given depth */
	switch(bpp)
	{
	  case 4:
	    tar_depth = 32;
	    break;
          case 3:
            tar_depth = 24;
            break;
          default:	/* 1 bpp */
            tar_depth = 8;
            break;
	}

	/* Allocate target buffer to flip and change color */
	tar_buf = (u_int8_t *)malloc(bpl * height);
	if(tar_buf != NULL)
	{
	    int x, y;
	    u_int8_t *tar_ptr;
	    const u_int8_t *src_ptr;

	    for(y = 0; y < height; y++)
	    {
		for(x = 0; x < width; x++)
		{
		    src_ptr = &buf[
			(bpl * y) + (x * bpp)
		    ];
		    tar_ptr = &tar_buf[
			(bpl * (height - y - 1)) +
			(x * bpp)
		    ];
		    switch(bpp)
		    {
		      case 4:	/* 32 bit src RGBA to tar BGRA */
			tar_ptr[0] = src_ptr[2];
			tar_ptr[1] = src_ptr[1];
			tar_ptr[2] = src_ptr[0];
			tar_ptr[3] = src_ptr[3];
			break;
		      case 3:	/* 32 bit src RGB to tar BGR */
                        tar_ptr[0] = src_ptr[2];
                        tar_ptr[1] = src_ptr[1];
                        tar_ptr[2] = src_ptr[0];
			break;
		      default:
			*tar_ptr = *src_ptr;
			break;
		    }
		}
	    }
	}

	/* Set up TGA data values for writing */
	td->fp = NULL;
	td->x = 0;
	td->y = 0;
	td->width = width;
	td->height = height;
	td->depth = bpp * 8;
	td->bits_per_pixel = td->depth;
	td->header_data = NULL;
	td->data = tar_buf;
	td->data_depth = bpp * 8;

	/* Write TGA image */
	status = TgaWriteToFile(path, td, tar_depth);

	/* Delete TGA values and the target buffer */
	TgaDestroyData(td);

	return((status == TgaSuccess) ? 0 : -1);
}


/*
 *	Redraw the scene as needed based on the detail level.
 *
 *	If in the menus then nothing will be redrawn.
 */
static void SARScreenShotDraw(
	sar_core_struct *core_ptr, int detail_level
)
{
	/* In menus? */
	if(core_ptr->cur_menu < 0)
	{	
	    return;
	}







}


/*
 *	Performs a screenshot of a rectangular area of the current GL
 *	frame buffer.
 *
 *	x, y, width, and height geometry values are in window
 *	coordinates with the origin in the upper left corner.
 *
 *	detail_level specifies the amount of detail, where 0 is the
 *	current GL frame buffer as is.  For values 1 and higher, the
 *	scene may be redrawn with additional graphics options enabled.
 */
void SARScreenShotRect(
	sar_core_struct *core_ptr, const char *dir, int detail_level,
        int x, int y, int width, int height
)
{
	char *path;
	int status;
	int bpp = 4, fb_width, fb_height;
	GLubyte *gl_buf;
        gw_display_struct *display = (core_ptr != NULL) ?
            core_ptr->display : NULL;
	snd_recorder_struct *recorder = (core_ptr != NULL) ?
	    core_ptr->recorder : NULL;
	const sar_option_struct *opt;
        if(display == NULL)
            return;

	opt = &core_ptr->option;

	if((width < 1) || (height < 1))
	    return;

	/* Redraw the scene as needed based on the detail level */
	SARScreenShotDraw(core_ptr, detail_level);

	/* At this point the GL frame buffer has been drawn or not
	 * drawn, either way we are now ready to read the GL frame
	 * buffer.
	 */

	/* Allocate the GL read buffer */
	gl_buf = (GLubyte *)malloc(width * height * bpp * sizeof(GLubyte));
	if(gl_buf == NULL)
	    return;

	/* Get actual size of frame buffer */
        GWContextGet(
            display, GWContextCurrent(display),
            NULL, NULL,
            NULL, NULL,
            &fb_width, &fb_height
        );

	/* Play initial click sound just before snapping screen */
	PLAY_SOUND(SAR_DEF_SOUND_SCREENSHOT);
	GWSetInputBusy(display);

	/* If the GL buffer is double buffered then use the back buffer,
	 * otherwise we use the front buffer.
	 */
	if(display->has_double_buffer)
	    glReadBuffer(GL_BACK);
	else
	    glReadBuffer(GL_FRONT);
	glReadPixels(
	    MAX(x, 0),
	    MAX(fb_height - (y + height), 0),
	    width, height,
	    (bpp == 4) ? GL_RGBA : ((bpp == 3) ? GL_RGB : GL_LUMINANCE),
	    GL_UNSIGNED_BYTE,
	    gl_buf
	);

	/* Generate a file name for the new image, the file name will
	 * be that of a file that does not exist.
	 */
	path = SARScreenShotFindName(dir);

	/* Write the read GL buffer to a TGA image file */
	status = SARScreenShotWriteTGA(path, width, height, 0, bpp, gl_buf);
	free(path);

	/* Delete the GL read buffer since it is no longer needed */
	free(gl_buf);

        /* Play finish click sound just after snapping screen */
	PLAY_SOUND(SAR_DEF_SOUND_ERROR);
        GWSetInputReady(display);
}

/*
 *	Performs a screenshot of the current GL frame buffer.
 */
void SARScreenShot(
	sar_core_struct *core_ptr, const char *dir, int detail_level
)
{
	int width, height;
	gw_display_struct *display = (core_ptr != NULL) ?
	    core_ptr->display : NULL;
	if(display == NULL)
	    return;

	GWContextGet(
	    display, GWContextCurrent(display),
	    NULL, NULL,
	    NULL, NULL,
	    &width, &height
	);

	SARScreenShotRect(core_ptr, dir, detail_level, 0, 0, width, height);
}