home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / moveicon / moveicon.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  4KB  |  154 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        off_x, off_y;
  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: moveicon offset_x offset_y { icon }\n");
  23.         exit(1);
  24.     }
  25.     /* get the offset */
  26.     off_x = atoi(argv[1]);
  27.     off_y = 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.     /* move the icon vertically */
  58.     if (off_y < 0) {
  59.         for (i = 0; i < NROW; i++) {
  60.             if (i - off_y < NROW) {
  61.                 for (j = 0; j < NCOL; j++) {
  62.                     data[i][j] = data[i - off_y][j];
  63.                 } /* end for */
  64.             } else {
  65.                 for (j = 0; j < NCOL; j++) {
  66.                     data[i][j] = (u_int) 0;
  67.                 } /* end for */
  68.             } /* end else */
  69.         } /* end for */
  70.     } else {
  71.         for (i = NROW - 1; i >= 0; i--) {
  72.             if (i - off_y >= 0) {
  73.                 for (j = 0; j < NCOL; j++) {
  74.                     data[i][j] = data[i - off_y][j];
  75.                 } /* end for */
  76.             } else {
  77.                 for (j = 0; j < NCOL; j++) {
  78.                     data[i][j] = (u_int) 0;
  79.                 } /* end for */
  80.             } /* end else */
  81.         } /* end for */
  82.     } /* end else */
  83.  
  84.     /* move the icon horizontally */
  85.     for (i = 0; i < NROW; i++) {
  86.         u_int    ofbits = 0;        /* overflow bits */
  87.         u_int    prev = 0;        /* previous overflow bits */
  88.  
  89.         if (off_x > 0) { 
  90.             /* do we need to shift words to the right? */
  91.             if (off_x >= WORDSIZ) {
  92.                 /* yes -- shift low to high */
  93.                 j = NCOL - 1;
  94.                 k = j - off_x / WORDSIZ;
  95.                 /* shift words to the right */
  96.                 while (j >= 0) {
  97.                     data[i][j] = data[i][k];
  98.                     data[i][k] = (u_int) 0;
  99.                     j--, k--;
  100.                 } /* end while */
  101.                 /* subtract the word shift from the offset */
  102.                 off_x = off_x % WORDSIZ;
  103.             } /* end if */
  104.             /* shift the bits in the words */
  105.             for (j = 0; j < NCOL; j++) {
  106.                 /* store the overflow bits */
  107.                 ofbits = data[i][j] << (WORDSIZ - off_x);
  108.                 /* set the new value */
  109.                 data[i][j] = prev | (data[i][j] >> off_x);
  110.                 /* make the current overflow bits previous */
  111.                 prev = ofbits;
  112.             } /* end for */
  113.         } else {
  114.             int    tmp_x = abs(off_x);
  115.  
  116.             /* do we need to shift words to the left? */
  117.             if (tmp_x >= 16) {
  118.                 /* yes -- shift high to low */
  119.                 j = tmp_x / WORDSIZ;
  120.                 k = 0;
  121.                 while (j < NCOL) {
  122.                     data[i][k] = data[i][j];
  123.                     data[i][j] = (u_int) 0;
  124.                     j++, k++;
  125.                 } /* end while */
  126.                 /* subtract the word shift from the offset */
  127.                 tmp_x = tmp_x % WORDSIZ;
  128.             } /* end if */
  129.             /* shift the bits in the words */
  130.             for (j = NCOL - 1; j >= 0; j--) {
  131.                 /* store the overflow bits */
  132.                 ofbits = data[i][j] >> (WORDSIZ - tmp_x);
  133.                 /* set the new value */
  134.                 data[i][j] = prev | (data[i][j] << tmp_x);
  135.                 /* make the current overflow bits previous */
  136.                 prev = ofbits;
  137.             } /* end for */
  138.         } /* end else */
  139.     } /* end for */
  140.  
  141.     /* output the icon file */
  142.     printf("/* Format_version=1, Width=%d, Height=%d, ", WIDTH, HEIGHT);
  143.     printf("Depth=1, Valid_bits_per_item=%d */", WORDSIZ);
  144.     for (i = 0; i < NROW; i++) {
  145.         /* print two rows on a line */
  146.         if (i % 2 == 0) printf("\n\t");
  147.         /* print the row */
  148.         for (j = 0; j < NCOL; j++) {
  149.             printf("0x%04x,", data[i][j] & 0xffff);
  150.         } /* end for */
  151.     } /* end for */
  152.     printf("\n");
  153. } /* end main() */
  154.