summaryrefslogtreecommitdiff
path: root/devices/gdevccr.c
blob: 439e9f6223aea42209cbcac2e15c239082e3fb70 (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
/* 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.
*/

/* CalComp Raster Format driver */
#include "gdevprn.h"

/*
 * Please contact the author, Ernst Muellner (ernst.muellner@oenzl.siemens.de),
 * if you have any questions about this driver.
 */

#define CCFILESTART(p) gp_fputc(0x02, p)
#define CCFILEEND(p) gp_fputc(0x04, p)
#define CCNEWPASS(p) gp_fputc(0x0c, p)
#define CCEMPTYLINE(p) gp_fputc(0x0a, p)
#define CCLINESTART(len,p) do{ gp_fputc(0x1b,p);gp_fputc(0x4b,p);gp_fputc(len>>8,p); \
                               gp_fputc(len&0xff,p);} while(0)

#define CPASS (0)
#define MPASS (1)
#define YPASS (2)
#define NPASS (3)

typedef struct cmyrow_s
          {
            int current;
            int _cmylen[NPASS];
            int is_used;
            char cname[4];
            char mname[4];
            char yname[4];
            unsigned char *_cmybuf[NPASS];
          } cmyrow;

#define clen _cmylen[CPASS]
#define mlen _cmylen[MPASS]
#define ylen _cmylen[YPASS]
#define cmylen _cmylen

#define cbuf _cmybuf[CPASS]
#define mbuf _cmybuf[MPASS]
#define ybuf _cmybuf[YPASS]
#define cmybuf _cmybuf

static int alloc_rb( gs_memory_t *mem, cmyrow **rb, int rows);
static int alloc_line( gs_memory_t *mem, cmyrow *row, int cols);
static void add_cmy8(cmyrow *rb, char c, char m, char y);
static void write_cpass(cmyrow *buf, int rows, int pass, gp_file * pstream);
static void free_rb_line( gs_memory_t *mem, cmyrow *rbuf, int rows, int cols);

struct gx_device_ccr_s {
        gx_device_common;
        gx_prn_device_common;
        /* optional parameters */
};
typedef struct gx_device_ccr_s gx_device_ccr;

#define bdev ((gx_device_ccr *)pdev)

/* ------ The device descriptors ------ */

/*
 * Default X and Y resolution.
 */
#define X_DPI 300
#define Y_DPI 300
#define DEFAULT_WIDTH_10THS_A3 117
#define DEFAULT_HEIGHT_10THS_A3 165

/* Macro for generating ccr device descriptors. */
#define ccr_prn_device(init, dev_name, margin, num_comp, depth, max_gray, max_rgb, print_page)\
{	prn_device_body(gx_device_ccr, init, dev_name,\
          DEFAULT_WIDTH_10THS_A3, DEFAULT_HEIGHT_10THS_A3, X_DPI, Y_DPI,\
          margin, margin, margin, margin,\
          num_comp, depth, max_gray, max_rgb, max_gray + 1, max_rgb + 1,\
          print_page)\
}

/* For CCR, we need our own color mapping procedures. */
static dev_proc_map_rgb_color(ccr_map_rgb_color);
static dev_proc_map_color_rgb(ccr_map_color_rgb);

/* And of course we need our own print-page routine. */
static dev_proc_print_page(ccr_print_page);

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

    set_dev_proc(dev, map_rgb_color, ccr_map_rgb_color);
    set_dev_proc(dev, map_color_rgb, ccr_map_color_rgb);
    set_dev_proc(dev, encode_color, ccr_map_rgb_color);
    set_dev_proc(dev, decode_color, ccr_map_color_rgb);
}

/* The device descriptors themselves */
gx_device_ccr far_data gs_ccr_device =
  ccr_prn_device(ccr_initialize_device_procs, "ccr", 0.2, 3, 8, 1, 1,
                 ccr_print_page);

/* ------ Color mapping routines ------ */
/* map an rgb color to a ccr cmy bitmap */
static gx_color_index
ccr_map_rgb_color(gx_device *pdev, const ushort cv[])
{
  ushort r, g, b;
  register int shift = gx_color_value_bits - 1;

  r = cv[0]; g = cv[1]; b = cv[2];
  r>>=shift;
  g>>=shift;
  b>>=shift;

  r=1-r; g=1-g; b=1-b; /* rgb -> cmy */
  return r<<2 | g<<1 | b;
}

/* map an ccr cmy bitmap to a rgb color */
static int
ccr_map_color_rgb(gx_device *pdev, gx_color_index color, ushort rgb[3])
{
  rgb[2]=(1-(color >>2))*gx_max_color_value; /* r */
  rgb[1]=(1-( (color & 0x2) >> 1))*gx_max_color_value; /* g */
  rgb[0]=(1-(color & 0x1))*gx_max_color_value; /* b */
  return 0;
}
/* ------ print page routine ------ */

