Explain Extended New Year 2019 post: a GIF decoder in SQL
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
01.bin.sql
02.header.sql
03.image.sql
04.image-header.sql
05.lzw-data.sql
06.lzw-stream.sql
07.indexes.sql
08.pixels.sql
LICENSE
README.md
cat.gif
gif.sql
pig.gif
pre.sql
sample.gif
winter-scenery.gif

README.md

explain-extended-2019

Explain Extended New Year 2019 post: a GIF decoder in PostgreSQL.

https://explainextended.com/2018/12/31/happy-new-year-10/

How to run it:

  1. Download a GIF file

  2. In bash shell, run:

    cat sample.gif | od -A n -vt x1 | tr -d '\n ' | psql -f pre.sql -c "COPY input FROM stdin" -f gif.sql
    

    This assumes you have psql credentials set up for your database, refer to psql manual if you don't.

    If you don't have access to psql, hex-encode you gif file and insert it in the temporary table manually:

    CREATE TEMPORARY TABLE
            input
            (
            data TEXT
            );
        
    INSERT
    INTO    input
    VALUES  ('<hex data here>');
    

    then run gif.sql in the same database session;

    This script does not create any new objects in the database and does require any write permissions.

    Requires PostgreSQL 9.3 or higher.

  3. Enjoy the rendered GIF:

 string_agg
------------
 .....
 .....
 .....
 ...####
 ...####
    ####...
    ####...
      .....
      .....
      .....

This is an excercise in SQL and does not render each and every GIF file. It does not work with animated GIF and those with transparency. If you open your file in mspaint.exe and save it as a GIF, it will probably work.

Large files might take a while to decode and render, be patient.

Happy New Year!