| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							| 
									
										
										
										
											2014-06-03 01:08:54 -04:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-17 16:54:31 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-26 17:13:07 -04:00
										 |  |  | #include "Common/MsgHandler.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "VideoCommon/ImageWrite.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "png.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-26 16:00:23 -05:00
										 |  |  | bool SaveData(const std::string& filename, const std::string& data) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::ofstream f; | 
					
						
							|  |  |  |   OpenFStream(f, filename, std::ios::binary); | 
					
						
							|  |  |  |   f << data; | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:25:12 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-15 13:00:38 +13:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | TextureToPng | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Inputs: | 
					
						
							|  |  |  | data      : This is an array of RGBA with 8 bits per channel. 4 bytes for each pixel. | 
					
						
							|  |  |  | row_stride: Determines the amount of bytes per row of pixels. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  | bool TextureToPng(const u8* data, int row_stride, const std::string& filename, int width, | 
					
						
							|  |  |  |                   int height, bool saveAlpha) | 
					
						
							| 
									
										
										
										
											2013-11-15 13:00:38 +13:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   bool success = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!data) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   char title[] = "Dolphin Screenshot"; | 
					
						
							|  |  |  |   char title_key[] = "Title"; | 
					
						
							|  |  |  |   png_structp png_ptr = nullptr; | 
					
						
							|  |  |  |   png_infop info_ptr = nullptr; | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  |   std::vector<u8> buffer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!saveAlpha) | 
					
						
							|  |  |  |     buffer.resize(width * 4); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Open file for writing (binary mode)
 | 
					
						
							|  |  |  |   File::IOFile fp(filename, "wb"); | 
					
						
							|  |  |  |   if (!fp.IsOpen()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     PanicAlertT("Screenshot failed: Could not open file \"%s\" (error %d)", filename.c_str(), | 
					
						
							|  |  |  |                 errno); | 
					
						
							|  |  |  |     goto finalise; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize write structure
 | 
					
						
							|  |  |  |   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); | 
					
						
							|  |  |  |   if (png_ptr == nullptr) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     PanicAlert("Screenshot failed: Could not allocate write struct"); | 
					
						
							|  |  |  |     goto finalise; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize info structure
 | 
					
						
							|  |  |  |   info_ptr = png_create_info_struct(png_ptr); | 
					
						
							|  |  |  |   if (info_ptr == nullptr) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     PanicAlert("Screenshot failed: Could not allocate info struct"); | 
					
						
							|  |  |  |     goto finalise; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Setup Exception handling
 | 
					
						
							|  |  |  |   if (setjmp(png_jmpbuf(png_ptr))) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     PanicAlert("Screenshot failed: Error during PNG creation"); | 
					
						
							|  |  |  |     goto finalise; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   png_init_io(png_ptr, fp.GetHandle()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Write header (8 bit color depth)
 | 
					
						
							|  |  |  |   png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, | 
					
						
							|  |  |  |                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   png_text title_text; | 
					
						
							|  |  |  |   title_text.compression = PNG_TEXT_COMPRESSION_NONE; | 
					
						
							|  |  |  |   title_text.key = title_key; | 
					
						
							|  |  |  |   title_text.text = title; | 
					
						
							|  |  |  |   png_set_text(png_ptr, info_ptr, &title_text, 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   png_write_info(png_ptr, info_ptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Write image data
 | 
					
						
							|  |  |  |   for (auto y = 0; y < height; ++y) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  |     const u8* row_ptr = data + y * row_stride; | 
					
						
							|  |  |  |     if (!saveAlpha) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  |       for (int x = 0; x < width; x++) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         for (int i = 0; i < 3; i++) | 
					
						
							|  |  |  |           buffer[4 * x + i] = row_ptr[4 * x + i]; | 
					
						
							|  |  |  |         buffer[4 * x + 3] = 0xff; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       row_ptr = buffer.data(); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-08 11:54:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // The old API uses u8* instead of const u8*. It doesn't write
 | 
					
						
							|  |  |  |     // to this pointer, but to fit the API, we have to drop the const qualifier.
 | 
					
						
							|  |  |  |     png_write_row(png_ptr, const_cast<u8*>(row_ptr)); | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // End write
 | 
					
						
							|  |  |  |   png_write_end(png_ptr, nullptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   success = true; | 
					
						
							| 
									
										
										
										
											2013-11-15 13:00:38 +13:00
										 |  |  | 
 | 
					
						
							|  |  |  | finalise: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (info_ptr != nullptr) | 
					
						
							|  |  |  |     png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); | 
					
						
							|  |  |  |   if (png_ptr != nullptr) | 
					
						
							|  |  |  |     png_destroy_write_struct(&png_ptr, (png_infopp) nullptr); | 
					
						
							| 
									
										
										
										
											2013-11-15 13:00:38 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return success; | 
					
						
							| 
									
										
										
										
											2013-11-15 13:00:38 +13:00
										 |  |  | } |