Pixels to a File
You'll write an image byte by byte with no graphics library at all, then draw a gradient, a circle and a line into it with your own loops.
About
Before any window or GPU, there is an array of numbers and a file format. PPM is a short header and a list of red, green and blue bytes, and you can write one from any language in ten lines. Everything in this track rests on the idea that a pixel is three numbers you chose, so this is the place to start. Resist the urge to import an image library, because the whole point is to see the bytes. By the end you'll have Bresenham's line algorithm, which has been drawing straight lines since 1962 and still does.
At a glance
- Per requirement
- 15 XP
- Completion bonus
- 75 XP
- Requirements
- 5
- Takes
- One evening
- Skill level
- Beginner (done it a few times)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Write a 256 by 256 PPM gradient with no image library. Open a file, write the header (P6, the width and height, then 255), and follow it with three bytes per pixel, with red running left to right and green running top to bottom. Open it in an image viewer. Most will read PPM, and GIMP or a free web converter will if yours won't. Take a screenshot of the gradient open in a viewer with your code visible beside it. Photo
-
2
Draw a filled circle using the distance from its center. For every pixel, work out the distance to the center and color it if that distance is under the radius. That's the whole trick, and it's the same idea the shader badges use later. Add a one-pixel soft edge by blending across the last pixel of the radius, because a hard edge looks jagged and you'll want to know why. Take a screenshot zoomed in on the edge so the blending shows. Photo
-
3
Draw lines with Bresenham's algorithm in all eight directions. The common trap is a line that works when it goes right and down and falls apart when it's steep or goes left. Draw a star of sixteen lines from the center to the edge, which tests every octant at once. Take a screenshot of the star with no gaps in any line. Photo
-
4
Show the difference gamma makes to a grey ramp. A ramp from black to white looks wrong when you write the raw value to the file, because screens expect sRGB. Write two bars, one with the raw value and one passed through the sRGB curve (close to the value raised to the power of 1 over 2.2), and look at where the middle grey lands. Take a screenshot of both bars and write one line about which one looks evenly spaced. Photo
-
5
Render sixty frames and stitch them into a short clip. Move the circle across the gradient, write one file per frame, and join them with ffmpeg, which is free. This is the loop you'll be writing for the rest of the track. Post the clip, and two seconds is plenty. Video