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();
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)
107 p->modeTesting = value;
109int isSnapshotModeTesting()
111 struct tSnapshot* t = &gglobal()->Snapshot;
113 return p->modeTesting;
117void fwl_set_SnapFile(
const char* file)
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)
130 tg->Snapshot.doSnapshot = FALSE;
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'};
249 fout = fopen(pathname,
"w+b");
251 if(bytesPerPixel == 3) bi.biCompression = FBI_RGB;
252 bi.biHeight = height;
255 bi.biBitCount = 8 * bytesPerPixel;
256 bi.biXPelsPerMeter = 0;
257 bi.biYPelsPerMeter = 0;
261 rowlength = width*bytesPerPixel;
262 extra = 4 - (rowlength % 4);
263 if(extra == 4) extra = 0;
264 alignedwidth = rowlength + extra;
267 bi.biSizeImage = alignedwidth * height;
268 bi.biSize =
sizeof(bi);
270 bi.biClrImportant = 0;
276 memcpy(&bfType,
"BM",2);
277 bmph.bfReserved1 = 0;
278 bmph.bfReserved2 = 0;
281 fwrite(&bfType,
sizeof(bfType),1,fout);
289 for(i=0;i<rowlength*height;i+=3)
292 buffer[i] = buffer[i+1];
301 fwrite(
"BM",2,1,fout);
303 bmph.bfOffBits = 2 + 12 + bi.biSize;
304 bmph.bfSize = bmph.bfOffBits + bi.biSizeImage;
305 bmph.bfReserved1 = 0;
306 bmph.bfReserved2 = 0;
308 fromLong(bmph.bfSize, &buf[0]);
309 fromShort(bmph.bfReserved1, &buf[4]);
310 fromShort(bmph.bfReserved2, &buf[6]);
311 fromLong(bmph.bfOffBits, &buf[8]);
312 fromLong(bi.biSize, &buf[12]);
314 fromLong(bi.biWidth, &buf[16]);
315 fromLong(bi.biHeight, &buf[20]);
316 fromShort(bi.biPlanes, &buf[24]);
317 fromShort(bi.biBitCount, &buf[26]);
318 fromLong(bi.biCompression, &buf[28]);
319 fromLong(bi.biSizeImage, &buf[32]);
320 fromLong(bi.biXPelsPerMeter, &buf[36]);
321 fromLong(bi.biYPelsPerMeter, &buf[40]);
322 fromLong(bi.biClrUsed, &buf[44]);
323 fromLong(bi.biClrImportant, &buf[48]);
324 fwrite(buf,52,1,fout);
330 for(i=0;i<rowlength*height;i+=3)
333 buffer[i] = buffer[i+2];
339 for(i=0;i<height;i++)
342 fwrite(&buffer[j],rowlength,1,fout);
344 fwrite(filler,extra,1,fout);
351int fw_mkdir(
const char* path);
363 char thisRawFile[2000];
364 char *mysnapb, *mytmp;
368 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
369 if (p->snapsnapB == NULL)
370 mysnapb =
"freewrl.snap";
372 mysnapb = p->snapsnapB;
374 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
375 else mytmp = p->seqtmp;
379 snprintf(thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp", mytmp, mysnapb, p->snapRawCount);
380 saveSnapshotBMP(thisRawFile, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
383void Snapshot1(
char *fname){
385 imgbuf = grabScreen(3,0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight);
386 saveSnapshotBMP(fname, imgbuf, 3, gglobal()->display.screenWidth, gglobal()->display.screenHeight);
390#if !(defined(_MSC_VER) || defined(IPHONE) || defined(AQUA))
392void fwl_init_SnapGif()
400void saveSnapshotBmp0(
char *folder,
char *prefix,
const char *sufx,
int count,
void *buffer,
int bpp,
int width,
int height){
401 char thisRawFile[2000];
402 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.bmp",folder,prefix,count);
403 saveSnapshotBMP(thisRawFile,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
404 printf (
"[2] snapshot is: %s\n",thisRawFile);
407void saveSnapshotImlib2Png(
char *folder,
char *prefix,
const char *sufx,
int count,
char *buffer,
int bpp,
int width,
int height){
408 char thisRawFile[2000];
411 char *inrow, *outrow;
413 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.%s",folder,prefix,count,sufx);
416 image = imlib_create_image(width,height);
417 imlib_context_set_image(image);
418 imlib_image_set_has_alpha(0);
419 imlib_image_set_format(sufx);
420 buf32 = (
char *)imlib_image_get_data();
421 for(i=0;i<height;i++){
422 inrow = &buffer[(width * bpp)*i];
423 outrow = &buf32[(width *4)*(height -i-1)];
424 for(j=0;j<width;j++){
425 outrow[j*4 +3] = 255;
428 outrow[j*4 +kk] = inrow[j*bpp +k];
432 imlib_image_put_back_data((DATA32*)buf32);
433 imlib_save_image(thisRawFile);
435 printf (
"[2] snapshot is: %s\n",thisRawFile);
438void saveSnapshotRawPng(
char *folder,
char *prefix,
const char *sufx,
int count,
void *buffer,
int bpp,
int width,
int height){
439 char thisRawFile[2000];
440 char thisGoodFile[2000];
445 snprintf (thisRawFile,
sizeof(thisRawFile),
"%s/%s.%04d.rgb",folder,prefix,count);
446 tmpfile = fopen(thisRawFile,
"w");
447 if (tmpfile == NULL) {
448 printf (
"cannot open temp file (%s) for writing\n",thisRawFile);
453 if (fwrite(buffer, 1, height*width*3, tmpfile) <= 0) {
454 printf (
"error writing snapshot to %s, aborting snapshot\n",thisRawFile);
463 snprintf (thisGoodFile,
sizeof(thisGoodFile),
"%s/%s.%04d.%s",folder,prefix,count,sufx);
464 snprintf(sysline,
sizeof(sysline),
"%s -size %dx%d -depth 8 -flip %s %s",
465 IMAGECONVERT,width, height,thisRawFile,thisGoodFile);
467 if (system (sysline) != 0) {
468 printf (
"Freewrl: error running convert line %s\n",sysline);
470 printf (
"[2] snapshot is: %s\n",thisGoodFile);
471 UNLINK (thisRawFile);
476static const char * suffix [] = {
"png",
"gif"};
482 char *mytmp, *mysnapb;
484 struct tSnapshot* t = &gglobal()->Snapshot;
488 printf(
"do Snapshot ... \n");
491 if (p->snapsnapB == NULL)
492 mysnapb =
"freewrl.snap";
494 mysnapb = p->snapsnapB;
497 if (p->seqtmp == NULL) mytmp =
"freewrl_tmp";
498 else mytmp = p->seqtmp;
501 if ((mydir = opendir(mytmp)) == NULL) {
503 if ((mydir = opendir(mytmp)) == NULL) {
504 ConsoleMessage (
"error opening Snapshot directory %s\n",mytmp);
512 buffer = MALLOC (GLvoid *, 3*gglobal()->display.screenWidth*gglobal()->display.screenHeight*
sizeof(
char));
515 FW_GL_PIXELSTOREI (GL_UNPACK_ALIGNMENT, 1);
516 FW_GL_PIXELSTOREI (GL_PACK_ALIGNMENT, 1);
517 FW_GL_READPIXELS (0,0,gglobal()->display.screenWidth,gglobal()->display.screenHeight,GL_RGB,GL_UNSIGNED_BYTE, buffer);
524 if(p->snapGif) sufx = suffix[1];
527 saveSnapshotBmp0(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
530 saveSnapshotImlib2Png(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);
532 saveSnapshotRawPng(mytmp,mysnapb,sufx,p->snapRawCount,buffer,3,gglobal()->display.screenWidth, gglobal()->display.screenHeight);