static int
ccr_print_page(gx_device_printer *pdev, gp_file *pstream)
{
  cmyrow *linebuf;
  int line_size = gdev_prn_raster((gx_device *)pdev);
  int pixnum = pdev->width;
  int lnum = pdev->height;
  int l, p, b;
  int cmy, c, m, y;
  byte *in;
  byte *data;
  int code = 0;

  if((in = (byte *)gs_malloc(pdev->memory, line_size, 1, "gsline")) == NULL)
     return_error(gs_error_VMerror);

  if(alloc_rb( pdev->memory, &linebuf, lnum))
    {
      gs_free(pdev->memory, in, line_size, 1, "gsline");
      return_error(gs_error_VMerror);
    }

  for ( l = 0; l < lnum; l++ )
     {  code = gdev_prn_get_bits(pdev, l, in, &data);
        if (code < 0)
            goto xit;
        if(alloc_line(pdev->memory, &linebuf[l], pixnum))
          {
            gs_free(pdev->memory, in, line_size, 1, "gsline");
            free_rb_line( pdev->memory, linebuf, lnum, pixnum );
            return_error(gs_error_VMerror);
          }
        for ( p=0; p< pixnum; p+=8)
          {
            c=m=y=0;
            for(b=0; b<8; b++)
            {
              c <<= 1; m <<= 1; y <<= 1;
              if(p+b < pixnum)
                cmy = *data;
              else
                cmy = 0;

              c |= cmy>>2;
              m |= (cmy>>1) & 0x1;
              y |= cmy & 0x1;
              data++;
            }
            add_cmy8(&linebuf[l], c, m, y);
          }
      }
CCFILESTART(pstream);
write_cpass(linebuf, lnum, YPASS, pstream);
CCNEWPASS(pstream);
write_cpass(linebuf, lnum, MPASS, pstream);
CCNEWPASS(pstream);
write_cpass(linebuf, lnum, CPASS, pstream);
CCFILEEND(pstream);

/* clean up */
xit:
gs_free(pdev->memory, in, line_size, 1, "gsline");
free_rb_line( pdev->memory, linebuf, lnum, pixnum );
return code;
}

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

static int alloc_rb( gs_memory_t *mem, cmyrow **rb, int rows)
  {
  *rb = (cmyrow*) gs_malloc(mem, rows, sizeof(cmyrow), "rb");
  if( *rb == 0)
    return_error(gs_error_VMerror);
  else
    {
      int r;
      for(r=0; r<rows; r++)
        {
          gs_snprintf((*rb)[r].cname, sizeof((*rb)[r].cname), "C%02x", r);
          gs_snprintf((*rb)[r].mname, sizeof((*rb)[r].mname), "M%02x", r);
          gs_snprintf((*rb)[r].yname, sizeof((*rb)[r].yname), "Y%02x", r);
          (*rb)[r].is_used=0;
        }
      return 0;
    }
}

static int alloc_line( gs_memory_t *mem, cmyrow *row, int cols)
{
  int suc;
  suc=((row->cbuf = (unsigned char *) gs_malloc(mem, cols,1, row->cname)) &&
       (row->mbuf = (unsigned char *) gs_malloc(mem, cols,1, row->mname)) &&
       (row->ybuf = (unsigned char *) gs_malloc(mem, cols,1, row->yname)));
  if(suc == 0)
       {
       gs_free(mem, row->cbuf, cols,1, row->cname);
       gs_free(mem, row->mbuf, cols,1, row->mname);
       gs_free(mem, row->ybuf, cols,1, row->yname);

       return_error(gs_error_VMerror);
     }
  row->is_used = 1;
  row->current = row->clen = row->mlen = row->ylen = 0;
  return 0;
}

static void add_cmy8(cmyrow *rb, char c, char m, char y)
{
  int cur=rb->current;
  rb->cbuf[cur]=c;
  if(c)
    rb->clen=cur+1;
  rb->mbuf[cur]=m;
  if(m)
    rb->mlen=cur+1;
  rb->ybuf[cur]=y;
  if(y)
    rb->ylen=cur+1;
  rb->current++;
  return;
}

static void write_cpass(cmyrow *buf, int rows, int pass, gp_file * pstream)
{
  int row, len;
    for(row=0; row<rows; row++)
      {
      len=buf[row].cmylen[pass];
      if(len == 0)
        CCEMPTYLINE(pstream);
      else
        {
          CCLINESTART(len,pstream);
          gp_fwrite( buf[row].cmybuf[pass], len, 1, pstream);
        }
    }
  return;
}

static void free_rb_line( gs_memory_t *mem, cmyrow *rbuf, int rows, int cols)
{
  int i;
  for(i=0; i<rows; i++)
    {
      if(rbuf[i].is_used)
        {
          gs_free(mem, rbuf[i].cbuf, cols, 1, rbuf[i].cname);
          gs_free(mem, rbuf[i].mbuf, cols, 1, rbuf[i].mname);
          gs_free(mem, rbuf[i].ybuf, cols, 1, rbuf[i].yname);
          rbuf[i].is_used = 0;
        }
      else
        break;
    }
  gs_free( mem, rbuf, rows, sizeof(cmyrow),  "rb");
  return;
}