Annotation of 43BSDTahoe/new/X/libibm/libsrc/bitpix.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char *rcsid_bitpix_c = "$Header: bitpix.c,v 10.1 86/11/19 10:40:11 jg Exp $";
                      3: #endif lint
                      4: /* Copyright 1985 Massachusetts Institute of Technology */
                      5: 
                      6: /* bitpix.c - Routines to cache bitmaps and pixmaps
                      7:  *
                      8:  *      StoreBitmap     Creates a bitmap
                      9:  *      FreeBitmap      Frees the storage taken by a bitmap
                     10:  *      CharBitmap      Creates a bitmap from a font character
                     11:  *      StorePixmap     Creates a pixmap
                     12:  *      FreePixmap      Frees the storage taken by a pixmap
                     13:  *      MakePixmap      Create a pixmap from a bitmap
                     14:  *      PixmapSave      Save a region of the screen
                     15:  *      PixmapGet       Read a region of the screen
                     16:  *
                     17:  *     Changes and additions by:
                     18:  *
                     19:  *             Scott Bates
                     20:  *             Brown University
                     21:  *             IRIS, Box 1946
                     22:  *                     Providence, RI 02912
                     23:  *
                     24:  *
                     25:  *             Copyright (c) 1986 Brown University
                     26:  *
                     27:  * Permission to use, copy, modify and distribute this software and its
                     28:  * documentation for any purpose and without fee is hereby granted, provided
                     29:  * that the above copyright notice appear in all copies, and that both
                     30:  * that copyright notice and this permission notice appear in supporting
                     31:  * documentation, and that the name of Brown University not be used in
                     32:  * advertising or publicity pertaining to distribution of the software
                     33:  * without specific, written prior permission. Brown University makes no
                     34:  * representations about the suitability of this software for any purpose.
                     35:  * It is provided "as-is" without express or implied warranty.
                     36:  */
                     37: 
                     38: #include "private.h"
                     39: #include "bitblt.h"
                     40: 
                     41: /*
                     42:  * Create bitmap from client supplied data
                     43:  */
                     44: 
                     45: BITMAP *StoreBitmap (width, height, data)
                     46:         register width, height;
                     47:         char *data;
                     48: {
                     49:         register BITMAP *bm;
                     50:         register size = BitmapSize(width, height);
                     51: 
                     52: #ifdef TRACE_X
                     53:        fprintf(stderr, "In StoreBitmap\n");
                     54:        fflush(stderr);
                     55: #endif TRACE_X
                     56: 
                     57:        /*
                     58:         * Allocate space for bitmap structure
                     59:         */
                     60: 
                     61:         bm = (BITMAP *) Xalloc (sizeof (BITMAP));
                     62: 
                     63:        /*
                     64:         * Fill in bitmap structure
                     65:         */
                     66: 
                     67:         bm->width = width;
                     68:         bm->height = height;
                     69:         bm->refcnt = 1;
                     70: 
                     71:        /*
                     72:         * Allocated space to hold bitimage data
                     73:         */
                     74: 
                     75:         if ((bm->data = (caddr_t) malloc (size)) == NULL) {
                     76:             free ((caddr_t) bm);
                     77:             return (NULL);
                     78:         }
                     79: 
                     80:        /*
                     81:         * Copy image data to newly allocated area and reverse bits
                     82:         * in each short of the image. The image data is in vax 
                     83:         * bit order and must be reversed for this hardware.
                     84:         */
                     85: 
                     86:         bcopy (data, bm->data, size);
                     87:        ReverseShortBits((u_short *) bm->data, size >> 1);
                     88: 
                     89:        /*
                     90:         * Return pointer to bitmap
                     91:         */
                     92: 
                     93:         return (bm);
                     94: }
                     95: 
                     96: /*
                     97:  * Free bitmap structure and bit image data
                     98:  */
                     99: 
                    100: FreeBitmap (bitmap)
                    101:         register BITMAP *bitmap;
                    102: {
                    103: 
                    104: #ifdef TRACE_X
                    105:        fprintf(stderr, "In FreeBitmap\n");
                    106:        fflush(stderr);
                    107: #endif TRACE_X
                    108: 
                    109:         free ((caddr_t) bitmap->data);
                    110:         free ((caddr_t) bitmap);
                    111: }
                    112: 
                    113: /*
                    114:  * Create character bitmap from font character
                    115:  */
                    116: 
                    117: BITMAP *CharBitmap (c, font)
                    118:         u_char c;
                    119:         register FONT *font;
                    120: {
                    121:         register width;
                    122:        register size;
                    123:         register BITMAP *bm;
                    124: 
                    125: #ifdef TRACE_X
                    126:        fprintf(stderr, "In CharBitmap\n");
                    127:        fflush(stderr);
                    128: #endif TRACE_X
                    129: 
                    130:        /*
                    131:         * Test for valid character
                    132:         */
                    133: 
                    134:         if (c < font->first || c > font->last) {
                    135:             errno = EINVAL;
                    136:             return (NULL);
                    137:         }
                    138: 
                    139:        /*
                    140:         * Determine width of character
                    141:         */
                    142: 
                    143:         if (font->fixed)
                    144:             width = font->avg_width;
                    145:         else
                    146:             width = FDATA(font)->widths[c];
                    147: 
                    148:        /*
                    149:         * Validate width
                    150:         */
                    151: 
                    152:         if (width == 0) {
                    153:             errno = EINVAL;
                    154:             return (NULL);
                    155:         }
                    156: 
                    157:        /* 
                    158:         * Allocate space for bitmap structure
                    159:         */
                    160: 
                    161:         bm = (BITMAP *) Xalloc (sizeof (BITMAP));
                    162: 
                    163:        /*
                    164:         * Fill in bitmap structure
                    165:         */
                    166: 
                    167:         bm->width = width;
                    168:         bm->height = font->height;
                    169:         bm->refcnt = 1;
                    170: 
                    171:        /*
                    172:         * Allocate space to hold bit image of character
                    173:         */
                    174: 
                    175:        size = BitmapSize(width, bm->height);
                    176:         if ((bm->data = (caddr_t) malloc (size)) == NULL) {
                    177:             free ((caddr_t) bm);
                    178:             errno = ENOMEM;
                    179:             return (NULL);
                    180:         }
                    181: 
                    182:        /*
                    183:         * Copy bit image of character to newly allocated area
                    184:         * and return pointer to character bitmap
                    185:         */
                    186: 
                    187:         CopyText ((caddr_t) &c, 1, font, bm);
                    188:         return (bm);
                    189: }
                    190: 
                    191: /*
                    192:  * Create pixmap from client supplied data
                    193:  */
                    194: 
                    195: /*ARGSUSED*/
                    196: PIXMAP *StorePixmap (width, height, format, data)
                    197:         int width, height, format;
                    198:         char *data;
                    199: {
                    200:         register BITMAP *bm;
                    201:         register PIXMAP *pm;
                    202: 
                    203: #ifdef TRACE_X
                    204:        fprintf(stderr, "In StorePixmap\n");
                    205:        fflush(stderr);
                    206: #endif TRACE_X
                    207: 
                    208:        /*
                    209:         * Create bitmap from the bit image data supplied.
                    210:         */
                    211: 
                    212:         if ((bm = StoreBitmap (width, height, data)) == NULL) {
                    213:             return (NULL);
                    214:        }
                    215: 
                    216:        /*
                    217:         * Reset reference count so bitmap will be deallocated 
                    218:         * correctly by FreePixmap 
                    219:         */
                    220:         
                    221:         bm->refcnt = 0;
                    222: 
                    223:        /*
                    224:         * Make pixmap from bitmap
                    225:         */
                    226: 
                    227:         if (pm = MakePixmap (bm, 1, 0))
                    228:             return (pm);
                    229: 
                    230:        /*
                    231:         * If unable to make pixmap free the bitmap
                    232:         */
                    233: 
                    234:         FreeBitmap (bm);
                    235:         return (NULL);
                    236: }
                    237: 
                    238: /*
                    239:  * Free pixmap resources
                    240:  */
                    241: 
                    242: FreePixmap (pm)
                    243:         register PIXMAP *pm;
                    244: {
                    245:         register BITMAP *bm;
                    246: 
                    247: #ifdef TRACE_X
                    248:        fprintf(stderr, "In FreePixmap\n");
                    249:        fflush(stderr);
                    250: #endif TRACE_X
                    251: 
                    252:        /*
                    253:         * If a bitmap is associated with this pixmap and its
                    254:         * reference count is equal to zero free it. Otherwise 
                    255:         * decrement its reference count and return.
                    256:         */
                    257: 
                    258:         if (pm->kind & BitmapPixmap) {
                    259:             bm = PDATA(pm);
                    260:             if (--bm->refcnt == 0)
                    261:                 FreeBitmap (bm);
                    262:         }
                    263: 
                    264:        /*
                    265:         * Free pixmap structure
                    266:         */
                    267: 
                    268:         free ((caddr_t) pm);
                    269: }
                    270: 
                    271: /*
                    272:  * Make a pixmap from a bitmap
                    273:  */
                    274: 
                    275: PIXMAP *MakePixmap (xymask, fore, back)
                    276:         register BITMAP *xymask;
                    277:         register fore, back;
                    278: {
                    279:         register PIXMAP *pm;
                    280: 
                    281: #ifdef TRACE_X
                    282:        fprintf(stderr, "In MakePixmap\n");
                    283:        fflush(stderr);
                    284: #endif TRACE_X
                    285: 
                    286:        /*
                    287:         * If no bitmap was supplied or the foreground and
                    288:         * background colors are the same then pixmap is
                    289:         * a constant pixmap (either all white or all black)
                    290:         */
                    291: 
                    292:         if (xymask == NULL || !((fore ^ back) & 1)) {
                    293:                if (fore & 1)
                    294:                        pm = &constpix1;
                    295:                else
                    296:                        pm = &constpix0;
                    297:                pm->refcnt++;
                    298:                return (pm);
                    299:         }
                    300: 
                    301:        /*
                    302:         * Allocate space for pixmap structure
                    303:         */
                    304: 
                    305:         pm = (PIXMAP *) Xalloc (sizeof (PIXMAP));
                    306: 
                    307:        /*
                    308:         * Fill in pixmap structure. Indicate that a bitmap is
                    309:         * associated with this pixmap (BitmapPixmap).
                    310:         */
                    311: 
                    312:         pm->width = xymask->width;
                    313:         pm->height = xymask->height;
                    314:         pm->refcnt = 1;
                    315:         pm->kind = BitmapPixmap;
                    316:         pm->data = (caddr_t) xymask;
                    317: 
                    318:        /*
                    319:         * Increment reference count of bitmap
                    320:         */
                    321: 
                    322:         xymask->refcnt++;
                    323: 
                    324:        /*
                    325:         * Indicate if pixmap can be used as a tile
                    326:         */
                    327: 
                    328:         if (xymask->width == TILE_WIDTH && xymask->height == TILE_HEIGHT) {
                    329:             pm->tile = CanBeTiled;
                    330:         } else {
                    331:             pm->tile = CannotBeTiled;
                    332:         }
                    333: 
                    334:        /*
                    335:         * Indicate if bit image needs to be inverted when displayed
                    336:         */
                    337: 
                    338:         if (back & 1)
                    339:             pm->kind |= InvertFlag;
                    340: 
                    341:        /*
                    342:         * Return pointer to pixmap
                    343:         */
                    344: 
                    345:         return (pm);
                    346: }
                    347: 
                    348: /*
                    349:  * Save rectangular screen image
                    350:  */
                    351: 
                    352: PIXMAP *PixmapSave (srcx, srcy, width, height)
                    353:         register srcx, srcy, width, height;
                    354: {
                    355:        register Blt_Rectangle *source = &SrcRect;
                    356:        register Blt_Rectangle *dest = &DstRect;
                    357:         register BITMAP *bm;
                    358:         PIXMAP *pm;
                    359: 
                    360: #ifdef TRACE_X
                    361:        fprintf(stderr, "In PixmapSave\n");
                    362:        fflush(stderr);
                    363: #endif TRACE_X
                    364: 
                    365:        /*
                    366:         * Allocate space for bitmap structure
                    367:         */
                    368: 
                    369:         bm = (BITMAP *) Xalloc (sizeof (BITMAP));
                    370: 
                    371: 
                    372:        /*
                    373:         * Fill in bitmap structure
                    374:         */
                    375: 
                    376:         bm->width = width;
                    377:         bm->height = height;
                    378:         bm->refcnt = 0;
                    379: 
                    380:        /*
                    381:         * Allocate space to hold screen image
                    382:         */
                    383: 
                    384:         if ((bm->data = 
                    385:                 (caddr_t) malloc (BitmapSize(width, height))) == NULL) {
                    386:             free ((caddr_t) bm);
                    387:             return (NULL);
                    388:         }
                    389: 
                    390:        /*
                    391:         * Fill in source and destination rectangles
                    392:         */
                    393: 
                    394:        FillInRect(srcx, srcy, width, height, source);
                    395:        FillInRect(0, 0, width, height, dest);
                    396: 
                    397:        /*
                    398:         * Copy screen image to allocated area
                    399:         */
                    400: 
                    401:         CopyBits ((u_short *) pbm.data, pbm.width, pbm.height, source,
                    402:                   (u_short *) bm->data, width, height, dest,
                    403:                  NILMASK, NIL, NIL, GXcopy, NIL, NILCLIP);
                    404: 
                    405:        /*
                    406:         * Make pixmap from bitmap and return pointer to pixmap
                    407:         */
                    408: 
                    409:         if (pm = MakePixmap (bm, 1, 0))
                    410:             return (pm);
                    411: 
                    412:        /*
                    413:         * If unable to make pixmap free bitmap
                    414:         */
                    415: 
                    416:         FreeBitmap (bm);
                    417:         return (NULL);
                    418: }
                    419: 
                    420: /*
                    421:  * Pass rectangular screen image to client
                    422:  */
                    423: 
                    424: /*ARGSUSED*/
                    425: PixmapGet (srcx, srcy, width, height, client, format, swapit)
                    426:         int srcx, srcy, width, height, client, format;
                    427: {
                    428:         register PIXMAP *pm;
                    429:         register BITMAP *bm;
                    430:         register size = BitmapSize(width, height);
                    431: 
                    432: #ifdef TRACE_X
                    433:        fprintf(stderr, "In PixmapGet\n");
                    434:        fflush(stderr);
                    435: #endif TRACE_X
                    436: 
                    437:        /*
                    438:         * Save screen image 
                    439:         */
                    440: 
                    441:         pm = PixmapSave (srcx, srcy, width, height);
                    442: 
                    443:        /*
                    444:         * Reverse the bits in each short of the image. The image
                    445:         * is in IBM bit order and needs to be in VAX bit order when
                    446:         * given to clients.
                    447:         */
                    448: 
                    449:         bm = (BITMAP *) pm->data;
                    450:        ReverseShortBits((u_short *) bm->data, size >> 1);
                    451: 
                    452:        /*
                    453:         * Swap shorts of image if required by client
                    454:         */
                    455: 
                    456:         if (swapit)
                    457:                 Swap_shorts ((short *) bm->data, size >> 1 );
                    458: 
                    459:        /*
                    460:         * Write screen image to client. Image size must be padded 
                    461:         * to 32 bit boundary.
                    462:         */
                    463: 
                    464:        Write (client, bm->data, size);
                    465:        if (size % 4) {
                    466:                Write(client, bm->data, 4 - (size % 4));
                    467:        }
                    468: 
                    469:        /*
                    470:         * Free pixmap and return
                    471:         */
                    472: 
                    473:         FreePixmap(pm);
                    474: }

unix.superglobalmegacorp.com

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