Advertisement
Ichabod2032

Pretty Color Bitmap Maker

Sep 10th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.65 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4.  
  5. //Makes pretty colored bitmaps! Yay!
  6. //Make sure to change the save location for the bitmap to your desktop, or wherever. Line 68.
  7. //Also, make sure to compile with the unsafe flag, since this code uses pointers.
  8. namespace Pointless
  9. {
  10.     class Program
  11.     {
  12.         static void Main()
  13.         {
  14.             MakeStuff("1");
  15.  
  16.             Console.WriteLine("Done!");
  17.             Console.ReadKey();
  18.         }
  19.  
  20.         //Makes pretty pictures!
  21.         static unsafe void MakeStuff(string thing)
  22.         {
  23.             Random r = new Random();
  24.             Helper helper = new Helper();
  25.  
  26.             //Note that when working with the byte arrays using Format32bppRgb, the order of colors is BGR instead of RGB.
  27.             // pixel[0] = Blue;
  28.             // pixel[1] = Green;
  29.             // pixel[2] = Red;
  30.             //As another side note, BitmapData.Stride is the length of a scan line in *bytes*,
  31.             // while BitmapData.Width is the length of a scan line in *pixels*
  32.  
  33.             using (Bitmap bmp = new Bitmap(800, 800, PixelFormat.Format32bppRgb))
  34.             {
  35.                 BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
  36.                 PixGet pixGet = new PixGet(data);
  37.  
  38.                 //First loop. Fills in lines diagonally south/west to north/east. Begins at the top left and ends when y reaches the last scanline.
  39.                 for (int y = 0; y < data.Height; y++)
  40.                 {
  41.                     for (int x = 0, altY = y; altY >= 0 && x < data.Width; x++, altY--)
  42.                     {
  43.                         byte* datum = pixGet.GetPixel(x, altY);
  44.                         byte[] next = helper.GetNextColor();
  45.  
  46.                         datum[2] = next[0];
  47.                         datum[1] = next[1];
  48.                         datum[0] = next[2];
  49.                     }
  50.                 }
  51.  
  52.                 //Second loop. Fills in lines diagonally south/west to north/east. Begins at the bottom left and ends when x reaches
  53.                 // the right side of the bitmap.
  54.                 for (int x = 1; x < data.Width; x++)
  55.                 {
  56.                     for (int altX = x, altY = data.Height - 1; altX < data.Width && altY >= 0; altX++, altY--)
  57.                     {
  58.                         byte* datum = pixGet.GetPixel(altX, altY);
  59.                         byte[] next = helper.GetNextColor();
  60.  
  61.                         datum[2] = next[0];
  62.                         datum[1] = next[1];
  63.                         datum[0] = next[2];
  64.                     }
  65.                 }
  66.  
  67.                 bmp.UnlockBits(data);
  68.                 bmp.Save(String.Format("C:\\datum_{0}.bmp", thing), ImageFormat.Bmp); //Change this location! omg!
  69.             }
  70.         }
  71.     }
  72.  
  73.     /// <summary>
  74.     /// Simple helper class to return a pointer to the pixel in a bitmap according to its x,y coordinates.
  75.     /// </summary>
  76.     public unsafe class PixGet
  77.     {
  78.         private byte* scan0;
  79.         private int bytesPerPixel;
  80.         private int stride;
  81.  
  82.         public PixGet(BitmapData data)
  83.         {
  84.             this.scan0 = (byte*)data.Scan0.ToPointer();
  85.             this.bytesPerPixel = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8;
  86.             this.stride = data.Stride;
  87.         }
  88.  
  89.         public byte* GetPixel(int x, int y)
  90.         {
  91.             return scan0 + (y * stride) + (x * bytesPerPixel);
  92.         }
  93.     }
  94.  
  95.  
  96.     /// <summary>
  97.     /// Helper class to get various pixel color values.
  98.     /// </summary>
  99.     public class Helper
  100.     {
  101.         private bool increasing = false;
  102.         private int previous = -1;
  103.         byte[] prevColor = null;
  104.         private Random r = new Random();
  105.  
  106.         /// <summary>
  107.         /// Gets the next integer in a range from 0 to 255.
  108.         /// Starts at 0 and increments to 255, then decrements down to 0.
  109.         /// The process repeats.
  110.         /// </summary>
  111.         /// <returns>A byte</returns>
  112.         public byte GetNext()
  113.         {
  114.             if (previous == -1)
  115.             {
  116.                 previous = 0;
  117.                 return (byte)previous;
  118.             }
  119.             else
  120.             {
  121.                 if (increasing)
  122.                 {
  123.                     if (previous == 255)
  124.                     {
  125.                         previous--;
  126.                         increasing = false;
  127.                     }
  128.                     else
  129.                         previous++;
  130.  
  131.                     return (byte)previous;
  132.                 }
  133.                 else
  134.                 {
  135.                     if (previous == 0)
  136.                     {
  137.                         previous++;
  138.                         increasing = true;
  139.                     }
  140.                     else
  141.                         previous--;
  142.  
  143.                     return (byte)previous;
  144.                 }
  145.             }
  146.         }
  147.  
  148.         /// <summary>
  149.         /// Gets a byte array with length of 3 corresponding to the R G B values of a pixel.
  150.         /// The first color returned is random. The subsequent colors are changed slightly from the
  151.         /// previous color based on a weighted random number.
  152.         /// </summary>
  153.         /// <returns>A byte[3] array</returns>
  154.         public byte[] GetNextColor()
  155.         {
  156.             if (prevColor == null)
  157.             {
  158.                 prevColor = new[] { (byte)r.Next(256), (byte)r.Next(256), (byte)r.Next(256) };
  159.                 return prevColor;
  160.             }
  161.             else
  162.             {
  163.                 byte R = getWeightedByte(prevColor[0]);
  164.                 byte G = getWeightedByte(prevColor[1]);
  165.                 byte B = getWeightedByte(prevColor[2]);
  166.  
  167.                 prevColor = new[] { R, G, B };
  168.                 return prevColor;
  169.             }
  170.         }
  171.  
  172.         /// <summary>
  173.         /// Gets a byte in close proximity to a previous value. Weighted, so values tend to drift between
  174.         /// 15 and 240.
  175.         /// </summary>
  176.         /// <param name="previous">The previous value to compare to. Uses an Int32 to allow negative values.</param>
  177.         /// <returns>A byte</returns>
  178.         private byte getWeightedByte(int previous)
  179.         {
  180.             int returnVal = previous;
  181.  
  182.             if (previous >= 0 && previous <= 15)
  183.                 returnVal += r.Next(-2, 5);
  184.             else if (previous <= 255 && previous >= 240)
  185.                 returnVal += r.Next(-4, 3);
  186.             else
  187.                 returnVal += r.Next(-2, 3);
  188.  
  189.             if (returnVal < 0) returnVal = 0;
  190.             else if (returnVal > 255) returnVal = 255;
  191.  
  192.             return (byte)returnVal;
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement