Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 30, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbrl/6afbc2405c2286675ed5 to your computer and use it in GitHub Desktop.
Save sbrl/6afbc2405c2286675ed5 to your computer and use it in GitHub Desktop.
An image generator that uses 3 functions to generate a PPM image. Useful for quick math image tests. #template
using System;
using System.IO;
public class Program
{
static string filename = "image.ppm";
static int width = 1024;
static int height = 1024;
public static void Main()
{
byte[] pixelData = new byte[width * height * 3];
for(int x = 0; x < width; ++x)
{
for(int y = 0; y < height; ++y)
{
int currentPixel = ((y * width) + x) * 3;
pixelData[currentPixel] = redPixel(x, y);
pixelData[currentPixel + 1] = greenPixel(x, y);
pixelData[currentPixel + 2] = bluePixel(x, y);
}
}
StreamWriter destination = new StreamWriter(filename);
destination.Write("P6\n{0} {1}\n{2}\n", width, height, 255);
destination.Flush();
destination.BaseStream.Write(pixelData, 0, pixelData.Length);
destination.Close();
}
public static byte redPixel(int x, int y)
{
return y;
}
public static byte greenPixel(int x, int y)
{
return (x + y) % 255;
}
public static byte bluePixel(int x, int y)
{
return x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment