Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6.  
  7. namespace Barbar.IsoTexture
  8. {
  9. class Program {
  10. static int Main(string[] args) {
  11. if (args == null || args.Length != 2 || string.IsNullOrEmpty(args[0]) || string.IsNullOrEmpty(args[1])) {
  12. Console.Out.WriteLine("Usage:");
  13. Console.Out.WriteLine("isotexture [input.(gif|png|jpg)] [output]");
  14. return -1;
  15. }
  16.  
  17. string basePath = AppDomain.CurrentDomain.BaseDirectory;
  18. string input, output;
  19. if (Path.IsPathRooted(args[0])) {
  20. input = args[0];
  21. } else {
  22. input = Path.Combine(basePath, args[0]);
  23. }
  24.  
  25. if (Path.IsPathRooted(args[1])) {
  26. output = args[1] + ".png";
  27. } else {
  28. output = Path.Combine(basePath, args[1] + ".png");
  29. }
  30.  
  31. if (!File.Exists(input)) {
  32. Console.Error.WriteLine("Input file does not exists.");
  33. return -1;
  34. }
  35.  
  36. if (File.Exists(output)) {
  37. Console.Out.WriteLine("Output file exists, overwrite ? [Y/N]");
  38. do {
  39. string response = Console.In.ReadLine();
  40. if (response.StartsWith("Y", StringComparison.OrdinalIgnoreCase)) {
  41. break;
  42. }
  43. if (response.StartsWith("N", StringComparison.OrdinalIgnoreCase)) {
  44. return -1;
  45. }
  46. } while (true);
  47. }
  48.  
  49. Bitmap inputImage = new Bitmap(input);
  50. if (inputImage.Width != inputImage.Height) {
  51. Console.Error.WriteLine("Image width must match image height.");
  52. return -1;
  53. }
  54.  
  55. var outputImage = CreateRotatedTexture(inputImage, 18);
  56. outputImage.Save(output, ImageFormat.Png);
  57.  
  58. return 0;
  59. }
  60.  
  61. private static Bitmap CreateRotatedTexture(Bitmap inputImage, int slices) {
  62. if (inputImage == null)
  63. throw new ArgumentNullException("inputImage");
  64.  
  65. if (inputImage.Width != inputImage.Height)
  66. throw new ArgumentException("Width and height of image must match.", "inputImage");
  67.  
  68. double xscale = Math.Pow(3, 0.5);
  69.  
  70. int size = inputImage.Width;
  71. var outputImage = new Bitmap((int)(xscale * size), size * slices, PixelFormat.Format32bppPArgb);
  72.  
  73. using (Graphics graphics = Graphics.FromImage(outputImage)) {
  74. graphics.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, outputImage.Width, outputImage.Height));
  75. }
  76.  
  77. for (var i = 0; i < slices; i++) {
  78. using (Graphics graphics = Graphics.FromImage(outputImage)) {
  79. Matrix matrix = new Matrix();
  80. matrix.Translate(0, i * size);
  81. matrix.Translate((float)(xscale * 0.5 * (double)size), size / 2);
  82. matrix.Scale((float)Math.Pow(1.5, 0.5), (float)Math.Pow(2, -0.5));
  83. matrix.Rotate(-45 + (360 / slices) * i);
  84. matrix.Translate(-size / 2, -size / 2);
  85.  
  86. graphics.Transform = matrix;
  87. graphics.DrawImage(inputImage, new Point(0, 0));
  88. graphics.Flush();
  89. }
  90. }
  91.  
  92. return outputImage;
  93. }
  94.  
  95.  
  96. private static Bitmap CreateIsoTexture(Bitmap inputImage) {
  97. if (inputImage == null)
  98. throw new ArgumentNullException("inputImage");
  99.  
  100. if (inputImage.Width != inputImage.Height)
  101. throw new ArgumentException("Width and height of image must match.", "inputImage");
  102.  
  103. double xscale = Math.Pow(3, 0.5);
  104.  
  105. int size = inputImage.Width;
  106. Bitmap outputImage = new Bitmap((int)(xscale * size), size, PixelFormat.Format32bppPArgb);
  107.  
  108. Matrix matrix = new Matrix();
  109. matrix.Translate((float)(xscale * 0.5 * (double)size), size / 2);
  110. matrix.Scale((float)Math.Pow(1.5, 0.5), (float)Math.Pow(2, -0.5));
  111. matrix.Rotate(-45);
  112. matrix.Translate(-size / 2, -size / 2);
  113.  
  114. using (Graphics graphics = Graphics.FromImage(outputImage)) {
  115. graphics.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, outputImage.Width, outputImage.Height));
  116. graphics.Transform = matrix;
  117. var attributes = new ImageAttributes();
  118. //attributes.SetColorKey(inputImage.GetPixel(0, 0), inputImage.GetPixel(0, 0));
  119.  
  120. graphics.DrawImage(inputImage, new Point(0,0));
  121. }
  122.  
  123. return outputImage;
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement