~ubuntu-branches/ubuntu/oneiric/ghostscript/oneiric

« back to all changes in this revision

Viewing changes to base/gdevmgr.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2011-07-15 16:49:55 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20110715164955-uga6qibao6kez05c
Tags: 9.04~dfsg~20110715-0ubuntu1
* New upstream release
   - GIT snapshot from Jult, 12 2011.
* debian/patches/020110406~a54df2d.patch,
  debian/patches/020110408~0791cc8.patch,
  debian/patches/020110408~507cbee.patch,
  debian/patches/020110411~4509a49.patch,
  debian/patches/020110412~78bb9a6.patch,
  debian/patches/020110418~a05ab8a.patch,
  debian/patches/020110420~20b6c78.patch,
  debian/patches/020110420~4ddefa2.patch: Removed upstream patches.
* debian/rules: Generate ABI version number (variable "abi") correctly,
  cutting off repackaging and pre-release parts.
* debian/rules: Added ./lcms2/ directory to DEB_UPSTREAM_REPACKAGE_EXCLUDES.
* debian/copyright: Added lcms2/* to the list of excluded files.
* debian/symbols.common: Updated for new upstream source. Applied patch
  which dpkg-gensymbols generated for debian/libgs9.symbols to this file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11
11
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12
12
*/
13
 
/* $Id: gdevmgr.c 11039 2010-04-08 15:22:28Z robin $*/
 
13
/* $Id$*/
14
14
/* MGR device driver */
15
15
#include "gdevprn.h"
16
16
#include "gdevpccm.h"
18
18
 
19
19
/* Structure for MGR devices, which extend the generic printer device. */
20
20
struct gx_device_mgr_s {
21
 
        gx_device_common;
22
 
        gx_prn_device_common;
23
 
        /* Add MGR specific variables */
24
 
        int mgr_depth;
25
 
        /* globals for greymapped printing */
 
21
        gx_device_common;
 
22
        gx_prn_device_common;
 
23
        /* Add MGR specific variables */
 
24
        int mgr_depth;
 
25
        /* globals for greymapped printing */
26
26
        unsigned char bgreytable[16];
27
27
        unsigned char bgreybacktable[16];
28
28
        unsigned char bgrey256table[256];
44
44
 
45
45
/* Macro for generating MGR device descriptors. */
46
46
#define mgr_prn_device(procs, dev_name, num_comp, depth, mgr_depth,\
47
 
        max_gray, max_rgb, dither_gray, dither_rgb, print_page)\
 
47
        max_gray, max_rgb, dither_gray, dither_rgb, print_page)\
48
48
{       prn_device_body(gx_device_mgr, procs, dev_name,\
49
 
          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, X_DPI, Y_DPI,\
50
 
          0, 0, 0, 0,\
51
 
          num_comp, depth, max_gray, max_rgb, dither_gray, dither_rgb,\
52
 
          print_page),\
53
 
          mgr_depth\
 
49
          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, X_DPI, Y_DPI,\
 
50
          0, 0, 0, 0,\
 
51
          num_comp, depth, max_gray, max_rgb, dither_gray, dither_rgb,\
 
52
          print_page),\
 
53
          mgr_depth\
54
54
}
55
55
 
56
56
/* For all mgr variants we do some extra things at opening time. */
67
67
    prn_procs(gdev_mgr_open, gdev_prn_output_page, gdev_prn_close);
