Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- //Makes pretty colored bitmaps! Yay!
- //Make sure to change the save location for the bitmap to your desktop, or wherever. Line 68.
- //Also, make sure to compile with the unsafe flag, since this code uses pointers.
- namespace Pointless
- {
- class Program
- {
- static void Main()
- {
- MakeStuff("1");
- Console.WriteLine("Done!");
- Console.ReadKey();
- }
- //Makes pretty pictures!
- static unsafe void MakeStuff(string thing)
- {
- Random r = new Random();
- Helper helper = new Helper();
- //Note that when working with the byte arrays using Format32bppRgb, the order of colors is BGR instead of RGB.
- // pixel[0] = Blue;
- // pixel[1] = Green;
- // pixel[2] = Red;
- //As another side note, BitmapData.Stride is the length of a scan line in *bytes*,
- // while BitmapData.Width is the length of a scan line in *pixels*
- using (Bitmap bmp = new Bitmap(800, 800, PixelFormat.Format32bppRgb))
- {
- BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
- PixGet pixGet = new PixGet(data);
- //First loop. Fills in lines diagonally south/west to north/east. Begins at the top left and ends when y reaches the last scanline.
- for (int y = 0; y < data.Height; y++)
- {
- for (int x = 0, altY = y; altY >= 0 && x < data.Width; x++, altY--)
- {
- byte* datum = pixGet.GetPixel(x, altY);
- byte[] next = helper.GetNextColor();
- datum[2] = next[0];
- datum[1] = next[1];
- datum[0] = next[2];
- }
- }
- //Second loop. Fills in lines diagonally south/west to north/east. Begins at the bottom left and ends when x reaches
- // the right side of the bitmap.
- for (int x = 1; x < data.Width; x++)
- {
- for (int altX = x, altY = data.Height - 1; altX < data.Width && altY >= 0; altX++, altY--)
- {
- byte* datum = pixGet.GetPixel(altX, altY);
- byte[] next = helper.GetNextColor();
- datum[2] = next[0];
- datum[1] = next[1];
- datum[0] = next[2];
- }
- }
- bmp.UnlockBits(data);
- bmp.Save(String.Format("C:\\datum_{0}.bmp", thing), ImageFormat.Bmp); //Change this location! omg!
- }
- }
- }
- /// <summary>
- /// Simple helper class to return a pointer to the pixel in a bitmap according to its x,y coordinates.
- /// </summary>
- public unsafe class PixGet
- {
- private byte* scan0;
- private int bytesPerPixel;
- private int stride;
- public PixGet(BitmapData data)
- {
- this.scan0 = (byte*)data.Scan0.ToPointer();
- this.bytesPerPixel = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8;
- this.stride = data.Stride;
- }
- public byte* GetPixel(int x, int y)
- {
- return scan0 + (y * stride) + (x * bytesPerPixel);
- }
- }
- /// <summary>
- /// Helper class to get various pixel color values.
- /// </summary>
- public class Helper
- {
- private bool increasing = false;
- private int previous = -1;
- byte[] prevColor = null;
- private Random r = new Random();
- /// <summary>
- /// Gets the next integer in a range from 0 to 255.
- /// Starts at 0 and increments to 255, then decrements down to 0.
- /// The process repeats.
- /// </summary>
- /// <returns>A byte</returns>
- public byte GetNext()
- {
- if (previous == -1)
- {
- previous = 0;
- return (byte)previous;
- }
- else
- {
- if (increasing)
- {
- if (previous == 255)
- {
- previous--;
- increasing = false;
- }
- else
- previous++;
- return (byte)previous;
- }
- else
- {
- if (previous == 0)
- {
- previous++;
- increasing = true;
- }
- else
- previous--;
- return (byte)previous;
- }
- }
- }
- /// <summary>
- /// Gets a byte array with length of 3 corresponding to the R G B values of a pixel.
- /// The first color returned is random. The subsequent colors are changed slightly from the
- /// previous color based on a weighted random number.
- /// </summary>
- /// <returns>A byte[3] array</returns>
- public byte[] GetNextColor()
- {
- if (prevColor == null)
- {
- prevColor = new[] { (byte)r.Next(256), (byte)r.Next(256), (byte)r.Next(256) };
- return prevColor;
- }
- else
- {
- byte R = getWeightedByte(prevColor[0]);
- byte G = getWeightedByte(prevColor[1]);
- byte B = getWeightedByte(prevColor[2]);
- prevColor = new[] { R, G, B };
- return prevColor;
- }
- }
- /// <summary>
- /// Gets a byte in close proximity to a previous value. Weighted, so values tend to drift between
- /// 15 and 240.
- /// </summary>
- /// <param name="previous">The previous value to compare to. Uses an Int32 to allow negative values.</param>
- /// <returns>A byte</returns>
- private byte getWeightedByte(int previous)
- {
- int returnVal = previous;
- if (previous >= 0 && previous <= 15)
- returnVal += r.Next(-2, 5);
- else if (previous <= 255 && previous >= 240)
- returnVal += r.Next(-4, 3);
- else
- returnVal += r.Next(-2, 3);
- if (returnVal < 0) returnVal = 0;
- else if (returnVal > 255) returnVal = 255;
- return (byte)returnVal;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement