home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / moveicon / trimicon.c < prev   
C/C++ Source or Header  |  1986-11-30  |  2KB  |  87 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3.  
  4. #define WIDTH        64
  5. #define HEIGHT        64
  6. #define WORDSIZ        16
  7. #define NCOL        (WIDTH / WORDSIZ)
  8. #define NROW        (HEIGHT)
  9.  
  10. main(argc, argv)
  11. int    argc;
  12. char    **argv;
  13. {
  14.     register int    i, j, k;
  15.     register char    c;
  16.     int        width, height;
  17.     u_int        data[NROW][NCOL];
  18.     FILE        *fp;
  19.  
  20.     /* check to make sure they specified the correct # of args */
  21.     if (argc != 3 && argc != 4) {
  22.         fprintf(stderr, "usage: trimicon width height { icon }\n");
  23.         exit(1);
  24.     }
  25.     /* get the size */
  26.     width = abs(atoi(argv[1]));
  27.     height = abs(atoi(argv[2]));
  28.     /* did they specify a filename? */
  29.     if (argc == 4 && argv[3] != (char *) NULL) {
  30.         /* yes -- open it */
  31.         if ((fp = fopen(argv[3], "r")) == NULL) {
  32.             fprintf(stderr, "Can't open file %s for reading\n", 
  33.                 argv[3]);
  34.             exit(1);
  35.         }
  36.     } else {
  37.         /* no -- set <fp> to be stdin */
  38.         fp = stdin;
  39.     }
  40.         
  41.     /* skip the comments */
  42.     while ((c = getc(fp)) != '\t');
  43.  
  44.     /* read in the icon */
  45.     for (i = 0; i < NROW; i++) {
  46.         for (j = 0; j < NCOL; j++) {
  47.             if (fscanf(fp, " 0x%x,", &data[i][j]) != 1) {
  48.                 fprintf("Error reading file %s\n", argv[3]);
  49.                 exit(1);
  50.             }
  51.         } /* end for */
  52.     } /* end for */
  53.  
  54.     /* close the file */
  55.     fclose(fp);
  56.  
  57.     for (i = 0; i < NROW; i++) {
  58.         if (i > height) {
  59.             for (j = 0; j < NCOL; j++) {
  60.                 data[i][j] = (u_int) 0;
  61.             }
  62.         } else {
  63.             for (j = 0; j < NCOL; j++) {
  64.                 if (j * WORDSIZ > width) {
  65.                     data[i][j] = (u_int) 0;
  66.                 } else if (j == width / WORDSIZ) {
  67.                     k = width - j * WORDSIZ;
  68.                     data[i][j] = (data[i][j] >> k) << k;
  69.                 } /* end else */
  70.             } /* end for */
  71.         } /* end else */
  72.     } /* end for */
  73.  
  74.     /* output the icon file */
  75.     printf("/* Format_version=1, Width=%d, Height=%d, ", width, height);
  76.     printf("Depth=1, Valid_bits_per_item=%d */", WORDSIZ);
  77.     for (i = 0; i < height; i++) {
  78.         /* print two rows on a line */
  79.         if (i % 2 == 0) printf("\n\t");
  80.         /* print the row */
  81.         for (j = 0; j * WORDSIZ < width; j++) {
  82.             printf("0x%04x,", data[i][j] & 0xffff);
  83.         } /* end for */
  84.     } /* end for */
  85.     printf("\n");
  86. } /* end main() */
  87.