using System.IO;
//using System.Linq;
// using MarketData.Utils;
//using System.Drawing.Imaging;
//using System.Runtime.InteropServices;
//using System.Drawing;
using SkiaSharp;

namespace MarketData.CNNProcessing
{
	public static class BitmapExtensions
	{
// This call is used to generate the CNN training, test, and validation bitmaps..  
// I noticed a large difference in the accuracy or the model when the quality parameter is 100.
// I therefore intentionally not using EncoderParameters to specify quality but instead taking the default quality.
      /// <summary>
      /// Save JPG encoded bitmap
      /// </summary>
      /// <param name="bitmap"></param>
      /// <param name="filename"></param>
			public static void SaveJPG100(this SKBitmap bitmap, string filename)
			{       
			  if(File.Exists(filename))File.Delete(filename);
				using FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
				using SKImage image = SKImage.FromBitmap(bitmap); 
				using SKData encodedImage = image.Encode(SKEncodedImageFormat.Jpeg,100);
				encodedImage.SaveTo(stream);		
			}

      /// <summary>
      /// Save JPG encoded bitmap
      /// </summary>
      /// <param name="bitmap"></param>
      /// <param name="stream"></param>
			public static void SaveJPG100(this SKBitmap bitmap, Stream stream)
			{
				using SKImage image = SKImage.FromBitmap(bitmap); 
				using SKData encodedImage = image.Encode(SKEncodedImageFormat.Jpeg,100);
				encodedImage.SaveTo(stream);			
			}

      /// <summary>
      /// Save PNG encoded bitmap
      /// </summary>
      /// <param name="bitmap"></param>
      /// <param name="filename"></param>
			public static void SavePNG100(this SKBitmap bitmap, string filename)
			{       
			  if(File.Exists(filename))File.Delete(filename);
				using FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
				using SKImage image = SKImage.FromBitmap(bitmap); 
				using SKData encodedImage = image.Encode(SKEncodedImageFormat.Png,100);
				encodedImage.SaveTo(stream);		
			}

      /// <summary>
      /// Save PNG encoded bitmap
      /// </summary>
      /// <param name="bitmap"></param>
      /// <param name="stream"></param>
			public static void SavePNG100(this SKBitmap bitmap, Stream stream)
			{
				using SKImage image = SKImage.FromBitmap(bitmap); 
				using SKData encodedImage = image.Encode(SKEncodedImageFormat.Png,100);
				encodedImage.SaveTo(stream);			
			}      
	}
}
