static private ReturnRectangularIntArray ( int Size1, int Size2 ) : int[][] | ||
Size1 | int | |
Size2 | int | |
Результат | int[][] |
internal BZip2BlockEntry() { unzftab = new int[256]; afm = new int[257]; afn = new int[257]; inUse = new bool[256]; inUse16 = new bool[16]; seqToUnseq = new int[256]; yy = new int[4096]; agg = new int[16]; selector = new sbyte[18002]; selectorMtf = new sbyte[18002]; //ORIGINAL LINE: len = new sbyte[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: len = RectangularArrays.ReturnRectangularSbyteArray(6, 258); //ORIGINAL LINE: limit = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: limit = RectangularArrays.ReturnRectangularIntArray(6, 258); //ORIGINAL LINE: _base = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: _base = RectangularArrays.ReturnRectangularIntArray(6, 258); //ORIGINAL LINE: perm = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: perm = RectangularArrays.ReturnRectangularIntArray(6, 258); agn = new int[6]; }
internal DataFileVariables() { afk = new int[256]; afm = new int[257]; afn = new int[257]; agc = new bool[256]; agd = new bool[16]; age = new int[256]; agf = new int[4096]; agg = new int[16]; agh = new sbyte[18002]; agi = new sbyte[18002]; //ORIGINAL LINE: agj = new sbyte[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: agj = RectangularArrays.ReturnRectangularSbyteArray(6, 258); //ORIGINAL LINE: agk = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: agk = RectangularArrays.ReturnRectangularIntArray(6, 258); //ORIGINAL LINE: agl = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: agl = RectangularArrays.ReturnRectangularIntArray(6, 258); //ORIGINAL LINE: agm = new int[6][258]; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: agm = RectangularArrays.ReturnRectangularIntArray(6, 258); agn = new int[6]; }
public Config(int pegs, int colors) { this.pegs = pegs; this.colors = colors; this.combinations = (int)Math.Pow(colors, pegs); grades = pegs * (pegs + 3) / 2; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: id2array = new int[grades][2]; id2array = RectangularArrays.ReturnRectangularIntArray(grades, 2); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: array2id = new int[pegs + 1][pegs + 1]; array2id = RectangularArrays.ReturnRectangularIntArray(pegs + 1, pegs + 1); int currentID = 0; for (int i = 0; i <= pegs; i++) { for (int z = 0; z <= pegs - i; z++) { if (i == pegs - 1 && z == 1) { continue; } array2id[i][z] = currentID; id2array[currentID] = new int[] { i, z }; currentID++; } } }
public virtual BufferedImage processImage(BufferedImage image) { originalImage = image; width = originalImage.Width; height = originalImage.Height; filteredImage = new BufferedImage(width, height, originalImage.Type); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: imageMatrix = new int[width][height]; imageMatrix = RectangularArrays.ReturnRectangularIntArray(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { imageMatrix[i][j] = (new Color(originalImage.getRGB(i, j))).Red; } } mean = calculateMean(); @var = calculateVariance(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { double normalizedPixel = 0; double squareError = 0; if (imageMatrix[i][j] > mean) { squareError = (imageMatrix[i][j] - mean) * (imageMatrix[i][j] - mean); normalizedPixel = (GOAL_MEAN_Renamed + Math.Sqrt(((GOAL_VARIANCE_Renamed * squareError / @var)))); } else { squareError = (imageMatrix[i][j] - mean) * (imageMatrix[i][j] - mean); normalizedPixel = (GOAL_MEAN_Renamed - Math.Sqrt(((GOAL_VARIANCE_Renamed * squareError / @var)))); } int alpha = (new Color(originalImage.getRGB(i, j))).Alpha; int rgb = (int)-normalizedPixel; int color = ImageUtilities.colorToRGB(alpha, rgb, rgb, rgb); filteredImage.setRGB(i, j, color); } } return(filteredImage); }
public sfmt19937Ctx(TPointer ctxAddr, int[] seeds) { this.index = 0; this.indexPos = 0; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: this.sfmt = new int[PSP_SFMT19937_LENGTH][4]; this.sfmt = RectangularArrays.ReturnRectangularIntArray(PSP_SFMT19937_LENGTH, 4); this.addr = ctxAddr; this.seedArray = seeds; }
protected internal static int[][] getSet(int n) { ArgChecker.isTrue(n > 1, "need n>1"); if (SETS.ContainsKey(n)) { return(SETS[n]); } int[][] res = new int[n][]; switch (n) { case 2: res[0] = new int[] { 1 }; res[1] = new int[] { -1 }; break; case 3: res[0] = new int[] { 1, 0 }; res[1] = new int[] { -1, 1 }; res[2] = new int[] { -1, -1 }; break; case 4: res[0] = new int[] { 1, 1, 0 }; res[1] = new int[] { 1, -1, 0 }; res[2] = new int[] { -1, 0, 1 }; res[3] = new int[] { -1, 0, -1 }; break; default: int n1 = n / 2; int n2 = n - n1; int[][] set1 = getSet(n1); int[][] set2 = (n1 == n2 ? set1 : getSet(n2)); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: res = new int[n][n - 1]; res = RectangularArrays.ReturnRectangularIntArray(n, n - 1); for (int i = 0; i < n1; i++) { res[i][0] = 1; Array.Copy(set1[i], 0, res[i], 1, n1 - 1); } for (int i = 0; i < n2; i++) { res[i + n1][0] = -1; Array.Copy(set2[i], 0, res[i + n1], n1, n2 - 1); } break; } SETS[n] = res; return(res); }
public virtual int [][] createC(double[][] D, int[][] Q) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] C = new int[N_Renamed][N_Renamed]; int[][] C = RectangularArrays.ReturnRectangularIntArray(N_Renamed, N_Renamed); for (int i = 0; i < N_Renamed; i++) { for (int j = 0; j < N_Renamed; j++) { C[i][j] = (int)Math.Round(D[i][j] / Q[i][j]); } } return(C); }
public virtual int [][] createN(double[][] Tinv, double[][] R, double[][] T) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] Nmatrix = new int [N_Renamed][N_Renamed]; int[][] Nmatrix = RectangularArrays.ReturnRectangularIntArray(N_Renamed, N_Renamed); double[][] tmp = multiply(multiply(Tinv, R), T); for (int i = 0; i < N_Renamed; i++) { for (int j = 0; j < N_Renamed; j++) { Nmatrix[i][j] = (int)(Math.Round(tmp[i][j]) + 128); } } return(Nmatrix); }
public virtual void TestManyFields() { int NUM_DOCS = AtLeast(200); int MAX_FIELDS = AtLeast(50); int[][] docs = RectangularArrays.ReturnRectangularIntArray(NUM_DOCS, 4); for (int i = 0; i < docs.Length; i++) { for (int j = 0; j < docs[i].Length; j++) { docs[i][j] = Random().Next(MAX_FIELDS); } } Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))); for (int i = 0; i < NUM_DOCS; i++) { Document d = new Document(); for (int j = 0; j < docs[i].Length; j++) { d.Add(GetField(docs[i][j])); } writer.AddDocument(d); } writer.ForceMerge(1); writer.Dispose(); SegmentInfos sis = new SegmentInfos(); sis.Read(dir); foreach (SegmentCommitInfo si in sis.Segments) { FieldInfos fis = SegmentReader.ReadFieldInfos(si); foreach (FieldInfo fi in fis) { Field expected = GetField(Convert.ToInt32(fi.Name)); Assert.AreEqual(expected.FieldType().Indexed, fi.Indexed); Assert.AreEqual(expected.FieldType().StoreTermVectors, fi.HasVectors()); } } dir.Dispose(); }
// ------------------ public Graph() // constructor { vertexList = new Vertex[MAX_VERTS]; // adjacency matrix //ORIGINAL LINE: adjMat = new int[MAX_VERTS][MAX_VERTS]; adjMat = RectangularArrays.ReturnRectangularIntArray(MAX_VERTS, MAX_VERTS); nVerts = 0; for (int j = 0; j < MAX_VERTS; j++) // set adjacency { for (int k = 0; k < MAX_VERTS; k++) // matrix to 0 { adjMat[j][k] = 0; } } theStack = new StackX(); } // end constructor
public virtual BufferedImage processImage(BufferedImage image) { originalImage = image; Attributes = image; int width = originalImage.Width; int height = originalImage.Height; filteredImage = new BufferedImage(width, height, originalImage.Type); int[][] filter1 = new int[][] { new int[] { -1, 0, 1 }, new int[] { -2, 0, 2 }, new int[] { -1, 0, 1 } }; int[][] filter2 = new int[][] { new int[] { 1, 2, 1 }, new int[] { 0, 0, 0 }, new int[] { -1, -2, -1 } }; for (int y = 1; y < height - 1; y++) { for (int x = 1; x < width - 1; x++) { // get 3-by-3 array of colors in neighborhood //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] gray = new int[3][3]; int[][] gray = RectangularArrays.ReturnRectangularIntArray(3, 3); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gray[i][j] = (int)lum(new Color(originalImage.getRGB(x - 1 + i, y - 1 + j))); } } // apply filter int gray1 = 0, gray2 = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { gray1 += gray[i][j] * filter1[i][j]; gray2 += gray[i][j] * filter2[i][j]; } } // int magnitude = 255 - truncate(Math.abs(gray1) + Math.abs(gray2)); int magnitude = 255 - truncate((int)Math.Sqrt(gray1 * gray1 + gray2 * gray2)); Color grayscale = new Color(magnitude, magnitude, magnitude); filteredImage.setRGB(x, y, grayscale.RGB); } } return(filteredImage); }
public virtual void thiningGuoHallIteration(int iter) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] marker = new int[width][height]; int[][] marker = RectangularArrays.ReturnRectangularIntArray(width, height); for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { int p2 = imageM[i - 1][j]; int p3 = imageM[i - 1][j + 1]; int p4 = imageM[i][j + 1]; int p5 = imageM[i + 1][j + 1]; int p6 = imageM[i + 1][j]; int p7 = imageM[i + 1][j - 1]; int p8 = imageM[i][j - 1]; int p9 = imageM[i - 1][j - 1]; int C = (~p2 & (p3 | p4)) + (~p4 & (p5 | p6)) + (~p6 & (p7 | p8)) + (~p8 & (p9 | p2)); int N1 = (p9 | p2) + (p3 | p4) + (p5 | p6) + (p7 | p8); int N2 = (p2 | p3) + (p4 | p5) + (p6 | p7) + (p8 | p9); int N = N1 < N2 ? N1 : N2; int m = iter == 0 ? ((p6 | p7 | ~p9) & p8) : ((p2 | p3 | ~p5) & p4); if (C == 1 && (N >= 2 && N <= 3) & m == 0) { marker[i][j] = 1; } } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int tmp = 1 - marker[i][j]; if (imageM[i][j] == tmp && imageM[i][j] == 1) { imageM[i][j] = 1; } else { imageM[i][j] = 0; } } } }
private int allocTable(int size) { int index = tableSize; tableSize += size; tableAllocated = tableSize; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] newTable = new int[tableAllocated][2]; int[][] newTable = RectangularArrays.ReturnRectangularIntArray(tableAllocated, 2); if (table != null) { for (int i = 0; i < index; i++) { newTable[i][0] = table[i][0]; newTable[i][1] = table[i][1]; } } table = newTable; return(index); }
private int[][] ReadZoomTable(SubFileParameter subFileParameter) { int rows = subFileParameter.ZoomLevelMax - subFileParameter.ZoomLevelMin + 1; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] zoomTable = new int[rows][2]; int[][] zoomTable = RectangularArrays.ReturnRectangularIntArray(rows, 2); int cumulatedNumberOfPois = 0; int cumulatedNumberOfWays = 0; for (int row = 0; row < rows; ++row) { cumulatedNumberOfPois += this.readBuffer.ReadUnsignedInt(); cumulatedNumberOfWays += this.readBuffer.ReadUnsignedInt(); zoomTable[row][0] = cumulatedNumberOfPois; zoomTable[row][1] = cumulatedNumberOfWays; } return(zoomTable); }
/* Prints all occurrences of * pat in txt */ public static int Search(char[] pat, char[] txt) { var patLength = pat.Length; var txtLength = txt.Length; var tf = RectangularArrays.ReturnRectangularIntArray(patLength + 1, NoOfChars); ComputeTf(pat, patLength, tf); // Process txt over FA. int i, state = 0; for (i = 0; i < txtLength; i++) { state = tf[state][txt[i]]; if (state == patLength) { return(i - patLength + 1); } } return(-1); }
/// <summary> /// Calculates a single black point for each block of pixels and saves it away. /// See the following thread for a discussion of this algorithm: /// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0 /// </summary> private static int[][] calculateBlackPoints(sbyte[] luminances, int subWidth, int subHeight, int width, int height) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] blackPoints = new int[subHeight][subWidth]; int[][] blackPoints = RectangularArrays.ReturnRectangularIntArray(subHeight, subWidth); for (int y = 0; y < subHeight; y++) { int yoffset = y << BLOCK_SIZE_POWER; int maxYOffset = height - BLOCK_SIZE; if (yoffset > maxYOffset) { yoffset = maxYOffset; } for (int x = 0; x < subWidth; x++) { int xoffset = x << BLOCK_SIZE_POWER; int maxXOffset = width - BLOCK_SIZE; if (xoffset > maxXOffset) { xoffset = maxXOffset; } int sum = 0; int min = 0xFF; int max = 0; for (int yy = 0, offset = yoffset * width + xoffset; yy < BLOCK_SIZE; yy++, offset += width) { for (int xx = 0; xx < BLOCK_SIZE; xx++) { int pixel = luminances[offset + xx] & 0xFF; sum += pixel; // still looking for good contrast if (pixel < min) { min = pixel; } if (pixel > max) { max = pixel; } } // short-circuit min/max tests once dynamic range is met if (max - min > MIN_DYNAMIC_RANGE) { // finish the rest of the rows quickly for (yy++, offset += width; yy < BLOCK_SIZE; yy++, offset += width) { for (int xx = 0; xx < BLOCK_SIZE; xx++) { sum += luminances[offset + xx] & 0xFF; } } } } // The default estimate is the average of the values in the block. int average = sum >> (BLOCK_SIZE_POWER * 2); if (max - min <= MIN_DYNAMIC_RANGE) { // If variation within the block is low, assume this is a block with only light or only // dark pixels. In that case we do not want to use the average, as it would divide this // low contrast area into black and white pixels, essentially creating data out of noise. // // The default assumption is that the block is light/background. Since no estimate for // the level of dark pixels exists locally, use half the min for the block. average = min >> 1; if (y > 0 && x > 0) { // Correct the "white background" assumption for blocks that have neighbors by comparing // the pixels in this block to the previously calculated black points. This is based on // the fact that dark barcode symbology is always surrounded by some amount of light // background for which reasonable black point estimates were made. The bp estimated at // the boundaries is used for the interior. // The (min < bp) is arbitrary but works better than other heuristics that were tried. int averageNeighborBlackPoint = (blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) + blackPoints[y - 1][x - 1]) >> 2; if (min < averageNeighborBlackPoint) { average = averageNeighborBlackPoint; } } } blackPoints[y][x] = average; } } return(blackPoints); }
// Returns total number of // palindrome substring of // length greater then equal to 2 public static int CountPS(char[] str, int n) { // create empty 2-D matrix that counts // all palindrome substring. dp[i][j] // stores counts of palindromic // substrings in st[i..j] int[][] rectangularIntArray = RectangularArrays.ReturnRectangularIntArray(n, n); // P[i][j] = true if substring str[i..j] // is palindrome, else false bool[][] rectangularBoolArray = RectangularArrays.ReturnRectangularBoolArray(n, n); // palindrome of single length for (int i = 0; i < n; i++) { rectangularBoolArray[i][i] = true; } // palindrome of length 2 for (int i = 0; i < n - 1; i++) { if (str[i] == str[i + 1]) { rectangularBoolArray[i][i + 1] = true; rectangularIntArray[i][i + 1] = 1; } } // Palindromes of length more then 2. // This loop is similar to Matrix Chain Multiplication. // We start with a gap of length 2 and fill DP table in a way that gap between starting and // ending indexes increases one by one by outer loop. for (int gap = 2; gap < n; gap++) { // Pick starting point for current gap for (int i = 0; i < n - gap; i++) { // Set ending point int j = gap + i; // If current string is palindrome if (str[i] == str[j] && rectangularBoolArray[i + 1][j - 1]) { rectangularBoolArray[i][j] = true; } // Add current palindrome substring // ( + 1) and rest palindrome substring // (dp[i][j-1] + dp[i+1][j]) remove common // palindrome substrings (- dp[i+1][j-1]) if (rectangularBoolArray[i][j] == true) { rectangularIntArray[i][j] = rectangularIntArray[i][j - 1] + rectangularIntArray[i + 1][j] + 1 - rectangularIntArray[i + 1][j - 1]; } else { rectangularIntArray[i][j] = rectangularIntArray[i][j - 1] + rectangularIntArray[i + 1][j] - rectangularIntArray[i + 1][j - 1]; } } } // return total palindromic substrings return(rectangularIntArray[0][n - 1]); }
// public method /// <summary> /// Return Edit distance with options of specify the costs of delete, insert, /// replace (substitute), swap (transpose), and case change. In general, /// the cost of delete and insert should be the same. The cost of replacement /// is higher and swap is lower, while the cost of case change is mimimum. /// The algorithm use x[i] for delete, y[j] for add. it is exchangable. /// A smart optino is provide to consider transpose with case changes, /// instead of two substitutes to minmize the costs. /// </summary> /// <param name="srcStr"> soruce string </param> /// <param name="tarStr"> target string </param> /// <param name="deleteCost"> cost of a deletion </param> /// <param name="insertCost"> cost of a insertion </param> /// <param name="replaceCost"> cost of a replacement (substitution) </param> /// <param name="swapCost"> cost of a swap (transposition) </param> /// <param name="caseChangeCost"> cost of case change </param> /// <param name="enhancedFlag"> true to use enhanced algorithm to min. swap and case /// </param> /// <returns> the total cost of edit distance </returns> public static int GetEditDistance(string srcStr, string tarStr, int deleteCost, int insertCost, int replaceCost, int swapCost, int caseChangeCost, bool enhancedFlag) { // add 1 extra cell srcStr = " " + srcStr; tarStr = " " + tarStr; int srcSize = srcStr.Length; int tarSize = tarStr.Length; int[][] matrix = RectangularArrays.ReturnRectangularIntArray(srcSize, tarSize); matrix[0][0] = 0; // initialize the first row: x for delete for (int i = 1; i < srcSize; i++) { matrix[i][0] = matrix[i - 1][0] + deleteCost; } //initalize the first column: y for insert for (int j = 1; j < tarSize; j++) { matrix[0][j] = matrix[0][j - 1] + insertCost; } // fill up the matrix for (int i = 1; i < srcSize; i++) { char srcChar = srcStr[i]; for (int j = 1; j < tarSize; j++) { char tarChar = tarStr[j]; if (srcChar == tarChar) { //no change required, so just carry the current cost up matrix[i][j] = matrix[i - 1][j - 1]; } else { int costOfD = matrix[i - 1][j] + deleteCost; int costOfI = matrix[i][j - 1] + insertCost; int costOfR = replaceCost + matrix[i - 1][j - 1]; // for the case of case change int costOfC = int.MaxValue; char srcCharLc = char.ToLower(srcChar); char tarCharLc = char.ToLower(tarChar); if (srcCharLc == tarCharLc) { costOfC = matrix[i - 1][j - 1] + caseChangeCost; } // for the case of Transpose, swap int costOfS = int.MaxValue; char srcChar1 = srcStr[i - 1]; char tarChar1 = tarStr[j - 1]; char srcChar1Lc = char.ToLower(srcChar1); char tarChar1Lc = char.ToLower(tarChar1); // transpose, Swap if ((i > 1) && (j > 1) && (srcChar == tarChar1) && (srcChar1 == tarChar)) { costOfS = matrix[i - 2][j - 2] + swapCost; } // transpose + 1 case change else if ((enhancedFlag == true) && (i > 1) && (j > 1) && (((srcCharLc == tarChar1Lc) && (srcChar1 == tarChar)) || ((srcChar == tarChar1) && (srcChar1Lc == tarCharLc)))) { costOfS = matrix[i - 2][j - 2] + swapCost + caseChangeCost; } // transpose + 2 case change else if ((enhancedFlag == true) && (i > 1) && (j > 1) && (srcCharLc == tarChar1Lc) && (srcChar1Lc == tarCharLc)) { costOfS = matrix[i - 2][j - 2] + swapCost + 2 * caseChangeCost; } matrix[i][j] = Minimum5(costOfD, costOfI, costOfR, costOfS, costOfC); } } } int cost = matrix[srcSize - 1][tarSize - 1]; return(cost); }
public virtual AbstractModel trainModel(int iterations, SequenceStream sequenceStream, int cutoff, bool useAverage) { this.iterations = iterations; this.sequenceStream = sequenceStream; DataIndexer di = new OnePassDataIndexer(new SequenceStreamEventStream(sequenceStream), cutoff, false); numSequences = 0; foreach (Sequence <Event> s in sequenceStream) { numSequences++; } outcomeList = di.OutcomeList; predLabels = di.PredLabels; pmap = new IndexHashTable <string>(predLabels, 0.7d); display("Incorporating indexed data for training... \n"); this.useAverage = useAverage; numEvents = di.NumEvents; this.iterations = iterations; outcomeLabels = di.OutcomeLabels; omap = new Dictionary <string, int?>(); for (int oli = 0; oli < outcomeLabels.Length; oli++) { omap[outcomeLabels[oli]] = oli; } outcomeList = di.OutcomeList; numPreds = predLabels.Length; numOutcomes = outcomeLabels.Length; if (useAverage) { updates = RectangularArrays.ReturnRectangularIntArray(numPreds, numOutcomes, 3); } display("done.\n"); display("\tNumber of Event Tokens: " + numEvents + "\n"); display("\t Number of Outcomes: " + numOutcomes + "\n"); display("\t Number of Predicates: " + numPreds + "\n"); parameters = new MutableContext[numPreds]; if (useAverage) { averageParams = new MutableContext[numPreds]; } allOutcomesPattern = new int[numOutcomes]; for (int oi = 0; oi < numOutcomes; oi++) { allOutcomesPattern[oi] = oi; } for (int pi = 0; pi < numPreds; pi++) { parameters[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]); if (useAverage) { averageParams[pi] = new MutableContext(allOutcomesPattern, new double[numOutcomes]); } for (int aoi = 0; aoi < numOutcomes; aoi++) { parameters[pi].setParameter(aoi, 0.0); if (useAverage) { averageParams[pi].setParameter(aoi, 0.0); } } } modelDistribution = new double[numOutcomes]; display("Computing model parameters...\n"); findParameters(iterations); display("...done.\n"); /// <summary> ///************* Create and return the model ***************** </summary> string[] updatedPredLabels = predLabels; /* * String[] updatedPredLabels = new String[pmap.size()]; * for (String pred : pmap.keySet()) { * updatedPredLabels[pmap.get(pred)]=pred; * } */ if (useAverage) { return(new PerceptronModel(averageParams, updatedPredLabels, outcomeLabels)); } else { return(new PerceptronModel(parameters, updatedPredLabels, outcomeLabels)); } }
/// <summary> /// Construct a patch string that transforms a to b. /// </summary> /// <param name="a">1st string</param> /// <param name="b">2nd string</param> /// <returns></returns> public string Exec(string a, string b) { if (a == null || b == null) { return(null); } int x; int y; int maxx; int maxy; int[] go = new int[4]; const int X = 1; const int Y = 2; const int R = 3; const int D = 0; /* * setup memory if needed => processing speed up */ maxx = a.Length + 1; maxy = b.Length + 1; if ((maxx >= sizex) || (maxy >= sizey)) { sizex = maxx + 8; sizey = maxy + 8; net = RectangularArrays.ReturnRectangularIntArray(sizex, sizey); way = RectangularArrays.ReturnRectangularIntArray(sizex, sizey); } /* * clear the network */ for (x = 0; x < maxx; x++) { for (y = 0; y < maxy; y++) { net[x][y] = 0; } } /* * set known persistent values */ for (x = 1; x < maxx; x++) { net[x][0] = x; way[x][0] = X; } for (y = 1; y < maxy; y++) { net[0][y] = y; way[0][y] = Y; } for (x = 1; x < maxx; x++) { for (y = 1; y < maxy; y++) { go[X] = net[x - 1][y] + DELETE; // way on x costs 1 unit go[Y] = net[x][y - 1] + INSERT; // way on y costs 1 unit go[R] = net[x - 1][y - 1] + REPLACE; go[D] = net[x - 1][y - 1] + ((a[x - 1] == b[y - 1]) ? NOOP : 100); // diagonal costs 0, when no change ushort min = (ushort)D; if (go[min] >= go[X]) { min = (ushort)X; } if (go[min] > go[Y]) { min = (ushort)Y; } if (go[min] > go[R]) { min = (ushort)R; } way[x][y] = min; net[x][y] = (ushort)go[min]; } } // read the patch string StringBuilder result = new StringBuilder(); char @base = (char)('a' - 1); char deletes = @base; char equals = @base; for (x = maxx - 1, y = maxy - 1; x + y != 0;) { switch (way[x][y]) { case X: if (equals != @base) { result.Append("-" + (equals)); equals = @base; } deletes++; x--; break; // delete case Y: if (deletes != @base) { result.Append("D" + (deletes)); deletes = @base; } if (equals != @base) { result.Append("-" + (equals)); equals = @base; } result.Append('I'); result.Append(b[--y]); break; // insert case R: if (deletes != @base) { result.Append("D" + (deletes)); deletes = @base; } if (equals != @base) { result.Append("-" + (equals)); equals = @base; } result.Append('R'); result.Append(b[--y]); x--; break; // replace case D: if (deletes != @base) { result.Append("D" + (deletes)); deletes = @base; } equals++; x--; y--; break; // no change } } if (deletes != @base) { result.Append("D" + (deletes)); deletes = @base; } return(result.ToString()); }
public virtual void thiningIteration(int iter) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] marker = new int[width][height]; int[][] marker = RectangularArrays.ReturnRectangularIntArray(width, height); for (int i = 1; i < width - 1; i++) { for (int j = 1; j < height - 1; j++) { int p2 = imageM[i - 1][j]; int p3 = imageM[i - 1][j + 1]; int p4 = imageM[i][j + 1]; int p5 = imageM[i + 1][j + 1]; int p6 = imageM[i + 1][j]; int p7 = imageM[i + 1][j - 1]; int p8 = imageM[i][j - 1]; int p9 = imageM[i - 1][j - 1]; // int A = (p2 == 0 && p3 == 1) + (p3 == 0 && p4 == 1) + // (p4 == 0 && p5 == 1) + (p5 == 0 && p6 == 1) + // (p6 == 0 && p7 == 1) + (p7 == 0 && p8 == 1) + // (p8 == 0 && p9 == 1) + (p9 == 0 && p2 == 1); int c1 = 0; //p2 == 0 && p3 == 1 int c2 = 0; //p3 == 0 && p4 == 1 int c3 = 0; //p4 == 0 && p5 == 1 int c4 = 0; //p5 == 0 && p6 == 1 int c5 = 0; //p6 == 0 && p7 == 1 int c6 = 0; //p7 == 0 && p8 == 1 int c7 = 0; //p8 == 0 && p9 == 1 int c8 = 0; //p9 == 0 && p2 == 1 if (p2 == 0 && p3 == 1) { c1 = 1; } if (p3 == 0 && p4 == 1) { c2 = 1; } if (p4 == 0 && p5 == 1) { c3 = 1; } if (p5 == 0 && p6 == 1) { c4 = 1; } if (p6 == 0 && p7 == 1) { c5 = 1; } if (p7 == 0 && p8 == 1) { c6 = 1; } if (p8 == 0 && p9 == 1) { c7 = 1; } if (p9 == 0 && p2 == 1) { c8 = 1; } int A = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8; int B = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9; int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8); int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8); if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0) { marker[i][j] = 1; } } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int tmp = 1 - marker[i][j]; if (imageM[i][j] == tmp && imageM[i][j] == 1) { imageM[i][j] = 1; } else { imageM[i][j] = 0; } } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void read(ByteBuffer f) throws java.io.IOException private void read(ByteBuffer f) { if (f.capacity() == 0) { return; } // PGF Header. headerOffset = readUHalf(f); headerSize = readUHalf(f); // Offset 4 PGFMagic_Renamed = readStringNZ(f, 4); // PGF0. revision = readWord(f); version = readWord(f); // Offset 16 charMapLength = readWord(f); charPointerLength = readWord(f); charMapBpe = readWord(f); charPointerBpe = readWord(f); skipUnknown(f, 2); // Offset 34 bpp = readUByte(f); skipUnknown(f, 1); // Offset 36 hSize = readWord(f); vSize = readWord(f); hResolution = readWord(f); vResolution = readWord(f); skipUnknown(f, 1); // Offset 53 fontName = readStringNZ(f, 64); fontType = readStringNZ(f, 64); skipUnknown(f, 1); // Offset 182 firstGlyph = readUHalf(f); lastGlyph = readUHalf(f); skipUnknown(f, 26); // Offset 212 maxAscender = readWord(f); maxDescender = readWord(f); maxLeftXAdjust = readWord(f); maxBaseYAdjust = readWord(f); minCenterXAdjust = readWord(f); maxTopYAdjust = readWord(f); // Offset 236 maxAdvance[0] = readWord(f); maxAdvance[1] = readWord(f); maxSize[0] = readWord(f); maxSize[1] = readWord(f); maxGlyphWidth = readUHalf(f); maxGlyphHeight = readUHalf(f); skipUnknown(f, 2); // Offset 258 dimTableLength = readUByte(f); xAdjustTableLength = readUByte(f); yAdjustTableLength = readUByte(f); advanceTableLength = readUByte(f); skipUnknown(f, 102); // NULL. // Offset 364 shadowMapLength = readWord(f); shadowMapBpe = readWord(f); skipUnknown(f, 4); // 24.0625. shadowScale[0] = readWord(f); shadowScale[1] = readWord(f); skipUnknown(f, 8); // 15.0. // Offset 392 if (revision == 3) { compCharMapBpe1 = readWord(f); compCharMapLength1 = readUHalf(f); skipUnknown(f, 2); compCharMapBpe2 = readWord(f); compCharMapLength2 = readUHalf(f); skipUnknown(f, 6); } // PGF Tables. //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: dimensionTable = new int[2][dimTableLength]; dimensionTable = RectangularArrays.ReturnRectangularIntArray(2, dimTableLength); for (int i = 0; i < dimTableLength; i++) { dimensionTable[0][i] = readWord(f); dimensionTable[1][i] = readWord(f); } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: xAdjustTable = new int[2][xAdjustTableLength]; xAdjustTable = RectangularArrays.ReturnRectangularIntArray(2, xAdjustTableLength); for (int i = 0; i < xAdjustTableLength; i++) { xAdjustTable[0][i] = readWord(f); xAdjustTable[1][i] = readWord(f); } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: yAdjustTable = new int[2][yAdjustTableLength]; yAdjustTable = RectangularArrays.ReturnRectangularIntArray(2, yAdjustTableLength); for (int i = 0; i < yAdjustTableLength; i++) { yAdjustTable[0][i] = readWord(f); yAdjustTable[1][i] = readWord(f); } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: advanceTable = new int[2][advanceTableLength]; advanceTable = RectangularArrays.ReturnRectangularIntArray(2, advanceTableLength); for (int i = 0; i < advanceTableLength; i++) { advanceTable[0][i] = readWord(f); advanceTable[1][i] = readWord(f); } int shadowCharMapSize = ((shadowMapLength * shadowMapBpe + 31) & ~31) / 8; shadowCharMap = new int[shadowCharMapSize]; for (int i = 0; i < shadowCharMapSize; i++) { shadowCharMap[i] = readUByte(f); } if (revision == 3) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: charmapCompressionTable1 = new int[2][compCharMapLength1]; charmapCompressionTable1 = RectangularArrays.ReturnRectangularIntArray(2, compCharMapLength1); for (int i = 0; i < compCharMapLength1; i++) { charmapCompressionTable1[0][i] = readUHalf(f); charmapCompressionTable1[1][i] = readUHalf(f); } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: charmapCompressionTable2 = new int[2][compCharMapLength2]; charmapCompressionTable2 = RectangularArrays.ReturnRectangularIntArray(2, compCharMapLength2); for (int i = 0; i < compCharMapLength2; i++) { charmapCompressionTable2[0][i] = readUHalf(f); charmapCompressionTable2[1][i] = readUHalf(f); } } int charMapSize = ((charMapLength * charMapBpe + 31) & ~31) / 8; charMap = new int[charMapSize]; for (int i = 0; i < charMapSize; i++) { charMap[i] = readUByte(f); } int charPointerSize = (((charPointerLength * charPointerBpe + 31) & ~31) / 8); charPointerTable = new int[charPointerSize]; for (int i = 0; i < charPointerSize; i++) { charPointerTable[i] = readUByte(f); } // PGF Fontdata. fontDataOffset = f.position(); fontDataLength = f.capacity() - fontDataOffset; fontData = new int[fontDataLength]; for (int i = 0; i < fontDataLength; i++) { fontData[i] = readUByte(f); } }
private void generateFontTexture(int @base, int bpl, int bufWidth, int bufHeight, int x, int y, int x64, int y64, int clipX, int clipY, int clipWidth, int clipHeight, int pixelformat, Glyph glyph, int glyphType, bool addColor) { if (((glyph.flags & FONT_PGF_BMP_OVERLAY) != FONT_PGF_BMP_H_ROWS) && ((glyph.flags & FONT_PGF_BMP_OVERLAY) != FONT_PGF_BMP_V_ROWS)) { return; } long bitPtr = glyph.ptr * 8; const int nibbleBits = 4; bool bitmapHorizontalRows = (glyph.flags & FONT_PGF_BMP_OVERLAY) == FONT_PGF_BMP_H_ROWS; int numberPixels = glyph.w * glyph.h; int scaleX = 1; int scaleY = 1; if (glyphType == FONT_PGF_GLYPH_TYPE_SHADOW) { scaleX = 64 / shadowScaleX; scaleY = 64 / shadowScaleY; } //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] bitmap = new int[glyph.h][glyph.w]; int[][] bitmap = RectangularArrays.ReturnRectangularIntArray(glyph.h, glyph.w); int pixelIndex = 0; while (pixelIndex < numberPixels && bitPtr + 8 < fontdataBits) { int nibble = getBits(nibbleBits, fontdata, bitPtr); bitPtr += nibbleBits; int count; int value = 0; if (nibble < 8) { value = getBits(nibbleBits, fontdata, bitPtr); bitPtr += nibbleBits; count = nibble + 1; } else { count = 16 - nibble; } for (int i = 0; i < count && pixelIndex < numberPixels; i++) { if (nibble >= 8) { value = getBits(nibbleBits, fontdata, bitPtr); bitPtr += nibbleBits; } int xx, yy; if (bitmapHorizontalRows) { xx = pixelIndex % glyph.w; yy = pixelIndex / glyph.w; } else { xx = pixelIndex / glyph.h; yy = pixelIndex % glyph.h; } bitmap[yy][xx] = value; pixelIndex++; } } for (int yy = 0; yy <= glyph.h; yy++) { for (int xx = 0; xx <= glyph.w; xx++) { int sample = this.sample(bitmap, xx, yy, x64, y64); // A pixel with value 0 is not changing the value in the buffer (tested on PSP) if (sample != 0) { int pixelX = x + xx * scaleX; int pixelY = y + yy * scaleY; if (pixelX >= clipX && pixelX < clipX + clipWidth && pixelY >= clipY && pixelY < clipY + clipHeight) { // 4-bit color value int pixelColor = sample; switch (pixelformat) { case sceFont.PSP_FONT_PIXELFORMAT_8: // 8-bit color value pixelColor |= pixelColor << 4; break; case sceFont.PSP_FONT_PIXELFORMAT_24: // 24-bit color value pixelColor |= pixelColor << 4; pixelColor |= pixelColor << 8; pixelColor |= pixelColor << 8; break; case sceFont.PSP_FONT_PIXELFORMAT_32: // 32-bit color value pixelColor |= pixelColor << 4; pixelColor |= pixelColor << 8; pixelColor |= pixelColor << 16; break; } for (int yyy = 0; yyy < scaleY; yyy++) { for (int xxx = 0; xxx < scaleX; xxx++) { if (addColor) { Debug.addFontPixel(@base, bpl, bufWidth, bufHeight, pixelX + xxx, pixelY + yyy, pixelColor, pixelformat); } else { Debug.setFontPixel(@base, bpl, bufWidth, bufHeight, pixelX + xxx, pixelY + yyy, pixelColor, pixelformat); } } } } } } } }
/// /// <param name="image"> The input image should be binary /// @return </param> public virtual BufferedImage processImage(BufferedImage image) { originalImage = image; width = originalImage.Width; height = originalImage.Height; filteredImage = new BufferedImage(width, height, originalImage.Type); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: imageM = new int[width][height]; imageM = RectangularArrays.ReturnRectangularIntArray(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int col = (new Color(originalImage.getRGB(i, j))).Red; if (blackLetters) { imageM[i][j] = 1 - (col / 255); } else { imageM[i][j] = col / 255; } } } while (true) { //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] start = new int[width][height]; int[][] start = RectangularArrays.ReturnRectangularIntArray(width, height); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { start[i][j] = imageM[i][j]; } } thiningGuoHallIteration(0); thiningGuoHallIteration(1); bool same = true; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (start[i][j] != imageM[i][j]) { same = false; goto MainforLoopBreak; } } MainforLoopContinue :; } MainforLoopBreak : if (same) { break; } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int alpha = (new Color(originalImage.getRGB(i, j))).Alpha; int col; if (blackLetters) { col = 255 - imageM[i][j] * 255; } else { col = imageM[i][j] * 255; } int rgb = ImageUtilities.colorToRGB(alpha, col, col, col); filteredImage.setRGB(i, j, rgb); } } return(filteredImage); }
private int GetTDistance(string target, string other) { char[] sa; int n; int[][] d; // cost array sa = target.ToCharArray(); n = sa.Length; int m = other.Length; d = RectangularArrays.ReturnRectangularIntArray(n + 1, m + 1); if (n == 0 || m == 0) { if (n == m) { return(0); } else { return(Math.Max(n, m)); } } // indexes into strings s and t int i; // iterates through s int j; // iterates through t char t_j; // jth character of t int cost; // cost for (i = 0; i <= n; i++) { d[i][0] = i; } for (j = 0; j <= m; j++) { d[0][j] = j; } for (j = 1; j <= m; j++) { t_j = other[j - 1]; for (i = 1; i <= n; i++) { cost = sa[i - 1] == t_j ? 0 : 1; // minimum of cell to the left+1, to the top+1, diagonally left and up +cost d[i][j] = Math.Min(Math.Min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + cost); // transposition if (i > 1 && j > 1 && target[i - 1] == other[j - 2] && target[i - 2] == other[j - 1]) { d[i][j] = Math.Min(d[i][j], d[i - 2][j - 2] + cost); } } } // our last action in the above loop was to switch d and p, so p now // actually has the most recent cost counts return(Math.Abs(d[n][m])); }
private int[][] embed(Bitmap src, int[][] water1) { int width = (src.Width + 7) / 8 * 8; int height = (src.Height + 7) / 8 * 8; // original image process const int N = 8; int[][] buff1 = RectangularArrays.ReturnRectangularIntArray(height, width); // Original image int[][] buff2 = RectangularArrays.ReturnRectangularIntArray(height, width); // DCT Original image coefficients int[][] buff3 = RectangularArrays.ReturnRectangularIntArray(height, width); // IDCT Original image coefficients int[][] b1 = RectangularArrays.ReturnRectangularIntArray(N, N); // DCT input int[][] b2 = RectangularArrays.ReturnRectangularIntArray(N, N); // DCT output int[][] b3 = RectangularArrays.ReturnRectangularIntArray(N, N); // IDCT input int[][] b4 = RectangularArrays.ReturnRectangularIntArray(N, N); // IDCT output // watermark image process const int W = 4; int[][] water2 = RectangularArrays.ReturnRectangularIntArray(128, 128); // random watermark image int[][] water3 = RectangularArrays.ReturnRectangularIntArray(128, 128); // DCT watermark image coefficients int[][] w1 = RectangularArrays.ReturnRectangularIntArray(W, W); // DCT input int[][] w2 = RectangularArrays.ReturnRectangularIntArray(W, W); // DCT output int[][] w3 = RectangularArrays.ReturnRectangularIntArray(W, W); // quantization output int[][] mfbuff1 = RectangularArrays.ReturnRectangularIntArray(128, 128); // embed coefficients int[] mfbuff2 = new int[width * height]; // 2 to 1 // random process... int a, b, c; int[] tmp = new int[128 * 128]; // random embed... int c1; int cc = 0; int[] tmp1 = new int[128 * 128]; // divide 8x8 block... int k = 0, l = 0; // init buf1 from src image... for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { if (_stopped) { return(null); } Color color = new Color(); color = src.GetPixel(x, y); double[] hsb = RGBtoHSB(color); // use brightness of the pixel... buff1[y][x] = (int)(hsb[2] * 255.0); } } for (int y = 0; y < height; y += N) { for (int x = 0; x < width; x += N) { for (int i = y; i < y + N; i++) { for (int j = x; j < x + N; j++) { b1[k][l] = buff1[i][j]; l++; } l = 0; k++; } k = 0; DCT o1 = new DCT(); o1.ForwardDCT(b1, b2); for (int p = y; p < y + N; p++) { for (int q = x; q < x + N; q++) { buff2[p][q] = b2[k][l]; l++; } l = 0; k++; } k = 0; } } Random r = new Random((int)randomizeWatermarkSeed); for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) { while (true) { c = r.Next(128 * 128); if (tmp[c] == 0) { break; } } a = c / 128; b = c % 128; water2[i][j] = water1[a][b]; tmp[c] = 1; } } k = 0; l = 0; for (int y = 0; y < 128; y += W) { for (int x = 0; x < 128; x += W) { for (int i = y; i < y + W; i++) { for (int j = x; j < x + W; j++) { w1[k][l] = water2[i][j]; l++; } l = 0; k++; } k = 0; DCT wm1 = new DCT(4); wm1.ForwardDCT(w1, w2); Qt qw1 = new Qt(); qw1.WaterQt(w2, w3); for (int p = y; p < y + W; p++) { for (int q = x; q < x + W; q++) { water3[p][q] = w3[k][l]; l++; } l = 0; k++; } k = 0; } } // Random Embedding Random r1 = new Random((int)randomizeEmbeddingSeed); for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) { while (true) { c1 = r1.Next(128 * 128); if (tmp1[c1] == 0) { break; } } a = c1 / 128; b = c1 % 128; mfbuff1[i][j] = water3[a][b]; tmp1[c1] = 1; } } ZigZag scan = new ZigZag(); scan.two2one(mfbuff1, mfbuff2); // WriteBack coefficients for (int i = 0; i < height; i += N) { for (int j = 0; j < width; j += N) { buff2[i + 1][j + 4] = mfbuff2[cc]; cc++; buff2[i + 2][j + 3] = mfbuff2[cc]; cc++; buff2[i + 3][j + 2] = mfbuff2[cc]; cc++; buff2[i + 4][j + 1] = mfbuff2[cc]; cc++; } } cc = 0; k = 0; l = 0; for (int y = 0; y < height; y += N) { for (int x = 0; x < width; x += N) { for (int i = y; i < y + N; i++) { for (int j = x; j < x + N; j++) { b3[k][l] = buff2[i][j]; l++; } l = 0; k++; } k = 0; DCT o2 = new DCT(); o2.InverseDCT(b3, b4); for (int p = y; p < y + N; p++) { for (int q = x; q < x + N; q++) { buff3[p][q] = b4[k][l]; l++; } l = 0; k++; } k = 0; } } return(buff3); }
public virtual void embed(Bitmap image, Bits data) { Bits bits; // make the size fit... if (data.size() > maxBitsData) { bits = new Bits(data.getBits(0, maxBitsData)); } else { bits = new Bits(data); while (bits.size() < maxBitsData) { bits.addBit(false); } } // add error correction... if (byteLenErrorCorrection > 0) { bits = Bits.bitsReedSolomonEncode(bits, byteLenErrorCorrection); } // create watermark image... int[][] watermarkBitmap = RectangularArrays.ReturnRectangularIntArray(128, 128); for (int y = 0; y < 128 / bitBoxSize * bitBoxSize; y++) //Yes, for some reason a/b*b != a...don't ask me why, it's also not equal to a/b^2, but seems it needs to be this way { for (int x = 0; x < 128 / bitBoxSize * bitBoxSize; x++) { if (bits.size() > x / bitBoxSize + y / bitBoxSize * (128 / bitBoxSize)) { if (_stopped) { return; } watermarkBitmap[y][x] = bits.getBit(x / bitBoxSize + y / bitBoxSize * (128 / bitBoxSize)) ? 255 : 0; } } } // embedding... int[][] grey = embed(image, watermarkBitmap); // added computed data to original image... for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { if (_stopped) { return; } Color color = image.GetPixel(x, y); double[] hsb = RGBtoHSB(color); // adjust brightness of the pixel... hsb[2] = (float)(hsb[2] * (1.0 - opacity) + grey[y][x] * opacity / 255.0); Color colorNew = HSBtoRGB(hsb[0], hsb[1], hsb[2]); image.SetPixel(x, y, colorNew); } } }
public float GetDistance(string target, string other) { IntsRef targetPoints; IntsRef otherPoints; int n; int[][] d; // cost array // NOTE: if we cared, we could 3*m space instead of m*n space, similar to // what LevenshteinDistance does, except cycling thru a ring of three // horizontal cost arrays... but this comparator is never actually used by // DirectSpellChecker, its only used for merging results from multiple shards // in "distributed spellcheck", and its inefficient in other ways too... // cheaper to do this up front once targetPoints = ToIntsRef(target); otherPoints = ToIntsRef(other); n = targetPoints.Length; int m = otherPoints.Length; //TODO The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: (ORIGINAL LINE: d = new int[n+1][m+1];) d = RectangularArrays.ReturnRectangularIntArray(n + 1, m + 1); if (n == 0 || m == 0) { if (n == m) { return(0); } else { return(Math.Max(n, m)); } } // indexes into strings s and t int i; // iterates through s int j; // iterates through t int t_j; // jth character of t int cost; // cost for (i = 0; i <= n; i++) { d[i][0] = i; } for (j = 0; j <= m; j++) { d[0][j] = j; } for (j = 1; j <= m; j++) { t_j = otherPoints.Ints[j - 1]; for (i = 1; i <= n; i++) { cost = targetPoints.Ints[i - 1] == t_j ? 0 : 1; // minimum of cell to the left+1, to the top+1, diagonally left and up +cost d[i][j] = Math.Min(Math.Min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + cost); // transposition if (i > 1 && j > 1 && targetPoints.Ints[i - 1] == otherPoints.Ints[j - 2] && targetPoints.Ints[i - 2] == otherPoints.Ints[j - 1]) { d[i][j] = Math.Min(d[i][j], d[i - 2][j - 2] + cost); } } } return(1.0f - ((float)d[n][m] / Math.Min(m, n))); }
private int[][] extractRaw(Bitmap src) { int width = (src.Width + 7) / 8 * 8; int height = (src.Height + 7) / 8 * 8; // original image const int N = 8; int[][] buff1 = RectangularArrays.ReturnRectangularIntArray(height, width); // watermarked image int[][] buff2 = RectangularArrays.ReturnRectangularIntArray(height, width); // DCT watermarked image coefficients int[][] b1 = RectangularArrays.ReturnRectangularIntArray(N, N); // DCT input int[][] b2 = RectangularArrays.ReturnRectangularIntArray(N, N); // DCT output // watermark const int W = 4; int[][] water1 = RectangularArrays.ReturnRectangularIntArray(128, 128); // extract watermark image int[][] water2 = RectangularArrays.ReturnRectangularIntArray(128, 128); // DCT watermark image coefficients int[][] water3 = RectangularArrays.ReturnRectangularIntArray(128, 128); // random watermark image int[][] w1 = RectangularArrays.ReturnRectangularIntArray(W, W); // dequantization output int[][] w2 = RectangularArrays.ReturnRectangularIntArray(W, W); // DCT input int[][] w3 = RectangularArrays.ReturnRectangularIntArray(W, W); // DCT output // random process int a, b, c, c1; int[] tmp = new int[128 * 128]; int[] tmp1 = new int[128 * 128]; int cc = 0; // middle frequency coefficients // final int mfbuff1[] = new int[128 * 128]; int[] mfbuff1 = new int[width * height]; int[][] mfbuff2 = RectangularArrays.ReturnRectangularIntArray(128, 128); // 1 to 2 // divide 8x8 block int k = 0, l = 0; // init buf1 from watermarked image src... for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { if (_stopped) { return(null); } Color color = new Color(); color = src.GetPixel(x, y); double[] hsb = RGBtoHSB(color); // use brightness of the pixel... buff1[y][x] = (int)(hsb[2] * 255.0); } } for (int y = 0; y < height; y += N) { for (int x = 0; x < width; x += N) { for (int i = y; i < y + N; i++) { for (int j = x; j < x + N; j++) { b1[k][l] = buff1[i][j]; l++; } l = 0; k++; } k = 0; DCT o1 = new DCT(); o1.ForwardDCT(b1, b2); for (int p = y; p < y + N; p++) { for (int q = x; q < x + N; q++) { buff2[p][q] = b2[k][l]; l++; } l = 0; k++; } k = 0; } } for (int i = 0; i < height; i += N) { for (int j = 0; j < width; j += N) { mfbuff1[cc] = buff2[i + 1][j + 4]; cc++; mfbuff1[cc] = buff2[i + 2][j + 3]; cc++; mfbuff1[cc] = buff2[i + 3][j + 2]; cc++; mfbuff1[cc] = buff2[i + 4][j + 1]; cc++; } } cc = 0; ZigZag scan = new ZigZag(); scan.one2two(mfbuff1, mfbuff2); // random extracting Random r1 = new Random((int)randomizeEmbeddingSeed); for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) { while (true) { c1 = r1.Next(128 * 128); if (tmp1[c1] == 0) { break; } } a = c1 / 128; b = c1 % 128; water1[a][b] = mfbuff2[i][j]; tmp1[c1] = 1; } } k = 0; l = 0; for (int y = 0; y < 128; y += W) { for (int x = 0; x < 128; x += W) { for (int i = y; i < y + W; i++) { for (int j = x; j < x + W; j++) { w1[k][l] = water1[i][j]; l++; } l = 0; k++; } k = 0; Qt qw2 = new Qt(); qw2.WaterDeQt(w1, w2); DCT wm2 = new DCT(4); wm2.InverseDCT(w2, w3); for (int p = y; p < y + W; p++) { for (int q = x; q < x + W; q++) { water2[p][q] = w3[k][l]; l++; } l = 0; k++; } k = 0; } } Random r = new Random((int)randomizeWatermarkSeed); for (int i = 0; i < 128; i++) { for (int j = 0; j < 128; j++) { while (true) { c = r.Next(128 * 128); if (tmp[c] == 0) { break; } } a = c / 128; b = c % 128; water3[a][b] = water2[i][j]; tmp[c] = 1; } } return(water3); }
public static void diff(string[] x, string[] y) { // number of lines of each file int M = x.Length; int N = y.Length; // opt[i][j] = Length of LCS of x[i..M] and y[j..N] //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: int[][] opt = new int[M+1][N+1]; int[][] opt = RectangularArrays.ReturnRectangularIntArray(M + 1, N + 1); // compute Length of LCS and all subproblems via dynamic programming for (int i = M - 1; i >= 0; i--) { for (int j = N - 1; j >= 0; j--) { if (x[i].Equals(y[j])) { opt[i][j] = opt[i + 1][j + 1] + 1; } else { opt[i][j] = System.Math.Max(opt[i + 1][j], opt[i][j + 1]); } } } // recover LCS itself and print out non-matching lines to standard output int i = 0, j = 0; while (i < M && j < N) { if (x[i].Equals(y[j])) { debug(" " + x[i]); i++; j++; } else if (opt[i + 1][j] >= opt[i][j + 1]) { info("- " + x[i++]); } else { info("+ " + y[j++]); } } // dump out one remainder of one string if the other is exhausted while (i < M || j < N) { if (i == M) { info("+ " + y[j++]); } else if (j == N) { info("- " + x[i++]); } } }