// 섬 데이터와 외곽선 데이터를 이용해 색칠을 자동으로 해 본다. // 색칠 후 이미지에 문제가 없는지 확인하기 위한 테스트 과정이다. static void ExecuteDetermineIslandTest(string sourceFileName, string bytesFileName) { Console.Out.WriteLine($"Running {nameof(ExecuteDetermineIslandTest)}"); var targetFileName = AppendToFileName(sourceFileName, "-DIT"); StageData stageData; using (var bytesFileStream = new FileStream(bytesFileName, FileMode.Open)) { var formatter = new BinaryFormatter(); try { // Deserialize the hashtable from the file and // assign the reference to the local variable. stageData = (StageData)formatter.Deserialize(bytesFileStream); } catch (SerializationException e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); throw; } } using (var image = Image.Load <Rgba32>(sourceFileName)) { foreach (var island in stageData.islandDataByMinPoint) { var minPoint = UInt32ToVector2Int(island.Key); var targetColor = UInt32ToRgba32(island.Value.rgba); var fillMinPoint = FloodFill.ExecuteFillIf(image, minPoint, Rgba32.White, targetColor, out var pixelArea, out _, out _); if (fillMinPoint != new Vector2Int(image.Width, image.Height) && pixelArea == island.Value.pixelArea) { } else { Console.WriteLine("Logic error in ExecuteDetermineIslandTest()!"); } } using (var stream = new FileStream(targetFileName, FileMode.Create)) { image.SaveAsPng(stream); stream.Close(); } } }
// 섬 데이터와 외곽선 데이터를 이용해 색칠을 자동으로 해 본다. // 색칠 후 이미지에 문제가 없는지 확인하기 위한 테스트 과정이다. static string ExecuteDetermineIslandTest(string sourceFileName, string bytesFileName, bool errorAsWarning, bool writeA1A2Tex) { Logger.WriteLine($"Running {nameof(ExecuteDetermineIslandTest)}"); var targetFileName = AppendToFileName(sourceFileName, "-DIT"); var a1TexFileName = AppendToFileName(targetFileName, "-A1"); var a2TexFileName = AppendToFileName(targetFileName, "-A2"); StageData stageData; using (var bytesFileStream = new FileStream(bytesFileName, FileMode.Open)) { var formatter = new BinaryFormatter(); try { // Deserialize the hashtable from the file and // assign the reference to the local variable. stageData = (StageData)formatter.Deserialize(bytesFileStream); } catch (SerializationException e) { Logger.WriteErrorLine("Failed to deserialize. Reason: " + e.Message); throw; } } var colorUintArray = stageData.CreateColorUintArray(); var colorUintDict = new Dictionary <uint, int>(); for (var i = 0; i < colorUintArray.Length; i++) { colorUintDict[colorUintArray[i]] = i + 1; // Palette Index 0은 외곽선 용으로 예비한다. } using (var image = Image.Load <Rgba32>(sourceFileName)) { var a1Tex = new Image <Rgba32>(image.Width, image.Height, AllZeros); var a2Tex = new Image <Rgba32>(image.Width, image.Height, AllZeros); var islandIndex = 1; // Island Index 0은 외곽선 용으로 예비한다. // dictionary의 이터레이션 순서에 의존하면 안되고, island data에서 지정한 index 순서로 하자. foreach (var island in stageData.islandDataByMinPoint.OrderBy(e => e.Value.index)) { var minPoint = UInt32ToVector2Int(island.Key); var targetColor = UInt32ToRgba32(island.Value.rgba); var paletteIndex = colorUintDict[island.Value.rgba]; var fillMinPoint = FloodFill.ExecuteFillIf(image, minPoint, White, targetColor, out var pixelArea, out _, out _, islandIndex, (islandIndexCallback, fx, fy) => { GetAlpha8Pair(islandIndexCallback, paletteIndex, out var a1, out var a2); a1Tex[fx, fy] = new Rgba32 { A = a1 }; a2Tex[fx, fy] = new Rgba32 { A = a2 }; }); if (fillMinPoint == new Vector2Int(image.Width, image.Height)) { if (errorAsWarning) { // 이번엔 오류로 안친다. Logger.WriteLine("Logic error in ExecuteDetermineIslandTest()! Invalid fillMinPoint"); } else { Logger.WriteErrorLine("Logic error in ExecuteDetermineIslandTest()! Invalid fillMinPoint"); } } if (pixelArea != island.Value.pixelArea) { if (errorAsWarning) { // 이번엔 오류로 안친다. Logger.WriteLine( $"Logic error in ExecuteDetermineIslandTest()! Pixel area {pixelArea} expected to be {island.Value.pixelArea}"); } else { Logger.WriteErrorLine( $"Logic error in ExecuteDetermineIslandTest()! Pixel area {pixelArea} expected to be {island.Value.pixelArea}"); } } islandIndex++; } using (var stream = new FileStream(targetFileName, FileMode.Create)) { image.SaveAsPng(stream); stream.Close(); } if (writeA1A2Tex) { using (var stream = new FileStream(a1TexFileName, FileMode.Create)) { a1Tex.SaveAsPng(stream); stream.Close(); } using (var stream = new FileStream(a2TexFileName, FileMode.Create)) { a2Tex.SaveAsPng(stream); stream.Close(); } } } return(targetFileName); }