Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PixConverter .NET Core support. #463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/Tesseract/BitmapToPixConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if NETFULL

using System;
using System;
using System.Drawing;
using System.Drawing.Imaging;

Expand Down Expand Up @@ -169,5 +167,3 @@ private unsafe void TransferDataFormat8bppIndexed(BitmapData imgData, PixData pi
}
}
}

#endif
3 changes: 0 additions & 3 deletions src/Tesseract/PixColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/Tesseract/PixConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if NETFULL

using System;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
Expand Down Expand Up @@ -36,5 +34,3 @@ public static Pix ToPix(Bitmap img)
}
}
}

#endif
86 changes: 55 additions & 31 deletions src/Tesseract/PixToBitmapConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if NETFULL

using System;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
Expand All @@ -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);
}
}
Expand All @@ -57,19 +69,21 @@ 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);
pixelPtr[0] = pixVal.Blue;
pixelPtr[1] = pixVal.Green;
pixelPtr[2] = pixVal.Red;
pixelPtr[3] = (byte)(alphaMask | pixVal.Alpha); // Allow user to include alpha or not
}
}
}
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -162,6 +188,4 @@ private PixelFormat GetPixelFormat(Pix pix)
}
}
}
}

#endif
}
3 changes: 3 additions & 0 deletions src/Tesseract/Tesseract.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<DocumentationFile>.\Tesseract.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Drawing.Common">
<Version>4.5.1</Version>
</PackageReference>
<PackageReference Include="System.Reflection.Emit">
<Version>4.3.0</Version>
</PackageReference>
Expand Down