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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#include <stdio.h>
#include <rasterfile.h>
/*
* RAS2TEXT -- This is a small standalone utility program to convert a color
* Sun rasterfile to a printable ASCII form suitable for inclusion as an image
* in a GUI. The dimensions of the raster are output, along with the RGB
* colortable and the hex-encoded and optionally run length encoded pixels.
*
* Usage: ras2text [-hex1 | -hex2] [-rle] fname
*
* The decoded Sun raster is written as text to the standard output.
*/
#define HEX1 1
#define HEX2 2
struct color {
unsigned char red, green, blue;
};
main (argc, argv)
int argc;
char **argv;
{
register unsigned char *op;
register unsigned char *ip;
register int i, j, v;
struct rasterfile r;
struct color colors[256];
unsigned char red[256], green[256], blue[256];
unsigned char hex1[256], hex2[256*2];
unsigned char *data, *obuf, *cbuf;
int encoding, compress, debug;
int fd, npix, nelem, maxval, n;
char *fname;
fname = NULL;
encoding = HEX2;
compress = 0;
debug = 0;
/* Process argument list. */
for (i=1; i < argc; i++)
if (argv[i][0] == '-') {
if (strcmp (argv[i], "-hex1") == 0)
encoding = HEX1;
else if (strcmp (argv[i], "-hex2") == 0)
encoding = HEX2;
else if (strcmp (argv[i], "-rle") == 0)
compress++;
else if (strcmp (argv[i], "-d") == 0)
debug++;
else
fprintf (stderr, "unknown switch %s\n", argv[i]);
} else
fname = argv[i];
/* Open file. */
if ((fd = open (fname, 0)) < 0) {
fprintf (stderr, "cannot open %s\n", fname);
exit (1);
}
/* Read file header. */
if (read(fd,&r,sizeof(r)) != sizeof(r) || r.ras_magic != RAS_MAGIC) {
fprintf (stderr, "not a Sun rasterfile: %s\n", fname);
exit (2);
}
/* Print header. */
printf ("width %d height %d depth %d\n",
r.ras_width, r.ras_height, r.ras_depth);
if (debug) {
fprintf (stderr, "width=%d height=%d depth=%d length=%d type=%d\n",
r.ras_width, r.ras_height, r.ras_depth, r.ras_length,
r.ras_type);
fprintf (stderr, "maptype=%d, maplength=%d\n",
r.ras_maptype, r.ras_maplength);
}
if (r.ras_type != RT_STANDARD) {
fprintf (stderr, "rasterfile not in standard format\n");
exit (3);
}
/* Read the colormap. */
if (n = r.ras_maplength / 3) {
if (read (fd, red, n) != n ||
read (fd, green, n) != n ||
read (fd, blue, n) != n) {
fprintf (stderr, "cannot read colormap\n");
exit (4);
}
}
/* Read the pixels. */
if ((data = (unsigned char *) malloc (npix=r.ras_length)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (5);
}
if ((npix = read(fd,data,npix)) != r.ras_length) {
fprintf (stderr, "data read error\n");
exit (6);
}
/* If bitmap convert to byte pixel array. */
if (r.ras_depth == 1) {
unsigned char *bits = data;
npix = r.ras_width * r.ras_height;
if ((data = (unsigned char *) malloc (npix)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (5);
}
for (i=0, op=data; i < npix; i++)
*op++ = !((bits[i/8] & (1 << (7 - (i % 8)))) != 0);
free (bits);
}
if ((obuf = (unsigned char *) malloc (npix*2)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (5);
}
/* Find largest pixel value. */
for (i=0, maxval=v=0; i < npix; i++)
if (data[i] > v)
v = data[i];
maxval = v;
if (debug)
fprintf (stderr, "max pixel value = %d\n", maxval);
/* Print the colormap. */
n = r.ras_maplength / 3;
for (i=0; i < n; i++) {
colors[i].red = red[i];
colors[i].green = green[i];
colors[i].blue = blue[i];
}
if (n >= maxval)
n = maxval + 1;
printf ("colormap (length %d) = {\n ", nelem = n);
for (i=0, n=1; i < nelem; i++, n++) {
printf ("{%3d %3d %3d} ",
colors[i].red, colors[i].green, colors[i].blue);
if (n && n > 4) {
printf ("\n ");
n = 0;
}
}
if (n)
printf ("\n");
printf ("}\n");
if (r.ras_length <= 0) {
fprintf (stderr, "no data\n");
exit (7);
}
/* Print the pixels.
*/
/* Generate binary to hex1 (64 element) lookup table. */
for (n=0, op=hex1; n < 256; n++) {
i = (n % 64);
if (i < 10)
*op++ = i + '0';
else if (i < 36)
*op++ = (i - 10) + 'A';
else if (i < 62)
*op++ = (i - 36) + 'a';
else if (i == 62)
*op++ = '$';
else
*op++ = '_';
}
/* Generate binary to hex2 (256 element) lookup table. */
for (n=0, op=hex2; n < 256; n++) {
i = ((n >> 4) & 017);
*op++ = (i < 10) ? i + '0' : (i-10) + 'A';
i = (n & 017);
*op++ = (i < 10) ? i + '0' : (i-10) + 'A';
}
/* Hex encode the data. */
if (encoding == HEX1) {
/* Hex1 encoding uses only one character per pixel but the
* pixel range is restricted to 0 to 63.
*/
for (j=0, ip=data, op=obuf; j < r.ras_height; j++) {
for (i=0; i < r.ras_width; i++)
*op++ = hex1[*ip++];
if (r.ras_width % 2)
ip++;
}
} else if (encoding == HEX2) {
/* Hex2 encoding uses 2 characters per pixel and supports
* pixel values in the range 0 to 255.
*/
for (j=0, ip=data, op=obuf; j < r.ras_height; j++) {
for (i=0; i < r.ras_width; i++) {
v = *ip++ * 2;
*op++ = hex2[v];
*op++ = hex2[v+1];
}
if (r.ras_width % 2)
ip++;
}
}
*op = '\0';
/* Run length compress the data. The compressed data stream
* contains a mixture of literal data codes and repeat codes.
* A "@" followed by a hex1-encoded number N causes the most
* recent pixel value to be repeated N+1 times, where N < 64.
* A "%" followed by a hex2-encoded number N causes the most
* recent pixel value to be repeated N+1 times, where N < 256.
*/
if (compress) {
npix = r.ras_length;
if ((cbuf = (unsigned char *) malloc (npix*3)) == NULL) {
fprintf (stderr, "out of memory\n");
exit (8);
}
ip = obuf;
op = cbuf;
*op++ = v = *ip++;
while (*ip) {
for (n=0; n < 256 && *ip == v; ip++, n++)
;
if (n == 0) {
*op++ = v = *ip++;
} else if (n < 3) {
while (--n >= 0)
*op++ = v;
} else if (n <= 64) {
*op++ = '@';
*op++ = hex1[n-1];
} else if (n <= 256) {
*op++ = '%';
*op++ = hex2[(n-1)*2];
*op++ = hex2[(n-1)*2+1];
}
}
*op = '\0';
free (obuf);
obuf = cbuf;
}
/* Output the encoded pixel data.
*/
printf ("pixels (%s%s) = {\n ",
(encoding == HEX1) ? "hex1" : "hex2", compress ? " rle" : "");
for (ip=obuf, n=1; *ip; ip++, n++) {
putchar (*ip);
if (n && n > 72) {
printf ("\n ");
n = 0;
}
}
if (n)
printf ("\n");
printf ("}\n");
free (data);
free (obuf);
close (fd);
exit (0);
}
|