An image generator that uses 3 functions to generate a PPM image. Useful for quick math image tests. #template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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