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
|
/* Copyright (C) 2001-2006 artofcode LLC.
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 that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id$ */
/* 12-bit & 24-bit RGB uncompressed TIFF driver */
#include "gdevprn.h"
#include "gdevtifs.h"
/*
* Thanks to Alan Barclay <alan@escribe.co.uk> for donating the original
* version of this code to Ghostscript.
*/
/* ------ The device descriptors ------ */
/* Default X and Y resolution */
#define X_DPI 72
#define Y_DPI 72
typedef struct gx_device_tiff_s {
gx_device_common;
gx_prn_device_common;
gdev_tiff_state tiff;
} gx_device_tiff;
private dev_proc_print_page(tiff12_print_page);
private dev_proc_print_page(tiff24_print_page);
private const gx_device_procs tiff12_procs =
prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
private const gx_device_procs tiff24_procs =
prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
const gx_device_printer gs_tiff12nc_device = {
prn_device_std_body(gx_device_tiff, tiff12_procs, "tiff12nc",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
X_DPI, Y_DPI,
0, 0, 0, 0,
24, tiff12_print_page)
};
const gx_device_printer gs_tiff24nc_device = {
prn_device_std_body(gx_device_tiff, tiff24_procs, "tiff24nc",
DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
X_DPI, Y_DPI,
0, 0, 0, 0,
24, tiff24_print_page)
};
/* ------ Private definitions ------ */
/* Define our TIFF directory - sorted by tag number */
typedef struct tiff_rgb_directory_s {
TIFF_dir_entry BitsPerSample;
TIFF_dir_entry Compression;
TIFF_dir_entry Photometric;
TIFF_dir_entry FillOrder;
TIFF_dir_entry SamplesPerPixel;
} tiff_rgb_directory;
typedef struct tiff_rgb_values_s {
TIFF_ushort bps[3];
} tiff_rgb_values;
private const tiff_rgb_directory dir_rgb_template =
{
/* C's ridiculous rules about & and arrays require bps[0] here: */
{TIFFTAG_BitsPerSample, TIFF_SHORT | TIFF_INDIRECT, 3, offset_of(tiff_rgb_values, bps[0])},
{TIFFTAG_Compression, TIFF_SHORT, 1, Compression_none},
{TIFFTAG_Photometric, TIFF_SHORT, 1, Photometric_RGB},
{TIFFTAG_FillOrder, TIFF_SHORT, 1, FillOrder_MSB2LSB},
{TIFFTAG_SamplesPerPixel, TIFF_SHORT, 1, 3},
};
private const tiff_rgb_values val_12_template = {
{4, 4, 4}
};
private const tiff_rgb_values val_24_template = {
{8, 8, 8}
};
/* ------ Private functions ------ */
private int
tiff12_print_page(gx_device_printer * pdev, FILE * file)
{
gx_device_tiff *const tfdev = (gx_device_tiff *)pdev;
int code;
/* Write the page directory. */
code = gdev_tiff_begin_page(pdev, &tfdev->tiff, file,
(const TIFF_dir_entry *)&dir_rgb_template,
sizeof(dir_rgb_template) / sizeof(TIFF_dir_entry),
(const byte *)&val_12_template,
sizeof(val_12_template), 0);
if (code < 0)
return code;
/* Write the page data. */
{
int y;
int raster = gdev_prn_raster(pdev);
byte *line = gs_alloc_bytes(pdev->memory, raster, "tiff12_print_page");
byte *row;
if (line == 0)
return_error(gs_error_VMerror);
for (y = 0; y < pdev->height; ++y) {
const byte *src;
byte *dest;
int x;
code = gdev_prn_get_bits(pdev, y, line, &row);
if (code < 0)
break;
for (src = row, dest = line, x = 0; x < raster;
src += 6, dest += 3, x += 6
) {
dest[0] = (src[0] & 0xf0) | (src[1] >> 4);
dest[1] = (src[2] & 0xf0) | (src[3] >> 4);
dest[2] = (src[4] & 0xf0) | (src[5] >> 4);
}
fwrite(line, 1, (pdev->width * 3 + 1) >> 1, file);
}
gdev_tiff_end_strip(&tfdev->tiff, file);
gdev_tiff_end_page(&tfdev->tiff, file);
gs_free_object(pdev->memory, line, "tiff12_print_page");
}
return code;
}
private int
tiff24_print_page(gx_device_printer * pdev, FILE * file)
{
gx_device_tiff *const tfdev = (gx_device_tiff *)pdev;
int code;
/* Write the page directory. */
code = gdev_tiff_begin_page(pdev, &tfdev->tiff, file,
(const TIFF_dir_entry *)&dir_rgb_template,
sizeof(dir_rgb_template) / sizeof(TIFF_dir_entry),
(const byte *)&val_24_template,
sizeof(val_24_template), 0);
if (code < 0)
return code;
/* Write the page data. */
{
int y;
int raster = gdev_prn_raster(pdev);
byte *line = gs_alloc_bytes(pdev->memory, raster, "tiff24_print_page");
byte *row;
if (line == 0)
return_error(gs_error_VMerror);
for (y = 0; y < pdev->height; ++y) {
code = gdev_prn_get_bits(pdev, y, line, &row);
if (code < 0)
break;
fwrite((char *)row, raster, 1, file);
}
gdev_tiff_end_strip(&tfdev->tiff, file);
gdev_tiff_end_page(&tfdev->tiff, file);
gs_free_object(pdev->memory, line, "tiff24_print_page");
}
return code;
}
|