xref: /plan9-contrib/sys/src/cmd/gs/src/gdevtfnx.c (revision d46c239f8612929b7dbade67d0d071633df3a15d)
1 /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of AFPL Ghostscript.
4 
5   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
6   distributor accepts any responsibility for the consequences of using it, or
7   for whether it serves any particular purpose or works at all, unless he or
8   she says so in writing.  Refer to the Aladdin Free Public License (the
9   "License") for full details.
10 
11   Every copy of AFPL Ghostscript must include a copy of the License, normally
12   in a plain ASCII text file named PUBLIC.  The License grants you the right
13   to copy, modify and redistribute AFPL Ghostscript, but only under certain
14   conditions described in the License.  Among other things, the License
15   requires that the copyright notice and this notice be preserved on all
16   copies.
17 */
18 
19 /*$Id: gdevtfnx.c,v 1.5 2001/02/03 18:50:01 raph Exp $ */
20 /* 12-bit & 24-bit RGB uncompressed TIFF driver */
21 #include "gdevprn.h"
22 #include "gdevtifs.h"
23 
24 /*
25  * Thanks to Alan Barclay <alan@escribe.co.uk> for donating the original
26  * version of this code to Ghostscript.
27  */
28 
29 /* ------ The device descriptors ------ */
30 
31 /* Default X and Y resolution */
32 #define X_DPI 72
33 #define Y_DPI 72
34 
35 typedef struct gx_device_tiff_s {
36     gx_device_common;
37     gx_prn_device_common;
38     gdev_tiff_state tiff;
39 } gx_device_tiff;
40 
41 private dev_proc_print_page(tiff12_print_page);
42 private dev_proc_print_page(tiff24_print_page);
43 
44 private const gx_device_procs tiff12_procs =
45 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
46 		gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
47 private const gx_device_procs tiff24_procs =
48 prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
49 		gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
50 
51 const gx_device_printer gs_tiff12nc_device = {
52     prn_device_std_body(gx_device_tiff, tiff12_procs, "tiff12nc",
53 			DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
54 			X_DPI, Y_DPI,
55 			0, 0, 0, 0,
56 			24, tiff12_print_page)
57 };
58 
59 const gx_device_printer gs_tiff24nc_device = {
60     prn_device_std_body(gx_device_tiff, tiff24_procs, "tiff24nc",
61 			DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
62 			X_DPI, Y_DPI,
63 			0, 0, 0, 0,
64 			24, tiff24_print_page)
65 };
66 
67 /* ------ Private definitions ------ */
68 
69 /* Define our TIFF directory - sorted by tag number */
70 typedef struct tiff_rgb_directory_s {
71     TIFF_dir_entry BitsPerSample;
72     TIFF_dir_entry Compression;
73     TIFF_dir_entry Photometric;
74     TIFF_dir_entry FillOrder;
75     TIFF_dir_entry SamplesPerPixel;
76 } tiff_rgb_directory;
77 typedef struct tiff_rgb_values_s {
78     TIFF_ushort bps[3];
79 } tiff_rgb_values;
80 
81 private const tiff_rgb_directory dir_rgb_template =
82 {
83 	/* C's ridiculous rules about & and arrays require bps[0] here: */
84     {TIFFTAG_BitsPerSample, TIFF_SHORT | TIFF_INDIRECT, 3, offset_of(tiff_rgb_values, bps[0])},
85     {TIFFTAG_Compression, TIFF_SHORT, 1, Compression_none},
86     {TIFFTAG_Photometric, TIFF_SHORT, 1, Photometric_RGB},
87     {TIFFTAG_FillOrder, TIFF_SHORT, 1, FillOrder_MSB2LSB},
88     {TIFFTAG_SamplesPerPixel, TIFF_SHORT, 1, 3},
89 };
90 
91 private const tiff_rgb_values val_12_template = {
92     {4, 4, 4}
93 };
94 
95 private const tiff_rgb_values val_24_template = {
96     {8, 8, 8}
97 };
98 
99 /* ------ Private functions ------ */
100 
101 private int
102 tiff12_print_page(gx_device_printer * pdev, FILE * file)
103 {
104     gx_device_tiff *const tfdev = (gx_device_tiff *)pdev;
105     int code;
106 
107     /* Write the page directory. */
108     code = gdev_tiff_begin_page(pdev, &tfdev->tiff, file,
109 				(const TIFF_dir_entry *)&dir_rgb_template,
110 			  sizeof(dir_rgb_template) / sizeof(TIFF_dir_entry),
111 				(const byte *)&val_12_template,
112 				sizeof(val_12_template), 0);
113     if (code < 0)
114 	return code;
115 
116     /* Write the page data. */
117     {
118 	int y;
119 	int raster = gdev_prn_raster(pdev);
120 	byte *line = gs_alloc_bytes(pdev->memory, raster, "tiff12_print_page");
121 	byte *row;
122 
123 	if (line == 0)
124 	    return_error(gs_error_VMerror);
125 
126 	for (y = 0; y < pdev->height; ++y) {
127 	    const byte *src;
128 	    byte *dest;
129 	    int x;
130 
131 	    code = gdev_prn_get_bits(pdev, y, line, &row);
132 	    if (code < 0)
133 		break;
134 
135 	    for (src = row, dest = line, x = 0; x < raster;
136 		 src += 6, dest += 3, x += 6
137 		) {
138 		dest[0] = (src[0] & 0xf0) | (src[1] >> 4);
139 		dest[1] = (src[2] & 0xf0) | (src[3] >> 4);
140 		dest[2] = (src[4] & 0xf0) | (src[5] >> 4);
141 	    }
142 	    fwrite(line, 1, (pdev->width * 3 + 1) >> 1, file);
143 	}
144 
145 	gdev_tiff_end_strip(&tfdev->tiff, file);
146 	gdev_tiff_end_page(&tfdev->tiff, file);
147 	gs_free_object(pdev->memory, line, "tiff12_print_page");
148     }
149 
150     return code;
151 }
152 
153 private int
154 tiff24_print_page(gx_device_printer * pdev, FILE * file)
155 {
156     gx_device_tiff *const tfdev = (gx_device_tiff *)pdev;
157     int code;
158 
159     /* Write the page directory. */
160     code = gdev_tiff_begin_page(pdev, &tfdev->tiff, file,
161 				(const TIFF_dir_entry *)&dir_rgb_template,
162 			  sizeof(dir_rgb_template) / sizeof(TIFF_dir_entry),
163 				(const byte *)&val_24_template,
164 				sizeof(val_24_template), 0);
165     if (code < 0)
166 	return code;
167 
168     /* Write the page data. */
169     {
170 	int y;
171 	int raster = gdev_prn_raster(pdev);
172 	byte *line = gs_alloc_bytes(pdev->memory, raster, "tiff24_print_page");
173 	byte *row;
174 
175 	if (line == 0)
176 	    return_error(gs_error_VMerror);
177 	for (y = 0; y < pdev->height; ++y) {
178 	    code = gdev_prn_get_bits(pdev, y, line, &row);
179 	    if (code < 0)
180 		break;
181 	    fwrite((char *)row, raster, 1, file);
182 	}
183 	gdev_tiff_end_strip(&tfdev->tiff, file);
184 	gdev_tiff_end_page(&tfdev->tiff, file);
185 	gs_free_object(pdev->memory, line, "tiff24_print_page");
186     }
187 
188     return code;
189 }
190