//Constructors public WordsearchRotation(Bitmap bitmap, int rows, int cols) { this.Bitmap = bitmap; this.rows = rows; this.cols = cols; this.Segmentation = null; }
public void TestConstructor1() { //Basic Tests int[] rows = { 1, 2, 3, 67, 12 }; int[] cols = { 89, 5, 4 }; int width = 90; int height = 68; Segmentation segmentation = new Segmentation(rows, cols, width, height); CollectionAssert.AreEqual(rows, segmentation.Rows); CollectionAssert.AreEqual(cols, segmentation.Cols); Assert.AreEqual(width, segmentation.Width); Assert.AreEqual(height, segmentation.Height); }
//Draws a solution onto a Bitmap, using a specified Segmentation to split the image into characters. // Draws each line from the centre of each segmentation cell public static void SolutionInPlace(Bitmap img, Segmentation segmentation, Solution solution, Color colour) { //Validation: Check that the image dimensions & segmentation dimensions match if (img.Width != segmentation.Width || img.Height != segmentation.Height) { throw new ArgumentException("Bitmap dimensions do not match Segmentation dimensions"); } //Work out the centre point for each character IntPoint[,] charCentres = new IntPoint[segmentation.NumCols, segmentation.NumRows]; for(int i = 0; i < segmentation.NumCols; i++) { int colStart = (i == 0) ? 0 : segmentation.Cols[i - 1]; int colEnd = (i == segmentation.NumCols - 1) ? segmentation.Width : segmentation.Cols[i]; int colCentre = (int)Math.Round((((double)colEnd - colStart) / 2) + colStart); for(int j = 0; j < segmentation.NumRows; j++) { int rowStart = (j == 0) ? 0 : segmentation.Rows[j - 1]; int rowEnd = (j == segmentation.NumRows - 1) ? segmentation.Height : segmentation.Rows[j]; int rowCentre = (int)Math.Round(rowStart + (((double)rowEnd - rowStart) / 2)); charCentres[i, j] = new IntPoint(colCentre, rowCentre); } } //Lock the image for write so we can alter it BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, img.PixelFormat); //Draw on the word positions foreach (WordPosition position in solution.Values) { IntPoint startPoint = charCentres[position.StartCol, position.StartRow]; IntPoint endPoint = charCentres[position.EndCol, position.EndRow]; Drawing.Line(imgData, startPoint, endPoint, colour); } img.UnlockBits(imgData); }
public void TestConstructor3() { //Test constructor when it's supplied with an invalid height int[] rows = { }; int[] cols = { 89, 5, 4 }; int width = 10; int height = -1; try { Segmentation segmentation = new Segmentation(rows, cols, width, height); Assert.Fail(); } catch (InvalidImageDimensionsException) { //Threw the correct exception, passed } catch { //Threw the wrong exception, failed Assert.Fail(); } }
public void TestSegment1() { //Basic Test Segmentation s = new Segmentation(2, 2, 2, 2); Bitmap b = new Bitmap(2, 2, PixelFormat.Format32bppRgb); b.SetPixel(0, 0, Color.Red); b.SetPixel(1, 0, Color.Green); b.SetPixel(0, 1, Color.Blue); b.SetPixel(1, 1, Color.Magenta); Bitmap[,] chars = SplitImage.Segment(b, s); Assert.AreEqual(Color.Red.ToArgb(), chars[0, 0].GetPixel(0, 0).ToArgb()); Assert.AreEqual(Color.Green.ToArgb(), chars[1, 0].GetPixel(0, 0).ToArgb()); Assert.AreEqual(Color.Blue.ToArgb(), chars[0, 1].GetPixel(0, 0).ToArgb()); Assert.AreEqual(Color.Magenta.ToArgb(), chars[1, 1].GetPixel(0, 0).ToArgb()); //Clean up b.Dispose(); chars.ToSingleDimension().DisposeAll(); }
public void TestRotate4() { //Test rotating 180 deg int[] rows = { 1, 2, 4 }; int[] cols = { 2, 7, 11 }; int width = 25; int height = 15; Segmentation s = new Segmentation(rows, cols, width, height); int[] expectedRows = { 10, 12, 13 }; int[] expectedCols = { 13, 17, 22 }; int expectedWidth = 25; int expectedHeight = 15; s.Rotate(180); CollectionAssert.AreEqual(expectedRows, s.Rows); CollectionAssert.AreEqual(expectedCols, s.Cols); Assert.AreEqual(expectedWidth, s.Width); Assert.AreEqual(expectedHeight, s.Height); }
public void TestRotate3() { //Check that rotations can only take place around 90 degrees Segmentation s = new Segmentation(10, 10, 20, 20); try { s.Rotate(5); //No Exception: Fail Assert.Fail(); } catch(ArgumentException) { //Correct exception: Pass } catch(Exception) { //Wrong type of exception: Fail Assert.Fail(); } }
public void TestConstructor17() { //Test constructor when it's supplied with invalid height int width = 10; int height = -1; int[,] rows = new int[,] { { 0, 1 }, { 2, 4 } }; int[,] cols = new int[,] { { 0, 3 }, { 4, 6 }, { 7, 8 } }; try { Segmentation s = new Segmentation(rows, cols, width, height); Assert.Fail(); //Threw no exception, fail } catch (InvalidImageDimensionsException) { //Threw correct exception, Pass } catch { //Threw wrong exception, fail Assert.Fail(); } }
public static void SolutionInPlace(Bitmap img, Segmentation segmentation, Solution solution) { SolutionInPlace(img, segmentation, solution, DrawDefaults.DEFAULT_COLOUR); }
public static Bitmap Solution(Bitmap img, Segmentation segmentation, Solution solution) { return Solution(img, segmentation, solution, DrawDefaults.DEFAULT_COLOUR); }
public static void WordPositionInPlace(Bitmap img, Segmentation segmentation, WordPosition wordPosition, Color colour) { //Validation: Check that the image dimensions & segmentation dimensions match if (img.Width != segmentation.Width || img.Height != segmentation.Height) { throw new ArgumentException("Bitmap dimensions do not match Segmentation dimensions"); } //Lock the image for write so we can alter it BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, img.PixelFormat); //Draw on the Word Position IntPoint start = getCentrePoint(segmentation, wordPosition.StartRow, wordPosition.StartCol); IntPoint end = getCentrePoint(segmentation, wordPosition.EndRow, wordPosition.EndCol); Drawing.Line(imgData, start, end, colour); img.UnlockBits(imgData); }
public void TestDeepCopy2() { //Test the copy is actually deep int[] rows = { 1, 2, 3, 67, 12 }; int[] cols = { 89, 5, 4 }; int width = 90; int height = 68; Segmentation orig = new Segmentation(rows, cols, width, height); Segmentation clone = orig.DeepCopy(); //Rotating through 90 deg changes all of the supplied parameters orig.Rotate90(); CollectionAssert.AreEqual(new int[] { 1, 2, 3, 67, 12 }, clone.Rows); CollectionAssert.AreEqual(new int[] { 89, 5, 4 }, clone.Cols); Assert.AreEqual(width, clone.Width); Assert.AreEqual(height, clone.Height); }
public void TestIsEquallySpaced2() { //Test with splitting indices that aren't equally spaced int[] rows = { 1, 3, 4 }; int[] cols = { 1, 3, 4 }; Segmentation s = new Segmentation(rows, cols, 5, 5); Assert.AreEqual(false, s.IsEquallySpaced); }
public void TestIsEquallySpaced1() { //Test with number of rows & cols (which therefore must be equally spaced) Segmentation s = new Segmentation(5, 12, 123, 456); Assert.AreEqual(true, s.IsEquallySpaced); }
public void TestNumCols1() { int[] rows = { }; int[] cols = { 1 }; Segmentation s = new Segmentation(rows, cols, 2, 2); Assert.AreEqual(2, s.NumCols); }
public void TestConstructor29() { //Test constructor when it's supplied with unordered cols int width = 10; int height = 5; int[,] rows = new int[,] { { 0, 1 }, { 2, 4 } }; int[,] cols = new int[,] { { 0, 3 }, { 7, 8 }, { 4, 6 } }; try { Segmentation s = new Segmentation(rows, cols, width, height); Assert.Fail(); //Threw no exception, fail } catch (InvalidRowsAndColsException) { //Threw correct exception, Pass } catch { //Threw wrong exception, fail Assert.Fail(); } }
public void TestConstructor25() { //Test constructor when it's supplied with invalid col array dimensions int width = 10; int height = 5; int[,] rows = new int[,] { { 0, 1 }, { 2, 4 } }; int[,] cols = new int[,] { { 0, 3, 3 }, { 4, 6, 6 }, { 7, 8, 8 } }; try { Segmentation s = new Segmentation(rows, cols, width, height); Assert.Fail(); //Threw no exception, fail } catch (UnexpectedArrayDimensionsException) { //Threw correct exception, Pass } catch { //Threw wrong exception, fail Assert.Fail(); } }
public void TestRotate5() { //Test rotating -270deg is the same as rotating +90 deg int[] rows = { 1, 2, 4 }; int[] cols = { 2, 7, 11 }; int width = 20; int height = 5; Segmentation s90 = new Segmentation((int[])rows.Clone(), (int[])cols.Clone(), width, height); //Deep copy the arrays so the segmentations have different copies Segmentation sMinus270 = new Segmentation(rows, cols, width, height); s90.Rotate(90); sMinus270.Rotate(-270); CollectionAssert.AreEqual(s90.Rows, sMinus270.Rows); CollectionAssert.AreEqual(s90.Cols, sMinus270.Cols); Assert.AreEqual(s90.Width, sMinus270.Width); Assert.AreEqual(s90.Height, sMinus270.Height); }
public void TestDeepCopy1() { //Basic test int[] rows = { 1, 2, 3, 67, 12 }; int[] cols = { 89, 5, 4 }; int width = 90; int height = 68; Segmentation orig = new Segmentation(rows, cols, width, height); Segmentation clone = orig.DeepCopy(); CollectionAssert.AreEqual(orig.Rows, clone.Rows); CollectionAssert.AreEqual(orig.Cols, clone.Cols); Assert.AreEqual(orig.Width, clone.Width); Assert.AreEqual(orig.Height, clone.Height); }
public void TestIsEquallySpaced3() { //Test with splitting indices that are equally spaced int[] rows = { 1, 2, 3, 4 }; int[] cols = { 1, 2, 3, 4, 5 }; Segmentation s = new Segmentation(rows, cols, 6, 5); Assert.AreEqual(true, s.IsEquallySpaced); }
public static void WordPositionInPlace(Bitmap img, Segmentation segmentation, WordPosition wordPosition) { WordPositionInPlace(img, segmentation, wordPosition, DrawDefaults.DEFAULT_COLOUR); }
public void TestIsEquallySpaced5() { //Test with splitting cols that are equally spaced, but rows that aren't int[] rows = { 1, 2, 4 }; int[] cols = { 1, 2, 3, 4, 5 }; Segmentation s = new Segmentation(rows, cols, 6, 5); Assert.AreEqual(false, s.IsEquallySpaced); }
/* * Private Helpers */ private static IntPoint getCentrePoint(Segmentation segmentation, int rowIdx, int colIdx) { //Validation: Check Row Index is within bounds if(rowIdx >= segmentation.NumRows) { throw new ArgumentException("Row Index is out of bounds"); } //Validation: Check Col Index is within bounds if(colIdx >= segmentation.NumCols) { throw new ArgumentException("Col Index is out of bounds"); } int colStart = (colIdx == 0) ? 0 : segmentation.Cols[colIdx - 1]; int colEnd = (colIdx == segmentation.NumCols - 1) ? segmentation.Width : segmentation.Cols[colIdx]; int colCentre = (int)Math.Round(colStart + (((double)colEnd - colStart) / 2)); int rowStart = (rowIdx == 0) ? 0 : segmentation.Rows[rowIdx - 1]; int rowEnd = (rowIdx == segmentation.NumRows - 1) ? segmentation.Height : segmentation.Rows[rowIdx]; int rowCentre = (int)Math.Round(rowStart + (((double)rowEnd - rowStart) / 2)); return new IntPoint(colCentre, rowCentre); }
public void TestRotate1() { //Basic test - gives expected output for a rotation through 90 degrees int[] rows = { 1, 2, 4 }; int[] cols = { 2, 7, 11 }; int width = 20; int height = 5; Segmentation s = new Segmentation(rows, cols, width, height); int[] expectedRows = { 2, 7, 11 }; int[] expectedCols = { 0, 2, 3 }; int expectedWidth = 5; int expectedHeight = 20; s.Rotate(90); CollectionAssert.AreEqual(expectedRows, s.Rows); CollectionAssert.AreEqual(expectedCols, s.Cols); Assert.AreEqual(expectedWidth, s.Width); Assert.AreEqual(expectedHeight, s.Height); }
public static Bitmap Solution(Bitmap origImg, Segmentation segmentation, Solution solution, Color colour) { Bitmap img = origImg.DeepCopy(); SolutionInPlace(img, segmentation, solution, colour); return img; }
public static Bitmap WordPosition(Bitmap img, Segmentation segmentation, WordPosition wordPosition) { return WordPosition(img, segmentation, wordPosition, DrawDefaults.DEFAULT_COLOUR); }
public void TestConstructor4() { //Test constructor when it's supplied with a negative row index int[] rows = { -1 }; int[] cols = { }; int width = 10; int height = 10; try { Segmentation segmentation = new Segmentation(rows, cols, width, height); Assert.Fail(); } catch (InvalidRowsAndColsException) { //Threw the correct exception, passed } catch { //Threw the wrong exception, failed Assert.Fail(); } }
public void TestConstructor15() { //Test constructor when it's supplied with row & col start & end indices whose segmentations will require rounding int width = 10; int height = 5; int[,] rows = new int[,] { { 0, 1 }, { 2, 4 } }; int[,] cols = new int[,] { { 0, 3 }, { 4, 6 }, { 7, 8 } }; Segmentation segmentation = new Segmentation(rows, cols, width, height); int[] rowsSeg = { 1 }; int[] colsSeg = { 3, 6 }; CollectionAssert.AreEqual(rowsSeg, segmentation.Rows); CollectionAssert.AreEqual(colsSeg, segmentation.Cols); }
public static Bitmap WordPosition(Bitmap origImg, Segmentation segmentation, WordPosition wordPosition, Color colour) { Bitmap img = origImg.DeepCopy(); WordPositionInPlace(img, segmentation, wordPosition, colour); return img; }
public void TestRotate2() { //Check a rotation returns back to itself when rotated 360 deg int[] rows = { 324, 563463, 43543212 }; int[] cols = { 5 }; int width = 6; int height = int.MaxValue; Segmentation s = new Segmentation(rows, cols, width, height); s.Rotate(360); int[] expectedRows = { 324, 563463, 43543212 }; int[] expectedCols = { 5 }; int expectedWidth = 6; int expectedHeight = int.MaxValue; CollectionAssert.AreEqual(expectedRows, rows); CollectionAssert.AreEqual(expectedCols, cols); Assert.AreEqual(expectedWidth, s.Width); Assert.AreEqual(expectedHeight, s.Height); }