summaryrefslogtreecommitdiff
path: root/devices/gdevimgn.c
blob: e0ee3e5963fa93069a4ccb3b893d516321b4ec37 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/*
 * Imagen ImPRESS printer driver - version 1.4
 *
 * This driver uses the Impress bitmap operation to print the page image.
 */

/* Written by Alan Millar (AMillar@bolis.sf-bay.org) August 4 1992.
      Basic bitmap dump.  */
/* Updated by Alan Millar Sept 21 1992.  Added resolution handling
      for 75, 150, and 300 dpi. */
/* Updated by Alan Millar June 05 1993.  General cleanup for
   beta test release. */
/* Updated by Alan Millar June 21 1993.  v1.3.  Combined multipage
   output into single imPress document.  Quote fewer special
   chars in byte stream mode.  imPress document header options
   can be set from environment variable IMPRESSHEADER */
/* Updated by Alan Millar July 04 1993.  v1.4.
   New makefile option USE_BYTE_STREAM instead of changing source.
   Swatch output redone to eliminate ALL blank swatches (swatchMap).
   Buffer copying changed to multi-byte operations (BIGTYPE).
   Page margins and A4 paper settings fixed, at least for Canon CX.
   */

/* -------------------------------------------------------- */
/* Instructions:

   - Add "imagen.dev" to DEVICE_DEVS in the makefile.  For example:
        DEVICE_DEVS2=laserjet.dev imagen.dev

   - Include or exclude USE_BYTE_STREAM in makefile as appropriate
     If you are compiling on Unix, re-run "tar_cat" to update the makefile
      from devs.mak

   - At run time, specify the resolution on the GS command line
      by using -r300 or -r150 or -r75
   - At run time, specify any imPress document options in
      the IMPRESSHEADER environment variable.
   */

/* -------------------------------------------------------- */
/* Hardware/software combinations tested:
   - ImageStation IP3 8/300 with parallel byte-stream interface,
       using GS 2.6.1 on Linux with GCC 2.3.3;
       earlier using GS 2.5.1 on MS-Dos with Turbo C++ 1.0
   - Sequenced-packet-protocol interface untested.
   */
/* -------------------------------------------------------- */
/* Bugs/Enhancements:
   - Driver does not use any Impress language features for
     drawing lines/arcs
   - Driver does not use resident or downloadable fonts.
   - Buffer output instead of system call for each byte?
  */

/* -------------------------------------------------------- */
#include "gdevprn.h"
#include "gxobj.h"
/* #include <stdio.h> should not be used in drivers */
#include <stdlib.h>

/* -------------------------------------------------------- */
/* Working Constants */

/* Byte stream quoting: convert special characters to hex.
   Specify by including/excluding -DUSE_BYTE_STREAM in makefile.
   This should match printer's hardware interface configuration.
   If printer interface is serial with sequenced-packet-protocol
     spooler software (ImageStation config# 11 = 01), then don't use it.
     Imagen "ipr" spooler software should not use byte stream.
   If printer interface is Centronics parallel byte stream,
     (ImageStation config# 11 = 03), then use byte stream.  */

#ifdef USE_BYTE_STREAM
#  define BYTE_STREAM 1
#else
#  define BYTE_STREAM 0
#endif

/* Byte stream quote character (ImageStation config# 15).
   Only needed when using byte stream */
#define QUOTE_CHAR (char) 0x02
/* Byte stream end-of-file character (ImageStation config# 14). */
#define EOF_CHAR   (char) 0x04
/* Other special characters to quote.  Put them here if spooler or
   hardware uses flow control, etc.   If not needed, set to
   a redundant value such as EOF_CHAR */
#define EXTRA_QUOTE1 (char) 0x11   /* ^Q */
#define EXTRA_QUOTE2 (char) 0x13   /* ^S */
#define EXTRA_QUOTE3 EOF_CHAR
#define EXTRA_QUOTE4 EOF_CHAR

/* -------------------------------------------------------- */
/* imPress header default options.
   Can be overridden at run-time with IMPRESSHEADER env variable */

#define IMPRESSHEADER "jobheader onerror, prerasterization off"

/* -------------------------------------------------------- */

#define CANON_CX

/* Printer engine max resolution.  300 for Canon CX models such as
   ImageStation IP3.  Others (240?) unverified */
#ifdef CANON_CX
#  define MAX_DPI 300
#endif
#ifndef MAX_DPI
#  define MAX_DPI 300
#endif

