Annotation of researchv9/X11/src/X.V11R1/lib/X/XWrBitF.c, revision 1.1.1.1

1.1       root        1: /* Copyright, 1987, Massachusetts Institute of Technology */
                      2: 
                      3: #include "copyright.h"
                      4: 
                      5: #include "Xlib.h"
                      6: #include "Xutil.h"
                      7: #include "Xlibint.h"
                      8: #include <stdio.h>
                      9: #include <strings.h>
                     10: 
                     11: #define ERR_RETURN 0
                     12: 
                     13: static char *Format_Image(image, resultsize)
                     14: XImage *image;
                     15: int *resultsize;
                     16: {
                     17:   register int x, c, b;
                     18:   register char *ptr;
                     19:   int y;
                     20:   char *data;
                     21:   int width, height;
                     22:   int bytes_per_line;
                     23: 
                     24:   width = image->width;
                     25:   height = image->height;
                     26: 
                     27:   bytes_per_line = (width+7)/8;
                     28:   *resultsize = bytes_per_line * height;           /* Calculate size of data */
                     29: 
                     30:   data = (char *) Xmalloc( *resultsize );           /* Get space for data */
                     31:   if (!data)
                     32:     return(ERR_RETURN);
                     33: 
                     34:   /*
                     35:    * The slow but robust brute force method of converting the image:
                     36:    */
                     37:   ptr = data;
                     38:   c = 0; b=1;
                     39:   for (y=0; y<height; y++) {
                     40:     for (x=0; x<width;) {
                     41:       if (XGetPixel(image, x, y))
                     42:        c |= b;
                     43:       b <<= 1;
                     44:       if (!(++x & 7)) {
                     45:        *(ptr++)=c;
                     46:        c=0; b=1;
                     47:       }
                     48:     }
                     49:     if (x & 7) {
                     50:       *(ptr++)=c;
                     51:       c=0; b=1;
                     52:     }
                     53:   }
                     54: 
                     55:   return(data);
                     56: }
                     57:    
                     58: #define BYTES_PER_OUTPUT_LINE 12
                     59: 
                     60: int XWriteBitmapFile(display, filename, bitmap, width, height, x_hot, y_hot)
                     61:      Display *display;
                     62:      char *filename;
                     63:      Pixmap bitmap;
                     64:      int width, height;
                     65:      int x_hot, y_hot;
                     66: {
                     67:   char *data, *ptr;
                     68:   int size, byte;
                     69:   int c;
                     70:   XImage *image;
                     71:   FILE *stream;
                     72:   char *name;
                     73: 
                     74:   if (!(name = rindex(filename, '/')))
                     75:     name = filename;
                     76:   else
                     77:     name++;
                     78: 
                     79:   if (!(stream = fopen(filename, "w")))
                     80:     return(BitmapOpenFailed);
                     81: 
                     82:   /* Convert bitmap to an image */
                     83:   image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap);
                     84: 
                     85:   /* Get standard format for data */
                     86:   data = Format_Image(image, &size);
                     87:   XDestroyImage(image);
                     88:   if (!data) {
                     89:     fclose(stream);
                     90:     return(BitmapNoMemory);
                     91:   }
                     92: 
                     93:   /* Write out standard header */
                     94:   fprintf(stream, "#define %s_width %d\n", name, image->width);
                     95:   fprintf(stream, "#define %s_height %d\n", name, image->height);
                     96:   if (x_hot != -1) {
                     97:     fprintf(stream, "#define %s_x_hot %d\n", name, x_hot);
                     98:     fprintf(stream, "#define %s_y_hot %d\n", name, y_hot);
                     99:   }
                    100: 
                    101:   /* Print out the data itself */
                    102:   fprintf(stream, "static char %s_bits[] = {", name);
                    103:   for (byte=0, ptr=data; byte<size; byte++, ptr++) {
                    104:     if (!byte)
                    105:       fprintf(stream, "\n   ");
                    106:     else if (!(byte % BYTES_PER_OUTPUT_LINE))
                    107:       fprintf(stream, ",\n   ");
                    108:     else
                    109:       fprintf(stream, ", ");
                    110:     c = *ptr;
                    111:     if (c<0)
                    112:       c += 256;
                    113:     fprintf(stream, "0x%02x", c);
                    114:   }
                    115:   fprintf(stream, "};\n");
                    116: 
                    117:   Xfree(data);
                    118:   fclose(stream);
                    119:   return(BitmapSuccess);
                    120: }

unix.superglobalmegacorp.com

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