#include <stdio.h>

/* Hacked from original raw2ppm to support Psilinux
 * 4bpp framebuffer by tony@atomide.com
 * 
 * See http://www.handhelds.org/z/wiki/IpaqScreenshots
 * for instructions.
 *
 * See http://dragons.espsw.net/ToyKeeper/raw2ppm.c
 * for the original source.
 * 
 * Instructions for compiling:
 * - On most machines, this should work:
 *     cc -o raw2ppm raw2ppm.c
 *
 * Instructions for capturing:
 *    cat /dev/fb0 > /tmp/screen.raw
 * Then convert to ppm:
 *    ./raw2ppm screen.raw > screen.ppm
 */

int main(int argc, char *argv[])
{
        int len;
        unsigned short buf[256];
        FILE *f;
        int w, h;
  
        w = 640;
        h = 240;
   
        printf("P6\n%d %d\n16\n", w, h);      
        f = fopen(argv[1], "rb");
        while ((len = fread(buf, 2, 256, f)) != 0) {
                int i;
                
                buf[i] = ((buf[i] & 0xff00) >> 8) + ((buf[i] & 0x00ff) << 8);
                for (i = 0; i < len; i++) {
                        unsigned char r, g, b, j, val;
                        
                        for (j = 0; j < 4; j++) {
                                val = (buf[i] >> (j*4)) & 0xf;
                                r = val;
                                g = val;
                                b = val;                               
                                printf("%c%c%c", r, g, b);
                        }
                }
        }
}
