xref: /plan9/sys/src/cmd/gs/src/gdevsunr.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gdevsunr.c,v 1.7 2004/08/10 13:02:36 stefan Exp $ */
18 /* Sun raster file driver */
19 #include "gdevprn.h"
20 
21 /*
22  * Currently, the only variety of this format supported in this file is
23  * Harlequin's 1-bit "SUN_RAS" with no colormap and odd "};\n" at tail.
24  */
25 
26 #define	RAS_MAGIC	0x59a66a95
27 #define RT_STANDARD	1	/* Raw pixrect image in 68000 byte order */
28 #define RMT_NONE	0	/* ras_maplength is expected to be 0 */
29 typedef struct sun_rasterfile_s {
30 	int	ras_magic;		/* magic number */
31 	int	ras_width;		/* width (pixels) of image */
32 	int	ras_height;		/* height (pixels) of image */
33 	int	ras_depth;		/* depth (1, 8, or 24 bits) of pixel */
34 	int	ras_length;		/* length (bytes) of image */
35 	int	ras_type;		/* type of file; see RT_* below */
36 	int	ras_maptype;		/* type of colormap; see RMT_* below */
37 	int	ras_maplength;		/* length (bytes) of following map */
38 } sun_rasterfile_t;
39 
40 #ifndef X_DPI
41 #  define X_DPI 72
42 #endif
43 #ifndef Y_DPI
44 #  define Y_DPI 72
45 #endif
46 
47 private dev_proc_print_page(sunhmono_print_page);
48 
49 const gx_device_printer gs_sunhmono_device =
50     prn_device(prn_std_procs, "sunhmono",
51 	       DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
52 	       X_DPI, Y_DPI,
53 	       0, 0, 0, 0,	/* margins */
54 	       1, sunhmono_print_page);
55 
56 private int
sunhmono_print_page(gx_device_printer * pdev,FILE * prn_stream)57 sunhmono_print_page(gx_device_printer * pdev, FILE * prn_stream)
58 {
59     int gsLineBytes = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
60     /* Output bytes have to be padded to 16 bits. */
61     int rasLineBytes = ROUND_UP(gsLineBytes, 2);
62     int lineCnt;
63     char *lineStorage; /* Allocated for passing storage to gdev_prn_get_bits() */
64     byte *data;
65     sun_rasterfile_t ras;
66     int code = 0;
67 
68     /*
69       errprintf("pdev->width:%d (%d/%d) gsLineBytes:%d rasLineBytes:%d\n",
70       pdev->width, pdev->width/8, pdev->width%8,gsLineBytes,rasLineBytes);
71     */
72     lineStorage = gs_malloc(pdev->memory, gsLineBytes, 1, "rasterfile_print_page(in)");
73     if (lineStorage == 0) {
74 	code = gs_note_error(gs_error_VMerror);
75 	goto out;
76     }
77     /* Setup values in header */
78     ras.ras_magic = RAS_MAGIC;
79     ras.ras_width = pdev->width;
80     ras.ras_height = pdev->height;
81     ras.ras_depth = 1;
82     ras.ras_length = (rasLineBytes * pdev->height);
83     ras.ras_type = RT_STANDARD;
84     ras.ras_maptype = RMT_NONE;
85     ras.ras_maplength = 0;
86     /* Write header */
87     fwrite(&ras, 1, sizeof(ras), prn_stream);
88     /* For each raster line */
89     for (lineCnt = 0; lineCnt < pdev->height; ++lineCnt) {
90 	gdev_prn_get_bits(pdev, lineCnt, lineStorage, &data);
91 	fwrite(data, 1, gsLineBytes, prn_stream);
92 	if (gsLineBytes % 2)
93 	    fputc(0, prn_stream); /* pad to even # of bytes with a 0 */
94     }
95     /* The weird file terminator */
96     fwrite("};\n", 1, 3, prn_stream);
97 out:
98     /* Clean up... */
99     gs_free(pdev->memory, lineStorage, gsLineBytes, 1, "rasterfile_print_page(in)");
100     return code;
101 }
102