summaryrefslogtreecommitdiff
path: root/contrib/gdevxes.c
blob: 6f2a9d5f5e8f22f151c87348c1bbcbd2b18339f6 (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
/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.  All rights reserved.

This file is part of Ghostscript.

Ghostscript is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY.  No author or distributor accepts responsibility to anyone for
the consequences of using it or for whether it serves any particular purpose
or works at all, unless he says so in writing.  Refer to the GNU General
Public License for full details.

Everyone is granted permission to copy, modify and redistribute Ghostscript,
but only under the conditions described in the GNU General Public License.
A copy of this license is supposed to have been given to you along with
Ghostscript so you can know your rights and responsibilities.  It should be
in a file named COPYING or COPYLEFT.  Among other things, the copyright
notice and this notice must be preserved on all copies.
 */

/*
gdevxes.c
Ghostscript driver for Xerox XES printer
  (2700, 3700, 4045, etc.)

Peter Flass - NYS LBDC <flass@lbdrscs.bitnet>
New York State Legislative Bill Drafting Commission
1450 Western Avenue, 3rd floor
Albany, NY  12203

This code is subject to the GNU General Public License

Operation: The page bitmap is scanned to determined the
actual margins.  A "graphics rectangle" is defined to
contain the included data and positioned on the page.
The bitmap is then re-read and "sixellized" by converting
each three bytes to four six-bit chunks (zero padding on
the right if necessary) and adding x'3F' to generate a
printable code.  Runs of up to 32767 identical characters
are compressed to an ascii count and a single character.

*/

#include "gdevprn.h"

/* Forward references */
static int sixel_print_page(gx_device_printer *pdev,
                             gp_file *prn_stream, const char *init);

/* The device descriptor */
static dev_proc_output_page(sixel_output_page);
static dev_proc_print_page(xes_print_page);

static void
sixel_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_mono(dev);

    set_dev_proc(dev, output_page, sixel_output_page);
}


#ifdef A4
#  define BOTTOM_MARGIN 0.5
#  define PAGE_LENGTH_PELS 3300
#else
#  define BOTTOM_MARGIN 0.4
#  define PAGE_LENGTH_PELS 3300
#endif

gx_device_printer gs_xes_device =
    prn_device(sixel_initialize_device_procs, "xes",
               DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
               300, 300,		/* x_dpi, y_dpi */
               0, BOTTOM_MARGIN, 0, 0,	/* left, bottom, right, top margin */
               1, xes_print_page);

/*
 * Initialization string: switch to graphics mode, 300 dpi
 * <ESC>+X	    Reset	soft terminal reset
 * <ESC>gw1;0,0,		Graphic window
 */

#define XES_RESET \
 "\033+X\n"
#define XES_GRAPHICS \
 "\033gw1;"

static int
xes_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,XES_RESET));
}

/* ------ Internal routines ------ */

/* Open the printer in text mode before gdev_prn_output_page */
/* opens it in binary mode. */
static int
sixel_output_page(gx_device *pdev, int num_copies, int flush)
{	int code = gdev_prn_open_printer(pdev, 0);
        if ( code < 0 )
                return code;
        return gdev_prn_output_page(pdev, num_copies, flush);
}

