home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / spiele / argon / argon.exe / src / graphics / grey.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-21  |  2.9 KB  |  132 lines

  1. /*
  2.   grey.c  image converter tool for 'Argon V'
  3.  
  4.   T.Harbaum@tu-bs.de - http://www.ibr.cs.tu-bs.de/~harbaum/pilot
  5.  
  6.   This program is distributed in the hope that it will be useful,
  7.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.   GNU General Public License for more details.
  10.  
  11. */
  12.  
  13. #include <stdio.h>
  14.  
  15. #define MAGIC 0x4d42
  16.  
  17. #pragma pack(1)
  18.  
  19. struct bmp16_hdr {
  20.   unsigned short magic;
  21.   unsigned short dummy0[4];
  22.   unsigned long  offset;
  23.   unsigned short dummy4[2];
  24.   unsigned long  width, height;
  25.   unsigned short dummy1;
  26.   unsigned short bpp;
  27.   unsigned long  dummy2;
  28.   unsigned long  length;
  29.   unsigned long  dummy3[2];
  30.   unsigned long  colors;
  31.   unsigned long  colors2;
  32. };
  33.  
  34. struct color {
  35.   unsigned char r,g,b,x;
  36. };
  37.  
  38. #pragma pack(4)
  39.  
  40. int
  41. main(int argc, char *argv[]) {
  42.   FILE   *in, *out;
  43.   struct bmp16_hdr bmphdr;
  44.   struct color colors[256], col;
  45.   int i, j, x, y, shade[256], width, height;
  46.   unsigned char *pixels;
  47.  
  48.   if(argc!=3) {
  49.     puts("usage: grey infile outfile");
  50.     exit(1);
  51.   }
  52.  
  53.   if((in = fopen(argv[1], "rb"))==0) {
  54.     puts("unable to open input file");
  55.     exit(1);
  56.   }
  57.  
  58.   if((out = fopen(argv[2], "wb"))==0) {
  59.     puts("unable to open output file");
  60.     exit(1);
  61.   }
  62.  
  63.   fread(&bmphdr, sizeof(struct bmp16_hdr), 1l, in);
  64.   width  = bmphdr.width;
  65.   height = bmphdr.height;
  66.  
  67.   /* this doesn't work on big endian, but the rest doesn't either */
  68.   if(bmphdr.magic != MAGIC) {
  69.     puts("illegal bitmap header magic");
  70.     exit(1);
  71.   }
  72.  
  73.   /* verify pixel depth */
  74.   if(bmphdr.bpp!=8) {
  75.     printf("illegal bitmap depth %d (8 bits required)\n", bmphdr.bpp);
  76.     exit(1);
  77.   }
  78.  
  79.   /* verify width */
  80.   if((width&7) != 0) {
  81.     printf("illegal bitmap width %d (multiple of 8 bits required)\n", width);
  82.     exit(1);
  83.   }
  84.  
  85.   printf("source: %d colors, mapping to 4 bit static map\n", bmphdr.colors);
  86.  
  87.   /* read colors */
  88.   fread(colors, bmphdr.colors, sizeof(struct color), in);
  89.  
  90.   for(i=0;i<bmphdr.colors;i++) {
  91.     /* calculate color assignment */
  92.     shade[i] = ((colors[i].r + colors[i].g + colors[i].b)*15)/(3*0xf0);
  93.   }
  94.  
  95.   /* write new header */
  96.   bmphdr.colors = bmphdr.colors2 = 16;
  97.   bmphdr.length += bmphdr.height*4;
  98.   bmphdr.offset = sizeof(struct bmp16_hdr)+bmphdr.colors*sizeof(struct color);
  99.   bmphdr.width += 2;
  100.   fwrite(&bmphdr, sizeof(struct bmp16_hdr), 1l, out);
  101.  
  102.   /* write static colors */
  103.   for(i=0;i<bmphdr.colors;i++) {
  104.     if(i<16) col.r = col.g = col.b = i * 0x11;
  105.     else     col.r = col.g = col.b = 0;
  106.  
  107.     col.x = 0;
  108.     fwrite(&col, sizeof(struct color), 1l, out);
  109.   }
  110.  
  111.   /* scan picture */
  112.   for(y=0;y<height;y++) {
  113.     for(x=0;x<width;x++)
  114.       fputc(shade[fgetc(in)], out);
  115.  
  116.     if(height-y<32) { 
  117.       fputc( (15-((height-y)/2)),out);
  118.       fputc( (15-((height-y)/2)),out);
  119.     } else {           
  120.       fputc(0,out);
  121.       fputc(0,out);
  122.     }
  123.  
  124.     fputc(0,out);
  125.     fputc(0,out);
  126.   }
  127.   
  128.   fclose(in);
  129.   fclose(out);
  130.   exit(0);
  131. }
  132.