00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005
00006 #define COLOUR_PAL_FILE "colour.pal"
00007
00008 typedef struct rgb_struct {
00009 int red;
00010 int grn;
00011 int blu;
00012 } RGB;
00013
00014
00015 int
00016 main (int argc, char *argv[])
00017 {
00018 char s[100];
00019 unsigned char c;
00020 int n;
00021 int r, g, b;
00022 int size;
00023 RGB pal[256];
00024 char *infile, *outfile;
00025 FILE *fp, *fpi;
00026 struct stat statbuf;
00027
00028 if (argc != 3) {
00029 fprintf (stderr, "Usage: csi2ppm infile outfile\n");
00030 exit (-1);
00031 }
00032 infile = argv[1];
00033 outfile = argv[2];
00034
00035
00036 if ((fp = fopen (COLOUR_PAL_FILE, "r")) == 0) {
00037 fprintf (stderr, "Can't find the colour pallet file");
00038 exit (-1);
00039 }
00040 n = 0;
00041 while (feof (fp) == 0 && n < 256) {
00042 fgets (s, 99, fp);
00043 if (sscanf (s, "%d %d %d %d", &n, &r, &g, &b) == 4) {
00044 pal[n].red = r;
00045 pal[n].grn = g;
00046 pal[n].blu = b;
00047 n++;
00048 }
00049 }
00050 if (n != 256) {
00051 fprintf (stderr, "Not enough palette entries\n");
00052 }
00053 fclose (fp);
00054
00055
00056 if (stat (infile, &statbuf) != 0) {
00057 fprintf (stderr, "Error getting size of input file: %s\n", infile);
00058 exit (-1);
00059 }
00060 size = statbuf.st_size;
00061 switch (size) {
00062 case 256:
00063 size = 1*16;
00064 break;
00065 case 1024:
00066 size = 2*16;
00067 break;
00068 case 2304:
00069 size = 3*16;
00070 break;
00071 case 4096:
00072 size = 4*16;
00073 break;
00074 default:
00075 fprintf (stderr, "Size of input file is wrong: %s\n", infile);
00076 exit (-1);
00077 }
00078
00079
00080 fpi = fopen (infile, "r");
00081 if (!fpi) {
00082 fprintf (stderr, "Error opening input file: %s\n", infile);
00083 exit (-1);
00084 }
00085 fp = fopen (outfile, "w");
00086 if (!fp) {
00087 fprintf (stderr, "Error opening output file: %s\n", outfile);
00088 exit (-1);
00089 }
00090 fprintf (fp,"P3\n# Created by csi2ppm from lincity file %s\n", infile);
00091
00092 fprintf (fp,"%d %d\n62\n",size,size);
00093 while (fread(&c,1,1,fpi)) {
00094 fprintf (fp, "%d %d %d\n", pal[c].red, pal[c].grn, pal[c].blu);
00095 }
00096 fclose (fpi);
00097 fclose (fp);
00098 }