386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / gsfile.c
CommitLineData
0cde343c
WJ
1/* Copyright (C) 1990 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* gsfile.c */
21/* Bitmap file writing routines for GhostScript library */
22#include "memory_.h"
23#include "gx.h"
24#include "gserrors.h"
25#include "gsmatrix.h" /* for gxdevice.h */
26#include "gxdevice.h"
27#include "gxdevmem.h"
28
29/* Dump the contents of a memory device in PPM format. */
30int
31gs_writeppmfile(gx_device_memory *md, FILE *file)
32{ int raster = gx_device_bytes_per_scan_line((gx_device *)md, 0);
33 int height = md->height;
34 int depth = md->color_info.depth;
35 uint rsize = raster * 3; /* * 3 just for mapped color */
36 byte *row = (byte *)gs_malloc(rsize, 1, "ppm file buffer");
37 const char *header;
38 int y;
39 int code = 0; /* return code */
40 if ( row == 0 ) /* can't allocate row buffer */
41 return_error(gs_error_VMerror);
42
43 /* A PPM file consists of: magic number, comment, */
44 /* width, height, maxvalue, (r,g,b).... */
45
46 /* Dispatch on the type of the device -- 1-bit mono, */
47 /* 8-bit (gray scale or color), 24- or 32-bit true color. */
48
49 switch ( depth )
50 {
51 case 1:
52 header = "P4\n# Ghostscript 1 bit mono image dump\n%d %d\n";
53 break;
54
55 case 8:
56 header = (gx_device_has_color(md) ?
57 "P6\n# Ghostscript 8 bit mapped color image dump\n%d %d\n255\n" :
58 "P5\n# Ghostscript 8 bit gray scale image dump\n%d %d\n255\n");
59 break;
60
61 case 24:
62 header = "P6\n# Ghostscript 24 bit color image dump\n%d %d\n255\n";
63 break;
64
65 case 32:
66 header = "P6\n# Ghostscript 32 bit color image dump\n%d %d\n255\n";
67 break;
68
69 default: /* shouldn't happen! */
70 code = gs_error_undefinedresult;
71 goto done;
72 }
73
74 /* Write the header. */
75 fprintf(file, header, md->width, height);
76
77 /* Dump the contents of the image. */
78 for ( y = 0; y < height; y++ )
79 { int count;
80 register byte *from, *to, *end;
81 (*md->procs->get_bits)((gx_device *)md, y, row, raster, 0);
82 switch ( depth )
83 {
84 case 8:
85 { /* Mapped color, consult the map. */
86 if ( gx_device_has_color(md) )
87 { /* Map color */
88 byte *palette = md->palette;
89 from = row + raster + raster;
90 memcpy(from, row, raster);
91 to = row;
92 end = from + raster;
93 while ( from < end )
94 { register byte *cp =
95 palette + (int)*from++ * 3;
96 to[0] = cp[0]; /* red */
97 to[1] = cp[1]; /* green */
98 to[2] = cp[2]; /* blue */
99 to += 3;
100 }
101 count = raster * 3;
102 }
103 else
104 { /* Map gray scale */
105 register byte *palette = md->palette;
106 from = to = row, end = row + raster;
107 while ( from < end )
108 *to++ = palette[(int)*from++ * 3];
109 count = raster;
110 }
111 }
112 break;
113 case 32:
114 { /* This case is different, because we must skip */
115 /* every fourth byte. */
116 from = to = row, end = row + raster;
117 while ( from < end )
118 { /* from[0] is unused */
119 to[0] = from[1]; /* red */
120 to[1] = from[2]; /* green */
121 to[2] = from[3]; /* blue */
122 from += 4;
123 to += 3;
124 }
125 count = to - row;
126 }
127 break;
128 default:
129 count = raster;
130 }
131 if ( fwrite(row, 1, count, file) < count )
132 { code = gs_error_ioerror;
133 goto done;
134 }
135 }
136
137done: gs_free((char *)row, rsize, 1, "ppm file buffer");
138 return code;
139}