Fix snapshot filenames

This commit is contained in:
statmat
2004-04-27 21:28:27 +00:00
parent 90a15b7201
commit 6b19fff805
2 changed files with 1850 additions and 1840 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,236 +1,230 @@
#include "zpng.h" #include "zpng.h"
#ifdef __WIN32__ #ifdef __WIN32__
#include <windows.h> #include <windows.h>
#include <sys/stat.h>
#ifdef __WIN32DBG__
#include <crtdbg.h> #ifdef __WIN32DBG__
#endif #include <crtdbg.h>
#endif #endif
#endif
#ifdef __MSDOS__
#include <sys/types.h> #ifdef __MSDOS__
#include <sys/stat.h> #include <sys/types.h>
#include <unistd.h> #include <sys/stat.h>
#include <stdlib.h> #include <unistd.h>
#endif #include <stdlib.h>
#endif
#ifdef __LINUX__
#include "../gblhdr.h" #ifdef __LINUX__
#endif #include "../gblhdr.h"
#endif
extern unsigned int vidbuffer;
extern unsigned int vidbuffer;
#ifdef __PNG__
#ifdef __PNG__
int Png_Dump(const char * filename, unsigned short width, unsigned short height, unsigned char * image_data, bool usebgr)
{ int Png_Dump(const char * filename, unsigned short width, unsigned short height, unsigned char * image_data, bool usebgr)
png_structp png_ptr; {
png_infop info_ptr; png_structp png_ptr;
png_bytep * row_pointers; png_infop info_ptr;
/*Set scanline width for 32-bit color data*/ png_bytep * row_pointers;
int scanline=width*4; /*Set scanline width for 32-bit color data*/
int i; /*counter.*/ int scanline=width*4;
int png_transforms=0; int i; /*counter.*/
png_color fake_pal; int png_transforms=0;
/*Try to open the file.*/ png_color fake_pal;
FILE *fp = fopen(filename, "wb"); /*Try to open the file.*/
if (!fp) FILE *fp = fopen(filename, "wb");
{ if (!fp)
return (-1); {
} return (-1);
}
fake_pal.red = 0;
fake_pal.green = 0; fake_pal.red = 0;
fake_pal.blue = 0; fake_pal.green = 0;
/*Try to create png write struct, fail if we cannot.*/ fake_pal.blue = 0;
png_ptr = png_create_write_struct /*Try to create png write struct, fail if we cannot.*/
(PNG_LIBPNG_VER_STRING, NULL,/*(png_voidp)user_error_ptr, png_ptr = png_create_write_struct
user_error_fn*/NULL, NULL/*user_warning_fn*/); (PNG_LIBPNG_VER_STRING, NULL,/*(png_voidp)user_error_ptr,
if (!png_ptr) user_error_fn*/NULL, NULL/*user_warning_fn*/);
return (-1); if (!png_ptr)
return (-1);
/*set png I/O source.*/
png_init_io(png_ptr, fp); /*set png I/O source.*/
png_init_io(png_ptr, fp);
/* set the zlib compression level */
png_set_compression_level(png_ptr, /* set the zlib compression level */
Z_BEST_COMPRESSION); png_set_compression_level(png_ptr,
Z_BEST_COMPRESSION);
/* set other zlib parameters */
png_set_compression_mem_level(png_ptr, 8); /* set other zlib parameters */
png_set_compression_strategy(png_ptr, png_set_compression_mem_level(png_ptr, 8);
Z_DEFAULT_STRATEGY); png_set_compression_strategy(png_ptr,
png_set_compression_window_bits(png_ptr, 15); Z_DEFAULT_STRATEGY);
png_set_compression_method(png_ptr, 8); png_set_compression_window_bits(png_ptr, 15);
png_set_compression_buffer_size(png_ptr, 8192); png_set_compression_method(png_ptr, 8);
png_set_compression_buffer_size(png_ptr, 8192);
/*try to create info struct. Fail and delete existing structs if info struct cannot be created.*/
info_ptr = png_create_info_struct(png_ptr); /*try to create info struct. Fail and delete existing structs if info struct cannot be created.*/
if (!info_ptr) info_ptr = png_create_info_struct(png_ptr);
{ if (!info_ptr)
png_destroy_write_struct(&png_ptr, {
(png_infopp)NULL); png_destroy_write_struct(&png_ptr,
return (-1); (png_infopp)NULL);
} return (-1);
}
/*set a lot of image info (code adapted from libpng documentation!)*/
png_set_IHDR(png_ptr, info_ptr, width, height, /*set a lot of image info (code adapted from libpng documentation!)*/
8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, png_set_IHDR(png_ptr, info_ptr, width, height,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
info_ptr->color_type=PNG_COLOR_TYPE_RGB_ALPHA;
info_ptr->color_type=PNG_COLOR_TYPE_RGB_ALPHA;
/*Allocate an array of scanline pointers*/
row_pointers=(png_bytep*)malloc(height*sizeof(png_bytep)); /*Allocate an array of scanline pointers*/
for (i=0;i<height;i++) row_pointers=(png_bytep*)malloc(height*sizeof(png_bytep));
{ for (i=0;i<height;i++)
#ifdef __UPSIDE_DOWN__ {
/*invert to normal image format.*/ #ifdef __UPSIDE_DOWN__
row_pointers[i]=&image_data[scanline*(height-i-1)]; /*invert to normal image format.*/
#else row_pointers[i]=&image_data[scanline*(height-i-1)];
row_pointers[i]=&image_data[scanline*i]; #else
#endif row_pointers[i]=&image_data[scanline*i];
} #endif
}
/*tell the png library what to encode.*/
png_set_rows(png_ptr, info_ptr, row_pointers); /*tell the png library what to encode.*/
png_set_rows(png_ptr, info_ptr, row_pointers);
if(usebgr)
png_transforms|=PNG_TRANSFORM_BGR; if(usebgr)
/*Write image to file*/ png_transforms|=PNG_TRANSFORM_BGR;
png_write_png(png_ptr, info_ptr, png_transforms, NULL); /*Write image to file*/
png_write_png(png_ptr, info_ptr, png_transforms, NULL);
/*close file*/
fclose(fp); /*close file*/
fclose(fp);
/*Destroy PNG structs*/
png_destroy_write_struct(&png_ptr, &info_ptr); /*Destroy PNG structs*/
png_destroy_write_struct(&png_ptr, &info_ptr);
/*clean up dynamically allocated RAM.*/
free(row_pointers); /*clean up dynamically allocated RAM.*/
free(row_pointers);
#ifdef __WIN32DBG__
_CrtDumpMemoryLeaks(); #ifdef __WIN32DBG__
#endif _CrtDumpMemoryLeaks();
return 0; #endif
} return 0;
}
char *generate_filename(void)
{ char *generate_filename(void)
extern char fnames; {
char *filename; extern char fnames;
char *tmp = &fnames; char *filename;
short i=0; char *tmp = &fnames;
#ifdef __WIN32__ char *tmp2 = 0;
SYSTEMTIME time; short i=0;
#else struct stat buf;
struct stat buf;
#endif #ifdef __MSDOS__
filename = (char *)malloc(14);
#ifdef __MSDOS__ for(i=0;i<10000;i++)
filename = (char *)malloc(14); {
for(i=0;i<10000;i++) if(i>1000)
{ sprintf(filename, "Image%03d.png", i);
if(i>1000) else sprintf(filename, "Imag%04d.png", i);
sprintf(filename, "Image%03d.png", i); if(stat(filename, &buf)==-1)
else sprintf(filename, "Imag%04d.png", i); break;
if(stat(filename, &buf)==-1) }
break; return filename;
} #endif
return filename;
#endif tmp++; // the first char is the string length
// removes the path if there is one
while (*tmp!=0) tmp++;
tmp++; // the first char is the string length while ((*tmp!='/') && (tmp!=&fnames)) tmp--;
// removes the path if there is one tmp++;
while (*tmp!=0) tmp++; // allocates enough memory to store the filename
while ((*tmp!='/') && (tmp!=&fnames)) tmp--; filename = (char *)malloc(strlen(tmp)+10);
tmp++; strcpy(filename, tmp);
// allocates enough memory to store the filename
#ifdef __LINUX__ tmp = filename+strlen(filename);
filename = (char *)malloc(strlen(tmp)+10); while (*tmp!='.') tmp--;
#endif
#ifdef __WIN32__ #ifdef __LINUX__
filename = (char *)malloc(strlen(tmp)+25); tmp2 = filename;
#endif while (tmp2<tmp) {
strcpy(filename, tmp); if (*tmp2 == ' ') *tmp2 = '_';
tmp = filename; tmp2++;
while (*tmp!='.') { }
if (*tmp == ' ') *tmp = '_'; #endif
tmp++;
} /*find first unused file*/
#ifdef __WIN32__ /*Note: this results in a 9999 image limit!*/
/*get system time.*/
GetLocalTime(&time); for(i=0;i<10000;i++)
{
/*make filename from local time*/ #ifdef __LINUX__
wsprintf(tmp," %d %02d_%02d %02d-%02d-%02d.png\0", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond); sprintf(tmp, "_%04d.png\0", i);
#endif #else
#ifdef __LINUX__ sprintf(tmp, " %04d.png\0", i);
/*find first unused file*/ #endif
if(stat(filename, &buf)==-1)
/*Note: this results in a 1000 image limit!*/ break;
}
for(i=0;i<10000;i++)
{ return filename;
sprintf(tmp, "_%04d.png", i); }
if(stat(filename, &buf)==-1)
break; void Grab_PNG_Data(void)
} {
#endif char *filename;
return filename; bool is_bgr_data=true;
}
/*These are the variables used to perform the 32-bit conversion*/
void Grab_PNG_Data(void) int i,j;
{ unsigned short* pixel;
char *filename; unsigned short conv_pixel;
bool is_bgr_data=true; /*Set scanline width for 32-bit color data: 4*256 = 1024*/
int scanline=1024;
/*These are the variables used to perform the 32-bit conversion*/ unsigned char *DIBits;
int i,j; unsigned int * DBits;
unsigned short* pixel;
unsigned short conv_pixel; filename = generate_filename();
/*Set scanline width for 32-bit color data: 4*256 = 1024*/
int scanline=1024; /*Allocate image buffer for DIB data*/
unsigned char *DIBits; DIBits=(unsigned char*)malloc(scanline*224);
unsigned int * DBits;
/*Cast pointer to 32-bit data type*/
filename = generate_filename(); DBits=(unsigned int*) DIBits;
/*Allocate image buffer for DIB data*/ /*Use zsKnight's 16 to 32 bit color conversion*/
DIBits=(unsigned char*)malloc(scanline*224); pixel=(unsigned short*)(vidbuffer);
for(i=0;i<224;i++)
/*Cast pointer to 32-bit data type*/ {
DBits=(unsigned int*) DIBits; for(j=0;j<256;j++)
{
/*Use zsKnight's 16 to 32 bit color conversion*/
pixel=(unsigned short*)(vidbuffer); conv_pixel=pixel[(i*288)+j+16];
for(i=0;i<224;i++) DBits[i*256+j]=((conv_pixel&0xF800)<<8)|
{ ((conv_pixel&0x07E0)<<5)|
for(j=0;j<256;j++) ((conv_pixel&0x001F)<<3)|0xFF000000;
{ }
}
conv_pixel=pixel[(i*288)+j+16];
DBits[i*256+j]=((conv_pixel&0xF800)<<8)| /*compress and write the PNG*/
((conv_pixel&0x07E0)<<5)| Png_Dump(filename, 256, 224, DIBits, is_bgr_data);
((conv_pixel&0x001F)<<3)|0xFF000000;
} free(DIBits);
} free(filename);
/*compress and write the PNG*/ #ifdef __WIN32DBG__
Png_Dump(filename, 256, 224, DIBits, is_bgr_data); _CrtDumpMemoryLeaks();
#endif
free(DIBits);
free(filename); }
#ifdef __WIN32DBG__ #endif
_CrtDumpMemoryLeaks();
#endif
}
#endif