summaryrefslogtreecommitdiff
path: root/Xprint/pcl/PclArea.c
blob: 00e6aff0477193bd5b6f7b0b58e190bad7eb36a8 (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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* $Xorg: PclArea.c,v 1.3 2000/08/17 19:48:07 cpqbld Exp $ */
/*******************************************************************
**
**    *********************************************************
**    *
**    *  File:		PclArea.c
**    *
**    *  Contents:
**    *                 Image and Area functions for the PCL DDX driver
**    *
**    *  Created:	10/23/95
**    *
**    *********************************************************
**
********************************************************************/
/*
(c) Copyright 1996 Hewlett-Packard Company
(c) Copyright 1996 International Business Machines Corp.
(c) Copyright 1996 Sun Microsystems, Inc.
(c) Copyright 1996 Novell, Inc.
(c) Copyright 1996 Digital Equipment Corp.
(c) Copyright 1996 Fujitsu Limited
(c) Copyright 1996 Hitachi, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the copyright holders shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from said
copyright holders.
*/
/* $XFree86: xc/programs/Xserver/Xprint/pcl/PclArea.c,v 1.8 2001/01/17 22:36:30 dawes Exp $ */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Pcl.h"
#include "pixmapstr.h"
#include "region.h"

#include "cfb.h"
#if 1
#include "cfb32.h"
#endif

void
PclPutImage(DrawablePtr pDrawable,
	    GCPtr pGC,
	    int depth,
	    int x,
	    int y,
	    int w,
	    int h,
	    int leftPad,
	    int format,
	    char *pImage)
{
    PixmapPtr pPixmap;
    unsigned long oldFg, oldBg;
    XID gcv[3];
    unsigned long oldPlanemask;
    unsigned long i;
    long bytesPer;

    if( ( w == 0 ) || ( h == 0 ) )
      return;

    if( format != XYPixmap )
      {
	  pPixmap = GetScratchPixmapHeader( pDrawable->pScreen,
					   w+leftPad, h, depth,
					   BitsPerPixel( depth ),
					   PixmapBytePad( w + leftPad,
					   depth ), (pointer)pImage );
	  if( !pPixmap )
	    return;

	  if( format == ZPixmap )
	    (void)(*pGC->ops->CopyArea)( (DrawablePtr)pPixmap, pDrawable, pGC,
				  leftPad, 0, w, h, x, y );
	  else
	    (void)(*pGC->ops->CopyPlane)( (DrawablePtr)pPixmap, pDrawable, pGC,
			 leftPad, 0, w, h, x, y, 1 );
	  FreeScratchPixmapHeader( pPixmap );
      }
    else
      {
	  pPixmap = GetScratchPixmapHeader( pDrawable->pScreen,
					   w+leftPad, h, depth,
					   BitsPerPixel( depth ),
					   PixmapBytePad( w + leftPad,
					   depth ), (pointer)pImage );

	  if( !pPixmap )
	    return;

	  depth = pGC->depth;
	  oldPlanemask = pGC->planemask;
	  oldFg = pGC->fgPixel;
	  oldBg = pGC->bgPixel;
	  gcv[0] = ~0L;
	  gcv[1] = 0;
	  DoChangeGC( pGC, GCForeground | GCBackground, gcv, 0 );
	  bytesPer = (long)h * BitmapBytePad( w + leftPad );

	  for( i = 1 << (depth-1); i != 0; i >>= 1, pImage += bytesPer )
	    {
		if( i & oldPlanemask )
		  {
		      gcv[0] = i;
		      DoChangeGC( pGC, GCPlaneMask, gcv, 0 );
		      ValidateGC( pDrawable, pGC );
		      if (pPixmap->drawable.depth <= 8 )
			cfbPutImage( (DrawablePtr)pPixmap, pGC, 1, x, y, w, h,
				  leftPad, XYBitmap, pImage );
		      else if (pPixmap->drawable.depth <= 32 )
			cfb32PutImage( (DrawablePtr)pPixmap, pGC, 1, x, y, w, h,
				  leftPad, XYBitmap, pImage );
		  }
	    }
	  gcv[0] = oldPlanemask;
	  gcv[1] = oldFg;
	  gcv[2] = oldBg;
	  DoChangeGC( pGC, GCPlaneMask | GCForeground | GCBackground,
		     gcv, 0 );

	  PclCopyArea( (DrawablePtr)pPixmap, pDrawable, pGC, leftPad,
		      0, w, h, x, y );
	  FreeScratchPixmapHeader( pPixmap );
      }
}

/*
 * PclMonoPixmapFragment()
 *
 * Given a 1-bit-deep pixmap, send the appropriate part of it to the
 * output file as a PCL raster graphics command.
 */
static void
PclMonoPixmapFragment(FILE *outFile,
		      PixmapPtr pix,
		      short x1,
		      short y1,
		      short x2,
		      short y2,
		      short dstx,
		      short dsty)
{
    char *bits, t[80], *row;
    int h, w, i;

    /*
     * Create a storage area large enough to hold the entire pixmap,
     * then use mfbGetImage to get the appropriate bits.
     */
    h = y2 - y1;
    w = BitmapBytePad( x2 - x1 );

    bits = (char *)xalloc( h * w );
    mfbGetImage( (DrawablePtr)pix, x1, y1, x2 - x1, h,
		XYPixmap, ~0, bits );

    /*
     * Move the cursor to the appropriate place on the page.  We have
     * to jump into HP-GL/2 to do this correctly, then go back to PCL
     * for the actual drawing.
     */
    sprintf( t, "\033%%0BPU%d,%d;\033%%1A", dstx, dsty );
    SEND_PCL( outFile, t );

    /*
     * Now, wrap the raster in the appropriate PCL code.  Right now,
     * it's going to go down the wire without any compression.  That
     * will have to be good enough for the sample implementation.
     */
    sprintf( t, "\033*t300R\033*r%dT\033*r%dS\033*r1A\033*b0M",
	    h, x2 - x1 );
    SEND_PCL( outFile, t );

    sprintf( t, "\033*b%dW", w );
    for( row = bits, i = 0; i <= h; i++, row += w )
      {
	  SEND_PCL( outFile, t );
	  SEND_PCL_COUNT( outFile, row, w );
      }

    SEND_PCL( outFile, "\033*rC" );

    /*
     * Clean things up a bit
     */
    xfree( bits );
}

static void
PclColorPixmapFragment(FILE *outFile,
		       PixmapPtr pix,
		       short x1,
		       short y1,
		       short x2,
		       short y2,
		       short dstx,
		       short dsty)
{
    char *bits, t[80], *row;
    int h, w, i;

    /*
     * Create a storage area large enough to hold the entire pixmap,
     * then use cfbGetImage to get the appropriate bits.
     */
    h = y2 - y1;
    w = PixmapBytePad( x2 - x1, pix->drawable.depth );

    bits = (char *)xalloc( h * w );
    if (pix->drawable.depth <= 8)
	cfbGetImage( (DrawablePtr)pix, x1, y1, x2 - x1, h,
		ZPixmap, ~0, bits );
    else if (pix->drawable.depth <= 32)
	cfb32GetImage( (DrawablePtr)pix, x1, y1, x2 - x1, h,
		ZPixmap, ~0, bits );

    /*
     * Move the cursor to the appropriate place on the page.  We have
     * to jump into HP-GL/2 to do this correctly, then go back to PCL
     * for the actual drawing.
     */
    sprintf( t, "\033%%0BPU%d,%d;\033%%1A", dstx, dsty );
    SEND_PCL( outFile, t );

    /*
     * Now, wrap the raster in the appropriate PCL code.  Right now,
     * it's going to go down the wire without any compression.  That
     * will have to be good enough for the sample implementation.
     */
    sprintf( t, "\033*t300R\033*r%dt%ds1A\033*b0M",
	    h, x2 - x1 );
    SEND_PCL( outFile, t );

    sprintf( t, "\033*b%dW", w );
    for( row = bits, i = 0; i < h; i++, row += w )
      {
	  SEND_PCL( outFile, t );
	  SEND_PCL_COUNT( outFile, row, w );
      }

    SEND_PCL( outFile, "\033*rC" );

    /*
     * Clean things up a bit
     */
    xfree( bits );
}

RegionPtr
PclCopyArea(DrawablePtr pSrc,
	    DrawablePtr pDst,
	    GCPtr pGC,
	    int srcx,
	    int srcy,
	    int width,
	    int height,
	    int dstx,
	    int dsty)
{
    PixmapPtr pixSrc = (PixmapPtr)pSrc;
/*
    FILE *srcFile;
    GC srcGC;
*/
    FILE *dstFile;
    GC dstGC;
    unsigned long valid;
    RegionPtr drawRegion, region, whole, ret;
    BoxRec box;
    BoxPtr prect;
    int nrect;
    void (*doFragment)(FILE *, PixmapPtr, short, short, short, short,
		       short, short );

    /*
     * Since we don't store any information on a per-window basis, we
     * can't copy from a window.
     */
    if( pSrc->type == DRAWABLE_WINDOW )
      return NULL;

    /*
     * If we're copying from a pixmap to a pixmap, we just use the
     * mfb/cfb code to do the work.
     */
    if( pDst->type == DRAWABLE_PIXMAP )
      {
	  if( pSrc->depth == 1 )
	    return mfbCopyArea( pSrc, pDst, pGC, srcx, srcy, width,
			height, dstx, dsty );
	  else if( pSrc->depth <= 8 )
	    return cfbCopyArea( pSrc, pDst, pGC, srcx, srcy, width,
			height, dstx, dsty );
	  else if( pSrc->depth <= 32 )
	    return cfb32CopyArea( pSrc, pDst, pGC, srcx, srcy, width,
			height, dstx, dsty );
      }

/*
    PclGetDrawablePrivateStuff( pSrc, &srcGC, &valid, &srcFile );
*/
    PclGetDrawablePrivateStuff( pDst, &dstGC, &valid, &dstFile );

    /*
     * If we're copying to a window, we have to do some actual
     * drawing, instead of just handing it off to mfb or cfb.  Start
     * by determining the region that will be drawn.
     */
    box.x1 = srcx;
    box.y1 = srcy;
    box.x2 = srcx + width;
    box.y2 = srcy + height;
    drawRegion = REGION_CREATE( pGC->pScreen, &box, 0 );
    REGION_TRANSLATE( pGC->pScreen, drawRegion, dstx, dsty );

    region = REGION_CREATE( pGC->pScreen, NULL, 0 );
    REGION_INTERSECT( pGC->pScreen, region, drawRegion, pGC->pCompositeClip );

    /*
     * Now select the operation to be performed on each box in the
     * region.
     */
    if( pSrc->depth == 1 )
      doFragment = PclMonoPixmapFragment;
    else
      doFragment = PclColorPixmapFragment;

    /*
     * Actually draw each section of the bitmap.
     */
    nrect = REGION_NUM_RECTS( region );
    prect = REGION_RECTS( region );

    while( nrect )
      {
	  (*doFragment)( dstFile, (PixmapPtr)pSrc, prect->x1 - dstx,
			prect->y1 - dsty, prect->x2 - dstx,
			prect->y2 - dsty, prect->x1, prect->y1 );

	  nrect--;
	  prect++;
      }

    /*
     * Update the destination's GC to the source's GC.
     */
/*
    PclSetDrawablePrivateGC( pDst, srcGC );
*/

    /*
     * Determine the region that needs to be returned.  This is the
     * region of the source that falls outside the boundary of the
     * pixmap.
     */
    box.x1 = 0;
    box.y1 = 0;
    box.x2 = pixSrc->drawable.width;
    box.y2 = pixSrc->drawable.height;
    whole = REGION_CREATE( pGC->pScreen, &box, 0 );
    ret = REGION_CREATE( pGC->pScreen, NULL, 0 );

    REGION_TRANSLATE( pGC->pScreen, drawRegion, -dstx, -dsty );
    REGION_SUBTRACT( pGC->pScreen, ret, drawRegion, whole );

    /*
     * Clean up the regions
     */
    REGION_DESTROY( pGC->pScreen, drawRegion );
    REGION_DESTROY( pGC->pScreen, region );
    REGION_DESTROY( pGC->pScreen, whole );

    if( REGION_NOTEMPTY( pGC->pScreen, ret ) )
      return ret;
    else
      {
	  REGION_DESTROY( pGC->pScreen, ret );
	  return NULL;
      }
}

RegionPtr
PclCopyPlane(DrawablePtr pSrc,
	     DrawablePtr pDst,
	     GCPtr pGC,
	     int srcx,
	     int srcy,
	     int width,
	     int height,
	     int dstx,
	     int dsty,
	     unsigned long plane)
{
    RegionPtr reg;
    GCPtr scratchGC;
    PixmapPtr scratchPix;

    /*
     * Since we don't store PCL on a per-window basis, there's no good
     * way to copy from a window.
     */
    if( pSrc->type == DRAWABLE_WINDOW )
      return NULL;

    /*
     * Copying from a pixmap to a pixmap is already implemented by
     * mfb/cfb.
     */
    if( pSrc->type == DRAWABLE_PIXMAP &&
       pDst->type == DRAWABLE_PIXMAP )
      {
	  if( pDst->depth == 1 )
	    return mfbCopyPlane( pSrc, pDst, pGC, srcx, srcy, width,
				height, dstx, dsty, plane );
	  else if( pDst->depth <= 8 )
	    return cfbCopyPlane( pSrc, pDst, pGC, srcx, srcy, width,
				height, dstx, dsty, plane );
	  else if( pDst->depth <= 32 )
	    return cfb32CopyPlane( pSrc, pDst, pGC, srcx, srcy, width,
				height, dstx, dsty, plane );
      }

    /*
     * We can use the mfb/cfbCopyPlane function to do the work of grabbing
     * the plane and converting it to the desired visual.  Once that's
     * done, we already know how to do a CopyArea.
     */
    scratchPix = (*pDst->pScreen->CreatePixmap)( pDst->pScreen, width,
						height, pDst->depth );

    scratchGC = GetScratchGC( pDst->depth, pDst->pScreen );
    CopyGC( pGC, scratchGC, ~0L );

    if( pDst->depth == 1 )
      {
	  mfbValidateGC( scratchGC, ~0L, (DrawablePtr)scratchPix );
	  mfbCopyPlane( pSrc, (DrawablePtr)scratchPix, scratchGC,
		       srcx, srcy, width, height, 0, 0, plane );
      }
    else if( pDst->depth <= 8 )
      {
	  cfbValidateGC( scratchGC, ~0L, (DrawablePtr)scratchPix );
	  cfbCopyPlane( pSrc, (DrawablePtr)scratchPix, scratchGC,
		       srcx, srcy, width, height, 0, 0, plane );
      }
    else if( pDst->depth <= 32 )
      {
	  cfb32ValidateGC( scratchGC, ~0L, (DrawablePtr)scratchPix );
	  cfb32CopyPlane( pSrc, (DrawablePtr)scratchPix, scratchGC,
		       srcx, srcy, width, height, 0, 0, plane );
      }

    reg = PclCopyArea( (DrawablePtr)scratchPix, pDst, pGC, 0, 0, width,
		      height, dstx, dsty );

    FreeScratchGC( scratchGC );

    (*pDst->pScreen->DestroyPixmap)( scratchPix );

    return reg;
}