decheckerboard.frink

Download or view decheckerboard.frink in plain text format


// Program to reverse-engineer an illusion by Akiyoshi Kitaoka:
//   https://twitter.com/AkiyoshiKitaoka/status/844356094369513473

// Save the full-sized image from that post into this directory, named
// checkerboard.jpg
i = new image["file:checkerboard.jpg"]

[w,h] = i.getSize[]
i2 = new image[w,h]

for x = 0 to w-1
   for y = 0 to h-1
   {
      [r,g,b] = i.getPixelInt[x,y]

      // Reproduce the grid and subtract it out
      phase = (x div 9 + y div 9) mod 2
      if phase == 0
      {
         r = r + 114
         g = g + 114
         b = b + 114
      } else
      {
         r = r - 116
         g = g - 116
         b = b - 116
      }

      // Stretch contrast
      r = (r - 128) * 3 + 128
      g = (g - 128) * 3 + 128
      b = (b - 128) * 3 + 128
      
      if (r < 0 or g < 0 or b < 0 or r > 255 or g > 255 or b > 255)
         println["Error at $x $y: $r $g $b (phase is $phase), pixel was " + i.getPixelInt[x,y]]
      i2.setPixelInt[x, y, r,g,b]
   }

i2.show[]
i2.write["decheckerboard.jpg"]
   


Download or view decheckerboard.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20145 days, 18 hours, 6 minutes ago.