31#if !defined(FRONTEND_DOES_SNAPSHOTS)
37#include <libFreeWRL.h>
39#include "../vrml_parser/Structs.h"
41#include "../vrml_parser/CParseGeneral.h"
42#include "../world_script/CScripts.h"
44#include "../scenegraph/Collision.h"
45#include "../scenegraph/quaternion.h"
46#include "../scenegraph/Viewer.h"
47#include "../input/SensInterps.h"
48#include "../x3d_parser/Bindable.h"
65 const char *default_seqtmp;
72void *Snapshot_constructor()
74 void *v = MALLOCV(
sizeof(
struct pSnapshot));
79void Snapshot_init(
struct tSnapshot* t)
82 t->doSnapshot = FALSE;
84 t->prv = Snapshot_constructor();
86 ppSnapshot p = (ppSnapshot)t->prv;
93 p->default_seqtmp =
"freewrl_tmp";
95 p->doSnapshot = FALSE;
96 p->doPrintshot = FALSE;
97 p->savedSnapshot = FALSE;
98 p->modeTesting = FALSE;
104void set_snapshotModeTesting(
int value)
106 ppSnapshot p = (ppSnapshot)gglobal()->Snapshot.prv;
107 p->modeTesting = value;
109int isSnapshotModeTesting()
111 struct tSnapshot* t = &gglobal()->Snapshot;
113 return p->modeTesting;
117void fwl_set_SnapFile(
const char* file)
119 ppSnapshot p = (ppSnapshot)gglobal()->Snapshot.prv;
121 p->snapsnapB = STRDUP(file);
122 TRACE_MSG(
"snapsnapB set to %s\n", p->snapsnapB);
123 printf(
"%s\n",p->snapsnapB);
126void fwl_set_SnapTmp(
const char* file)
129 ttglobal tg = gglobal();
130 tg->Snapshot.doSnapshot = FALSE;
132 ppSnapshot p = (ppSnapshot)tg->Snapshot.prv;
133 p->seqtmp = STRDUP(file);
134 TRACE_MSG(
"seqtmp set to %s\n", p->seqtmp);
142static char * grabScreen(
int bytesPerPixel,
int x,
int y,
int width,
int height)
147 if(bytesPerPixel == 3) pixelType = GL_RGB;
148 if(bytesPerPixel == 4) pixelType = GL_RGBA;
149 buffer = MALLOC (GLvoid *, bytesPerPixel*width*height*
sizeof(
char));
152 FW_GL_PIXELSTOREI (GL_UNPACK_ALIGNMENT, 1);
153 FW_GL_PIXELSTOREI (GL_PACK_ALIGNMENT, 1);
155 FW_GL_READPIXELS (x,y,width,height,pixelType,GL_UNSIGNED_BYTE, buffer);
160#if defined( _MSC_VER) || defined (IPHONE) || defined(AQUA)
163void fwl_toggleSnapshot(){}
164void fwl_init_SnapGif(){}
165void saveSnapSequence() {}
175#define FDWORD unsigned long
177#define FWORD unsigned short
178#define FBYTE unsigned char
187 FDWORD biCompression;
189 FLONG biXPelsPerMeter;
190 FLONG biYPelsPerMeter;
192 FDWORD biClrImportant;
218static void fromLong(
unsigned long myword,
char *buffer)
220 buffer[0] = (
unsigned char)((myword & 0x000000ff) >> 0);
221 buffer[1] = (
unsigned char)((myword & 0x0000ff00) >> 8);
222 buffer[2] = (
unsigned char)((myword & 0x00ff0000) >> 16);
223 buffer[3] = (
unsigned char)((myword & 0xff000000) >> 24);
227static void fromShort(
unsigned short myword,
char *buffer)
229 buffer[0] = (myword & 0x00ff) >> 0;
230 buffer[1] = (myword & 0xff00) >> 8;
235void saveSnapshotBMP(
char *pathname,
char *buffer,
int bytesPerPixel,
int width,
int height)
239 int rowlength, extra, alignedwidth, i;
244 char filler[3] = {
'\0',
'\0',
'\0'};
250 fout = fopen(pathname,
"w+b");
252 if(bytesPerPixel == 3) bi.biCompression = FBI_RGB;
253 bi.biHeight = height;
256 bi.biBitCount = 8 * bytesPerPixel;
257 bi.biXPelsPerMeter = 0;
258 bi.biYPelsPerMeter = 0;
262 rowlength = width*bytesPerPixel;
263 extra = 4 - (rowlength % 4);
264 if(extra == 4) extra = 0;
265 alignedwidth = rowlength + extra;
268 bi.biSizeImage = alignedwidth * height;
269 bi.biSize =
sizeof(bi);
271 bi.biClrImportant = 0;
277 memcpy(&bfType,
"BM",2);
278 bmph.bfReserved1 = 0;
279 bmph.bfReserved2 = 0;
282 fwrite(&bfType,
sizeof(bfType),1,fout);
290 for(i=0;i<rowlength*height;i+=3)
293 buffer[i] = buffer[i+1];
302 fwrite(
"BM",2,1,fout);
304 bmph.bfOffBits = 2 + 12 + bi.biSize;
305 bmph.bfSize = bmph.bfOffBits + bi.biSizeImage;
306 bmph.bfReserved1 = 0;
307 bmph.bfReserved2 = 0;
309 fromLong(bmph.bfSize, &buf[0]);
310 fromShort(bmph.bfReserved1, &buf[4]);
311 fromShort(bmph.bfReserved2, &buf[6]);
312 fromLong(bmph.bfOffBits, &buf[8]);
313 fromLong(bi.biSize, &buf[12]);
315 fromLong(bi.biWidth, &buf[16]);
316 fromLong(bi.biHeight, &buf[20]);
317 fromShort(bi.biPlanes, &buf[24]);
318 fromShort(bi.biBitCount, &buf[26]);
319 fromLong(bi.biCompression, &buf[28]);
320 fromLong(bi.biSizeImage, &buf[32]);
321 fromLong(bi.biXPelsPerMeter, &buf[36]);
322 fromLong(bi.biYPelsPerMeter, &buf[40]);
323 fromLong(bi.biClrUsed, &buf[44]);
324 fromLong(bi.biClrImportant, &buf[48]);
325 fwrite(buf,52,1,fout);
331 for(i=0;i<rowlength*height;i+=bytesPerPixel)
334 buffer[i] = buffer[i+2];
340 for(i=0;i<height;i++)
343 fwrite(&buffer[j],rowlength,1,fout);
345 fwrite(filler,extra,1,fout);
352int fw_mkdir(
const char* path);
364 char thisRawFile[2000];
365 char *mysnapb, *mytmp;
367 ppSnapshot p = (ppSnapshot)gglobal()->Snapshot.prv;
369 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
370 if (p->snapsnapB == NULL)
371 mysnapb =
"freewrl.snap";
373 mysnapb = p->snapsnapB;
375 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
376 else mytmp = p->seqtmp;
380 snprintf(thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp", mytmp, mysnapb, p->snapRawCount);
381 saveSnapshotBMP(thisRawFile, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
384void Snapshot1(
char *fname){
386 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
387 saveSnapshotBMP(fname, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
391#if !(defined(_MSC_VER) || defined(IPHONE) || defined(AQUA))
393void fwl_init_SnapGif()
396 ppSnapshot p = (ppSnapshot)gglobal()->Snapshot.prv;
401void saveSnapshotBmp0(
char *folder,
char *prefix,
const char *sufx,
int count,
void *buffer,
int bpp,
int width,
int height){
402 char thisRawFile[2000];
403 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp",folder,prefix,count);
404 saveSnapshotBMP(thisRawFile,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
405 printf (
"[2] snapshot is: %s\n",thisRawFile);
408void saveSnapshotImlib2Png(
char *folder,
char *prefix,
const char *sufx,
int count,
char *buffer,
int bpp,
int width,
int height){
409 char thisRawFile[2000];
412 char *inrow, *outrow;
414 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.%s",folder,prefix,count,sufx);
417 image = imlib_create_image(width,height);
418 imlib_context_set_image(image);
419 imlib_image_set_has_alpha(0);
420 imlib_image_set_format(sufx);
421 buf32 = (
char *)imlib_image_get_data();
422 for(i=0;i<height;i++){
423 inrow = &buffer[(width * bpp)*i];
424 outrow = &buf32[(width *4)*(height -i-1)];
425 for(j=0;j<width;j++){
426 outrow[j*4 +3] = 255;
429 outrow[j*4 +kk] = inrow[j*bpp +k];
433 imlib_image_put_back_data((DATA32*)buf32);
434 imlib_save_image(thisRawFile);
436 printf (
"[2] snapshot is: %s\n",thisRawFile);
439void saveSnapshotRawPng(
char *folder,
char *prefix,
const char *sufx,
int count,
void *buffer,
int bpp,
int width,
int height){
440 char thisRawFile[2000];
441 char thisGoodFile[2000];
446 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.rgb",folder,prefix,count);
447 tmpfile = fopen(thisRawFile,
"w");
448 if (tmpfile == NULL) {
449 printf (
"cannot open temp file (%s) for writing\n",thisRawFile);
454 if (fwrite(buffer, 1, height*width*3, tmpfile) <= 0) {
455 printf (
"error writing snapshot to %s, aborting snapshot\n",thisRawFile);
464 snprintf (thisGoodFile,
sizeof(thisGoodFile),
"%s/%s.%04d.%s",folder,prefix,count,sufx);
465 snprintf(sysline,
sizeof(sysline),
"%s -size %dx%d -depth 8 -flip %s %s",
466 IMAGECONVERT,width, height,thisRawFile,thisGoodFile);
468 if (system (sysline) != 0) {
469 printf (
"Freewrl: error running convert line %s\n",sysline);
471 printf (
"[2] snapshot is: %s\n",thisGoodFile);
472 UNLINK (thisRawFile);
477static const char * suffix [] = {
"png",
"gif"};
483 char *mytmp, *mysnapb;
485 struct tSnapshot* t = &gglobal()->Snapshot;
489 printf(
"do Snapshot ... \n");
492 if (p->snapsnapB == NULL)
493 mysnapb =
"freewrl.snap";
495 mysnapb = p->snapsnapB;
498 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
499 else mytmp = p->seqtmp;
502 if ((mydir = opendir(mytmp)) == NULL) {
504 if ((mydir = opendir(mytmp)) == NULL) {
505 ConsoleMessage (
"error opening Snapshot directory %s\n",mytmp);
513 buffer = MALLOC (GLvoid *, 3*gglobal()->display.screenWidth*gglobal()->display.screenHeight*
sizeof(
char));
516 FW_GL_PIXELSTOREI (GL_UNPACK_ALIGNMENT, 1);
517 FW_GL_PIXELSTOREI (GL_PACK_ALIGNMENT, 1);
518 FW_GL_READPIXELS (0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight,GL_RGB,GL_UNSIGNED_BYTE, buffer);
525 if(p->snapGif) sufx = suffix[1];
528 saveSnapshotBmp0(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
531 saveSnapshotImlib2Png(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
533 saveSnapshotRawPng(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);