internal static void TraceImage ( Pdf417Decoder Pdf417Decoder, int CenterX, int CenterY, int Left, int Top, int Right, int Bottom ) { int Width = Left + Right + 1; int Height = Top + Bottom + 1; int OrgX = CenterX - Left; int OrgY = CenterY - Top; char[] Line = new char[Width + 1]; for (int Y = 0; Y < Height; Y++) { Line[0] = Y == Top ? '>' : ' '; int R = OrgY + Y; for (int X = 0; X < Width; X++) { int C = OrgX + X; if (R < 0 || R >= Pdf417Decoder.ImageHeight || C < 0 || C >= Pdf417Decoder.ImageWidth) { Line[X + 1] = ' '; } else { Line[X + 1] = Pdf417Decoder.ImageMatrix[R, C] ? 'X' : '.'; } } Pdf417Trace.Write(new string(Line)); } return; }
// Decode the received codewords internal static int TestCodewords ( int[] Codewords, int ErrorCorrectionLength ) { // create codewords polynomial Polynomial PolyCodewords = new Polynomial(Codewords); // create syndrom coefficients array int[] Syndrome = new int[ErrorCorrectionLength]; // assume no errors bool Error = false; // test for errors // if the syndrom array is all zeros, there is no error for (int Index = ErrorCorrectionLength; Index > 0; Index--) { if ((Syndrome[ErrorCorrectionLength - Index] = PolyCodewords.EvaluateAt(Modulus.ExpTable[Index])) != 0) { Error = true; } } // no errors if (!Error) { return(0); } // convert syndrom array to polynomial Polynomial PolySyndrome = new Polynomial(Syndrome); // Greatest Common Divisor (return -1 if error cannot be corrected) if (!EuclideanAlgorithm(ErrorCorrectionLength, PolySyndrome, out Polynomial ErrorLocator, out Polynomial ErrorEvaluator)) { return(-1); } // error locator (return -1 if error cannot be corrected) int[] ErrorLocations = FindErrorLocations(ErrorLocator); if (ErrorLocations == null) { return(-1); } // formal derivatives Polynomial FormalDerivative = FindFormalDerivatives(ErrorLocator); // This is directly applying Forney's Formula int Errors = ErrorLocations.Length; for (int Index = 0; Index < Errors; Index++) { // error location int ErrLoc = ErrorLocations[Index]; // error position (return -1 if error cannot be corrected) int ErrPos = Codewords.Length - 1 - Modulus.LogTable[Modulus.Inverse(ErrLoc)]; if (ErrPos < 0) { return(-1); } // error magnitude int ErrorMagnitude = Modulus.Divide(Modulus.Negate(ErrorEvaluator.EvaluateAt(ErrLoc)), FormalDerivative.EvaluateAt(ErrLoc)); // correct codeword Codewords[ErrPos] = Modulus.Subtract(Codewords[ErrPos], ErrorMagnitude); // save error position in error locations array ErrorLocations[Index] = ErrPos; } #if DEBUG && ERRCORRECT Array.Sort(ErrorLocations); StringBuilder Str1 = new StringBuilder(); for (int Row = 0; Row < DataRows; Row++) { Str1.Clear(); Str1.AppendFormat("{0}: ", Row); for (int Col = 1; Col <= DataColumns; Col++) { int Pos = DataColumns * Row + Col - 1; int Index = Array.BinarySearch(ErrorLocations, Pos); if (Index < 0) { Str1.AppendFormat("{0}, ", Codewords[Pos]); } else { Str1.AppendFormat("{0}*, ", Codewords[Pos]); } } Pdf417Trace.Write(Str1.ToString()); } #endif // message was successfuly repaired return(Errors); }