Encode an image inside of the source The golf challenge is to encode and compress the following image inside a source file.



To do this you need to write 3 functions: red, green, and blue which accept x/y coordinates of the image and return the corresponding R/G/B pixel value between 0-255.

Here is the C/C++ test code:

#include &lt;stdio.h&gt;
#include "your_file"
int main() {
  int x, y;
  for(y = 0; y &lt; 32; ++y)
  for(x = 0; x &lt; 32; ++x)
    printf("%i %i %i\n", red(x, y), blue(x, y), green(x, y));
}


And the output: http://pastebin.com/A770ckxL (you can use this to generate your image data)

Rules &amp; details:


This is a golf
Only your code/file is golfed - the test code is separate
The character set used is ASCII, however control characters in strings may only be used if escaped (like '\n' and '\r', etc)
Everything must be contained inside the source - no file loading
Your output must match the example output. This means loseless compression.


Languages:

The problem was written with C/C++ in mind, but I'm removing those restrictions. With that said, I'll still recommend using them.
C, 796 754 712 703 692 685 682 670 666 662 656 648 chars

Changelog:


754->712: Added return to the #define, replacing if with ? statements (thanks @FUZxxl), removing int from the functions parameter list.
712->703: Shameless copy from bunnit again :) Moved the whole image creation into the #define
703->692: Merged p[] and h[], some more ?: improvements
692->685: Incrementing b so i isn't needed anymore. m=b instead of m=11 and n&lt;2e3 instead of i&lt;356 - these are close to undefined behaviour/memory corruption, but it seems I'm lucky :)
685->682: k is now (32,16,8,4,2,1,0) instead of (5,4,3,2,1,0). Gotcha, D C ;)
682->670: Split p[] and h[], converted h[] to a char* - awwww, there's a sleepy kitty in it ^&lt;+_=&gt;-
670->666: while => for, l=l*2+... => l+=l+...
666->662: m=m&gt;9?... => c[n++]=m&gt;9?...
662->656: Reversed bit order in b[], so we can map to 64-127 instead of 0-63 and don't need bit index k anymore. Thanks @Piotr Tarsa. Replaced ?: (GCC extension) with ||. Thanks @JamesB
656->648: Shameless copy from Shelwien :) (multi-character constants for p[])


Image is converted to a Base64-like string (ASCII 37-100), using huffman coding to encode colors 0-9 using 3-6 bits and a special color 10 (pixel is same as previous one) using only 1 bit.

#define P (x,y){for(;n&lt;2e3;j/=2){j&gt;1||(j=*b+++27);l+=l+(j&amp;1);for(m=0;m&lt;11;m++)l-h[m]+32||(c[n++]=m&gt;9?c[n-1]:m,l=0,m=b);}return 255&amp;p[c[x+y*32]]
char*h="$^&lt;+_=&gt;-,* ",*b="F0(%A=A=%SE&amp;?AEVF1E01IN8X&amp;WA=%S+E+A-(,+IZZM&amp;=%]U5;SK;cM84%WE*cAZ7dJT3R.H1I2@;a^/2DIK&amp;=&amp;&gt;^X/2U*0%'0E+;VC&lt;-0c&gt;&amp;YU'%],;]70R=.[1U4EZ:Y=6[0WU4%SQARE0=-XDcXd_WW*UAF&amp;cFZJJ0EV*(a(P05S3IXA&gt;51cH:S5SAE6+W%/[]7SF(153UM]4U()(53DA+J:]&amp;5+5KX,L6&gt;*4I,/UMBcML9WKLa9%UYIHKWW(9-*):(-ZW(9%T'N&amp;9;C,C/Ea/Y7(JJ\\6CD9E,2%J*,ac]NIW8(M=VFac)/^)?IS-;W&amp;45^%*N7&gt;V,,C-4N35FMQaF,EaWX&amp;*EJ4'";p[]={0,'R@+','aXL',7783255,'4k`',16354410,'NNv',5295994,4671418,9975021},c[1024],j,l,m,n;red P;}blue P&gt;&gt;16;}green P&gt;&gt;8;}


Copied two things from bunnit's answer, the #define and the whole image being decoded completely every time red/green/blue are called. There's some room for additional improvement, so expect some updates :) I'm not sure about code compliance, used GCC 4.6.1 to compile and test.

Compression-wise, I think arithmetic coding would help as the distribution is pretty skewed, but maybe code overhead would be too heavy in this case. LZW also should do a very good job. Colors are quite local, so adaptive coding might be an idea.

More readable 754 character version with some comments:

#define P 255&amp;p[c[x+y*32]]
// Base64 coded image bitstream, ASCII 37-100
// Huffman codes: 100, 111110, 11100, 1011, 111111, 11101, 11110, 1101, 1100, 1010, 0
char b[]="FYU%3+3+%B&amp;E;3&amp;HF1&amp;Y1.JWXE83+%B=&amp;=3)U]=.PP*E+%,('?B&gt;?D*Wa%8&amp;MD3P7dNbARIV1.Q[?4L9Qc.&gt;E+EKLX9Q(MY%5Y&amp;=?HC_)YDKE0(5%,]?,7YR+I@1(a&amp;PO0+G@Y8(a%B23R&amp;Y+)XcDXd&lt;88M(3FEDFPNNY&amp;HMU4UZY'BA.X3K'1DVOB'B3&amp;G=8%9@,7BFU1'A(*,a(U-U'Ac3=NO,E'='&gt;X]^GKMa.]9(*SD*^/8&gt;^4/%(0.V&gt;88U/)M-OU)P8U/%b5JE/?C]C9&amp;4907UNN`GCc/&amp;]Q%NM]4D,J.8WU*+HF4D-9L-;.B)?8Ea'L%MJ7KH]]C)aJA'F*24F]&amp;48XEM&amp;Na5";
// Colors, order GBR (one char shorter than all the others)
p[]={0,5390379,6379596,7783255,3435360,16354410,5131894,5295994,4671418,9975021};
// Huffman codes for colors 0-10
h[]={4,62,28,11,63,29,30,13,12,10,0};
// Array for image data
c[1024];
i,j,k,l,m,n;
red(int x,int y){
  while(i&lt;356){
    k--;
    if (k&lt;0) {
      j=b[i++]-37;
      k=5;
    }
    l*=2;
    if (j&amp;(1&lt;&lt;k)) l++;
    for(m=0;m&lt;11;m++){
      if(l==h[m]){
        if (m&gt;9) m=c[n-1];
        c[n++]=m;
        l=0;
        m=12;
      }
    }
  }
  return P;
}
blue(int x,int y){return P&gt;&gt;16;}
green(int x,int y){return P&gt;&gt;8;}