/* Determine imPress scaling factor from GS resolution.
   Magnify can be 0, 1, or 2.
    0 = MAX_DPI, 1 = MAX_DPI / 2, 2 = MAX_DPI / 4
   Assuming MAX_DPI is 300, you can specify -r75 or -r150
    or -r300 on the GS command line  */
#define getMagnification  ( \
  ( pdev->x_pixels_per_inch > (MAX_DPI >> 1) ) ? 0 : \
  ( pdev->x_pixels_per_inch > (MAX_DPI >> 2) ) ? 1 : \
  2 )

/* Page dimensions from gdevprn.h - specify -DA4 in makefile for A4 paper */
#define WIDTH_10THS   DEFAULT_WIDTH_10THS
#define HEIGHT_10THS  DEFAULT_HEIGHT_10THS

/* Width in inches of unprintable edge of paper.  May need fine tuning.
   Canon CX engine in ImageStation IP3 8/300 will only print 8 inches
   wide on any paper size.  May vary for other engines */

#ifdef CANON_CX
#  define MARG_L 0.15
#  define MARG_R ( (float)WIDTH_10THS / 10.0 - 8.0 - MARG_L)
#endif
#ifndef MARG_L
#  define MARG_L 0.2
#endif
#ifndef MARG_R
#  define MARG_R 0.2
#endif
#define MARG_T 0.1
#define MARG_B 0.2

/* Flag for displaying debug messages at run-time.  Higher
        number = higher detail */
#define IM_DEBUG 0
#define DebugMsg(Level,P1,P2) if (Level<=IM_DEBUG) {errprintf_nomem(P1,P2 );}

/*-------------------------------------------*/
  /* Impress bitmaps are made up of 32x32 bit swatches.
     A swatch is four bytes (32 bits) wide by 32 bytes high,
     totalling 128 bytes.  */
#define HorzBytesPerSw 4
#define HorzBitsPerSw (HorzBytesPerSw * 8)
#define VertBytesPerSw 32
#define TotalBytesPerSw (HorzBytesPerSw * VertBytesPerSw)

/*-------------------------------------------*/
/* Attempt at optimization to something faster than byte-by-byte copying.
   imPress swatches are 4 bytes wide, so type must align on a 4-byte
   boundary.  Swatch interleaving restricts the copy to 4 bytes in a row.
   Type must be numeric where value is zero when all bytes in it are zero. */
#if ARCH_SIZEOF_LONG == 4
#  define BIGTYPE unsigned long int
#else
#  if ARCH_SIZEOF_SHORT == 4
#    define BIGTYPE unsigned short int
#  else
#    if ARCH_SIZEOF_SHORT == 2
#      define BIGTYPE unsigned short
#    endif
#  endif
#endif
#ifndef BIGTYPE
#define BIGTYPE byte
#endif

#define BIGSIZE ( sizeof( BIGTYPE ) )

/*-------------------------------------------*/
/*	IMAGEN imPress Command opcodes					*/
/* from DVIIMP.C */
#define iSP		128	/* advance one space			*/
#define	iSP1		129	/* advance one space + 1 pixel		*/
#define iMPLUS		131	/* Move one pixel forward		*/
#define	iMMINUS		132	/* Move one pixel back			*/
#define iMMOVE		133	/* Move in main advance direction	*/
#define iSMOVE		134	/* Move in secondary advance direction	*/

#define iABS_H		135	/* Move to H position			*/
#define iREL_H		136	/* Move in H direction			*/
#define iABS_V		137	/* Move to V position			*/
#define iREL_V		138	/* Move in V direction			*/

#define	iCRLF		197	/* move to beginning of next line	*/

#define	iSET_HV_SYSTEM	205	/* Define new coordinate system		*/
#define	iSET_ADV_DIRS	206	/* Define advance directions		*/

#define	iPAGE		213	/* Set H and V to 0			*/
#define iENDPAGE	219	/* print the current page		*/

#define iBITMAP		235	/* Print a full bitmap			*/
#define iSET_MAGNIFICATION 236
                                /* magnify the page by 1, 2, 4		*/
#define iNOOP		254	/* no operation				*/
#define iEOF		255	/* end of impress document		*/

/*-------------------------------------------*/
/*-------------------------------------------*/
/* The device descriptor */

