summaryrefslogtreecommitdiff
path: root/graphics/netpbm/patches/patch-bx
blob: 2a83b1f6268146d8d85212cb185f75579b156854 (plain)
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
--- ./ppm/ppmtoxpm.c.orig	Mon Jan 31 02:44:41 1994
+++ ./ppm/ppmtoxpm.c	Fri Aug 18 05:30:36 1995
@@ -27,11 +27,25 @@
 **    
 **  - lowercase conversion of RGB names def'ed out,
 **    considered harmful.
+**
+** Michael Pall (pall@rz.uni-karlsruhe.de) - 29 Nov 93:
+**  - Use the algorithm from xpm-lib for pixel encoding
+**    (base 93 not base 28 -> saves a lot of space for colorful xpms)
 */
 
+#include <stdio.h>
+#include <ctype.h>
 #include "ppm.h"
 #include "ppmcmap.h"
 
+#if defined(SYSV) || defined(SVR4)
+#include <string.h>
+#ifndef index
+#define index strchr
+#endif
+#else					/* SYSV */
+#include <strings.h>
+#endif					/* SYSV */
 
 /* Max number of colors allowed in ppm input. */
 #define MAXCOLORS    256
@@ -39,15 +53,19 @@
 /* Max number of rgb mnemonics allowed in rgb text file. */
 #define MAX_RGBNAMES 1024
 
-/* Lower bound and upper bound of character-pixels printed in XPM output.
-   Be careful, don't want the character '"' in this range. */
-/*#define LOW_CHAR  '#'  <-- minimum ascii character allowed */
-/*#define HIGH_CHAR '~'  <-- maximum ascii character allowed */
-#define LOW_CHAR  '`'
-#define HIGH_CHAR 'z'
+#define MAXPRINTABLE 92			/* number of printable ascii chars
+					 * minus \ and " for string compat
+					 * and ? to avoid ANSI trigraphs. */
+
+static char *printable =
+" .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZ\
+ASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
+
 
 #define max(a,b) ((a) > (b) ? (a) : (b))
 
+void read_rgb_names();			/* forward reference */
+void gen_cmap();			/* forward reference */
 
 typedef struct {			/* rgb values and ascii names (from
 					 * rgb text file) */
@@ -62,17 +80,8 @@
 					 * mnemonic or #rgb value */
 }      cixel_map;
 
-
-/* prototypes/forward reference */
-static void   read_rgb_names ARGS((char *, rgb_names *, int *));
-static char * gen_numstr ARGS((int, int, int));
-static void   gen_cmap ARGS((colorhist_vector, int, pixval, int, rgb_names *, int, cixel_map *, int *));
-
-
 pixel **pixels;
 
-
-int
 main(argc, argv)
     int argc;
     char *argv[];
@@ -88,11 +97,11 @@
 
     /* Used for rgb value -> rgb mnemonic mapping */
     int map_rgb_names = 0;
-    rgb_names *rgbn;    /* rgb_names rgbn[MAX_RGBNAMES]; */
+    rgb_names rgbn[MAX_RGBNAMES];
     int rgbn_max;
 
     /* Used for rgb value -> character-pixel string mapping */
-    cixel_map *cmap;    /* cixel_map cmap[MAXCOLORS]; */
+    cixel_map cmap[MAXCOLORS];
     int charspp;			/* chars per pixel */
 
     char out_name[100], rgb_fname[100], *cp;
@@ -188,17 +197,9 @@
      * If a rgb text file was specified, read in the rgb mnemonics. Does not
      * return if fatal error occurs. 
      */
-    rgbn = (rgb_names *) malloc(MAX_RGBNAMES * sizeof(rgb_names));
-    if (rgbn == (rgb_names *) NULL)
-	pm_error("out of memory");
-
     if (map_rgb_names)
 	read_rgb_names(rgb_fname, rgbn, &rgbn_max);
 
-    cmap = (cixel_map *)malloc(ncolors * sizeof(cixel_map));
-    if (cmap == (cixel_map *) NULL)
-	pm_error("out of memory");
- 
     /* Now generate the character-pixel colormap table. */
     gen_cmap(chv, ncolors, maxval, map_rgb_names, rgbn, rgbn_max,
 	     cmap, &charspp);
