void SaveScreenshot() { Player* py = &g_player[g_curP]; LoadedTex screenshot; screenshot.channels = 3; screenshot.sizeX = py->width; screenshot.sizeY = py->height; screenshot.data = (unsigned char*)malloc( sizeof(unsigned char) * py->width * py->height * 3 ); if(!screenshot.data) { OutOfMem(__FILE__, __LINE__); return; } memset(screenshot.data, 0, py->width * py->height * 3); glReadPixels(0, 0, py->width, py->height, GL_RGB, GL_UNSIGNED_BYTE, screenshot.data); FlipImage(&screenshot); char relative[256]; std::string datetime = FileDateTime(); sprintf(relative, "screenshots/%s.jpg", datetime.c_str()); char fullpath[MAX_PATH+1]; FullPath(relative, fullpath); g_log<<"Writing screenshot "<<fullpath<<std::endl; g_log.flush(); SaveJPEG(fullpath, &screenshot, 0.9f); //free(screenshot.data); }
bool SaveJPEG(File* file, Image* image) { COR_GUARD("SaveJPEG"); if (!image) { return false; } //Hack for now FILE* filePtr = (FILE*)file->GetFilePtr(); if(!filePtr) { return false; } // If the image format isn't supported directly by this function, // clone to a supported format and try to save with that. switch (image->getFormat()) { case PF_R8G8B8: break; default: { COR_LOG("Unsupported pixel format... cloning"); std::auto_ptr<Image> cloned(CloneImage(image, PF_R8G8B8)); return SaveJPEG(file, cloned.get()); } } const int width = image->getWidth(); const int height = image->getHeight(); jpeg_compress_struct cinfo; jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); int nChannels = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); cinfo.input_components = nChannels; cinfo.num_components = nChannels; cinfo.image_width = width; cinfo.image_height = height; cinfo.data_precision = 8; cinfo.input_gamma = 1.0; jpeg_set_quality(&cinfo, 75, FALSE); jpeg_stdio_dest(&cinfo, filePtr); jpeg_start_compress(&cinfo, TRUE); unsigned char *curr_scanline = (unsigned char *)image->getPixels(); for (int y = 0; y < height; y++){ jpeg_write_scanlines(&cinfo, &curr_scanline, 1); curr_scanline += nChannels * width; } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); return true; }
int main(int argc, char** argv) { if (argc != 5 && argc != 6 && argc != 7) { printf("Usage: program SourceJPEG DestJPEG DestWidth DestHeight [bpp] [rotation]\r\n"); return -1; } RawImage_t* myImage; int bpp = 32; int rotation = 0; if (argc > 5) bpp = atoi(argv[5]); if (argc > 6) rotation = atoi(argv[6]); char* lastDot = strrchr(argv[1], '.'); if (lastDot && !strcasecmp(lastDot, ".png")) { FILE* fp = fopen(argv[1], "rb"); myImage = LoadPNG(fp, atoi(argv[3]), atoi(argv[4])); fclose(fp); } else if (lastDot && !strcasecmp(lastDot, ".gif")) { int fp = open(argv[1], O_RDONLY | O_BINARY); myImage = LoadGIF(fp, atoi(argv[3]), atoi(argv[4])); close(fp); } else if (lastDot && (!strcasecmp(lastDot, ".tif") || !strcasecmp(lastDot, ".tiff"))) { int fp = open(argv[1], O_RDONLY | O_BINARY); myImage = LoadTIFF(fp, argv[1], atoi(argv[3]), atoi(argv[4])); close(fp); } else { FILE* fp = fopen(argv[1], "rb"); myImage = LoadJPEG(fp, atoi(argv[3]), atoi(argv[4]), bpp, rotation); fclose(fp); } printf("Finished loading image\r\n");fflush(stdout); if (!myImage) { printf("FAILED loading image\r\n"); return -1; } FILE* fp = fopen(argv[2], "wb"); if (strstr(argv[2], ".png")) { if (SavePNG(myImage, fp)) printf("FAILED saving PNG image\r\n"); } else if (SaveJPEG(myImage, fp)) { printf("FAILED saving JPEG image\r\n"); } fclose(fp); free(myImage->pPlane); free(myImage); return 0; }