/* rawdump.c
 *
 * This file is part of vdr-skinneutrino
 * Copyright (C) 2007 Sascha Wessel <sawe@users.sf.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


/*
 * src:
 * dbox2/apps/tuxbox/neutrino/src/driver/framebuffer.[h|cpp]
 *
 * raw icons:
 * dbox2/apps/tuxbox/neutrino/data/icons/
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

struct rawHeader {
	uint8_t width_lo;
	uint8_t width_hi;
	uint8_t height_lo;
	uint8_t height_hi;
	uint8_t transp;
};

int main(int argc, char **argv) {
	struct rawHeader header;
	uint16_t width, height;
	int fd;

	fd = open(argv[1], O_RDONLY);

	if (fd == -1) {
		printf("error while loading icon: %s\n", argv[1]);
		return 1;
	}

	read(fd, &header, sizeof(struct rawHeader));
	width  = (header.width_hi  << 8) | header.width_lo;
	height = (header.height_hi << 8) | header.height_lo;

	unsigned char pixbuf[768];
	int count, count2;
	for (count=0; count<height; count ++ ) {
		read(fd, &pixbuf, width >> 1 );
		unsigned char *pixpos = (unsigned char*) &pixbuf;
		for (count2=0; count2<width >> 1; count2 ++ ) {
			unsigned char compressed = *pixpos;
			unsigned char pix1 = (compressed & 0xf0) >> 4;
			unsigned char pix2 = (compressed & 0x0f);

			if (pix1 != header.transp)
				printf("%c", '0' + pix1);
			else
				printf(" ");
			if (pix2 != header.transp)
				printf("%c", '0' + pix2);
			else
				printf(" ");
			pixpos++;
		}
		printf("\n");
	}
	close(fd);
	return 0;
}

