summaryrefslogtreecommitdiff
path: root/ilbm/ilbmimage.c
blob: ce9a8c6faa6ebdc88e967d2c5e626070cd7c3551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* $XFree86$ */
#include <stdio.h>

/* Modified jun 95 by Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
   to use interleaved bitplanes instead of normal bitplanes */

#include "X.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "scrnintstr.h"
#include "gcstruct.h"
#include "ilbm.h"
#include "maskbits.h"
#include "servermd.h"

void
ilbmPutImage(pDraw, pGC, depth, x, y, width, height, leftPad, format, pImage)
	DrawablePtr pDraw;
	GCPtr pGC;
	int depth, x, y, width, height;
	int leftPad;
	int format;
	char *pImage;
{
	PixmapPtr pPixmap;

#if 1
	fprintf(stderr, "ilbmPutImage()\n");
	fprintf(stderr, "\tdepth = %d, x = %d, y = %d, width = %d, height = %d, "
			  "leftPad = %d\n", depth, x, y, width, height, leftPad);
	switch (format) {
		case XYBitmap:
			fprintf(stderr, "\tformat = XYBitmap\n");
			break;
		case XYPixmap:
			fprintf(stderr, "\tformat = XYPixmap\n");
			break;
		case ZPixmap:
			fprintf(stderr, "\tformat = ZPixmap\n");
			break;
		default:
			fprintf(stderr, "\tformat = %d\n");
			break;
	}
#endif

	if ((width == 0) || (height == 0))
		return;

	if (format != ZPixmap || depth == 1 || pDraw->depth == 1) {
		if (format == XYBitmap) {
			char *ptmp;
			int realwidth;
			int size;
			int aux;
			int d, yy, xx;
			char *ss, *dd;

			realwidth = BitmapBytePad(width+leftPad);
			aux = depth*realwidth;
			size = height*aux;

#if 1
			fprintf(stderr, "\trealwidth = %d, aux = %d, size = %d\n", realwidth,
					  aux, size);
#endif

			if (!(ptmp = (char *)ALLOCATE_LOCAL(size)))
				return;

			/*
			 *		Convert from bitplanes to interleaved bitplanes
			 */

			ss = (char *)pImage;
			for (d = 0; d < depth; d++) {
				dd = ptmp+d*realwidth;
				for (yy = 0; yy < height; yy++) {
					for (xx = 0; xx < realwidth; xx++)
#if 1
					{
						fprintf(stderr, "*(%d) = *(%d)\n", (&dd[xx])-ptmp,
								  ss-(char *)pImage);
#endif
						dd[xx] = *(ss++);
#if 1
					}
#endif
					dd += aux;
				}
			}

			pPixmap = GetScratchPixmapHeader(pDraw->pScreen, width+leftPad, height,
														depth, depth,
														BitmapBytePad(width+leftPad),
														(pointer)ptmp);
			if (!pPixmap) {
				DEALLOCATE_LOCAL(ptmp);
				return;
			}
			pGC->fExpose = FALSE;
			(void)(*pGC->ops->CopyPlane)((DrawablePtr)pPixmap, pDraw, pGC, leftPad,
												  0, width, height, x, y, 1);
			DEALLOCATE_LOCAL(ptmp);
		} else {
#if 0
			/* XXX: bit plane order wronge ! */
			pPixmap->drawable.depth = 1;
			pPixmap->drawable.bitsPerPixel = 1;

			switch (pGC->alu) {
				case GXcopy:
					doBitBlt = ilbmDoBitbltCopy;
					break;
				case GXxor:
					doBitBlt = ilbmDoBitbltXor;
					break;
				case GXcopyInverted:
					doBitBlt = ilbmDoBitbltCopyInverted;
					break;
				case GXor:
					doBitBlt = ilbmDoBitbltOr;
					break;
				default:
					doBitBlt = ilbmDoBitbltGeneral;
					break;
			}

			for (plane = (1L << (pPixmap->drawable.depth - 1)); plane;
				  plane >>= 1) {
				(void)ilbmBitBlt((DrawablePtr)pPixmap, pDraw, pGC, leftPad, 0,
									  width, height, x, y, doBitBlt, plane);
				/* pDraw->devKind += sizeDst; */
			}
#else
			char *ptmp;
			int realwidth;
			int size;
			int aux;
			int d, yy, xx;
			char *ss, *dd;

			realwidth = BitmapBytePad(width+leftPad);
			aux = depth*realwidth;
			size = height*aux;

#if 1
			fprintf(stderr, "\trealwidth = %d, aux = %d, size = %d\n", realwidth,
					  aux, size);
#endif

			if (!(ptmp = (char *)ALLOCATE_LOCAL(size)))
				return;

			/*
			 *		Convert from bitplanes to interleaved bitplanes
			 */

			ss = (char *)pImage;
			for (d = 0; d < depth; d++) {
				dd = ptmp+d*realwidth;
				for (yy = 0; yy < height; yy++) {
					for (xx = 0; xx < realwidth; xx++)
#if 1
					{
						fprintf(stderr, "*(%d) = *(%d)\n", (&dd[xx])-ptmp,
								  ss-(char *)pImage);
#endif
						dd[xx] = *(ss++);
#if 1
					}
#endif
					dd += aux;
				}
			}

			pPixmap = GetScratchPixmapHeader(pDraw->pScreen, width+leftPad, height,
														depth, depth,
														BitmapBytePad(width+leftPad),
														(pointer)ptmp);
			if (!pPixmap) {
				DEALLOCATE_LOCAL(ptmp);
				return;
			}

			pGC->fExpose = FALSE;
			(void)(*pGC->ops->CopyArea)((DrawablePtr)pPixmap, pDraw, pGC, leftPad,
												 0, width, height, x, y);
			DEALLOCATE_LOCAL(ptmp);
#endif
		}

		pGC->fExpose = TRUE;
		FreeScratchPixmapHeader(pPixmap);
	} else {
		/* Chunky to planar conversion required */

		PixmapPtr pPixmap;
		ScreenPtr pScreen = pDraw->pScreen;
		int widthSrc;
		int start_srcshift;
		register int b;
		register int dstshift;
		register int shift_step;
		register PixelType dst;
		register PixelType srcbits;
		register PixelType *pdst;
		register PixelType *psrc;
		int start_bit;
		register int nl;
		register int h;
		register int d;
		int auxDst;
		PixelType *pdstBase;
		int widthDst;
		int depthDst;

		/* Create a tmp pixmap */
		pPixmap = (pScreen->CreatePixmap)(pScreen, width, height, depth);
		if (!pPixmap)
			return;

		ilbmGetPixelWidthAuxDepthAndPointer((DrawablePtr)pPixmap, widthDst,
														auxDst, depthDst, pdstBase);

		widthSrc = PixmapWidthInPadUnits(width, depth);
		/* XXX: if depth == 8, use fast chunky to planar assembly function.*/
		if (depth > 4) {
			start_srcshift = 24;
			shift_step = 8;
		} else {
			start_srcshift = 28;
			shift_step = 4;
		}

		for (d = 0; d < depth; d++, pdstBase += widthDst) {	/* @@@ NEXT PLANE @@@ */
			register PixelType *pdstLine = pdstBase;
			start_bit = start_srcshift + d;
			psrc = (PixelType *)pImage;
			h = height;

			while (h--) {
				pdst = pdstLine;
				pdstLine += auxDst;
				dstshift = PPW - 1;
				dst = 0;
				nl = widthSrc;
				while (nl--) {
					srcbits = *psrc++;
					for (b = start_bit; b >= 0; b -= shift_step) {
						dst |= ((srcbits >> b) & 1) << dstshift;
						if (--dstshift < 0) {
							dstshift = PPW - 1;
							*pdst++ = dst;
							dst = 0;
						}
					}
				}
				if (dstshift != PPW - 1)
					*pdst++ = dst;
			}
		} /* for (d = ...) */

		pGC->fExpose = FALSE;
		(void)(*pGC->ops->CopyArea)((DrawablePtr)pPixmap, pDraw, pGC, leftPad, 0,
											 width, height, x, y);
		pGC->fExpose = TRUE;
		(*pScreen->DestroyPixmap)(pPixmap);
	}
}