static dev_proc_print_page(imagen_print_page);
static dev_proc_open_device(imagen_prn_open);
static dev_proc_close_device(imagen_prn_close);

/* Since the print_page doesn't alter the device, this device can print in the background */
static void
imagen_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_mono_bg(dev);

    set_dev_proc(dev, open_device, imagen_prn_open);
    set_dev_proc(dev, close_device, imagen_prn_close);
}

#define ppdev ((gx_device_printer *)pdev)

/*-------------------------------------------*/
const gx_device_printer far_data gs_imagen_device =
  prn_device(imagen_initialize_device_procs,
        "imagen",
        WIDTH_10THS,
        HEIGHT_10THS,
        MAX_DPI,				/* x_dpi */
        MAX_DPI,				/* y_dpi */
        MARG_L,MARG_R,MARG_T,MARG_B,		/* margins */
        1, imagen_print_page);

/*-------------------------------------------*/

/*-------------------------------------------*/
static void
iWrite(gp_file *Out, byte Val)
{ /* iWrite */
  const char *hexList = "0123456789ABCDEF";

  /* if we are doing byte-stream, quote characters that would otherwise
     match EOF and QUOTE itself, or other special chars */
  /* Imagen quoting takes one character and writes out the QUOTE
     character followed by the hex digits of the quoted character */
  if (BYTE_STREAM &&
     (   Val == QUOTE_CHAR   || Val == EOF_CHAR
      || Val == EXTRA_QUOTE1 || Val == EXTRA_QUOTE2
      || Val == EXTRA_QUOTE3 || Val == EXTRA_QUOTE4 ) ) {
    gp_fputc (QUOTE_CHAR, Out);
    gp_fputc ((char) hexList[Val / 0x10], Out);
    gp_fputc ((char) hexList[Val % 0x10], Out);
  } else { /* quoted char */
    /* Not doing quoting, just send it out */
    gp_fputc(Val, Out);
  } /* quoted char */
} /* iWrite */

/* Write out 16bit, high byte first */
static void
iWrite2(gp_file *Out, int Val)
{ /* iWrite2 */
  iWrite(Out,(byte) (Val >> 8) & 0x00FF );
  iWrite(Out,(byte) Val        & 0x00FF );
} /* iWrite2 */

/* --------------------------------------------------------- */

static int
imagen_prn_open(gx_device *pdev)
{ /* imagen_prn_open */
  int	code;

  const char *impHeader;

  /* ----------------------------------------- */
  DebugMsg(1,"%s\n","Start of imagen_prn_open");
  DebugMsg(2,"BIGSIZE = %ld \n",BIGSIZE);

  code = gdev_prn_open(pdev);
  if ( code < 0 ) return code;

  /* ----------------------------------------- */

  DebugMsg(2,"opening file: %s\n",ppdev->fname);
  code = gdev_prn_open_printer(pdev, 1);
  if ( code < 0 ) return code;

  impHeader = getenv("IMPRESSHEADER");
  if (impHeader == NULL ) {
    impHeader = IMPRESSHEADER ;
  } /* if impHeader */

  gp_fprintf(ppdev->file,"@document(language impress, %s)",impHeader);

  code = gdev_prn_close_printer(pdev);
  if ( code < 0 ) return code;

  /* ----------------------------------------- */
  DebugMsg(1,"%s\n","End of imagen_prn_open");

  return code;
} /* imagen_prn_open */

static int
imagen_prn_close(gx_device *pdev)
{ /* imagen_prn_close */
  int		code;

  /* ----------------------------------------- */
  DebugMsg(1,"%s\n","Start of imagen_prn_close");

  code = gdev_prn_open_printer(pdev, 1);
  if ( code < 0 ) return code;

  /* Write imPress end of document marker */
  iWrite(ppdev->file,iEOF);

  /* And byte stream end of file */
  if (BYTE_STREAM) {
    /* DON'T use iWrite because actual EOF should not be quoted! */
    gp_fputc(EOF_CHAR,ppdev->file);
  } /* if byte stream */

  gp_fflush(ppdev->file);

  code = gdev_prn_close_printer(pdev);
  if ( code < 0 ) return code;

  code = gdev_prn_close(pdev);

  DebugMsg(1,"%s\n","End of imagen_prn_close");

  return(code);
} /* imagen_prn_close */

