done for now
This commit is contained in:
parent
ba93b57205
commit
ae5aa7fffb
|
|
@ -1,93 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using Pgm;
|
||||
|
||||
class PgmCreatorDemo {
|
||||
|
||||
//TYPICAL USAGE OF THE PGMCREATOR CLASS:
|
||||
static void MakeLenaBlackWhite() {
|
||||
// for the specification of PGM-Files, see: http://netpbm.sourceforge.net/doc/pgm.html
|
||||
|
||||
// A byte-array is used for storing the gray-color-values.
|
||||
// The img has the size 256x256 pixel;
|
||||
// Each pixel has a gray-color-value from 0-255 (0...black, 255...white).
|
||||
// Note also that the FIRST dimension (row-index) corresponds
|
||||
// to the x-Coordinate (!!!) in the image, the second dimension (col-index)
|
||||
// to the y-coordinate (!!!). The y coordinate goes from up to down!
|
||||
// Thus, an index of [50, 250] is at the left and bottom of the image.
|
||||
// (The Write/ReadPGM-Methods internally transpose the matrix.)
|
||||
|
||||
// Open Lena, make it black-white (threshold is the value 128), and save it:
|
||||
try {
|
||||
byte[,] lenaImgData = PgmCreator.ReadPgmFile("lena.pgm");
|
||||
for (int x = 0; x < lenaImgData.GetLength(0); x++) {
|
||||
for (int y = 0; y < lenaImgData.GetLength(1); y++) {
|
||||
if (lenaImgData[x, y] < 128) lenaImgData[x, y] = 0;
|
||||
else lenaImgData[x, y] = 255;
|
||||
}
|
||||
}
|
||||
PgmCreator.WritePgmFile("lena_black_white.pgm", lenaImgData, PgmType.P5);
|
||||
Console.WriteLine("Success making Lena black-white!");
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
Console.WriteLine("Where ist the file? " + e.Message);
|
||||
}
|
||||
catch (FormatException e) {
|
||||
Console.WriteLine("Wrong file format: " + e.Message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args) {
|
||||
TestBinaryAndASCIIFormat();
|
||||
MakeLenaBlackWhite();
|
||||
}
|
||||
|
||||
//Helper Method which produces a byte-array representing a gradient image:
|
||||
private static byte[,] ProduceGradientImage() {
|
||||
byte[,] imgData = new byte[256, 256];
|
||||
|
||||
// fill the imgData-Array with the gray-color-values:
|
||||
for (int y = 0; y < imgData.GetLength(1); y++) {
|
||||
for (int x = 0; x < imgData.GetLength(0); x++) {
|
||||
// produce a gray-color gradient image:
|
||||
// top-left corner: black, bottom-right corner: white.
|
||||
// Note that x and y go from 0 to 255, so x/2 + y/2 cannot exceed the value 254.
|
||||
imgData[x, y] = (byte)(x / 2 + y / 2);
|
||||
}
|
||||
}
|
||||
return imgData;
|
||||
}
|
||||
|
||||
//Tests whether both pgm file formats work:
|
||||
private static void TestBinaryAndASCIIFormat() {
|
||||
byte[,] imgData = ProduceGradientImage();
|
||||
|
||||
byte[,] readDataAscii;
|
||||
byte[,] readDataBinary;
|
||||
try {
|
||||
// write imgData to P2 file (ascii):
|
||||
PgmCreator.WritePgmFile("testASCII.pgm", imgData, PgmType.P2);
|
||||
// read from P2 file (ascii):
|
||||
readDataAscii = PgmCreator.ReadPgmFile("testASCII.pgm");
|
||||
// write image data to P5 file (binary):
|
||||
PgmCreator.WritePgmFile("testBinary.pgm", readDataAscii, PgmType.P5);
|
||||
// read P5 file (binary):
|
||||
readDataBinary = PgmCreator.ReadPgmFile("testBinary.pgm");
|
||||
|
||||
//check if the image loaded from the P2-type file (ascii)
|
||||
//is the same as from the P5-type file (binary):
|
||||
for (int y = 0; y < imgData.GetLength(1); y++) {
|
||||
for (int x = 0; x < imgData.GetLength(0); x++) {
|
||||
Debug.Assert(readDataAscii[x,y] == readDataBinary[x,y]);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("Success in testing two generated files of both types!");
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
107
Program.cs
107
Program.cs
|
|
@ -1,14 +1,111 @@
|
|||
using System;
|
||||
using Pgm;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
|
||||
namespace task_78
|
||||
{
|
||||
class Program
|
||||
public class Program
|
||||
{
|
||||
static void Main(string[] args) {
|
||||
Font inFont = readFont(Directory.GetCurrentDirectory() + "\\font.pgm");
|
||||
if (inFont == null)
|
||||
return;
|
||||
|
||||
writeFont(inFont, "font_print.bmp");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
static Font readFont(string path) {
|
||||
return new Font(PgmCreator.ReadPgmFile(path));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves all Font.Char-Objects listed within the Font class into a new bmp file.
|
||||
/// </summary>
|
||||
/// <param name="font"></param>
|
||||
/// <param name="path"></param>
|
||||
static void writeFont(Font font, string path) {
|
||||
// create a new bmp with with equal dimensions to the font's data array.
|
||||
Bitmap bmp = new Bitmap(font.data.GetLength(0), font.data.GetLength(1));
|
||||
// incrementor to not draw every character into the same spot.
|
||||
int shift = 0;
|
||||
// Iterate through all characters
|
||||
foreach (Font.Char c in font.chars) {
|
||||
for (int x = 0; x < c.w; x++) {
|
||||
for (int y = 0; y < c.h; y++) {
|
||||
// Print every single pixel of each character
|
||||
try {
|
||||
bmp.SetPixel(shift + x, y, Color.FromArgb(255, c.data[x, y], c.data[x, y], c.data[x, y]));
|
||||
} catch(Exception e) {
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Shift Char.Width pixels to the right + 1 Pixel for Seperation.
|
||||
shift += c.w + 1;
|
||||
}
|
||||
bmp.Save(path);
|
||||
Console.WriteLine("Bitmap saved!");
|
||||
}
|
||||
}
|
||||
|
||||
public class Font {
|
||||
public byte[,] data { get; private set; }
|
||||
public List<Char> chars { get; private set; }
|
||||
|
||||
public Font(byte[,] data) {
|
||||
this.chars = new List<Char>();
|
||||
this.data = data;
|
||||
this.getCharacters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// attempts to read all characters stored in the data[,] information by splitting all characters using the grayscale value of '150' as given in the task.
|
||||
/// all readable characters will be stored in the chars-List<Char>
|
||||
/// </summary>
|
||||
private void getCharacters() {
|
||||
int i = 0, count = 0;
|
||||
// a new list, containing byte[]s to store the vertical row of pixels for each character
|
||||
List<byte[]> strip = new List<byte[]>();
|
||||
// clear the character list, just in case this font was already written before.
|
||||
chars.Clear();
|
||||
do {
|
||||
// Do we have a seperating greyscale code?
|
||||
if (data[i, 0] == 150 && i > 0) {
|
||||
// create a new 2 dimensional byte where we can temporarily write the character into
|
||||
byte[,] c = new byte[strip.Count, strip[0].Length];
|
||||
for (int j = 0; j < strip.Count; j++) {
|
||||
for (int k = 0; k < strip[0].Length; k++) {
|
||||
c[j, k] = strip[j][k];
|
||||
}
|
||||
}
|
||||
// increment the char counter
|
||||
count++;
|
||||
// add a new char struct to the list
|
||||
chars.Add(new Font.Char { key = "c_" + count, data = c, w = c.GetLength(0), h = c.GetLength(1) });
|
||||
// clear the strip storage for the next character
|
||||
strip.Clear();
|
||||
Console.WriteLine("Found Character " + count);
|
||||
} else {
|
||||
// if no seperating color code was detected, we add another byte[] to the strip list
|
||||
byte[] s = new byte[data.GetLength(1)];
|
||||
for(int j = 0; j < s.Length; j++) {
|
||||
s[j] = data[i, j];
|
||||
}
|
||||
strip.Add(s);
|
||||
}
|
||||
|
||||
} while (i++ < data.GetLength(0) - 1);
|
||||
}
|
||||
|
||||
public struct Char
|
||||
{
|
||||
public string key;
|
||||
public byte[,] data;
|
||||
public int w, h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
|
|
@ -43,11 +44,16 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="PgmCreator.cs" />
|
||||
<Compile Include="PgmCreatorDemo.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="font.pgm">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
|
|
|||
Loading…
Reference in New Issue