68
68
static gx_device_procs mgrN_procs =
69
69
    prn_color_procs(gdev_mgr_open, gdev_prn_output_page, gdev_prn_close,
70
 
        gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
 
70
        gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
71
71
static gx_device_procs cmgr4_procs =
72
72
    prn_color_procs(gdev_mgr_open, gdev_prn_output_page, gdev_prn_close,
73
 
        pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
 
73
        pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
74
74
static gx_device_procs cmgr8_procs =
75
75
    prn_color_procs(gdev_mgr_open, gdev_prn_output_page, gdev_prn_close,
76
 
        mgr_8bit_map_rgb_color, mgr_8bit_map_color_rgb);
 
76
        mgr_8bit_map_rgb_color, mgr_8bit_map_color_rgb);
77
77
 
78
78
/* The device descriptors themselves */
79
79
gx_device_mgr far_data gs_mgrmono_device =
93
93
 
94
94
/* Define a "cursor" that keeps track of where we are in the page. */
95
95
typedef struct mgr_cursor_s {
96
 
        gx_device_mgr *dev;
97
 
        int bpp;                        /* bits per pixel */
98
 
        uint line_size;                 /* bytes per scan line */
99
 
        byte *data;                     /* output row buffer */
100
 
        int lnum;                       /* row within page */
 
96
        gx_device_mgr *dev;
 
97
        int bpp;                        /* bits per pixel */
 
98
        uint line_size;                 /* bytes per scan line */
 
99
        byte *data;                     /* output row buffer */
 
100
        int lnum;                       /* row within page */
101
101
} mgr_cursor;
102
102
 
103
103
/* Begin an MGR output page. */
105
105
static int
106
106
mgr_begin_page(gx_device_mgr *bdev, FILE *pstream, mgr_cursor *pcur)
107
107
{       struct b_header head;
108
 
        uint line_size =
109
 
                gdev_prn_raster((gx_device_printer *)bdev) + 3;
110
 
        byte *data = (byte *)gs_malloc(bdev->memory, line_size, 1, "mgr_begin_page");
111
 
        if ( data == 0 )
112
 
                return_error(gs_error_VMerror);
113
 
 
114
 
        /* Write the header */
115
 
        B_PUTHDR8(&head, bdev->width, bdev->height, bdev->mgr_depth);
116
 
        fprintf(pstream, "");
117
 
        if ( fwrite(&head, 1, sizeof(head), pstream) < sizeof(head) )
118
 
                return_error(gs_error_ioerror);
119
 
        fflush(pstream);
120
 
 
121
 
        /* Initialize the cursor. */
122
 
        pcur->dev = bdev;
123
 
        pcur->bpp = bdev->color_info.depth;
124
 
        pcur->line_size = line_size;
125
 
        pcur->data = data;
126
 
        pcur->lnum = 0;
127
 
        return 0;
 
108
        uint line_size =
 
109
                gdev_prn_raster((gx_device_printer *)bdev) + 3;
 
110
        byte *data = (byte *)gs_malloc(bdev->memory, line_size, 1, "mgr_begin_page");
 
111
        if ( data == 0 )
 
112
                return_error(gs_error_VMerror);
 
113
 
 
114
        /* Write the header */
 
115
        B_PUTHDR8(&head, bdev->width, bdev->height, bdev->mgr_depth);
 
116
        fprintf(pstream, "");
 
117
        if ( fwrite(&head, 1, sizeof(head), pstream) < sizeof(head) )
 
118
                return_error(gs_error_ioerror);
 
119
        fflush(pstream);
 
120
 
 
121
        /* Initialize the cursor. */
 
122
        pcur->dev = bdev;
 
123
        pcur->bpp = bdev->color_info.depth;
 
124
        pcur->line_size = line_size;
 
125
        pcur->data = data;
 
126
        pcur->lnum = 0;
 
127
        return 0;
128
128
}
129
129
 
130
130
/* Advance to the next row.  Return 0 if more, 1 if done. */
131
131
static int
132
132
mgr_next_row(mgr_cursor *pcur)
133
133
{       if ( pcur->lnum >= pcur->dev->height )
134
 
        {       gs_free(((gx_device_printer *)pcur->dev)->memory,
135
 
                        (char *)pcur->data, pcur->line_size, 1,
136
 
                        "mgr_next_row(done)");
137
 
                return 1;
138
 
           }
139
 
        gdev_prn_copy_scan_lines((gx_device_printer *)pcur->dev,
140
 
                                 pcur->lnum++, pcur->data, pcur->line_size);
141
 
        return 0;
 
134
        {       gs_free(((gx_device_printer *)pcur->dev)->memory,
 
135
                        (char *)pcur->data, pcur->line_size, 1,
 
136
                        "mgr_next_row(done)");
 
137
                return 1;
 
138
           }
 
139
        gdev_prn_copy_scan_lines((gx_device_printer *)pcur->dev,
 
140
                                 pcur->lnum++, pcur->data, pcur->line_size);
 
141
        return 0;
142
142
}
143
143
 
144
144
/* ------ Individual page printing routines ------ */
149
149
static int
150
150
mgr_print_page(gx_device_printer *pdev, FILE *pstream)
151
151
{       mgr_cursor cur;
152
 
        int mgr_wide;
153
 
        int code = mgr_begin_page(bdev, pstream, &cur);
154
 
        if ( code < 0 ) return code;
155
 
 
156
 
        mgr_wide = bdev->width;
157
 
        if (mgr_wide & 7)
158
 
           mgr_wide += 8 - (mgr_wide & 7);
159
 
 
160
 
        while ( !(code = mgr_next_row(&cur)) )
161
 
           {    if ( fwrite(cur.data, sizeof(char), mgr_wide / 8, pstream) <
 
152
        int mgr_wide;
 
153
        int code = mgr_begin_page(bdev, pstream, &cur);
 
154
        if ( code < 0 ) return code;
 
155
 
 
156
        mgr_wide = bdev->width;
 
157
        if (mgr_wide & 7)
 
158
           mgr_wide += 8 - (mgr_wide & 7);
 
159
 
 
160
        while ( !(code = mgr_next_row(&cur)) )
 
161
           {    if ( fwrite(cur.data, sizeof(char), mgr_wide / 8, pstream) <
162
162
                    mgr_wide / 8)
163
 
                return_error(gs_error_ioerror);
164
 
           }
165
 
        return (code < 0 ? code : 0);
 
163
                return_error(gs_error_ioerror);
 
164
           }
 
165
        return (code < 0 ? code : 0);
166
166
}
167
167
 
168
 
 
169
168
/* Print a gray-mapped page. */
170
169
static int
171
170
mgrN_print_page(gx_device_printer *pdev, FILE *pstream)
172
171
{       mgr_cursor cur;
173
 
        int i = 0, j, k, mgr_wide;
174
 
        uint mgr_line_size;
175
 
        byte *bp, *data = NULL, *dp;
176
 
        gx_device_mgr *mgr = (gx_device_mgr *)pdev;
177
 
 
178
 
        int code = mgr_begin_page(bdev, pstream, &cur);
179
 
        if ( code < 0 ) return code;
180
 
 
181
 
        mgr_wide = bdev->width;
182
 
        if ( bdev->mgr_depth == 2 && mgr_wide & 3 )
 
172
        int i = 0, j, k, mgr_wide;
 
173
        uint mgr_line_size;
 
174
        byte *bp, *data = NULL, *dp;
 
175
        gx_device_mgr *mgr = (gx_device_mgr *)pdev;
 
176
 
 
177
        int code = mgr_begin_page(bdev, pstream, &cur);
 
178
        if ( code < 0 ) return code;
 
179
 
 
180
        mgr_wide = bdev->width;
 
181
        if ( bdev->mgr_depth == 2 && mgr_wide & 3 )
183
182
            mgr_wide += 4 - (mgr_wide & 3);
184
 
        if ( bdev->mgr_depth == 4 && mgr_wide & 1 )
 
183
        if ( bdev->mgr_depth == 4 && mgr_wide & 1 )
185
184
            mgr_wide++;
186
 
        mgr_line_size = mgr_wide / ( 8 / bdev->mgr_depth );
 
185
        mgr_line_size = mgr_wide / ( 8 / bdev->mgr_depth );
187
186
 
188
 
        if ( bdev->mgr_depth == 4 )
 
187
        if ( bdev->mgr_depth == 4 )
189
188
            for ( i = 0; i < 16; i++ ) {
190
 
                mgr->bgreytable[i] = mgrlut[LUT_BGREY][RGB_RED][i];
191
 
                mgr->bgreybacktable[mgr->bgreytable[i]] = i;
 
189
                mgr->bgreytable[i] = mgrlut[LUT_BGREY][RGB_RED][i];
 
190
                mgr->bgreybacktable[mgr->bgreytable[i]] = i;
192
191
            }
193
192
 
194
 
        if ( bdev->mgr_depth == 8 ) {
 
193
        if ( bdev->mgr_depth == 8 ) {
195
194
            for ( i = 0; i < 16; i++ ) {
196
 
                mgr->bgrey256table[i] = mgrlut[LUT_BGREY][RGB_RED][i] << 4;
197
 
                mgr->bgrey256backtable[mgr->bgrey256table[i]] = i;
 
195
                mgr->bgrey256table[i] = mgrlut[LUT_BGREY][RGB_RED][i] << 4;
 
196
                mgr->bgrey256backtable[mgr->bgrey256table[i]] = i;
198
197
            }
199
198
            for ( i = 16,j = 0; i < 256; i++ ) {
200
 
                for ( k = 0; k < 16; k++ )
 
199
                for ( k = 0; k < 16; k++ )
201
200
                  if ( j == mgrlut[LUT_BGREY][RGB_RED][k] << 4 ) {
202
201
                    j++;
203
202
                    break;
204
203
                  }
205
 
                mgr->bgrey256table[i] = j;
206
 
                mgr->bgrey256backtable[j++] = i;
 
204
                mgr->bgrey256table[i] = j;
 
205
                mgr->bgrey256backtable[j++] = i;
207
206
            }
208
 
        }
209
 
 
210
 
        if ( bdev->mgr_depth != 8 )
211
 
            data = (byte *)gs_malloc(pdev->memory, mgr_line_size, 1, "mgrN_print_page");
212
 
 
213
 
        while ( !(code = mgr_next_row(&cur)) )
214
 
           {
215
 
                switch (bdev->mgr_depth) {
216
 
                        case 2:
217
 
                                for (i = 0,dp = data,bp = cur.data; i < mgr_line_size; i++) {
218
 
                                        *dp =   *(bp++) & 0xc0;
219
 
                                        *dp |= (*(bp++) & 0xc0) >> 2;
220
 
                                        *dp |= (*(bp++) & 0xc0) >> 4;
 
207
        }
 
208
 
 
209
        if ( bdev->mgr_depth != 8 )
 
210
            data = (byte *)gs_malloc(pdev->memory, mgr_line_size, 1, "mgrN_print_page");
 
211
 
 
212
        while ( !(code = mgr_next_row(&cur)) )
 
213
           {
 
214
                switch (bdev->mgr_depth) {
 
215
                        case 2:
 
216
                                for (i = 0,dp = data,bp = cur.data; i < mgr_line_size; i++) {
 
217
                                        *dp =   *(bp++) & 0xc0;
 
218
                                        *dp |= (*(bp++) & 0xc0) >> 2;
 
219
                                        *dp |= (*(bp++) & 0xc0) >> 4;
221
220
                                    *(dp++) |= (*(bp++) & 0xc0) >> 6;
222
 
                                }
223
 
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
224
 
                                        return_error(gs_error_ioerror);
225
 
                                break;
 
221
                                }
 
222
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
 
223
                                        return_error(gs_error_ioerror);
 
224
                                break;
226
225
 
227
 
                        case 4:
228
 
                                for (i = 0,dp = data, bp = cur.data; i < mgr_line_size; i++) {
229
 
                                        *dp =  mgr->bgreybacktable[*(bp++) >> 4] << 4;
 
226
                        case 4:
 
227
                                for (i = 0,dp = data, bp = cur.data; i < mgr_line_size; i++) {
 
228
                                        *dp =  mgr->bgreybacktable[*(bp++) >> 4] << 4;
230
229
                                    *(dp++) |= mgr->bgreybacktable[*(bp++) >> 4];
231
 
                                }
232
 
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
233
 
                                        return_error(gs_error_ioerror);
234
 
                                break;
235
 
 
236
 
                        case 8:
237
 
                                for (i = 0,bp = cur.data; i < mgr_line_size; i++, bp++)
238
 
                                      *bp = mgr->bgrey256backtable[*bp];
239
 
                                if ( fwrite(cur.data, sizeof(cur.data[0]), mgr_line_size, pstream)
240
 
                                        < mgr_line_size )
241
 
                                        return_error(gs_error_ioerror);
242
 
                                break;
243
 
                }
244
 
           }
245
 
        if (bdev->mgr_depth != 8)
246
 
            gs_free(bdev->memory, (char *)data, mgr_line_size, 1, "mgrN_print_page(done)");
247
 
 
248
 
        if (bdev->mgr_depth == 2) {
 
230
                                }
 
231
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
 
232
                                        return_error(gs_error_ioerror);
 
233
                                break;
 
234
 
 
235
                        case 8:
 
236
                                for (i = 0,bp = cur.data; i < mgr_line_size; i++, bp++)
 
237
                                      *bp = mgr->bgrey256backtable[*bp];
 
238
                                if ( fwrite(cur.data, sizeof(cur.data[0]), mgr_line_size, pstream)
 
239
                                        < mgr_line_size )
 
240
                                        return_error(gs_error_ioerror);
 
241
                                break;
 
242
                }
 
243
           }
 
244
        if (bdev->mgr_depth != 8)
 
245
            gs_free(bdev->memory, (char *)data, mgr_line_size, 1, "mgrN_print_page(done)");
 
246
 
 
247
        if (bdev->mgr_depth == 2) {
249
248
            for (i = 0; i < 4; i++) {
250
249
               mgr->clut[i].colnum = i;
251
250
               mgr->clut[i].red    = mgr->clut[i].green = mgr->clut[i].blue = clut2mgr(i, 2);
252
 
            }
253
 
        }
254
 
        if (bdev->mgr_depth == 4) {
 
251
            }
 
252
        }
 
253
        if (bdev->mgr_depth == 4) {
255
254
            for (i = 0; i < 16; i++) {
256
255
               mgr->clut[i].colnum = i;
257
256
               mgr->clut[i].red    = mgr->clut[i].green = mgr->clut[i].blue = clut2mgr(mgr->bgreytable[i], 4);
258
 
            }
259
 
        }
260
 
        if (bdev->mgr_depth == 8) {
 
257
            }
 
258
        }
 
259
        if (bdev->mgr_depth == 8) {
261
260
            for (i = 0; i < 256; i++) {
262
261
               mgr->clut[i].colnum = i;
263
262
               mgr->clut[i].red    = mgr->clut[i].green = mgr->clut[i].blue = clut2mgr(mgr->bgrey256table[i], 8);
264
 
            }
265
 
        }
 
263
            }
 
264
        }
