|
|
1.1 root 1: /*
2: * $Source: /u1/X/libis/RCS/bitpix.c,v $
3: * $Header: bitpix.c,v 1.1 86/11/17 14:33:09 swick Rel $
4: */
5:
6: #ifndef lint
7: static char *rcsid_bitpix_c = "$Header: bitpix.c,v 1.1 86/11/17 14:33:09 swick Rel $";
8: #endif lint
9:
10: #include "is-copyright.h"
11:
12: /*
13: *
14: * StoreBitmap Creates a bitmap
15: * FreeBitmap Frees the storage taken by a bitmap
16: * CharBitmap Creates a bitmap from a font character
17: * StorePixmap Creates a pixmap
18: * FreePixmap Frees the storage taken by a pixmap
19: * MakePixmap Create a pixmap from a bitmap
20: * PixmapSave Save a region of the screen
21: * PixmapGet Read a region of the screen
22: *
23: * Copyright (c) 1986, Integrated Solutions, Inc.
24: */
25:
26: #include "Xis.h"
27: #include <errno.h>
28:
29: extern int errno;
30: extern char *malloc();
31:
32: DEVICE *CurrentDevice;
33:
34: PIXMAP *MakePixmap();
35:
36: /*
37: * StoreBitmap
38: *
39: * Store "data" of dimensions ("width, height") in BITMAP data
40: * structure.
41: *
42: * WARNING: Bits are put into "X" order
43: */
44: BITMAP *StoreBitmap(width, height, data)
45: int width, height;
46: char *data;
47: {
48: register BITMAP *bm;
49: register RASTER *r;
50: int size;
51:
52: if ((bm = (BITMAP *) malloc(sizeof (BITMAP))) == NULL) {
53: return (NULL);
54: }
55: bm->width = width;
56: bm->height = height;
57: bm->refcnt = 1;
58:
59: if ((r = (RASTER *) malloc(sizeof(RASTER))) == NULL) {
60: free((caddr_t) bm);
61: return (NULL);
62: }
63: bm->data = (caddr_t) r;
64:
65: size = BitmapSize(width, height);
66: if ((r->address = (short *) malloc(size)) == NULL) {
67: free((RASTER *) bm->data);
68: free((caddr_t) bm);
69: return (NULL);
70: }
71:
72: if (data != NULL) {
73: bcopy((char *)data, r->address, size);
74: SwapShorts((short *) r->address, size);
75: SwapBits((short *) r->address, size);
76: }
77: r->width = ((width+15) >>4) << 1;
78:
79: #ifdef DEBUG
80: if (debug & D_Bitmaps)
81: printf("0x%x = StoreBitmap(width=%d, height=%d, data=0x%x)\n",
82: bm, width, height, data);
83: #endif DEBUG
84:
85: return (bm);
86: }
87:
88: /*
89: * FreeBitmap
90: *
91: * Free bitmap data and bitmap structure.
92: */
93: FreeBitmap(bitmap)
94: register BITMAP *bitmap;
95: {
96:
97: #ifdef DEBUG
98: if (debug & D_Bitmaps)
99: printf("FreeBitmap(bitmap=0x%x)\n", bitmap);
100: #endif DEBUG
101:
102: free (((RASTER *) bitmap->data)->address);
103: free ((caddr_t) bitmap->data);
104: free ((caddr_t) bitmap);
105: }
106:
107: /*
108: * CharBitmap
109: *
110: * Copies a character bitmap from a font
111: */
112: BITMAP *CharBitmap(c, font)
113: unsigned c;
114: register FONT *font;
115: {
116: int width;
117: register BITMAP *bm;
118:
119: #ifdef DEBUG
120: if (debug & D_Bitmaps)
121: printf("CharBitmap(c='%c', font=0x%x)\n", c, font);
122: #endif DEBUG
123:
124: if (c < font->first || c > font->last) {
125: errno = EINVAL;
126: return (NULL);
127: }
128:
129: if (font->fixed)
130: width = font->avg_width;
131: else
132: width = FDATA(font)->widths[c - font->first];
133:
134: if (width == 0) {
135: errno = EINVAL;
136: return (NULL);
137: }
138:
139: bm = (BITMAP *) Xalloc(sizeof (BITMAP));
140: bm->width = width;
141: bm->height = font->height;
142: bm->refcnt = 1;
143: if ((bm->data = (caddr_t)malloc(BitmapSize(width, bm->height))) == NULL) {
144: free((caddr_t) bm);
145: errno = ENOMEM;
146: return (NULL);
147: }
148:
149: CopyText ((caddr_t) &c, 1, font, bm);
150: return (bm);
151: }
152:
153: /*
154: * StorePixmap
155: *
156: * Create a pixmap
157: */
158: PIXMAP *StorePixmap(width, height, format, data)
159: int width, height, format;
160: char *data;
161: {
162: register PIXMAP *pm;
163: register RASTER *r;
164: int size, i;
165:
166: #ifdef DEBUG
167: if (debug & D_Pixmaps)
168: printf("StorePixmap(width=%d, height=%d, format=%d, data=0x%x)\n",
169: width, height, format, data);
170: #endif DEBUG
171:
172: if (format != XYFormat /* 0 */) {
173: return (NULL);
174: }
175: if ((pm = (PIXMAP *) malloc(sizeof(PIXMAP))) == NULL) {
176: return (NULL);
177: }
178: pm->width = width;
179: pm->height = height;
180: if (width == TILE_WIDTH && height == TILE_HEIGHT)
181: pm->tile = CanBeTiled;
182: else
183: pm->tile = CannotBeTiled;
184: pm->kind = XYFORMAT | CurrentDevice->planes;
185: pm->refcnt = 1;
186:
187: if ((r = (RASTER *) malloc(sizeof(RASTER)*CurrentDevice->planes)) == NULL) {
188: free((caddr_t) pm);
189: return (NULL);
190: }
191: pm->data = (caddr_t) r;
192: size = BitmapSize(width, height);
193:
194: for (i=0; i < CurrentDevice->planes; i++, r++) {
195:
196: if ((r->address = (short *) malloc(size)) == NULL) {
197: while (i--)
198: free((caddr_t) ((--r)->address));
199: free((caddr_t) pm->data);
200: free((caddr_t) pm);
201: return (NULL);
202: }
203:
204: if (data != NULL) {
205: bcopy((char *)data, r->address, size);
206: data += size;
207: SwapShorts((short *) r->address, size);
208: SwapBits((short *) r->address, size);
209: }
210:
211: r->width = ((width+15) >>4) << 1;
212: }
213:
214: return (pm);
215: }
216:
217: /* FreePixmap
218: *
219: * Frees the storage consumed by the pixmap.
220: */
221: FreePixmap(pixmap)
222: register PIXMAP *pixmap;
223: {
224: register RASTER* r = (RASTER *)pixmap->data;
225: register int i;
226:
227: #ifdef DEBUG
228: if (debug & D_Pixmaps)
229: printf("FreePixmap(pixmap=0x%x)\n", pixmap);
230: #endif DEBUG
231:
232: for (i=0; i < (pixmap->kind & 0xf); i++, r++)
233: free((caddr_t) r->address);
234: free((caddr_t) pixmap->data);
235: free((caddr_t) pixmap);
236: }
237:
238: /*
239: * MakePixmap
240: *
241: * Make pixmap from bitmap
242: */
243: static PIXMAP constpix[] = {
244: /* width, height, refcnt, tile, kind, *data */
245: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 0},
246: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 1},
247: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 2},
248: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 3},
249: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 4},
250: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 5},
251: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 6},
252: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 7},
253: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 8},
254: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 9},
255: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 10},
256: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 11},
257: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 12},
258: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 13},
259: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 14},
260: {1, 1, 1, CanBeTiled, CONSTANT | 4, (caddr_t) 15},
261: };
262:
263: PIXMAP *MakePixmap(xymask, fore, back)
264: register BITMAP *xymask;
265: int fore, back;
266: {
267: register PIXMAP *pm = NULL;
268: register RASTER* r;
269: register int i, size;
270: #ifdef DEBUG
271: int sfore=fore, sback=back;
272: #endif DEBUG
273:
274: if (xymask == NULL) {
275: pm = &constpix[fore & 0xf];
276: pm->refcnt++;
277: #ifdef DEBUG
278: if (debug & D_Pixmaps)
279: printf("0x%x = MakePixmap(xymask=0x%x, fore=%d, back=%d)\n",
280: pm, xymask, fore, back);
281: #endif DEBUG
282: return (pm);
283: }
284:
285: pm = (PIXMAP *) Xalloc(sizeof(PIXMAP));
286: pm->width = xymask->width;
287: pm->height = xymask->height;
288: pm->refcnt = 1;
289: if (xymask->width == TILE_WIDTH && xymask->height == TILE_HEIGHT)
290: pm->tile = CanBeTiled;
291: else
292: pm->tile = CannotBeTiled;
293: pm->kind = XYFORMAT | CurrentDevice->planes;
294: r = (RASTER *) Xalloc(sizeof(RASTER) * CurrentDevice->planes);
295: pm->data = (caddr_t) r;
296:
297: for (i=0; i < CurrentDevice->planes; i++, r++) {
298:
299: size = BitmapSize(pm->width, pm->height);
300: r->address = (short *) Xalloc(size);
301: r->width = ((pm->width+15) >> 4) << 1;
302:
303: if (fore & 1) {
304: if (back & 1) {
305: /* foreground and background are 1 */
306: /* set to ~0; mask does not matter */
307: register short *d, *limitd;
308: d = r->address;
309: limitd = d + (size>>1);
310: for ( ; d < limitd; )
311: *d++ = ~0;
312: } else {
313: /* foreground is 1 and background is 0 */
314: /* set to mask */
315: bcopy(((RASTER *)(xymask->data))->address, r->address, size);
316: }
317: } else {
318: if (back & 1) {
319: /* foreground is 0 and background is 1 */
320: /* set to inverted mask */
321: register short *s, *d, *limitd;
322: s = ((RASTER *)(xymask->data))->address;
323: d = r->address;
324: limitd = d + (size>>1);
325: for ( ; d < limitd; )
326: *d++ = ~*s++;
327: } else {
328: /* foreground and background are 0 */
329: /* set to 0; mask does not matter */
330: register short *d, *limitd;
331: d = r->address;
332: limitd = d + (size>>1);
333: for ( ; d < limitd; )
334: *d++ = 0;
335: }
336: }
337: fore >>= 1;
338: back >>= 1;
339: }
340:
341: #ifdef DEBUG
342: if (debug & D_Pixmaps)
343: printf("0x%x = MakePixmap(xymask=0x%x, fore=%d, back=%d)\n",
344: pm, xymask, sfore, sback);
345: #endif DEBUG
346:
347: return (pm);
348: }
349:
350: /*
351: * PixmapSave
352: *
353: * Save a region of the screen
354: */
355: PIXMAP *PixmapSave(srcx, srcy, width, height)
356: register int srcx, srcy, width, height;
357: {
358: register PIXMAP *pm = NULL;
359: register RASTER *r;
360: register int i;
361: CLIP bounds;
362:
363: #ifdef DEBUG
364: if (debug & D_Pixmaps)
365: printf("PixmapSave(srcx=%d, srcy=%d, width=%d, height=%d)\n",
366: srcx, srcy, width, height);
367: #endif DEBUG
368:
369: bounds.top = srcy;
370: bounds.left = srcx;
371: bounds.width = width;
372: bounds.height = height;
373: pm = (PIXMAP *) Xalloc(sizeof(PIXMAP));
374: pm->width = width;
375: pm->height = height;
376: pm->refcnt = 1;
377: if (width == TILE_WIDTH && height == TILE_HEIGHT)
378: pm->tile = CanBeTiled;
379: else
380: pm->tile = CannotBeTiled;
381: pm->kind = XYFORMAT | CurrentDevice->planes;
382: r = (RASTER *) Xalloc(sizeof(RASTER) * CurrentDevice->planes);
383: pm->data = (caddr_t) r;
384:
385: for (i=0; i < CurrentDevice->planes; r++, i++) {
386: r->address = (short *) Xalloc(BitmapSize(width, height));
387: r->width = ((width+15) >> 4) << 1;
388: }
389: CheckCursor(bounds);
390: GIP_RasterOp(GIPcopy,
391: &ScreenPixmap, srcx, srcy,
392: pm, 0, 0,
393: (BITMAP *)NULL, 0, 0,
394: width, height,
395: ~0);
396: RestoreCursor();
397: return (pm);
398: }
399:
400: /*
401: * PixmapGet
402: *
403: *
404: */
405: PixmapGet(srcx, srcy, width, height, client, format, swapit)
406: int srcx, srcy, width, height, client, format, swapit;
407: {
408: PIXMAP *pm;
409: register RASTER *r;
410: register int size = BitmapSize(width, height);
411: register int i;
412:
413: #ifdef DEBUG
414: if (debug & D_Pixmaps)
415: printf("PixmapGet(srcx=%d, srcy=%d, width=%d, height=%d, client=%d, format=%d, swapit=%d)\n",
416: srcx, srcy, width, height, client, format, swapit);
417: #endif DEBUG
418:
419: pm = PixmapSave(srcx, srcy, width, height);
420:
421: r = (RASTER *) pm->data;
422: for (i=0; i< CurrentDevice->planes; i++, r++) {
423: SwapBits(r->address, size);
424: if (!swapit) /* our bitmaps need swapping just VAX (un)normal */
425: Swap_shorts((short *) r->address, size >> 1);
426: Write(client, r->address, size);
427: }
428: /* Pad amount written to 32-bit boundary */
429: if ((size*CurrentDevice->planes)%4) {
430: Write(client, r->address, 4 - ((size*CurrentDevice->planes)%4));
431: }
432:
433: FreePixmap(pm);
434: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.