/* Send the page to the printer. */
static int
sixel_print_page(gx_device_printer *pdev, gp_file *prn_stream, const char *init)
{
    byte *buf, *b, *end;
    byte last = '\0';
    byte tmp[4];
    char run[8], *t;
    int lnum, l, line_size;
    int width, height;
    int top, bottom, left, right;
    int count = 0;

    line_size = gdev_mem_bytes_per_scan_line(pdev);
    height = pdev->height;
    /* Default page rectangle */
    top    = pdev->height;
    left   = line_size;
    right  = 0;
    bottom = 0;

    buf = (byte *)gs_malloc(pdev->memory->non_gc_memory, line_size, 1, "sixel_print_page");
    end = buf + line_size - 1;

    /* Check allocation */
    if (!buf)
      return_error(gs_error_VMerror);

    /* Compute required window size */
    for (lnum=0; lnum < pdev->height; lnum++ ) {
        gdev_prn_copy_scan_lines(pdev, lnum, buf, line_size);
        for( b=buf; b<=end; b++ ) if(*b) break;
        if ( b<=end ) {
          top  = min( top, lnum );
          left = min( left, (int)(b-buf));
          bottom = max( bottom, lnum );
          for( b=end; b>=buf; b-- ) if(*b) break;
          if ( b>=buf ) right = max( right, (int)(b-buf) );
          } /* endif */
        } /* endfor */
        width = right - left + 1;	/* width in bytes */
        height= bottom- top  + 1;	/* height in pels */
        /* round width to multiple of 3 bytes */
        width = ( (width+2) / 3 ) * 3;
        right = min( line_size-1, left+width-1 );
        end = buf + right;		/* recompute EOL  */

    gp_fputs( init, prn_stream );

    /* Position and size graphics window */
    gp_fprintf( prn_stream, "%s%d,%d,%d,%d\n",
             XES_GRAPHICS,
             left*8, PAGE_LENGTH_PELS-top,
             width*8, height );

    /* Print lines of graphics                 */
    for (lnum = top; lnum <= bottom; lnum++ ) {
        gdev_prn_copy_scan_lines(pdev, lnum, buf, line_size);
        for ( b=buf+left; b<=end ; ) {
          /* grab data in 3-byte chunks   */
          /* with zero pad at end-of-line */
          tmp[0]=tmp[1]=tmp[2]='\0';
          tmp[0]=*b++;
          if (b<=end) tmp[1]=*b++;
          if (b<=end) tmp[2]=*b++;
          /* sixellize data */
          tmp[3] = ( tmp[2] & 0x3F) + 0x3F;
          tmp[2] = ( tmp[2] >> 6 |
                    (tmp[1] & 0x0F) << 2 ) + 0x3F;
          tmp[1] = ( tmp[1] >> 4 |
                    (tmp[0] & 0x03) << 4 ) + 0x3F;
          tmp[0] = ( tmp[0] >> 2 ) + 0x3F;
          /* build runs of identical characters */
          /* longest run length is 32767 bytes  */
          for ( l=0; l<4; l++) {
              if ( tmp[l] == last ) {
                count++;
                if (count==32767) {
                  run[gs_snprintf(run, sizeof(run), "%d", count)]='\0';
                  for (t=run; *t; t++) gp_fputc( *t, prn_stream );
                  gp_fputc( last, prn_stream );
                  last = '\0';
                  count = 0;
                  } /* end if count */
                } /* end if tmp[l] */
              else {
                  /* emit single character or run */
                  switch (count) {
                    case 0: break;
                    case 1: gp_fputc( last, prn_stream );
                            break;
                    default:run[gs_snprintf(run, sizeof(run), "%d", count)]='\0';
                            for (t=run; *t; t++) gp_fputc( *t, prn_stream );
                            gp_fputc( last, prn_stream );
                            break;
                    } /* end switch */
                  last = tmp[l];
                  count = 1;
                  } /* end else */
          } /* end for l */
        } /* end for b */
    } /* end for lnum */

    /* Write final run */
    switch (count) {
      case 0: break;
      case 1: gp_fputc( last, prn_stream );
              break;
      default:run[gs_snprintf(run, sizeof(run), "%d", count)]='\0';
              for (t=run; *t; t++) gp_fputc( *t, prn_stream );
              gp_fputc( last, prn_stream );
              break;
      } /* end switch */

    /* Eject page and reset */
    gp_fprintf( prn_stream, "\f%s", XES_RESET );
    gp_fflush(prn_stream);

    gs_free(pdev->memory->non_gc_memory, (char *)buf, line_size, 1, "sixel_print_page");

    return(0);
}