File: writePS.c

package info (click to toggle)
xpaint 2.5.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,448 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (253 lines) | stat: -rw-r--r-- 6,390 bytes parent folder | download | duplicates (3)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: writePS.c,v 1.3 1996/04/19 09:34:34 torsten Exp $ */

#include <stdio.h>
#include "image.h"
#ifdef sco
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <string.h>

#ifndef TRUE
#define TRUE	1
#define FALSE	0
#endif

static void writePreview(FILE * fd, Image * image)
{
}

#if 0
static void writeCmap(FILE * fd, Image * image)
{
    int runlen, maxCount;
    int col, used;
    int i, x, y;
    unsigned char *ucp;
    char buf[280];
    short map[256];
    int isRLE;

    /*
    **	Remap the colors to the number used
     */
    for (i = 0; i < 256; i++)
	map[i] = -1;
    for (ucp = image->data, y = 0; y < image->width * image->height; y++, ucp++)
	map[*ucp] = 1;
    for (used = i = 0; i < 256; i++)
	if (map[i] != -1)
	    map[i] = used++;

    /*
    **	If compression will help
     */
    fprintf(fd, "/rgbmap %d string def\n", used * 3);
    fputs("% RLE drawing routine\n\
/buffer 2 string def							\n\
/rgb (000) def								\n\
/drawcolormappedimage {							\n\
  /buffer  1   string def						\n\
  /rgbval  3   string def						\n\
  /npixels 384 string def	% 128 * 3				\n\
  { currentfile buffer readhexstring pop  % read run information	\n\
    /len exch 0 get store						\n\
    len 128 ge								\n\
    { 0 1 len 128 sub	 % this is a run of raw data			\n\
      { currentfile buffer readhexstring pop pop			\n\
	/rgbval rgbmap buffer 0 get 3 mul 3 getinterval store		\n\
	npixels exch 3 mul rgbval putinterval				\n\
      } for								\n\
      npixels 0 len 127 sub 3 mul getinterval				\n\
    }									\n\
    { % else it's run data						\n\
      currentfile buffer readhexstring pop pop				\n\
      /rgbval rgbmap buffer 0 get 3 mul 3 getinterval store		\n\
      0 1 len { npixels exch 3 mul rgval putinterval } for		\n\
      npixels 0 len 1 add 3 mul getinterval				\n\
    } ifelse								\n\
  }									\n\
  false 3 colorimage							\n\
} bind def\n", fd);

    fprintf(fd, "%%%% get the rgb map\n");
    fprintf(fd, "currentfile rgbmap readhexstring\n");

    ucp = image->cmapData;
    for (col = i = 0; i < image->cmapSize; i++, ucp += 3) {
	if (map[i] == -1)
	    continue;

	fprintf(fd, "%02.2x%02.2x%02.2x ", ucp[0], ucp[1], ucp[2]);
	if (++col == 10) {
	    putc('\n', fd);
	    col = 0;
	}
    }
    if (col != 0)
	putc('\n', fd);
    fprintf(fd, "pop pop\n\n");

    fprintf(fd, "%d %d 8\n", image->width, image->height);
    fprintf(fd, "[ %d 0 0 -%d 0 %d ]\n",
	    image->width, image->height, image->height);
    fprintf(fd, "drawcolormappedimage\n");

    ucp = image->data;
    maxCount = 0x7f;

#define PUT(fd, val) do {			\
	fprintf(fd, "%02.2x", val);		\
	if (++col == 35)			\
		{ col = 0; putc('\n', fd); }	\
	} while (0)

#define	WRITE(fd, flg, buf, len) do { int _i;			\
	PUT(fd, (len) + (flg ? 0 : 128));			\
	if (flg)						\
		PUT(fd, map[buf[0]]);				\
	else							\
		for (_i = 0; _i < (len); _i++)			\
			PUT(fd, map[buf[_i]]);			\
	putc(' ', fd);						\
	} while (0)

    col = 0;
    for (y = 0; y < image->height; y++) {
	int prev;

	runlen = 0;
	for (x = 0; x < image->width; x++, ucp++) {
	    int v = *ucp;

	    if (runlen == 0)
		isRLE = TRUE;
	    else if (runlen == maxCount) {
		WRITE(fd, isRLE, buf, runlen);
		runlen = 0;
		isRLE = TRUE;
	    } else if (isRLE && v != prev) {
		if (runlen == 1)
		    isRLE = FALSE;
		else {
		    WRITE(fd, isRLE, buf, runlen);
		    runlen = 0;
		}
	    } else if (!isRLE && v == prev) {
		WRITE(fd, isRLE, buf, runlen);
		buf[0] = prev;
		runlen = 1;
		isRLE = TRUE;
	    }
	    buf[runlen++] = v;
	    prev = v;
	}

	if (runlen != 0)
	    WRITE(fd, isRLE, buf, runlen);
	col = 0;
	putc('\n', fd);
    }
    putc('\n', fd);
#undef WRITE
#undef PUT
}
#endif

static void writeRGB(FILE * fd, Image * image)
{
    unsigned char *ucp;
    int x, y;
    int col = 0;

    fprintf(fd, "/line %d string def\n", image->width * 3);
    fprintf(fd, "%d %d 8\n", image->width, image->height);
    fprintf(fd, "[ %d 0 0 -%d 0 %d ]\n",
	    image->width, image->height, image->height);
    fprintf(fd, "{currentfile line readhexstring pop}\n");
    fprintf(fd, "false 3 colorimage\n");

    for (y = 0; y < image->height; y++)
	for (x = 0; x < image->width; x++) {
	    ucp = ImagePixel(image, x, y);
	    fprintf(fd, "%02x%02x%02x", ucp[0], ucp[1], ucp[2]);
	    if (++col == 12) {
		putc('\n', fd);
		col = 0;
	    }
	}
    if (col != 0)
	putc('\n', fd);
}

int WritePS(char *file, Image * image)
{
    FILE *fd;
    char *cp, buf[256];
    time_t t;

    if ((fd = fopen(file, "w")) == NULL)
	return 1;

    if ((cp = strrchr(file, '/')) == NULL)
	cp = file;
    else
	cp++;
    strcpy(buf, cp);
    if ((cp = strrchr(buf, '.')) != NULL)
	*cp = '\0';

    time(&t);
    fprintf(fd, "%%!PS-Adobe-2.0 EPSF-2.0\n");
    fprintf(fd, "%%%%Creator: XPaint\n%%%%Title: %s\n%%%%Pages: 1\n", buf);
    fprintf(fd, "%%%%BoundingBox: %d %d %d %d\n", 0, 0,
	    image->width, image->height);
    fprintf(fd, "%%%%CreationDate: %s", ctime(&t));
    fprintf(fd, "%%%%EndComments\n");

    /*
    **	Write a preview image
     */

    writePreview(fd, image);

    fprintf(fd, "%%%%EndProlog\n%%%%Page: 1 1\n");
    fprintf(fd, "\n\ngsave\n\n");

    /*
    **	Write the actual image
     */

    fprintf(fd, "/inch {72 mul} def\n");
    fprintf(fd, "%d %d scale\n", image->width, image->height);

#if 0
    if (image->cmapSize == 0 || image->cmapSize > 256)
	writeRGB(fd, image);
    else
	writeCmap(fd, image);
#else
    writeRGB(fd, image);
#endif

    fprintf(fd, "%%\n\ngrestore\nshowpage\n%%%%Trailer\n");

    fclose(fd);

    return 0;
}