2017-02-28 09:59:17 +01:00
using System ;
using System.Collections.Generic ;
2017-03-23 14:36:06 +01:00
using System.Diagnostics ;
2017-02-28 09:59:17 +01:00
using System.IO ;
using System.Linq ;
2017-03-23 14:36:06 +01:00
using System.Numerics ;
2017-02-28 09:59:17 +01:00
using System.Threading.Tasks ;
2017-09-19 15:21:39 +02:00
using MaddoLibrary.Base.Log ;
2017-03-23 14:36:06 +01:00
using SixLabors.Fonts ;
2017-09-19 14:49:29 +02:00
using SixLabors.ImageSharp ;
using SixLabors.ImageSharp.Drawing ;
2017-03-23 14:36:06 +01:00
using Font = SixLabors . Fonts . Font ;
using FontFamily = SixLabors . Fonts . FontFamily ;
using FontStyle = SixLabors . Fonts . FontStyle ;
2017-09-19 14:49:29 +02:00
using Rgba32 = SixLabors . ImageSharp . Rgba32 ;
using Image = SixLabors . ImageSharp . Image ;
2017-02-28 09:59:17 +01:00
namespace CatalogLib
{
public class ImgSharpCreator : IImageProcessor
{
2017-09-19 14:49:29 +02:00
2017-03-23 14:36:06 +01:00
private FileInfo _currentFile ;
2017-02-28 09:59:17 +01:00
public void Start ( FileInfo workFile )
{
2017-03-23 14:36:06 +01:00
_currentFile = workFile ;
2017-04-17 13:06:09 +02:00
2017-06-18 16:23:12 +02:00
using ( Image < Rgba32 > image = Image . Load ( workFile . FullName ) /* new Image(workFile.FullName)*/ )
2017-02-28 09:59:17 +01:00
{
2017-09-19 15:21:39 +02:00
MaddoLogger . Log ( "Loaded Image: {0}" , workFile . FullName ) ;
2017-07-04 12:40:51 +02:00
//image.Rotate(-90);
2017-03-16 18:48:25 +01:00
2017-07-04 12:40:51 +02:00
if ( PicSettings . Instance . GeneraleRotazioneAutomatica )
{
2017-09-19 14:49:29 +02:00
image . Mutate ( img = > img . AutoOrient ( ) ) ;
2017-09-19 15:21:39 +02:00
MaddoLogger . Log ( "Rotated Image: {0}" , workFile . FullName ) ;
2017-09-19 14:49:29 +02:00
//image.AutoOrient();
2017-07-04 12:40:51 +02:00
//var exif = image.MetaData.ExifProfile;
//if (exif != null)
//{
// var o = exif.GetValue(ExifTag.Orientation);
// if (o != null)
// {
// var v = (ushort)o.Value;
// switch (v)
// {
// //1 = Horizontal(normal)
// //2 = Mirror horizontal
// //3 = Rotate 180
// //4 = Mirror vertical
// //5 = Mirror horizontal and rotate 270 CW
// //6 = Rotate 90 CW
// //7 = Mirror horizontal and rotate 90 CW
// //8 = Rotate 270 CW
// case 1:
// break;
// case 2:
// image.Flip(FlipType.Horizontal);
// break;
// case 3:
// //image.Rotate(180f);
// //image.Rotate(90);
// image.Rotate(RotateType.Rotate180);
// break;
// case 4:
// image.Flip(FlipType.Vertical);
// break;
// case 5:
// image.RotateFlip(RotateType.Rotate270, FlipType.Horizontal);
// break;
// case 6:
// image.Rotate(RotateType.Rotate90);
// break;
// case 7:
// image.RotateFlip(RotateType.Rotate90, FlipType.Horizontal);
// break;
// case 8:
// //image.Rotate(-90);
// image.Rotate(RotateType.Rotate270);
// image.MetaData.ExifProfile.SetValue(ExifTag.Orientation, new ExifValue(exif.GetValue(ExifTag.Orientation)).Value = (ushort)1);
// break;
// }
// }
//}
2017-03-16 18:48:25 +01:00
}
2017-04-17 13:06:09 +02:00
if ( PicSettings . Instance . EnableText )
{
SetExtraText ( image ) ;
2017-09-19 15:21:39 +02:00
MaddoLogger . Log ( "Drawn text on Image: {0}" , workFile . FullName ) ;
2017-04-17 13:06:09 +02:00
}
2017-03-23 14:36:06 +01:00
2017-03-16 18:48:25 +01:00
//JpegDecoder j = new JpegDecoder();
2017-03-23 14:36:06 +01:00
var va = Vector . IsHardwareAccelerated ;
2017-09-19 15:21:39 +02:00
MaddoLogger . Log ( "Hardware Accelerated: {0}" , va ) ;
2017-04-18 08:26:28 +02:00
//image.Resize(PicSettings.Instance.FotoLarghezza, PicSettings.Instance.FotoAltezza);
2017-03-23 14:36:06 +01:00
//image.Resize(2240, 2240);
//var fff = FontCollection.SystemFonts.Find(PicSettings.Instance.NomeFont);
//var font = new Font(fff, (float)PicSettings.Instance.DimensioneFont, FontStyle.Regular);
//image.DrawText("sssssssssssssssssssssssssssssssssssssssssssssss", font, Color.Black, new Vector2(200, 200));
2017-03-17 16:23:29 +01:00
image . Save ( Path . Combine ( PicSettings . Instance . DirectoryDestinazione , workFile . Name ) ) ;
2017-03-16 18:48:25 +01:00
//image.Resize(200, 200).Save("");
2017-09-19 15:21:39 +02:00
MaddoLogger . Log ( "Saved Image: {0} to: {1}" , workFile . FullName , Path . Combine ( PicSettings . Instance . DirectoryDestinazione , workFile . Name ) ) ;
2017-02-28 09:59:17 +01:00
}
}
2017-03-17 16:23:29 +01:00
2017-06-18 16:23:12 +02:00
private void SetExtraText ( Image < Rgba32 > image )
2017-03-17 16:23:29 +01:00
{
2017-03-23 14:36:06 +01:00
if ( string . IsNullOrWhiteSpace ( PicSettings . Instance . TestoApplicareOrizzontale ) )
{
Debug . WriteLine ( $"{_currentFile.Name} No text" ) ;
return ;
}
2017-07-04 12:40:51 +02:00
var fo = SixLabors . Fonts . SystemFonts . Find ( PicSettings . Instance . NomeFont ) ;
//var fff = FontCollection.SystemFonts.Find(PicSettings.Instance.NomeFont);
2017-03-23 14:36:06 +01:00
//var fff = FontCollection.SystemFonts.Find("Segoe Print");
2017-07-04 12:40:51 +02:00
var font = new Font ( fo , ( float ) PicSettings . Instance . DimensioneFont , FontStyle . Regular ) ;
2017-03-23 14:36:06 +01:00
//var font = new Font(fff, 8f, FontStyle.Regular);
2017-06-18 16:23:12 +02:00
//Color c = Color.FromHex(FlipRgbString(PicSettings.Instance.ColoreTestoRGB));
Rgba32 g = Rgba32 . FromHex ( FlipRgbString ( PicSettings . Instance . ColoreTestoRGB ) ) ;
2017-07-04 12:40:51 +02:00
//TextMeasurer measurer = new TextMeasurer();
//var size = measurer.MeasureText(PicSettings.Instance.TestoApplicareOrizzontale, font, 72);
//float scalingFactor = Math.Min(image.Width / size.Width, image.Height / size.Height);
//Font scaledFont = new Font(font, scalingFactor * font.Size);
//image.DrawText(PicSettings.Instance.TestoApplicareOrizzontale, scaledFont, g, new Vector2(0, 0));
2017-04-18 08:26:28 +02:00
2017-07-04 12:40:51 +02:00
Vector2 center = new Vector2 ( image . Width / 2 , image . Height / 2 ) ; //center horizontally, 10px down
var size = TextMeasurer . Measure ( PicSettings . Instance . TestoApplicareOrizzontale , new RendererOptions ( font ) ) ;
2017-04-18 08:26:28 +02:00
float scalingFactor = Math . Min ( image . Width / size . Width , image . Height / size . Height ) ;
Font scaledFont = new Font ( font , scalingFactor * font . Size ) ;
2017-09-19 15:21:39 +02:00
image . Mutate ( x = > x . DrawText ( PicSettings . Instance . TestoApplicareOrizzontale , scaledFont , g , center , new TextGraphicsOptions ( true ) { HorizontalAlignment = HorizontalAlignment . Center , VerticalAlignment = VerticalAlignment . Bottom } ) ) ;
//image.DrawText(PicSettings.Instance.TestoApplicareOrizzontale, scaledFont, g, center, new TextGraphicsOptions(true) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Bottom });
2017-07-04 12:40:51 +02:00
2017-03-23 14:36:06 +01:00
2017-07-04 12:40:51 +02:00
}
private void WriteTextFixed ( Image < Rgba32 > image )
{
var fo = SixLabors . Fonts . SystemFonts . Find ( "Microsoft Sans Serif" ) ;
var font = new Font ( fo , 8 , FontStyle . Regular ) ;
Rgba32 g = Rgba32 . FromHex ( "#FF00FFFF" ) ;
Vector2 center = new Vector2 ( image . Width / 2 , image . Height / 2 ) ;
var size = TextMeasurer . Measure ( "Test test test test test" , new RendererOptions ( font ) ) ;
float scalingFactor = Math . Min ( image . Width / size . Width , image . Height / size . Height ) ;
Font scaledFont = new Font ( font , scalingFactor * font . Size ) ;
2017-09-19 15:21:39 +02:00
image . Mutate ( x = > x . DrawText ( "Test test test test test" , scaledFont , g , center , new TextGraphicsOptions ( true ) { HorizontalAlignment = HorizontalAlignment . Center , VerticalAlignment = VerticalAlignment . Bottom } ) ) ;
2017-03-17 16:23:29 +01:00
}
2017-04-18 08:26:28 +02:00
private string FlipRgbString ( string originalString )
{
2017-07-04 12:40:51 +02:00
string s ;
if ( originalString . Length = = 7 )
{
s = string . Concat ( "#" , originalString . Substring ( 1 , 6 ) ) ;
}
else
{
s = string . Concat ( "#" , originalString . Substring ( 3 , 6 ) , originalString . Substring ( 1 , 2 ) ) ;
}
2017-04-18 08:26:28 +02:00
return s ;
}
2017-02-28 09:59:17 +01:00
}
}