Playing with JavaScript, photos and 3D LUTS (lookup tables)

Today I am showing you an experiment I made by messing around with JavaScript, photos and 3D lookup tables. First: what is a 3D lookup table? From Wikipedia: In the film industry, 3D lookup tables (3D LUTs) are used to map one color space to another. They are commonly used to calculate preview colors for a monitor or digital projector of how an image will be reproduced on another display device, typically the final digitally projected image or release print of a movie. Long story short, a 3D lookup table can be used to process the colors of your photos – or movies – just like Instagram does. Source image is processed pixel by pixel and the color of each pixel is changed according to the lookup table (LUT from now on), and that’s it. A LUT can be a series of numbers of a PNG image, usually made by 512×512 divided in blocks of 64×64 pixels, like this one: And can make you modify your photos this way, on the fly, only with JavaScript: I found this simple script on GitHub and modified a bit to create the result you can see at this page. The source code is also very easy to understand:
<html&gt;
    <head&gt;
    </head&gt;
    <body&gt;
        <div style = "text-align:center"&gt;
            <p&gt;This image...</p&gt;
            <canvas id = "imageCanvas" width="640" height="480" style = "border:2px solid black"&gt;</canvas&gt;
        </div&gt;
        <div style = "text-align:center"&gt;
            <p&gt;...with this lookup table...</p&gt;
            <canvas id="lutCanvas" width="512" height="512" style = "border:2px solid black"&gt;</canvas&gt;
        </div&gt;
        <div style = "text-align:center"&gt;
            <p&gt;... gives this result!</p&gt;
            <canvas id="resultCanvas" width="640" height="480" style = "border:2px solid black"&gt;</canvas&gt;
        </div&gt;
        <script&gt;
            var imagesLoaded = 0;

            function URLToCanvas(url, id){
                var theCanvas = document.getElementById(id);
                var context = theCanvas.getContext("2d");
                var img = new Image;
                img.src = url;
                img.onload = function(){
                    context.drawImage(img, 0, 0);
                    imagesLoaded ++;
                    if(imagesLoaded == 2){
                        applyLUT("imageCanvas", "lutCanvas", "resultCanvas");
                    }
                }
            }

            URLToCanvas("car.png", "imageCanvas");
            URLToCanvas("lut.png", "lutCanvas");

            function applyLUT(imageID, lutID, resultID) {
                var imageContext = document.getElementById(imageID).getContext("2d");
                var lutContext = document.getElementById(lutID).getContext("2d");
                var imageData = imageContext.getImageData(0, 0, 640, 480);
                var lutData = lutContext.getImageData(0, 0, 512, 512);
                for (var i = 0; i < imageData.data.length; i += 4) {
                    var r = Math.floor(imageData.data[i] / 4);
                    var g = Math.floor(imageData.data[i + 1] / 4);
                    var b = Math.floor(imageData.data[i + 2] / 4);
                    var lutX = (b % 8) * 64 + r;
                    var lutY = Math.floor(b / 8) * 64 + g;
                    var lutIndex = (lutY * 512 + lutX) * 4;
                    imageData.data[i] = lutData.data[lutIndex];
                    imageData.data[i + 1] = lutData.data[lutIndex + 1];;
                    imageData.data[i + 2] = lutData.data[lutIndex + 2];;
                }
                document.getElementById(resultID).getContext("2d").putImageData(imageData, 0, 0);
            }
        </script&gt;
    </body&gt;
</html&gt;
And as you can imagine, the most difficult part is not applying a LUT, it’s creating a LUT which turns your photos into something interesting. This was only an experiment, but it would be interesting to do something a bit more complex with a HTML5 engine, still playing with it, you can download the source code of the example and play on your own.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers