public void LoadMasksFromString(string mstring) { foreach (string text in mstring.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) { Mask m = new Mask(text); masks.Add(m); } FormNav.ShowMessage(masks.Count.ToString() + " masks loaded"); }
public void OnMouseUp(Point mousePositionEnd, int actionType) { isMouseDown = false; Bitmap bm = (Bitmap)pbox.Image; if (mousePositionEnd != mousePositionStart) // it's a drag { // get real start and end Point start = new Point(Math.Min(mousePositionStart.X, mousePositionEnd.X), Math.Min(mousePositionStart.Y, mousePositionEnd.Y)); Point end = new Point(Math.Max(mousePositionStart.X, mousePositionEnd.X), Math.Max(mousePositionStart.Y, mousePositionEnd.Y)); Mask mask = new Mask(start, end, centerTilexy.GetNeighbor(-centerX, -centerY)); switch (actionType) { case 0: // get statistics Paint(); // Very important! Paint first to get rid of red line drawn by drag of mouse byte minr = byte.MaxValue, ming = byte.MaxValue, minb = byte.MaxValue; byte maxr = 0, maxg = 0, maxb = 0; for (int w = start.X; w <= end.X; w++) { for (int h = start.Y; h <= end.Y; h++) { Color c = bm.GetPixel(w, h); if (c.A == 0) { continue; } maxr = Math.Max(maxr, c.R); maxg = Math.Max(maxg, c.G); maxb = Math.Max(maxb, c.B); minr = Math.Min(minr, c.R); ming = Math.Min(ming, c.G); minb = Math.Min(minb, c.B); } } FormNav.ShowMessage("Min RGB: " + minr + "/" + ming + "/" + minb); FormNav.ShowMessage("Max RGB: " + maxr + "/" + maxg + "/" + maxb); break; case 1: // zoom in TileXY txy = centerTilexy; if (FindTileInMask(mask, false, ref txy)) { centerTilexy = txy; } break; case 2: // add to mask masks.Add(mask); FormNav.ShowMessage("new mask added"); break; case 3: // find PNG TileXY txy2 = centerTilexy; if (FindTileInMask(mask, true, ref txy2)) { centerTilexy = txy2; } break; case 4: // guess valid pixel range // important, paint to original images before doing the calculation Paint(true); // first, get pixel cnt array int[,] pixelCnt = new int[3, 256]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 256; j++) { pixelCnt[i, j] = 0; } } for (int w = start.X; w <= end.X; w++) { for (int h = start.Y; h <= end.Y; h++) { Color c = bm.GetPixel(w, h); if (c.A == 0) { continue; } pixelCnt[0, c.R]++; pixelCnt[1, c.G]++; pixelCnt[2, c.B]++; } } // then, convert to pixel sum for (int i = 0; i < 3; i++) { for (int j = 1; j < 256; j++) { pixelCnt[i, j] += pixelCnt[i, j - 1]; } } double[] ratios = new double[] { 0.6, 0.7, 0.8, 0.9, 0.95, 0.96, 0.965, 0.97, 0.975, 0.98, 0.985, 0.99, 0.995, 0.998 }; int[,] guessedRanges = new int[3, ratios.Length]; for (int i = 0; i < 3; i++) { int j = 0; for (int k = 0; k < ratios.Length; k++) { for (; j < 50; j++) { if (((double)pixelCnt[i, j]) / pixelCnt[i, 50] > ratios[k]) { break; } } guessedRanges[i, k] = j; } } cbGuessedPixelValidRanges.Items.Clear(); for (int i = 0; i < ratios.Length; i++) { cbGuessedPixelValidRanges.Items.Add((guessedRanges[0, i] + 1) + "," + (guessedRanges[1, i] + 1) + "," + (guessedRanges[2, i] + 1) + ",255,255,255"); } FormNav.ShowMessage("Pixel Valid Ranges are guessed"); break; default: break; } // paint anyway Paint(); } else // it's a single click { FormNav.ShowMessage("Color: " + bm.GetPixel(mousePositionEnd.X, mousePositionEnd.Y)); // get top left tile TileXY tileTopLeft = centerTilexy.GetNeighbor(-centerX, -centerY); for (int i = 0; i < masks.Count; i++) { if (masks[i].ContainsTilePixel(tileTopLeft, mousePositionEnd)) { FormNav.ShowMessage("Mask " + i + ": " + masks[i].ToString()); } } } }