diff --git a/desmume/src/NDSSystem.cpp b/desmume/src/NDSSystem.cpp index 9f903f547..d83ba06ac 100644 --- a/desmume/src/NDSSystem.cpp +++ b/desmume/src/NDSSystem.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "common.h" #include "NDSSystem.h" @@ -1058,6 +1059,138 @@ int NDS_ImportSave(const char *filename) return 0; } +static int WritePNGChunk(FILE *fp, uint32 size, const char *type, const uint8 *data) +{ + uint32 crc; + + uint8 tempo[4]; + + tempo[0]=size>>24; + tempo[1]=size>>16; + tempo[2]=size>>8; + tempo[3]=size; + + if(fwrite(tempo,4,1,fp)!=1) + return 0; + if(fwrite(type,4,1,fp)!=1) + return 0; + + if(size) + if(fwrite(data,1,size,fp)!=size) + return 0; + + crc = crc32(0,(uint8 *)type,4); + if(size) + crc = crc32(crc,data,size); + + tempo[0]=crc>>24; + tempo[1]=crc>>16; + tempo[2]=crc>>8; + tempo[3]=crc; + + if(fwrite(tempo,4,1,fp)!=1) + return 0; + return 1; +} +int NDS_WritePNG(const char *fname) +{ + int x, y; + int width=256; + int height=192*2; + u16 * bmp = (u16 *)GPU_screen; + FILE *pp=NULL; + uint8 *compmem = NULL; + uLongf compmemsize = (uLongf)( (height * (width + 1) * 3 * 1.001 + 1) + 12 ); + + if(!(compmem=(uint8 *)malloc(compmemsize))) + return 0; + + if(!(pp=fopen(fname, "wb"))) + { + return 0; + } + { + static uint8 header[8]={137,80,78,71,13,10,26,10}; + if(fwrite(header,8,1,pp)!=1) + goto PNGerr; + } + + { + uint8 chunko[13]; + + chunko[0] = width >> 24; // Width + chunko[1] = width >> 16; + chunko[2] = width >> 8; + chunko[3] = width; + + chunko[4] = height >> 24; // Height + chunko[5] = height >> 16; + chunko[6] = height >> 8; + chunko[7] = height; + + chunko[8]=8; // 8 bits per sample(24 bits per pixel) + chunko[9]=2; // Color type; RGB triplet + chunko[10]=0; // compression: deflate + chunko[11]=0; // Basic adapative filter set(though none are used). + chunko[12]=0; // No interlace. + + if(!WritePNGChunk(pp,13,"IHDR",chunko)) + goto PNGerr; + } + + { + uint8 *tmp_buffer; + uint8 *tmp_inc; + tmp_inc = tmp_buffer = (uint8 *)malloc((width * 3 + 1) * height); + + for(y=0;y>10; + pixel-=r<<10; + g = pixel>>5; + pixel-=g<<5; + b = pixel; + r*=255/31; + g*=255/31; + b*=255/31; + tmp_inc[0] = b; + tmp_inc[1] = g; + tmp_inc[2] = r; + tmp_inc += 3; + } + } + + if(compress(compmem, &compmemsize, tmp_buffer, height * (width * 3 + 1))!=Z_OK) + { + if(tmp_buffer) free(tmp_buffer); + goto PNGerr; + } + if(tmp_buffer) free(tmp_buffer); + if(!WritePNGChunk(pp,compmemsize,"IDAT",compmem)) + goto PNGerr; + } + if(!WritePNGChunk(pp,0,"IEND",0)) + goto PNGerr; + + free(compmem); + fclose(pp); + + return 1; + +PNGerr: + if(compmem) + free(compmem); + if(pp) + fclose(pp); + return(0); +} + typedef struct { u32 size; diff --git a/desmume/src/NDSSystem.h b/desmume/src/NDSSystem.h index bd5fe19d3..ba648e208 100644 --- a/desmume/src/NDSSystem.h +++ b/desmume/src/NDSSystem.h @@ -34,6 +34,8 @@ #include +int NDS_WritePNG(const char *fname); + extern volatile BOOL execute; extern BOOL click; extern char pathToROM[MAX_PATH]; diff --git a/desmume/src/windows/hotkey.cpp b/desmume/src/windows/hotkey.cpp index b5b492503..e608ff406 100644 --- a/desmume/src/windows/hotkey.cpp +++ b/desmume/src/windows/hotkey.cpp @@ -70,19 +70,34 @@ void CopyCustomKeys (SCustomKeys *dst, const SCustomKeys *src) void HK_PrintScreen(int param) { OPENFILENAME ofn; + char * ptr; char filename[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = MainWindow->getHWnd(); - ofn.lpstrFilter = "Bmp file (*.bmp)\0*.bmp\0Any file (*.*)\0*.*\0\0"; + ofn.lpstrFilter = "png file (*.png)\0*.png\0Bmp file (*.bmp)\0*.bmp\0Any file (*.*)\0*.*\0\0"; ofn.nFilterIndex = 1; ofn.lpstrFile = filename; ofn.lpstrTitle = "Print Screen Save As"; - ofn.nMaxFile = MAX_PATH; - ofn.lpstrDefExt = "bmp"; - ofn.Flags = OFN_OVERWRITEPROMPT; - GetSaveFileName(&ofn); - NDS_WriteBMP(filename); + ofn.nMaxFile = MAX_PATH; + ofn.lpstrDefExt = "png"; + ofn.Flags = OFN_OVERWRITEPROMPT; + GetSaveFileName(&ofn); + + ptr = strrchr(filename,'.');//look for the last . in the filename + + if ( ptr != 0 ) { + if (( strcmp ( ptr, ".BMP" ) == 0 ) || + ( strcmp ( ptr, ".bmp" ) == 0 )) + { + NDS_WriteBMP(filename); + } + if (( strcmp ( ptr, ".PNG" ) == 0 ) || + ( strcmp ( ptr, ".png" ) == 0 )) + { + NDS_WritePNG(filename); + } + } } void HK_StateSaveSlot(int num)