* Export function in Bitmap that determines file type from extension 4.x-dev

Tue, 15 Dec 2020 12:54:04 +0200

author
cemkalyoncu
date
Tue, 15 Dec 2020 12:54:04 +0200
branch
4.x-dev
changeset 1517
868dcba12c8a
parent 1516
dc526bcc1ad3
child 1518
ca727fb96027

* Export function in Bitmap that determines file type from extension

Source/Gorgon/Graphics/Bitmap.cpp file | annotate | diff | comparison | revisions
Source/Gorgon/Graphics/Bitmap.h file | annotate | diff | comparison | revisions
--- a/Source/Gorgon/Graphics/Bitmap.cpp	Sun Dec 13 10:53:14 2020 +0200
+++ b/Source/Gorgon/Graphics/Bitmap.cpp	Tue Dec 15 12:54:04 2020 +0200
@@ -149,6 +149,25 @@
 		return data->ImportBMP(file);
 	}
 
+	bool Bitmap::Export(const std::string &filename) {
+		auto dotpos = filename.find_last_of('.');
+		if(dotpos!=-1) {
+			auto ext = filename.substr(dotpos+1);
+
+			if(String::ToLower(ext) == "png") {
+				return ExportPNG(filename);
+			}
+			else if(String::ToLower(ext) == "jpg" || String::ToLower(ext) =="jpeg") {
+				return ExportJPEG(filename);
+			}
+			else if(String::ToLower(ext) == "bmp") {
+				return ExportBMP(filename);
+			}
+		}
+		
+		return false;
+	}
+
 	bool Bitmap::ExportPNG(const std::string &filename) {
 		ASSERT(data, "Image data does not exists");
 
@@ -207,7 +226,7 @@
 		return data->ExportBMP(out);
 	}
 
-	bool Bitmap::ExportJPG(const std::string &filename, int quality) {
+	bool Bitmap::ExportJPEG(const std::string &filename, int quality) {
 		ASSERT(data, "Image data does not exists");
 
 		if(GetMode()!=Graphics::ColorMode::RGB &&
--- a/Source/Gorgon/Graphics/Bitmap.h	Sun Dec 13 10:53:14 2020 +0200
+++ b/Source/Gorgon/Graphics/Bitmap.h	Tue Dec 15 12:54:04 2020 +0200
@@ -524,6 +524,12 @@
 		/// If image data is already discarded, there is no way to retrieve it. May throw if color mode is not supported
 		/// by PNG encoding. PNG encoding allows: RGB, RGBA, Grayscale, Grayscale alpha. Additionally, Alpha only
 		/// images are saved as grayscale alpha.
+		bool Export(const std::string &filename);
+        
+		/// Exports the data of the image resource to a PNG file. This function requires image data to be present.
+		/// If image data is already discarded, there is no way to retrieve it. May throw if color mode is not supported
+		/// by PNG encoding. PNG encoding allows: RGB, RGBA, Grayscale, Grayscale alpha. Additionally, Alpha only
+		/// images are saved as grayscale alpha.
 		bool ExportPNG(const std::string &filename);
 
 		/// Exports the data of the image resource to a PNG file. This function requires image data to be present.
@@ -547,7 +553,7 @@
 		/// Exports the data of the image resource to a JPG file. This function requires image data to be present.
 		/// If image data is already discarded, there is no way to retrieve it. May throw if color mode is not supported
 		/// by PNG encoding. JPG encoding allows: RGB and Grayscale. Quality is between 0 and 100.
-		bool ExportJPG(const std::string &filename, int quality = 90);
+		bool ExportJPEG(const std::string &filename, int quality = 90);
 
 		/// Exports the data of the image resource to a JPG file. This function requires image data to be present.
 		/// If image data is already discarded, there is no way to retrieve it. May throw if color mode is not supported

mercurial