home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch5_5 / show.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-04  |  3.1 KB  |  112 lines

  1. #include <malloc.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <starbase.c.h>
  7.  
  8. typedef unsigned char icolor[3];
  9.  
  10. char command[100]="/usr/bin/X11/xwcreate -depth 24 -geometry ";
  11. char screen[100]="/dev/screen/";
  12. int watchmode=0;
  13.  
  14. main(ac, av)
  15. int ac;
  16. char *av[];
  17. {
  18.     FILE *f;
  19.     int width, height, w, h;
  20.     int fildes;
  21.     icolor *row;
  22.     unsigned char *r, *g, *b;
  23.     register int i, j;
  24.     register long t;
  25.         
  26.     while(av[1]&&*av[1]=='-') {
  27.         switch(av[1][1]) {
  28.           case 'w':
  29.             watchmode=1;
  30.             break;
  31.         }
  32.         ac--;
  33.         av++;
  34.     }
  35.     if(ac!=2) {
  36.         printf("usage: show file\n");
  37.         exit(1);
  38.     }
  39.     if(!(f=fopen(av[1],"rb"))) {
  40.         printf("failed to open file %s\n", av[1]);
  41.         exit(1);
  42.     }
  43.     fread(&width, sizeof(int), 1, f);
  44.     fread(&height, sizeof(int), 1, f);
  45.     row=(icolor*)malloc(width*sizeof(icolor));
  46.     r=(unsigned char*)malloc(width*sizeof(unsigned char));
  47.     g=(unsigned char*)malloc(width*sizeof(unsigned char));
  48.     b=(unsigned char*)malloc(width*sizeof(unsigned char));
  49.     if(!row||!r||!g||!b) {
  50.         printf("not enough memory\n");
  51.         exit(1);
  52.     }
  53.     if(!system(NULL)) {
  54.         printf("command processor not available\n");
  55.         exit(1);
  56.     }
  57.     sprintf(&command[strlen(command)], "%dx%d+10+10 %s", width, height, av[1]);
  58.     system(command);
  59.     if(errno) {
  60.         printf("failed to create window\n");
  61.         exit(1);
  62.     }
  63.     strcat(screen, av[1]);
  64.     if((fildes=gopen(screen,OUTDEV,0,INIT))<0) {
  65.         printf("failed to open screen device\n");
  66.         exit(1);
  67.     }
  68.     shade_mode(fildes, CMAP_FULL|INIT, 0);
  69.     for(;;) {
  70.         for(i=0; i<height; i++) {
  71.             if(fread(row, sizeof(icolor), width, f)!=width)
  72.                 break;
  73.             for(j=0; j<width; j++) {
  74.                 r[j]=row[j][0];
  75.                 g[j]=row[j][1];
  76.                 b[j]=row[j][2];
  77.             }
  78.             bank_switch(fildes, 2, 0);
  79.             dcblock_write(fildes, 0, i, width, 1, r, 1);          
  80.             bank_switch(fildes, 1, 0);
  81.             dcblock_write(fildes, 0, i, width, 1, g, 1);          
  82.             bank_switch(fildes, 0, 0);
  83.             dcblock_write(fildes, 0, i, width, 1, b, 1);          
  84.         }
  85.         fclose(f);
  86.         if(!watchmode)
  87.             break;
  88.         for(j=0; j<width; j++)
  89.             r[j]=0;
  90.         for(; i<height; i++) {
  91.             bank_switch(fildes, 2, 0);
  92.             dcblock_write(fildes, 0, i, width, 1, r, 1);          
  93.             bank_switch(fildes, 1, 0);
  94.             dcblock_write(fildes, 0, i, width, 1, r, 1);
  95.             bank_switch(fildes, 0, 0);
  96.             dcblock_write(fildes, 0, i, width, 1, r, 1);
  97.         }
  98.         for(t=clock(); (clock()-t)/CLOCKS_PER_SEC<2L;);
  99.         if(!(f=fopen(av[1],"rb"))) {
  100.             printf("failed to open file %s\n", av[1]);
  101.             exit(1);
  102.         }
  103.         fread(&w, sizeof(int), 1, f);
  104.         fread(&h, sizeof(int), 1, f);
  105.         if(!(w==width&&h==height)) {
  106.             printf("picture size changed\n");
  107.             exit(1);
  108.         }
  109.     }
  110.     exit(0);
  111. }   
  112.