Annotation of researchv9/X11/src/X.V11R1/util/bm-convert/bm-convert.c, revision 1.1

1.1     ! root        1: /* 
        !             2:  * $Locker:  $ 
        !             3:  */ 
        !             4: static char    *rcsid = "$Header: bm-convert.c,v 1.1 87/06/24 10:54:58 toddb Exp $";
        !             5: #include <stdio.h>
        !             6: #include <strings.h>
        !             7: 
        !             8: #define boolean int
        !             9: 
        !            10: unsigned short *ReadX10BitmapFile (prefix, width, height, x_hot, y_hot)
        !            11:     char *prefix;         /* RETURN */
        !            12:     int *width, *height;  /* RETURN */
        !            13:     int *x_hot, *y_hot;  /* RETURN */
        !            14:     {
        !            15:     char variable[81];
        !            16:     int status, value, i, data_length;
        !            17:     unsigned short *data;
        !            18:     FILE *file = stdin;
        !            19: 
        !            20:     *width = *height = -1;
        !            21:     *x_hot = *y_hot = -1;
        !            22:     while ((status = fscanf (file, "#define %80s %d\n", variable, &value))==2)
        !            23:        {
        !            24:        if (StringEndsWith (variable, "width"))
        !            25:            *width = value;
        !            26:        else if (StringEndsWith (variable, "height"))
        !            27:            *height = value;
        !            28:        else if (StringEndsWith (variable, "x_hot"))
        !            29:            *x_hot = value;
        !            30:        else if (StringEndsWith (variable, "y_hot"))
        !            31:            *y_hot = value;
        !            32:        }
        !            33: 
        !            34:     if (*width <= 0) {
        !            35:         fprintf (stderr, "No valid width parameter found\n");
        !            36:        exit (-1);
        !            37:        }
        !            38:        
        !            39:     if (*height <= 0) {
        !            40:         fprintf (stderr, "No valid height parameter found\n");
        !            41:        exit (-2);
        !            42:        }
        !            43: 
        !            44:     data_length = ((*width+15)/16) * *height;
        !            45:     data = (unsigned short *) malloc (data_length*sizeof(unsigned short));
        !            46:     
        !            47:     status = fscanf (file, "static short %80s = { 0x%4hx", variable,
        !            48:        data);  /* fills in 0th element of data array */
        !            49:     if ((status != 2) || !StringEndsWith (variable, "bits[]")) {
        !            50:        fprintf (stderr, "First element of short bits array invalid or not found\n");
        !            51:        exit (-3);
        !            52:        }
        !            53:     
        !            54:     {
        !            55:     int prefix_len = strlen(variable) - strlen("_bits[]");
        !            56:     strncpy (prefix, variable, prefix_len);
        !            57:     prefix[prefix_len] = '\0';
        !            58:     }
        !            59: 
        !            60:     for (i=1;i<data_length;i++) {
        !            61:        /* fill in i'th element of data array */
        !            62:        status = fscanf (file, ", 0x%4hx", data + i);
        !            63:        if (status != 1) {
        !            64:            fprintf (stderr, "%dth short bits array element is invalid\n", i+1);
        !            65:            exit (-4);
        !            66:            }
        !            67:        }
        !            68:     return (data);
        !            69:     }
        !            70: 
        !            71: WriteX11BitmapFile (prefix, width, height, x_hot, y_hot, data)
        !            72:     char *prefix;
        !            73:     int width, height, x_hot, y_hot;
        !            74:     unsigned short *data;
        !            75:     {
        !            76:     int i, j;
        !            77:     int shorts_per_line = (width + 15) / 16;
        !            78:     int data_length = shorts_per_line * height;
        !            79:     FILE *file = stdout;
        !            80:     boolean line_pad = ((width % 16) > 0) && ((width % 16) < 9);
        !            81: 
        !            82:     fprintf (file, "#define %s_width %d\n", prefix, width);
        !            83:     fprintf (file, "#define %s_height %d\n", prefix, height);
        !            84:     if (x_hot >= 0)
        !            85:        fprintf (file, "#define %s_x_hot %d\n", prefix, x_hot);
        !            86:     if (y_hot >= 0)
        !            87:        fprintf (file, "#define %s_y_hot %d\n", prefix, y_hot);
        !            88:     fprintf (file, "static char %s_bits[] = {", prefix);
        !            89:     for (i = j = 0; i < data_length; i++, j++) {
        !            90:        unsigned short datum = data[i];
        !            91:        unsigned char b0 = datum & 0xff;
        !            92:        unsigned char b1 = (datum & 0xff00) >> 8;
        !            93:         if (j == 0) fprintf (file, "\n   ");
        !            94:        else if ((j % 12) == 0) fprintf (file, ",\n   ");
        !            95:        else fprintf (file, ", ");
        !            96:        fprintf (file, "0x%02x", b0);
        !            97:        if (!line_pad || ((i + 1) % shorts_per_line)) {
        !            98:            if ((++j % 12) == 0) fprintf (file, ",\n   ");
        !            99:            else fprintf (file, ", ");
        !           100:            fprintf (file, "0x%02x", b1);
        !           101:            }
        !           102:        }
        !           103:     fprintf (file, "};\n");
        !           104:     }
        !           105: 
        !           106: /* StringEndsWith returns TRUE if "s" ends with "suffix", else returns FALSE */
        !           107: boolean StringEndsWith (s, suffix)
        !           108:   char *s, *suffix;
        !           109:   {
        !           110:   int s_len = strlen (s);
        !           111:   int suffix_len = strlen (suffix);
        !           112:   return (strcmp (s + s_len - suffix_len, suffix) == 0);
        !           113:   }
        !           114: 
        !           115: main() {
        !           116:     char prefix[80];
        !           117:     int width, height, x_hot, y_hot;
        !           118:     unsigned short *data = ReadX10BitmapFile (prefix, &width, &height, &x_hot, &y_hot);
        !           119:     WriteX11BitmapFile (prefix, width, height, x_hot, y_hot, data);
        !           120:     }
        !           121: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.