void
ilbmGetImage(pDrawable, sx, sy, width, height, format, planemask, pdstLine)
	DrawablePtr pDrawable;
	int sx, sy, width, height;
	unsigned int format;
	unsigned long planemask;
	char *pdstLine;
{
	BoxRec box;
	DDXPointRec ptSrc;
	RegionRec rgnDst;
	ScreenPtr pScreen;
	PixmapPtr pPixmap;

#if 1
	fprintf(stderr, "ilbmGetImage()\n");
	fprintf(stderr, "\tsx = %d, sy = %d, width = %d, height = %d, "
			  "planemask = 0x%08x\n", sx, sy, width, height, planemask);
	switch (format) {
		case XYBitmap:
			fprintf(stderr, "\tformat = XYBitmap\n");
			break;
		case XYPixmap:
			fprintf(stderr, "\tformat = XYPixmap\n");
			break;
		case ZPixmap:
			fprintf(stderr, "\tformat = ZPixmap\n");
			break;
		default:
			fprintf(stderr, "\tformat = %d\n");
			break;
	}
#endif

	if ((width == 0) || (height == 0))
		return;

	pScreen = pDrawable->pScreen;
	sx += pDrawable->x;
	sy += pDrawable->y;

	if (format == XYPixmap || pDrawable->depth == 1) {
		pPixmap = GetScratchPixmapHeader(pScreen, width, height, 1, 1,
													BitmapBytePad(width), (pointer)pdstLine);
		if (!pPixmap)
			return;

		ptSrc.x = sx;
		ptSrc.y = sy;
		box.x1 = 0;
		box.y1 = 0;
		box.x2 = width;
		box.y2 = height;
		REGION_INIT(pScreen, &rgnDst, &box, 1);

		pPixmap->drawable.depth = 1;
		pPixmap->drawable.bitsPerPixel = 1;
		/* dix layer only ever calls GetImage with 1 bit set in planemask
		 * when format is XYPixmap.
		 */
		ilbmDoBitblt(pDrawable, (DrawablePtr)pPixmap, GXcopy, &rgnDst, &ptSrc,
						 planemask);

		FreeScratchPixmapHeader(pPixmap);
		REGION_UNINIT(pScreen, &rgnDst);
	} else {
		/* Planar to chunky conversion required */

		PixelType *psrcBits;
		PixelType *psrcLine;
		PixelType startmask, endmask;
		int depthSrc;
		int widthSrc;
		int auxSrc;
		int sizeDst;
		int widthDst;
		register PixelType *psrc;
		register PixelType *pdst;
		register PixelType dst;
		register PixelType srcbits;
		register int d;
		register int b;
		register int dstshift;
		register int shift_step;
		register int start_endbit;
		int start_startbit;
		register int end_endbit;
		register int start_dstshift;
		register int nl;
		register int h;
		int nlmiddle;

		widthDst = PixmapWidthInPadUnits(width, pDrawable->depth);
		sizeDst = widthDst * height;

		/* Clear the dest image */
		bzero(pdstLine, sizeDst << 2);

		ilbmGetPixelWidthAuxDepthAndPointer(pDrawable, widthSrc, auxSrc,
														depthSrc, psrcBits);

		psrcBits = ilbmScanline(psrcBits, sx, sy, auxSrc);

		start_startbit = PPW - 1 - (sx & PIM);
		if ((sx & PIM) + width < PPW) {
			maskpartialbits(sx, width, startmask);
			nlmiddle = 0;
			endmask = 0;
			start_endbit = PPW - ((sx + width) & PIM);
		} else {
			maskbits(sx, width, startmask, endmask, nlmiddle);
			start_endbit = 0;
			end_endbit = PPW - ((sx + width) & PIM);
		}
		/* ZPixmap images have either 4 or 8 bits per pixel dependent on
		 * depth.
		 */
		if (depthSrc > 4) {
			start_dstshift = 24;
			shift_step = 8;
		} else {
			start_dstshift = 28;
			shift_step = 4;
		}
#define SHIFT_BITS(start_bit,end_bit) \
for (b = (start_bit); b >= (end_bit); b--) { \
	dst |= ((srcbits >> b) & 1) << dstshift; \
	if ((dstshift -= shift_step) < 0) { \
		dstshift = start_dstshift + d; \
		*pdst++ = dst; \
		dst = *pdst; \
	} \
} \

		for (d = 0; d < depthSrc; d++, psrcBits += widthSrc) {	/* @@@ NEXT PLANE @@@ */
			psrcLine = psrcBits;
			pdst = (PixelType *)pdstLine;
			h = height;

			while (h--) {
				psrc = psrcLine;
				psrcLine += auxSrc;
				dst = *pdst;
				dstshift = start_dstshift + d;

				if (startmask) {
					srcbits = *psrc++ & startmask;
					SHIFT_BITS(start_startbit, start_endbit);
				}

				nl = nlmiddle;
				while (nl--) {
					srcbits = *psrc++;
					SHIFT_BITS(PPW - 1, 0);
				}
				if (endmask) {
					srcbits = *psrc & endmask;
					SHIFT_BITS(PPW - 1, end_endbit);
				}

				if (dstshift != start_dstshift + d)
					*pdst++ = dst;
			} /* while (h--) */
		} /* for (d = ...) */
	}
}