LogoWCF

Chillenge 22: Santa Says Cheese

The elves bought a fancy new camera, the Snowflake 3000, to take Santa’s official Christmas card photo. There’s just one problem: the software that turns the camera’s readings into a real picture is broken. All the elves have is the raw sensor data, a big grid of numbers, and no idea what the photo looks like.

A camera sensor is a grid of tiny light buckets. Each bucket sits under a red, green, or blue filter, so it only measures how much of one color reached it. The colors come in a repeating 2×2 block (called a Bayer pattern), which tiles across the whole sensor:

R
G
G
B

One 2×2 block

R
G
R
G
G
B
G
B
R
G
R
G
G
B
G
B

Tiles across the sensor (first block outlined)

Your job is to “develop” the photo by turning each 2×2 block of buckets into one full-color pixel. You don’t need to know anything about photography to do this, just follow the steps.

Problem: Read snapshot.txt and develop it into an image. The file looks like this:

320 192          <- width and height of the sensor
256 4095         <- black level and white level
210 165          <- red gain and blue gain (as percentages)
1043 872 ...     <- 192 rows of 320 numbers each
...

Walk through the sensor in 2×2 blocks (starting at the top left, so you will have a 160×96 image when you finish). In each block the top-left value is red, the top-right and bottom-left are green, and the bottom-right is blue. For each block:

  1. Subtract the black level from all four values. (The sensor never reads exactly zero. If a result goes negative, use 0.) Let M be the white level minus the black level.
  2. Pick the colors: red is the top-left value, blue is the bottom-right value, and green is the average of the other two (round down).
  3. White balance: multiply red by the red gain divided by 100, and blue by the blue gain divided by 100 (round down, and never go above M). Green stays the same. The camera’s sensor is better at seeing green, so without this the whole photo looks green.
  4. Brighten: sensors measure light in a straight line, but our eyes don’t. Turn each color value into a number from 0 to 255 using floor(255 × √(value / M)).

That gives you a red, green, and blue number from 0 to 255 for each pixel.

Example: Using the black level and gains above, this block:

1200 2100
2060 1500

becomes 944 1844 / 1804 1244 after step 1, then red = 944, green = (1844 + 1804) / 2 = 1824, blue = 1244 after step 2. White balance makes that 1982, 1824, and 2052. After brightening, the pixel is 183 175 186.

Check your answers: Once you have the picture, tell the elves what the sign in the photo says. You can also check a few pixels. Pixels are numbered from the top left starting at 0,0, using x,y. Report the red, green, and blue values for:

PixelWhere it is
118,16The moon
28,52The Christmas tree
140,48Snowman’s scarf
57,80The sign
76,69The sign’s writing

(Don’t worry if some of your values are off by one because of rounding, that’s fine.)

Hint: How do I see my image? The simplest way to save an image is the PPM text format. Write P3 on the first line, then the width and height on the second line, then 255 on the third line. After that write the red, green, and blue numbers of each pixel, one pixel after another, in order from the top left. Many image programs (GIMP, ImageMagick, or an online PPM viewer) can open it. A 160×96 image is tiny, so zoom in. Or, use any image library in your language to save a PNG.

Challenge: Your picture is only half the size of the sensor because every 2×2 block was squashed into one pixel. Real cameras don’t throw that detail away. Try making a full-size 320×192 image instead, where every sensor spot gets its own pixel. Each spot only knows one color, so estimate the two missing colors by averaging the nearest neighbors that did measure them (this is called demosaicing). Compare it with your first picture. Are the edges of the snowman sharper? There’s no answer box for this one, so show off your results in your solution!

Open in the sandbox
Uh oh! That's not right

Check Your Answers

Solutions