xref: /plan9-contrib/sys/src/cmd/gs/src/gdevsunr.c (revision 6a9fc400c33447ef5e1cda7185cb4de2c8e8010e)
1 /* Copyright (C) 1999 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: gdevsunr.c,v 1.4 2001/08/01 00:48:23 stefan911 Exp $ */
20 /* Sun raster file driver */
21 #include "gdevprn.h"
22 
23 /*
24  * Currently, the only variety of this format supported in this file is
25  * Harlequin's 1-bit "SUN_RAS" with no colormap and odd "};\n" at tail.
26  */
27 
28 #define	RAS_MAGIC	0x59a66a95
29 #define RT_STANDARD	1	/* Raw pixrect image in 68000 byte order */
30 #define RMT_NONE	0	/* ras_maplength is expected to be 0 */
31 typedef struct sun_rasterfile_s {
32 	int	ras_magic;		/* magic number */
33 	int	ras_width;		/* width (pixels) of image */
34 	int	ras_height;		/* height (pixels) of image */
35 	int	ras_depth;		/* depth (1, 8, or 24 bits) of pixel */
36 	int	ras_length;		/* length (bytes) of image */
37 	int	ras_type;		/* type of file; see RT_* below */
38 	int	ras_maptype;		/* type of colormap; see RMT_* below */
39 	int	ras_maplength;		/* length (bytes) of following map */
40 } sun_rasterfile_t;
41 
42 #ifndef X_DPI
43 #  define X_DPI 72
44 #endif
45 #ifndef Y_DPI
46 #  define Y_DPI 72
47 #endif
48 
49 private dev_proc_print_page(sunhmono_print_page);
50 
51 const gx_device_printer gs_sunhmono_device =
52     prn_device(prn_std_procs, "sunhmono",
53 	       DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
54 	       X_DPI, Y_DPI,
55 	       0, 0, 0, 0,	/* margins */
56 	       1, sunhmono_print_page);
57 
58 private int
59 sunhmono_print_page(gx_device_printer * pdev, FILE * prn_stream)
60 {
61     int gsLineBytes = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
62     /* Output bytes have to be padded to 16 bits. */
63     int rasLineBytes = ROUND_UP(gsLineBytes, 2);
64     int lineCnt;
65     char *lineStorage; /* Allocated for passing storage to gdev_prn_get_bits() */
66     byte *data;
67     sun_rasterfile_t ras;
68     int code = 0;
69 
70     /*
71       errprintf("pdev->width:%d (%d/%d) gsLineBytes:%d rasLineBytes:%d\n",
72       pdev->width, pdev->width/8, pdev->width%8,gsLineBytes,rasLineBytes);
73     */
74     lineStorage = gs_malloc(gsLineBytes, 1, "rasterfile_print_page(in)");
75     if (lineStorage == 0) {
76 	code = gs_note_error(gs_error_VMerror);
77 	goto out;
78     }
79     /* Setup values in header */
80     ras.ras_magic = RAS_MAGIC;
81     ras.ras_width = pdev->width;
82     ras.ras_height = pdev->height;
83     ras.ras_depth = 1;
84     ras.ras_length = (rasLineBytes * pdev->height);
85     ras.ras_type = RT_STANDARD;
86     ras.ras_maptype = RMT_NONE;
87     ras.ras_maplength = 0;
88     /* Write header */
89     fwrite(&ras, 1, sizeof(ras), prn_stream);
90     /* For each raster line */
91     for (lineCnt = 0; lineCnt < pdev->height; ++lineCnt) {
92 	gdev_prn_get_bits(pdev, lineCnt, lineStorage, &data);
93 	fwrite(data, 1, gsLineBytes, prn_stream);
94 	if (gsLineBytes % 2)
95 	    fputc(0, prn_stream); /* pad to even # of bytes with a 0 */
96     }
97     /* The weird file terminator */
98     fwrite("};\n", 1, 3, prn_stream);
99 out:
100     /* Clean up... */
101     gs_free(lineStorage, gsLineBytes, 1, "rasterfile_print_page(in)");
102     return code;
103 }
104