-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
298 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
using System.Text; | ||
using SD = System.Drawing; | ||
namespace Tesseract | ||
{ | ||
public class PixToBitmapConverter | ||
{ | ||
public Bitmap Convert(Pix pix, bool includeAlpha = false) | ||
{ | ||
var pixelFormat = GetPixelFormat(pix); | ||
var depth = pix.Depth; | ||
var img = new Bitmap(pix.Width, pix.Height, pixelFormat); | ||
|
||
BitmapData imgData = null; | ||
PixData pixData = null; | ||
try { | ||
// TODO: Set X and Y resolution | ||
|
||
// transfer pixel data | ||
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) { | ||
TransferData32(pixData, imgData, includeAlpha ? 0 : 255); | ||
} else if (depth == 16) { | ||
TransferData16(pixData, imgData); | ||
} else if (depth == 8) { | ||
TransferData8(pixData, imgData); | ||
} else if (depth == 1) { | ||
TransferData1(pixData, imgData); | ||
} | ||
return img; | ||
} catch (Exception) { | ||
img.Dispose(); | ||
throw; | ||
} finally { | ||
if (imgData != null) { | ||
img.UnlockBits(imgData); | ||
} | ||
} | ||
} | ||
|
||
private unsafe void TransferData32(PixData pixData, BitmapData imgData, int alphaMask) | ||
{ | ||
var imgFormat = imgData.PixelFormat; | ||
var height = imgData.Height; | ||
var width = imgData.Width; | ||
|
||
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++) { | ||
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 | ||
} | ||
} | ||
} | ||
|
||
private unsafe void TransferData16(PixData pixData, BitmapData imgData) | ||
{ | ||
var imgFormat = imgData.PixelFormat; | ||
var height = imgData.Height; | ||
var width = imgData.Width; | ||
|
||
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++) { | ||
ushort pixVal = (ushort)PixData.GetDataTwoByte(pixLine, x); | ||
|
||
imgLine[x] = pixVal; | ||
} | ||
} | ||
} | ||
|
||
private unsafe void TransferData8(PixData pixData, BitmapData imgData) | ||
{ | ||
var imgFormat = imgData.PixelFormat; | ||
var height = imgData.Height; | ||
var width = imgData.Width; | ||
|
||
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++) { | ||
byte pixVal = (byte)PixData.GetDataByte(pixLine, x); | ||
|
||
imgLine[x] = pixVal; | ||
} | ||
} | ||
} | ||
|
||
private unsafe void TransferData1(PixData pixData, BitmapData imgData) | ||
{ | ||
var imgFormat = imgData.PixelFormat; | ||
var height = imgData.Height; | ||
var width = imgData.Width/8; | ||
|
||
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++) { | ||
byte pixVal = (byte)PixData.GetDataByte(pixLine, x); | ||
|
||
imgLine[x] = pixVal; | ||
} | ||
} | ||
} | ||
|
||
private void TransferPalette(Pix pix, Bitmap img) | ||
{ | ||
var pallete = img.Palette; | ||
var maxColors = pallete.Entries.Length; | ||
var lastColor = maxColors - 1; | ||
var colormap = pix.Colormap; | ||
if (colormap != null && colormap.Count <= maxColors) { | ||
var colormapCount = colormap.Count; | ||
for (int i = 0; i < colormapCount; i++) { | ||
pallete.Entries[i] = (SD.Color)colormap[i]; | ||
} | ||
} else { | ||
for (int i = 0; i < maxColors; i++) { | ||
var value = (byte)(i * 255 / lastColor); | ||
pallete.Entries[i] = SD.Color.FromArgb(value, value, value); | ||
} | ||
} | ||
// This is required to force the palette to update! | ||
img.Palette = pallete; | ||
} | ||
|
||
|
||
private PixelFormat GetPixelFormat(Pix pix) | ||
{ | ||
switch (pix.Depth) { | ||
case 1: return PixelFormat.Format1bppIndexed; | ||
//case 2: return PixelFormat.Format4bppIndexed; | ||
//case 4: return PixelFormat.Format4bppIndexed; | ||
case 8: return PixelFormat.Format8bppIndexed; | ||
case 16: return PixelFormat.Format16bppGrayScale; | ||
case 32: return PixelFormat.Format32bppArgb; | ||
default: throw new InvalidOperationException(String.Format("Pix depth {0} is not supported.", pix.Depth)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
load_system_dawg F | ||
load_freq_dawg F | ||
user_words_suffix user-words | ||
user_patterns_suffix user-patterns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1-\d\d\d-GOOG-411 | ||
www.\n\\\*.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
the | ||
quick | ||
brown | ||
fox | ||
jumped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.