266
265
#if !arch_is_big_endian
267
 
        swap_bwords( (unsigned char *) mgr->clut, sizeof( struct nclut ) * i );
 
266
        swap_bwords( (unsigned char *) mgr->clut, sizeof( struct nclut ) * i );
268
267
#endif
269
 
        if ( fwrite(&mgr->clut, sizeof(struct nclut), i, pstream) < i )
 
268
        if ( fwrite(&mgr->clut, sizeof(struct nclut), i, pstream) < i )
270
269
            return_error(gs_error_ioerror);
271
 
        return (code < 0 ? code : 0);
 
270
        return (code < 0 ? code : 0);
272
271
}
273
272
 
274
273
/* Print a color page. */
275
274
static int
276
275
cmgrN_print_page(gx_device_printer *pdev, FILE *pstream)
277
276
{       mgr_cursor cur;
278
 
        int i = 0, j, mgr_wide, r, g, b, colors8 = 0;
279
 
        uint mgr_line_size;
280
 
        byte *bp, *data, *dp;
281
 
        ushort prgb[3];
282
 
        unsigned char table[256], backtable[256];
283
 
        gx_device_mgr *mgr = (gx_device_mgr *)pdev;
284
 
 
285
 
        int code = mgr_begin_page(bdev, pstream, &cur);
286
 
        if ( code < 0 ) return code;
287
 
 
288
 
        mgr_wide = bdev->width;
289
 
        if (bdev->mgr_depth == 4 && mgr_wide & 1)
 
277
        int i = 0, j, mgr_wide, r, g, b, colors8 = 0;
 
278
        uint mgr_line_size;
 
279
        byte *bp, *data, *dp;
 
280
        ushort prgb[3];
 
281
        unsigned char table[256], backtable[256];
 
282
        gx_device_mgr *mgr = (gx_device_mgr *)pdev;
 
283
 
 
284
        int code = mgr_begin_page(bdev, pstream, &cur);
 
285
        if ( code < 0 ) return code;
 
286
 
 
287
        mgr_wide = bdev->width;
 
288
        if (bdev->mgr_depth == 4 && mgr_wide & 1)
290
289
            mgr_wide++;
291
 
        mgr_line_size = mgr_wide / (8 / bdev->mgr_depth);
292
 
        data = (byte *)gs_malloc(pdev->memory, mgr_line_size, 1, "cmgrN_print_page");
 
290
        mgr_line_size = mgr_wide / (8 / bdev->mgr_depth);
 
291
        data = (byte *)gs_malloc(pdev->memory, mgr_line_size, 1, "cmgrN_print_page");
293
292
 
294
 
        if ( bdev->mgr_depth == 8 ) {
 
293
        if ( bdev->mgr_depth == 8 ) {
295
294
            memset( table, 0, sizeof(table) );
296
295
            for ( r = 0; r <= 6; r++ )
297
 
                for ( g = 0; g <= 6; g++ )
298
 
                    for ( b = 0; b <= 6; b++ )
299
 
                        if ( r == g && g == b )
 
296
                for ( g = 0; g <= 6; g++ )
 
297
                    for ( b = 0; b <= 6; b++ )
 
298
                        if ( r == g && g == b )
300
299
                            table[ r + (256-7) ] = 1;
301
 
                        else
 
300
                        else
302
301
                            table[ (r << 5) + (g << 2) + (b >> 1) ] = 1;
303
302
            for ( i = j = 0; i < sizeof(table); i++ )
304
 
                if ( table[i] == 1 ) {
 
303
                if ( table[i] == 1 ) {
305
304
                    backtable[i] = j;
306
305
                    table[j++] = i;
307
 
                }
 
306
                }
308
307
            colors8 = j;
309
 
        }
310
 
        while ( !(code = mgr_next_row(&cur)) )
311
 
           {
312
 
                switch (bdev->mgr_depth) {
313
 
                        case 4:
314
 
                                for (i = 0,dp = data, bp = cur.data; i < mgr_line_size; i++) {
315
 
                                        *dp =  *(bp++) << 4;
 
308
        }
 
309
        while ( !(code = mgr_next_row(&cur)) )
 
310
           {
 
311
                switch (bdev->mgr_depth) {
 
312
                        case 4:
 
313
                                for (i = 0,dp = data, bp = cur.data; i < mgr_line_size; i++) {
 
314
                                        *dp =  *(bp++) << 4;
316
315
                                    *(dp++) |= *(bp++) & 0x0f;
317
 
                                }
318
 
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
319
 
                                        return_error(gs_error_ioerror);
320
 
                                break;
321
 
 
322
 
                        case 8:
323
 
                                for (i = 0,bp = cur.data; i < mgr_line_size; i++, bp++)
324
 
                                      *bp = backtable[*bp] + MGR_RESERVEDCOLORS;
325
 
                                if ( fwrite(cur.data, sizeof(cur.data[0]), mgr_line_size, pstream) < mgr_line_size )
326
 
                                        return_error(gs_error_ioerror);
327
 
                                break;
328
 
                }
329
 
           }
330
 
        gs_free(bdev->memory, (char *)data, mgr_line_size, 1, "cmgrN_print_page(done)");
331
 
 
332
 
        if (bdev->mgr_depth == 4) {
 
316
                                }
 
317
                                if ( fwrite(data, sizeof(byte), mgr_line_size, pstream) < mgr_line_size )
 
318
                                        return_error(gs_error_ioerror);
 
319
                                break;
 
320
 
 
321
                        case 8:
 
322
                                for (i = 0,bp = cur.data; i < mgr_line_size; i++, bp++)
 
323
                                      *bp = backtable[*bp] + MGR_RESERVEDCOLORS;
 
324
                                if ( fwrite(cur.data, sizeof(cur.data[0]), mgr_line_size, pstream) < mgr_line_size )
 
325
                                        return_error(gs_error_ioerror);
 
326
                                break;
 
327
                }
 
328
           }
 
329
        gs_free(bdev->memory, (char *)data, mgr_line_size, 1, "cmgrN_print_page(done)");
 
330
 
 
331
        if (bdev->mgr_depth == 4) {
333
332
            for (i = 0; i < 16; i++) {
334
333
               pc_4bit_map_color_rgb((gx_device *)0, (gx_color_index) i, prgb);
335
334
               mgr->clut[i].colnum = i;
336
335
               mgr->clut[i].red    = clut2mgr(prgb[0], 16);
337
336
               mgr->clut[i].green  = clut2mgr(prgb[1], 16);
338
337
               mgr->clut[i].blue   = clut2mgr(prgb[2], 16);
339
 
            }
340
 
        }
341
 
        if (bdev->mgr_depth == 8) {
 
338
            }
 
339
        }
 
340
        if (bdev->mgr_depth == 8) {
342
341
            for (i = 0; i < colors8; i++) {
343
342
               mgr_8bit_map_color_rgb((gx_device *)0, (gx_color_index)
344
343
                   table[i], prgb);
346
345
               mgr->clut[i].red    = clut2mgr(prgb[0], 16);
347
346
               mgr->clut[i].green  = clut2mgr(prgb[1], 16);
348
347
               mgr->clut[i].blue   = clut2mgr(prgb[2], 16);
349
 
            }
350
 
        }
 
348
            }
 