/*-------------------------------------------*/
/* Send the page to the printer. */
static int
imagen_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
  int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  const int align_size = obj_align_round((line_size/BIGSIZE)+1);
  /* input buffer: one line of bytes rasterized by gs */
  byte *in = (byte *)gs_malloc(pdev->memory, BIGSIZE, align_size, "imagen_print_page(in)");
  /* output buffer: 32 lines, interleaved into imPress swatches */
  byte *out;
  /* working pointer into output buffer */
  byte *swatch;
  byte *temp;
  /* map of which swatches in a row are completely blank, or are non-blank */
  byte *swatchMap;
  /* starting line number on page of a row of swatches */
  int lnum ;
  /* line number within a row of swatches */
  int swatchLine;
  /* ending line number of row of swatches */
  int lastLine;
  /* how many swatches can fit on a row */
  int swatchCount;
  /* index into row of non-blank swatch */
  int startSwatch;
  int endSwatch;
  /* Scaling factor for resolution */
  int Magnify;
  /* page totals */
  int totalBlankSwatches;
  int totalGreySwatches;
  int code = 0;

  /* ----------------------------------------- */
  /* Start of routine                          */
  /* ----------------------------------------- */

  DebugMsg(1,"%s\n","Start of imagen_print_page");

  /* ----------------------------------------- */
  Magnify = getMagnification ;

  /* Impress bitmaps are made up of 32x32 bit swatches.
     A swatch is four bytes wide by 32 bytes high.
     See how many swatches will fit horizontally. */

  swatchCount = (line_size + HorzBytesPerSw - 1) / HorzBytesPerSw;

  totalBlankSwatches = 0 ;
  totalGreySwatches = 0 ;
  DebugMsg(2,"Swatch count = %d\n",swatchCount);
  DebugMsg(2,"Line size = %d\n",line_size );

  out = (byte *)gs_malloc(pdev->memory, TotalBytesPerSw , swatchCount + 1,
                    "imagen_print_page(out)");

  swatchMap = (byte *)gs_malloc(pdev->memory, BIGSIZE,swatchCount / BIGSIZE + 1,
        "imagen_print_page(swatchMap)" );

  if ( in == 0 || out == 0 || swatchMap == 0)
    goto xit;

  /* Initialize the page */
  iWrite(prn_stream,iPAGE);

  /* Tell ImPress what resolution we will be using */
  iWrite(prn_stream,iSET_MAGNIFICATION);
      iWrite(prn_stream,Magnify);

  /*------------------------------------------------------*/
  /* main loop down page */
  lnum = 0;
  while (lnum <= pdev->height) {

    /* erase swatch map.  */
    for (swatch = swatchMap; swatch < swatchMap + swatchCount ;
                swatch += BIGSIZE ) {
      * (BIGTYPE *)swatch = (BIGTYPE) 0;
    } /* for  */

    /* get scan lines to fill swatches */
    swatchLine = 0;
    lastLine = VertBytesPerSw - 1;

    /* Check if we don't have a full-height row of swatches at end of page */
    if (lnum + lastLine > pdev->height ) {
      /* back up last row so it overlaps with previous.  Not a problem
        on a laser printer, because the overlapping part will be identical */
      lnum = pdev->height - lastLine ;
    }; /* not full height */

    DebugMsg (3,"lnum = %d \n",lnum);

    /* ------------------------------------------------------- */
    /* get 32 lines and interleave into a row of swatches */
    for (swatchLine = 0 ; swatchLine <= lastLine; swatchLine++) {

      /* blank out end of buffer for BIGSIZE overlap */
      for (temp = in + line_size; temp < in + align_size*BIGSIZE; temp++){
        *temp = 0;
      } /* for temp */

      /* get one line */
      code = gdev_prn_copy_scan_lines(pdev, lnum + swatchLine, in, line_size);
      if (code < 0)
          goto xit;
      DebugMsg(5,"Got scan line %d ", lnum + swatchLine);
      DebugMsg(5,"line %d \n", swatchLine);

      /* interleave scan line into swatch buffer */
      /* a swatch is a 4 byte * 32 byte square.  Swatches are placed
         next to each other.  The first scan line maps into the first
         four bytes of the first swatch, then the first four of the second
         swatch, etc.
         To get this on the page:
           A1  A1  A1  A1  B1  B1  B1  B1  C1  C1  C1  C1
           A2  A2  A2  A2  B2  B2  B2  B2  C2  C2  C2  C2
           ...
           A32 A32 A32 A32 B32 B32 B32 B32 C32 C32 C32 C32
         You have to send it as:
           A1 A1 A1 A1 A2 ... A32 B1 B1 .. B32 C1 C1 ... C32   */

      /* set initial offset into swatch buffer based on which
         line in the swatch we are processing */
      swatch = out + swatchLine * HorzBytesPerSw;
      DebugMsg(5,"offset: swatch = %d \n",(int) (swatch - out) );
      temp = in;
      while ( temp < in + line_size ) {
        /* copy multi-byte to swatch buffer */
        * (BIGTYPE *)swatch = * (BIGTYPE *)temp;
        if ( * (BIGTYPE *)temp ) {
          /* mark map if not blank */
          swatchMap[(swatch - out)/TotalBytesPerSw] = (byte) 1 ;
        } /* if not zero */

        temp   += (BIGSIZE > HorzBytesPerSw) ? HorzBytesPerSw : BIGSIZE ;
        swatch += (BIGSIZE > HorzBytesPerSw) ? HorzBytesPerSw : BIGSIZE ;

        /* if we copied four bytes, skip to next swatch */
        if ( ((temp - in) % HorzBytesPerSw ) == 0 ) {
          swatch += (TotalBytesPerSw - HorzBytesPerSw) ;
        } /* if need to skip */
      } /* while < line_size */

    } /* for swatchLine */

    /* ------------------------------------------------- */
    /* we now have full swatches. */
    /* Send to printer */

    /* go through swatch map to find non-blank swatches.
       Skip over completely blank swatches */
    startSwatch = 0;
    while (startSwatch < swatchCount ) {
      if (swatchMap[startSwatch] == 0 ) {
        /* skip blank swatch */
        DebugMsg(6,"Skip blank %d \n",startSwatch);
        totalBlankSwatches++;
        startSwatch++;
      } else { /* if swatch == 0 */
        /* we hit a non-blank swatch. */
        totalGreySwatches++;

        /* See how many there are in a row */
        endSwatch = startSwatch;
        while ( (endSwatch < swatchCount) && swatchMap[endSwatch] ) {
          endSwatch++;
          totalGreySwatches++;
        } /* while */
        /* endSwatch is one past last non-blank swatch */
        DebugMsg(6,"Grey swatches %d ",startSwatch);
        DebugMsg(6,"until %d \n",endSwatch);

        /* vertical position: scan line, shifted for magnification */
        iWrite(prn_stream, iABS_V);
          iWrite2(prn_stream, lnum << Magnify);

        /* horizontal position = swatch number * 32 bits/swatch */
        iWrite(prn_stream,iABS_H);
           iWrite2(prn_stream, startSwatch * HorzBitsPerSw << Magnify );
        iWrite(prn_stream,iBITMAP);  /* start bitmap */
          iWrite(prn_stream,0x07);     /* bit OR with page */
          iWrite(prn_stream,(endSwatch - startSwatch)); /* horizontal
                number of swatches */
          iWrite(prn_stream, 1) ; /* vertical number of swatches */
        /* write out swatch buffer */
        for (swatch = out + startSwatch * TotalBytesPerSw;
                swatch < out + endSwatch * TotalBytesPerSw; swatch++) {
          iWrite(prn_stream,*swatch);
        } /* for swatch */

        /* swatches have been printed, see if there are still
           more in this row */
        startSwatch = endSwatch;
      } /* if swatch == 0 */

    } /* while startSwatch */

    /* Whole row of swatches is done.  Go on to next row of swatches */
    lnum += lastLine + 1;

  } /* while lnum */

  /* Eject the page */
  iWrite(prn_stream,iENDPAGE);

  gp_fflush(prn_stream);

xit:
  gs_free(pdev->memory, (char *)out, TotalBytesPerSw, swatchCount+1, "imagen_print_page(out)");
  gs_free(pdev->memory, (char *)swatchMap, BIGSIZE, swatchCount / BIGSIZE + 1,
        "imagen_print_page(swatchMap)" );
  gs_free(pdev->memory, (char *)in, BIGSIZE, align_size, "imagen_print_page(in)");
  /* ----------------------------------------- */

  DebugMsg(1,"Debug: Grey: %d \n",totalGreySwatches);
  DebugMsg(1,"Debug: Blank: %d \n",totalBlankSwatches );
  DebugMsg(1,"%s\n","End of imagen_print_page");

  /* ----------------------------------------- */
  return code;

} /* imagen_print_page */