/* build cell table */ public static Cell[,] buildCellTable(Bitmap image, float threshold) { Cell[,] cellArr = new Cell[image.Width/2, image.Height/3]; Color color = new Color(); for (int majr = 0; majr < cellArr.GetLength(1); majr++){ for (int majc = 0; majc < cellArr.GetLength(0); majc++){ cellArr[majc, majr] = new Cell(); for (int minr = 0; minr < 3; minr++){ for (int minc = 0; minc < 2; minc++){ color = image.GetPixel((2 * majc) + minc, (3 * majr) + minr); if (color.GetBrightness() <= threshold){ cellArr[majc, majr].addDot((3 * minc) + minr + 1); } } } } } return cellArr; }
public int numMatches(Cell otherCell) { int numMatches = 0; if (dot1 && dot1 == otherCell.dot1) numMatches++; if (dot2 && dot2 == otherCell.dot2) numMatches++; if (dot3 && dot3 == otherCell.dot3) numMatches++; if (dot4 && dot4 == otherCell.dot4) numMatches++; if (dot5 && dot5 == otherCell.dot5) numMatches++; if (dot6 && dot6 == otherCell.dot6) numMatches++; return numMatches; }
/* write output file */ public static void writeOutput(Cell[,] cellArr, Hashtable map, string fileName) { TextWriter text = new StreamWriter(fileName); for (int y = 0; y < cellArr.GetLength(1); y++){ for (int x = 0; x < cellArr.GetLength(0); x++){ text.Write(map[cellArr[x, y].getKey()]); } text.WriteLine(); } text.Close(); }
public static int[,] buildBinTable(Bitmap image) { int cellx = 0; int celly = 0; int cellnum = 0; int cellArrx = 0; int cellArry = 0; Cell[,] fullPage = new Cell[image.Width/2, image.Height/3]; int[,] binArray = new int[image.Width, image.Height]; Color color = new Color(); for (int y = 0; y < image.Height; y+=3) { for (int x = 0; x < image.Width; x+=2) { int count = 1; cellx = 0; int tempx = x; while (cellx < 2) { int tempy = y; celly = 0; while (celly < 3) { color = image.GetPixel(tempx, tempy); switch (count) { case 1: fullPage[cellArrx, cellArry] = new Cell(); if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(1); } else { binArray[tempx, tempy] = 1; } break; case 2: if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(2); } else { binArray[tempx, tempy] = 1; } break; case 3: if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(3); } else { binArray[tempx, tempy] = 1; } break; case 4: if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(4); } else { binArray[tempx, tempy] = 1; } break; case 5: if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(5); } else { binArray[x, tempy] = 1; } break; case 6: if (color.GetBrightness() < 0.5) { binArray[tempx, tempy] = 0; fullPage[cellArrx, cellArry].addDot(6); } else { binArray[tempx, tempy] = 1; } cellnum++; break; } celly++; tempy++; count++; } cellx++; tempx++; } Console.WriteLine(fullPage[cellArrx, cellArry].getKey()); cellArrx++; } cellArrx = 0; cellArry++; } Console.WriteLine("writing to output file"); Console.WriteLine("how many cells in the array"); Console.WriteLine(cellnum); return binArray; }
//Hashtable charset = new Hashtable(); static void Main(string[] args) { Hashtable charset = new Hashtable(); string line; string [] word; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("C:\\Documents and Settings\\t-sausan\\Desktop\\charset.txt"); while ((line = file.ReadLine()) != null) { word = line.Split(' '); Console.Write("Key: " ); Console.WriteLine(word[0]); Console.Write("Value: "); Console.WriteLine(word[1]); charset.Add(word[0], word[1]); } charset.Add("0", " "); file.Close(); Cell temp1 = new Cell(true, true, true, true,true, true); Cell temp2 = new Cell(true, false, false, false, false, false); Cell temp3 = new Cell(true, true, false, true, true, true); Cell temp4 = new Cell(true, false, true, true, true, true); Cell temp5 = new Cell(true, true, true, true, true, false); Cell temp6 = new Cell(true, true, false, false, true, true); Cell temp7 = new Cell(false, false, true, true, true, true); Cell temp8 = new Cell(false, false, false, false, false, false); string tem1 = temp1.getKey(); string tem2 = temp2.getKey(); string tem7 = temp7.getKey(); string tem8 = temp8.getKey(); string name = charset[tem1].ToString(); Console.WriteLine(name); name = charset[tem8].ToString(); Console.WriteLine(name); // Suspend the screen. Console.ReadLine(); Bitmap bmp = new Bitmap("C:\\Documents and Settings\\t-sausan\\My Documents\\My Pictures\\elephant.bmp"); Console.WriteLine("-- READING IMAGE --"); buildBinImage(bmp, "newImage.bmp"); Console.WriteLine("-- READING IMAGE COMPLETE --"); Console.WriteLine(); Console.WriteLine("-- BUILDING TABLE --"); int[,] binArray = buildBinTable(bmp); Console.WriteLine("-- BUILDING TABLE COMPLETE --"); Cell[,] fullPage = buildCelTable(bmp); TextWriter text = new StreamWriter("test.txt"); TextWriter braille = new StreamWriter("Braille.txt"); for (int z = 0; z < bmp.Height / 3; z++) { for (int w = 0; w < bmp.Width / 2; w++) { text.WriteLine("\n({0}, {1})", w, z); text.Write("Dot 1"); text.WriteLine(fullPage[w, z].getDot(1)); text.Write("Dot 2"); text.WriteLine(fullPage[w, z].getDot(2)); text.Write("Dot 3"); text.WriteLine(fullPage[w, z].getDot(3)); text.Write("Dot 4"); text.WriteLine(fullPage[w, z].getDot(4)); text.Write("Dot 5"); text.WriteLine(fullPage[w, z].getDot(5)); text.Write("Dot 6"); text.WriteLine(fullPage[w, z].getDot(6)); string tempkey = fullPage[w,z].getKey(); braille.Write(charset[tempkey].ToString()); } braille.WriteLine(); } text.Close(); braille.Close(); Console.WriteLine(); //printTable(binArray, bmp); Console.WriteLine(); //perform tests on empty cell Cell testC1 = new Cell(); Console.WriteLine(testC1); Console.WriteLine("testC1 numDots: {0}\n", testC1.getNumDots()); Console.WriteLine("--Testing addDot()"); Console.WriteLine("testC1.addDot(1): {0}", testC1.addDot(1)); Console.WriteLine("testC1.addDot(5): {0}", testC1.addDot(5)); Console.WriteLine("testC1 numDots: {0}\n", testC1.getNumDots()); Console.WriteLine("--Testing removeDot()"); Console.WriteLine("testC1.removeDot(6): {0}", testC1.removeDot(6)); Console.WriteLine("testC1.removeDot(5): {0}", testC1.removeDot(5)); Console.WriteLine("testC1 numDots: {0}\n", testC1.getNumDots()); Console.WriteLine(testC1); Console.WriteLine(); //perform tests on partially initialized cell Cell testC2 = new Cell(true, false, false, true, true, false); Console.WriteLine(testC2); Console.WriteLine("testC2 numDots: {0}\n", testC2.getNumDots()); Console.WriteLine("--Testing addDot()"); Console.WriteLine("testC2.addDot(1): {0}", testC2.addDot(1)); Console.WriteLine("testC2.addDot(6): {0}", testC2.addDot(6)); Console.WriteLine("testC2 numDots: {0}\n", testC2.getNumDots()); Console.WriteLine("--Testing removeDot()"); Console.WriteLine("testC1.removeDot(5): {0}", testC2.removeDot(5)); Console.WriteLine("testC1.removeDot(3): {0}", testC2.removeDot(3)); Console.WriteLine("testC2 numDots: {0}\n", testC2.getNumDots()); Console.WriteLine(testC2); //TextWriter text = new StreamWriter("test.txt"); //text.Write("{0} ", color.GetBrightness()); //text.WriteLine("---"); //text.Close(); }
/* public static Cell[,] buildCellTable(Bitmap image) { Cell[,] cellArr = new Cell[image.Width / 4, image.Height / 6]; Color color = new Color(); for (int majr = 0; majr < image.Height; majr++) { int count =0; int pixelcount =0; for (int majc = 0; majc < image.Width; majc+=2) { for (int i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { color = image.GetPixel(majc + j, majr + i); if(color.GetBrightness() < 0.5) pixelcount ++; } } if (pixelcount > 2) cellArr[majc, majr].addDot(count); switch (count) { } } } return cellArr; } */ /* public static Cell[,] resizeCell(Cell[,] Big){ int width = Big.GetLength(0)/2; int height = Big.GetLength(1)/3; Cell[,] small = new Cell[width, height]; for (int majr = 0; majr < height; majr++){ for (int majc = 0; majc < width; majc++){ small[majc, majr] = new Cell(); for (int minr = 0; minr < 3; minr++){ // for (int minr = 0; minr < 2; minr++){ for (int minc = 0; minc < 2; minc++){ int count =0; for (int i =0; i<6; i++) { if(Big[(2 * majc) + minc, (3 * majr) + minr].getDot(i)) count ++; } if(count >= 2) { small[majc, majr].addDot((3 * minc) + minr + 1); } } } } } return small; }*/ public static Cell[,] resizeCell(Cell[,] Big) { int width = Big.GetLength(0) / 2; int height = Big.GetLength(1) / 2; int smallw = 0; int smallh = 0; Cell[,] small = new Cell[width, height]; for (int majr = 0; majr <Big.GetLength(1); majr+=2) { if (majr != 0) { smallh++; } smallw = 0; for (int majc = 0; majc < Big.GetLength(0); majc+=2) { if(smallw < small.GetLength(0) && smallh < small.GetLength(1)){ small[smallw, smallh] = new Cell(); int count = 0; int curw = majc; int curl = majr; while(count < 6) { int dotcount=0; switch (count) { case 1: if (Big[majc, majr].getDot(1)) dotcount++; if (Big[majc, majr].getDot(2)) dotcount++; if (Big[majc, majr].getDot(4)) dotcount++; if (Big[majc, majr].getDot(5)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(1); } break; case 2: if (Big[majc, majr].getDot(3)) dotcount++; if (Big[majc, majr].getDot(6)) dotcount++; if (Big[majc, majr+1].getDot(1)) dotcount++; if (Big[majc+1, majr+1].getDot(4)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(2); } break; case 3: if (Big[majc, majr+1].getDot(2)) dotcount++; if (Big[majc, majr+1].getDot(3)) dotcount++; if (Big[majc, majr+1].getDot(5)) dotcount++; if (Big[majc, majr+1].getDot(6)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(3); } break; case 4: if (Big[majc+1, majr].getDot(1)) dotcount++; if (Big[majc+1, majr].getDot(2)) dotcount++; if (Big[majc+1, majr].getDot(4)) dotcount++; if (Big[majc+1, majr].getDot(5)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(4); } break; case 5: if (Big[majc+1, majr].getDot(3)) dotcount++; if (Big[majc+1, majr].getDot(6)) dotcount++; if (Big[majc+1, majr+1].getDot(1)) dotcount++; if (Big[majc+1, majr+1].getDot(4)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(5); } break; case6: if (Big[majc+1, majr+1].getDot(2)) dotcount++; if (Big[majc+1, majr+1].getDot(5)) dotcount++; if (Big[majc+1, majr+1].getDot(3)) dotcount++; if (Big[majc+1, majr+1].getDot(6)) dotcount++; if (dotcount >= 1) { small[smallw, smallh].addDot(6); } break; } count++; if(count == 6){ smallw++; } } } } } return small; }