349
        }
351
350
#if !arch_is_big_endian
352
 
        swap_bwords( (unsigned char *) mgr->clut, sizeof( struct nclut ) * i );
 
351
        swap_bwords( (unsigned char *) mgr->clut, sizeof( struct nclut ) * i );
353
352
#endif
354
 
        if ( fwrite(&mgr->clut, sizeof(struct nclut), i, pstream) < i )
 
353
        if ( fwrite(&mgr->clut, sizeof(struct nclut), i, pstream) < i )
355
354
            return_error(gs_error_ioerror);
356
 
        return (code < 0 ? code : 0);
 
355
        return (code < 0 ? code : 0);
357
356
}
358
357
 
359
 
 
360
358
/* Color mapping routines for 8-bit color with a fixed palette */
361
359
/* (3 bits of R, 3 bits of G, 2 bits of B). */
362
360
/* We have to trade off even spacing of colors along each axis */
367
365
gx_color_index
368
366
mgr_8bit_map_rgb_color(gx_device *dev, const gx_color_value cv[])
369
367
{
370
 
        uint rv = cv[0] / (gx_max_color_value / 7 + 1);
371
 
        uint gv = cv[1] / (gx_max_color_value / 7 + 1);
372
 
        uint bv = cv[2] / (gx_max_color_value / 7 + 1);
373
 
        return (gx_color_index)
374
 
                (rv == gv && gv == bv ? rv + (256-7) :
375
 
                 (rv << 5) + (gv << 2) + (bv >> 1));
 
368
        uint rv = cv[0] / (gx_max_color_value / 7 + 1);
 
369
        uint gv = cv[1] / (gx_max_color_value / 7 + 1);
 
370
        uint bv = cv[2] / (gx_max_color_value / 7 + 1);
 
371
        return (gx_color_index)
 
372
                (rv == gv && gv == bv ? rv + (256-7) :
 
373
                 (rv << 5) + (gv << 2) + (bv >> 1));
376
374
}
377
375
int
378
376
mgr_8bit_map_color_rgb(gx_device *dev, gx_color_index color,
379
377
  gx_color_value prgb[3])
