1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gdevjpx.c 6300 2005-12-28 19:56:24Z giles $ */
15
/* JPX encode filter test device */
20
#include "sjpx_luratech.h"
23
/* Structure for the JPX-writing device. */
24
typedef struct gx_device_jpx_s {
29
/* The device descriptor */
30
static dev_proc_print_page(jpx_print_page);
32
/* ------ The device descriptors ------ */
34
/* Default X and Y resolution. */
42
static dev_proc_print_page(jpx_print_page);
44
/* 24 bit RGB default */
45
static const gx_device_procs jpxrgb_procs =
46
prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
47
gx_default_rgb_map_rgb_color,
48
gx_default_rgb_map_color_rgb);
49
const gx_device_printer gs_jpxrgb_device = {
50
prn_device_std_body(gx_device_jpx, jpxrgb_procs, "jpx",
51
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
52
X_DPI, Y_DPI, /* resolution */
53
0, 0, 0, 0, /* margins */
54
24, /* bits per pixel */
59
static const gx_device_procs jpxgray_procs =
60
prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
61
gx_default_gray_map_rgb_color,
62
gx_default_gray_map_color_rgb);
63
const gx_device_printer gs_jpxgray_device = {
64
prn_device_body(gx_device_jpx, jpxgray_procs, "jpxgray",
65
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
66
X_DPI, Y_DPI, /* resolution */
67
0, 0, 0, 0, /* margins */
68
1, 8, 255, 0, 256, 0, /* components, depth and min/max values */
73
static dev_proc_map_color_rgb(jpx_cmyk_map_color_rgb);
74
static dev_proc_map_cmyk_color(jpx_cmyk_map_cmyk_color);
75
static const gx_device_procs jpxcmyk_procs =
77
gx_default_get_initial_matrix,
78
NULL, /* sync_output */
82
jpx_cmyk_map_color_rgb,
83
NULL, /* fill_rectangle */
84
NULL, /* tile_rectangle */
86
NULL, /* copy_color */
91
jpx_cmyk_map_cmyk_color,
92
NULL, /* get_xfont_procs */
93
NULL, /* get_xfont_device */
94
NULL, /* map_rgb_alpha_color */
95
gx_page_device_get_page_device /* get_page_device */
97
const gx_device_printer gs_jpxcmyk_device = {
98
prn_device_std_body(gx_device_jpx, jpxcmyk_procs, "jpxcmyk",
99
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
100
X_DPI, Y_DPI, /* resolution */
101
0, 0, 0, 0, /* margins */
102
32, /* bits per pixel */
106
/* private color conversion routines;
107
we don't seem to have defaults for cmyk. */
109
jpx_cmyk_map_color_rgb(gx_device * dev, gx_color_index color,
110
gx_color_value prgb[3])
112
int not_k = color & 0xff,
113
r = not_k - ~(color >> 24),
114
g = not_k - ~((color >> 16) & 0xff),
115
b = not_k - ~((color >> 8) & 0xff);
117
prgb[0] = (r < 0 ? 0 : gx_color_value_from_byte(r));
118
prgb[1] = (g < 0 ? 0 : gx_color_value_from_byte(g));
119
prgb[2] = (b < 0 ? 0 : gx_color_value_from_byte(b));
123
static gx_color_index
124
jpx_cmyk_map_cmyk_color(gx_device * dev, const gx_color_value cv[])
126
gx_color_index color = ~(
127
gx_color_value_to_byte(cv[3]) +
128
((uint)gx_color_value_to_byte(cv[2]) << 8) +
129
((uint)gx_color_value_to_byte(cv[1]) << 16) +
130
((uint)gx_color_value_to_byte(cv[0]) << 24));
132
return (color == gx_no_color_index ? color ^ 1 : color);
136
/* Send the page to the file. */
138
jpx_print_page(gx_device_printer * pdev, FILE * prn_stream)
140
gx_device_jpx *jdev = (gx_device_jpx *) pdev;
141
gs_memory_t *mem = jdev->memory;
142
int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
143
byte *in = gs_alloc_bytes(mem, line_size, "jpx_print_page(in)");
150
stream_jpxe_state state;
154
code = gs_note_error(gs_error_VMerror);
157
/* Create the jpx encoder state. */
158
s_init_state((stream_state *)&state, &s_jpxe_template, 0);
159
if (state.template->set_defaults)
160
(*state.template->set_defaults) ((stream_state *) & state);
161
state.width = jdev->width;
162
state.height = jdev->height;
163
switch (jdev->color_info.depth) {
164
case 32: state.colorspace = gs_jpx_cs_cmyk; break;
165
case 24: state.colorspace = gs_jpx_cs_rgb; break;
166
case 8: state.colorspace = gs_jpx_cs_gray; break;
168
state.colorspace = gs_jpx_cs_gray; /* safest option */
169
dlprintf1("unexpected color_info depth %d\n",
170
jdev->color_info.depth);
172
state.bpc = 8; /* currently only 8 bits per component is supported */
174
/* ask for lossless encoding */
175
/* state.lossless = 1; */
176
/* or, set the quality level different from the default */
177
/* state.quality = 35; */
179
/* Set up the streams. */
180
fbuf_size = max(512 /* arbitrary */ , state.template->min_out_size);
181
jbuf_size = state.template->min_in_size;
182
if ((fbuf = gs_alloc_bytes(mem, fbuf_size, "jpx_print_page(fbuf)")) == 0 ||
183
(jbuf = gs_alloc_bytes(mem, jbuf_size, "jpx_print_page(jbuf)")) == 0
185
code = gs_note_error(gs_error_VMerror);
189
swrite_file(&fstrm, prn_stream, fbuf, fbuf_size);
191
s_std_init(&cstrm, jbuf, jbuf_size, &s_filter_write_procs,
193
cstrm.state = (stream_state *) & state;
194
cstrm.procs.process = state.template->process;
196
if (state.template->init)
197
(*state.template->init) (cstrm.state);
199
/* Copy the data to the output. */
200
for (lnum = 0; lnum < jdev->height; ++lnum) {
204
if (cstrm.end_status) {
205
code = gs_note_error(gs_error_ioerror);
208
gdev_prn_get_bits(pdev, lnum, in, &data);
209
sputs(&cstrm, data, state.stride, &ignore_used);
216
gs_free_object(mem, jbuf, "jpx_print_page(jbuf)");
217
gs_free_object(mem, fbuf, "jpx_print_page(fbuf)");
218
gs_free_object(mem, in, "jpx_print_page(in)");
221
gs_free_object(mem, in, "jpx_print_page(in)");