From 741db0dd6ef5b9eb6854957c5cbe85221d6cb857 Mon Sep 17 00:00:00 2001 From: Greg Horvath Date: Tue, 19 Feb 2019 00:20:51 -0500 Subject: [PATCH] PixConverter .NET Core support. --- src/Tesseract/BitmapToPixConverter.cs | 6 +- src/Tesseract/PixColor.cs | 3 - src/Tesseract/PixConverter.cs | 6 +- src/Tesseract/PixToBitmapConverter.cs | 86 +++++++++++++++++---------- src/Tesseract/Tesseract.csproj | 3 + 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/Tesseract/BitmapToPixConverter.cs b/src/Tesseract/BitmapToPixConverter.cs index 752ac79f..6d47e53f 100644 --- a/src/Tesseract/BitmapToPixConverter.cs +++ b/src/Tesseract/BitmapToPixConverter.cs @@ -1,6 +1,4 @@ -#if NETFULL - -using System; +using System; using System.Drawing; using System.Drawing.Imaging; @@ -169,5 +167,3 @@ private unsafe void TransferDataFormat8bppIndexed(BitmapData imgData, PixData pi } } } - -#endif \ No newline at end of file diff --git a/src/Tesseract/PixColor.cs b/src/Tesseract/PixColor.cs index 7dc4a912..79005751 100644 --- a/src/Tesseract/PixColor.cs +++ b/src/Tesseract/PixColor.cs @@ -52,7 +52,6 @@ public uint ToRGBA() alpha); } -#if NETFULL public static explicit operator System.Drawing.Color(PixColor color) { return System.Drawing.Color.FromArgb(color.alpha, color.red, color.green, color.blue); @@ -62,8 +61,6 @@ public static explicit operator PixColor(System.Drawing.Color color) { return new PixColor(color.R, color.G, color.B, color.A); } -#endif - #region Equals and GetHashCode implementation public override bool Equals(object obj) diff --git a/src/Tesseract/PixConverter.cs b/src/Tesseract/PixConverter.cs index 0cab5463..cc3ed754 100644 --- a/src/Tesseract/PixConverter.cs +++ b/src/Tesseract/PixConverter.cs @@ -1,6 +1,4 @@ -#if NETFULL - -using System; +using System; using System.Collections.Generic; using System.Drawing; using System.Text; @@ -36,5 +34,3 @@ public static Pix ToPix(Bitmap img) } } } - -#endif \ No newline at end of file diff --git a/src/Tesseract/PixToBitmapConverter.cs b/src/Tesseract/PixToBitmapConverter.cs index 85635b75..dcd7432d 100644 --- a/src/Tesseract/PixToBitmapConverter.cs +++ b/src/Tesseract/PixToBitmapConverter.cs @@ -1,6 +1,4 @@ -#if NETFULL - -using System; +using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; @@ -19,33 +17,47 @@ public Bitmap Convert(Pix pix, bool includeAlpha = false) BitmapData imgData = null; PixData pixData = null; - try { + try + { // TODO: Set X and Y resolution // transfer pixel data - if ((pixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) { + if ((pixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) + { TransferPalette(pix, img); } // transfer data pixData = pix.GetData(); imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, pixelFormat); - - if (depth == 32) { + + if (depth == 32) + { TransferData32(pixData, imgData, includeAlpha ? 0 : 255); - } else if (depth == 16) { + } + else if (depth == 16) + { TransferData16(pixData, imgData); - } else if (depth == 8) { + } + else if (depth == 8) + { TransferData8(pixData, imgData); - } else if (depth == 1) { + } + else if (depth == 1) + { TransferData1(pixData, imgData); } return img; - } catch (Exception) { + } + catch (Exception) + { img.Dispose(); throw; - } finally { - if (imgData != null) { + } + finally + { + if (imgData != null) + { img.UnlockBits(imgData); } } @@ -57,11 +69,13 @@ private unsafe void TransferData32(PixData pixData, BitmapData imgData, int alph var height = imgData.Height; var width = imgData.Width; - for (int y = 0; y < height; y++) { + for (int y = 0; y < height; y++) + { byte* imgLine = (byte*)imgData.Scan0 + (y * imgData.Stride); uint* pixLine = (uint*)pixData.Data + (y * pixData.WordsPerLine); - for (int x = 0; x < width; x++) { + for (int x = 0; x < width; x++) + { var pixVal = PixColor.FromRgba(pixLine[x]); byte* pixelPtr = imgLine + (x << 2); @@ -69,7 +83,7 @@ private unsafe void TransferData32(PixData pixData, BitmapData imgData, int alph pixelPtr[1] = pixVal.Green; pixelPtr[2] = pixVal.Red; pixelPtr[3] = (byte)(alphaMask | pixVal.Alpha); // Allow user to include alpha or not - } + } } } @@ -79,11 +93,13 @@ private unsafe void TransferData16(PixData pixData, BitmapData imgData) var height = imgData.Height; var width = imgData.Width; - for (int y = 0; y < height; y++) { + for (int y = 0; y < height; y++) + { uint* pixLine = (uint*)pixData.Data + (y * pixData.WordsPerLine); ushort* imgLine = (ushort*)imgData.Scan0 + (y * imgData.Stride); - for (int x = 0; x < width; x++) { + for (int x = 0; x < width; x++) + { ushort pixVal = (ushort)PixData.GetDataTwoByte(pixLine, x); imgLine[x] = pixVal; @@ -97,11 +113,13 @@ private unsafe void TransferData8(PixData pixData, BitmapData imgData) var height = imgData.Height; var width = imgData.Width; - for (int y = 0; y < height; y++) { + for (int y = 0; y < height; y++) + { uint* pixLine = (uint*)pixData.Data + (y * pixData.WordsPerLine); byte* imgLine = (byte*)imgData.Scan0 + (y * imgData.Stride); - for (int x = 0; x < width; x++) { + for (int x = 0; x < width; x++) + { byte pixVal = (byte)PixData.GetDataByte(pixLine, x); imgLine[x] = pixVal; @@ -113,13 +131,15 @@ private unsafe void TransferData1(PixData pixData, BitmapData imgData) { var imgFormat = imgData.PixelFormat; var height = imgData.Height; - var width = imgData.Width/8; + var width = imgData.Width / 8; - for (int y = 0; y < height; y++) { + for (int y = 0; y < height; y++) + { uint* pixLine = (uint*)pixData.Data + (y * pixData.WordsPerLine); byte* imgLine = (byte*)imgData.Scan0 + (y * imgData.Stride); - for (int x = 0; x < width; x++) { + for (int x = 0; x < width; x++) + { byte pixVal = (byte)PixData.GetDataByte(pixLine, x); imgLine[x] = pixVal; @@ -133,13 +153,18 @@ private void TransferPalette(Pix pix, Bitmap img) var maxColors = pallete.Entries.Length; var lastColor = maxColors - 1; var colormap = pix.Colormap; - if (colormap != null && colormap.Count <= maxColors) { + if (colormap != null && colormap.Count <= maxColors) + { var colormapCount = colormap.Count; - for (int i = 0; i < colormapCount; i++) { + for (int i = 0; i < colormapCount; i++) + { pallete.Entries[i] = (SD.Color)colormap[i]; } - } else { - for (int i = 0; i < maxColors; i++) { + } + else + { + for (int i = 0; i < maxColors; i++) + { var value = (byte)(i * 255 / lastColor); pallete.Entries[i] = SD.Color.FromArgb(value, value, value); } @@ -151,7 +176,8 @@ private void TransferPalette(Pix pix, Bitmap img) private PixelFormat GetPixelFormat(Pix pix) { - switch (pix.Depth) { + switch (pix.Depth) + { case 1: return PixelFormat.Format1bppIndexed; //case 2: return PixelFormat.Format4bppIndexed; //case 4: return PixelFormat.Format4bppIndexed; @@ -162,6 +188,4 @@ private PixelFormat GetPixelFormat(Pix pix) } } } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Tesseract/Tesseract.csproj b/src/Tesseract/Tesseract.csproj index be56eee6..854a8bc6 100644 --- a/src/Tesseract/Tesseract.csproj +++ b/src/Tesseract/Tesseract.csproj @@ -47,6 +47,9 @@ .\Tesseract.xml + + 4.5.1 + 4.3.0