380
378
{       static const gx_color_value ramp[8] =
381
 
        {       0, gx_max_color_value / 6, gx_max_color_value / 3,
382
 
                gx_max_color_value / 2, 2 * (gx_max_color_value / 3),
383
 
                5 * (gx_max_color_value / 6), gx_max_color_value,
384
 
                /* The 8th entry is not actually ever used, */
385
 
                /* except to fill out the palette. */
386
 
                gx_max_color_value
387
 
        };
 
379
        {       0, gx_max_color_value / 6, gx_max_color_value / 3,
 
380
                gx_max_color_value / 2, 2 * (gx_max_color_value / 3),
 
381
                5 * (gx_max_color_value / 6), gx_max_color_value,
 
382
                /* The 8th entry is not actually ever used, */
 
383
                /* except to fill out the palette. */
 
384
                gx_max_color_value
 
385
        };
388
386
#define icolor (uint)color
389
 
        if ( icolor >= 256-7 )
390
 
        {       prgb[0] = prgb[1] = prgb[2] = ramp[icolor - (256-7)];
391
 
        }
392
 
        else
393
 
        {       prgb[0] = ramp[(icolor >> 5) & 7];
394
 
                prgb[1] = ramp[(icolor >> 2) & 7];
395
 
                prgb[2] = ramp[(icolor & 3) << 1];
396
 
        }
 
387
        if ( icolor >= 256-7 )
 
388
        {       prgb[0] = prgb[1] = prgb[2] = ramp[icolor - (256-7)];
 
389
        }
 
390
        else
 
391
        {       prgb[0] = ramp[(icolor >> 5) & 7];
 
392
                prgb[1] = ramp[(icolor >> 2) & 7];
 
393
                prgb[2] = ramp[(icolor & 3) << 1];
 
394
        }
397
395
#undef icolor
398
 
        return 0;
 
396
        return 0;
399
397
}
400
398
 
401
 
 
402
399
/* convert the 8-bit look-up table into the standard MGR look-up table */
403
400
static unsigned int
404
401
clut2mgr(
412
409
  return((v*i)/0x10000);
413
410
}
414
411
 
415
 
 
416
412
/*
417
413
 * s w a p _ b w o r d s
418
414
 */