@@ -231,12 +232,12 @@
 /* This routine reads a rgb text file.  It stores the rgb values (0->65535)
    and the rgb mnemonics (malloc'ed) into the "rgbn" array.  Returns the
    number of entries stored in "rgbn_max". */
-static
 void
 read_rgb_names(rgb_fname, rgbn, rgbn_max)
     char *rgb_fname;
-    rgb_names *rgbn;
-    int *rgbn_max;
+    rgb_names rgbn[MAX_RGBNAMES];
+int *rgbn_max;
+
 {
     FILE *rgbf;
     int i, items, red, green, blue;
@@ -303,16 +304,16 @@
 }					/* read_rgb_names */
 
 /*---------------------------------------------------------------------------*/
-/* Given a number and a base, (base == HIGH_CHAR-LOW_CHAR+1), this routine
+/* Given a number and a base (MAXPRINTABLE), this routine
    prints the number into a malloc'ed string and returns it.  The length of
    the string is specified by "digits".  The ascii characters of the printed
-   number range from LOW_CHAR to HIGH_CHAR.  The string is LOW_CHAR filled,
-   (e.g. if LOW_CHAR==0, HIGH_CHAR==1, digits==5, i=3, routine would return
-   the malloc'ed string "00011"). */
-static
+   number range from printable[0] to printable[MAXPRINTABLE].  The string is
+   printable[0] filled, (e.g. if printable[0]==0, printable[1]==1,
+   MAXPRINTABLE==2, digits==5, i=3, routine would return the malloc'ed
+   string "00011"). */
 char *
-gen_numstr(i, base, digits)
-    int i, base, digits;
+gen_numstr(i, digits)
+    int i, digits;
 {
     char *str, *p;
     int d;
@@ -325,9 +326,9 @@
     p = str + digits;
     *p-- = '\0';			/* nul terminate string */
     while (p >= str) {
-	d = i % base;
-	i /= base;
-	*p-- = (char) ((int) LOW_CHAR + d);
+	d = i % MAXPRINTABLE;
+	i /= MAXPRINTABLE;
+	*p-- = printable[d];
     }
 
     return str;
@@ -336,7 +337,6 @@
 
 /*---------------------------------------------------------------------------*/
 /* This routine generates the character-pixel colormap table. */
-static
 void
 gen_cmap(chv, ncolors, maxval, map_rgb_names, rgbn, rgbn_max,
 	 cmap, charspp)
@@ -348,16 +348,16 @@
 					 * == unsigned short) */
     int map_rgb_names;			/* == 1 if mapping rgb values to rgb
 					 * mnemonics */
-    rgb_names *rgbn;                    /* rgb mnemonics from rgb text file */
+    rgb_names rgbn[MAX_RGBNAMES];	/* rgb mnemonics from rgb text file */
 int rgbn_max;				/* number of rgb mnemonics in table */
 
 /* output: */
-cixel_map *cmap;                        /* pixel strings and ascii rgb
+cixel_map cmap[MAXCOLORS];		/* pixel strings and ascii rgb
 					 * colors */
 int *charspp;				/* characters per pixel */
 
 {
-    int i, j, base, cpp, mval, red, green, blue, r, g, b, matched;
+    int i, j, cpp, mval, red, green, blue, r, g, b, matched;
     char *str;
 
     /*
@@ -365,9 +365,8 @@
      * to be forced to link with libm.a, so using a division loop rather
      * than a log function. 
      */
-    base = (int) HIGH_CHAR - (int) LOW_CHAR + 1;
     for (cpp = 0, j = ncolors; j; cpp++)
-	j /= base;
+	j /= MAXPRINTABLE;
     *charspp = cpp;
 
     /*
@@ -392,10 +391,11 @@
 
 	/*
 	 * The character-pixel string is simply a printed number in base
-	 * "base" where the digits of the number range from LOW_CHAR to
-	 * HIGH_CHAR and the printed length of the number is "cpp". 
+	 * MAXPRINTABLE where the digits of the number range from
+	 * printable[0] .. printable[MAXPRINTABLE-1] and the printed length
+	 * of the number is "cpp". 
 	 */
-	cmap[i].cixel = gen_numstr(i, base, cpp);
+	cmap[i].cixel = gen_numstr(i, cpp);
 
 	/* Fetch the rgb value of the current colormap entry. */
 	red = PPM_GETR(chv[i].color);