aboutsummaryrefslogtreecommitdiff
path: root/vendor/x11iraf/util
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
commitfa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 (patch)
treebdda434976bc09c864f2e4fa6f16ba1952b1e555 /vendor/x11iraf/util
downloadiraf-linux-fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4.tar.gz
Initial commit
Diffstat (limited to 'vendor/x11iraf/util')
-rw-r--r--vendor/x11iraf/util/ras2bin.c201
-rw-r--r--vendor/x11iraf/util/ras2text.c277
2 files changed, 478 insertions, 0 deletions
diff --git a/vendor/x11iraf/util/ras2bin.c b/vendor/x11iraf/util/ras2bin.c
new file mode 100644
index 00000000..93be1203
--- /dev/null
+++ b/vendor/x11iraf/util/ras2bin.c
@@ -0,0 +1,201 @@
+#include <stdio.h>
+
+/*
+ * RAS2BIN -- Convert a hex encoded raster to binary.
+ *
+ * Usage: ras2bin -encoding encoding -nx nx -ny ny infile outfile
+ *
+ * encoding hex1, hex2, hex1-rle, hex2-rle
+ * nx, ny raster size in pixels
+ * input input hex encoded, possible RLE (run length) encoded
+ * block of text. whitespace is ignored otherwise the file
+ * should contain only data.
+ * output output file to receive the binary array of pixels
+ *
+ * Perhaps this ought to take ras2text output and generate a Sun rasterfile,
+ * but currently we just decode the pixel data and output it in binary.
+ */
+main (argc, argv)
+int argc;
+char **argv;
+{
+ char *infile=NULL, *outfile=NULL;
+ char pixels[102400];
+ int in, out, nx, ny, bias;
+ char *encoding;
+ int i;
+
+ for (i=1; i < argc; i++)
+ if (argv[i][0] == '-') {
+ if (strncmp (argv[i], "-en", 3) == 0)
+ encoding = argv[++i];
+ else if (strcmp (argv[i], "-nx") == 0)
+ nx = atoi (argv[++i]);
+ else if (strcmp (argv[i], "-ny") == 0)
+ ny = atoi (argv[++i]);
+ else if (strcmp (argv[i], "-bias") == 0)
+ bias = atoi (argv[++i]);
+ } else if (!infile) {
+ infile = argv[i];
+ } else if (!outfile)
+ outfile = argv[i];
+
+ if (!infile || !outfile || nx <= 0 || ny <= 0) {
+ fprintf (stderr, "bad usage\n");
+ exit (1);
+ }
+
+ printf ("input = %s\n", infile);
+ if ((in = open (infile, 0)) < 0) {
+ fprintf (stderr, "cannot open %s\n", infile);
+ exit (2);
+ }
+ printf ("read %d chars from input\n", read (in, pixels, 102400));
+ close (in);
+
+ printf ("output = %s\n", outfile);
+ if ((out = creat (outfile, 0644)) < 0) {
+ fprintf (stderr, "cannot open %s\n", outfile);
+ exit (3);
+ }
+
+ printRaster (out, pixels, encoding, nx, ny, bias);
+ close (out);
+}
+
+printRaster (out, pixels, encoding, nx, ny, bias)
+int out;
+char *pixels;
+char *encoding;
+int nx, ny;
+int bias;
+{
+ register char *ip;
+ register unsigned char *op;
+ register int v, i, j;
+ static unsigned char hex1[256], hex2[256];
+ static int have_tables = 0;
+ unsigned char *data, *otop;
+ char *sv_pixels = pixels;
+
+ /* Generate hex to binary lookup tables in first call. */
+ if (!have_tables) {
+ /* Generate char-to-binary table for the hex1 encoding. */
+ for (i=0; i < 256; i++)
+ hex1[i] = 0177;
+ for (i='0'; i <= '9'; i++)
+ hex1[i] = i - '0';
+ for (i='A'; i <= 'Z'; i++)
+ hex1[i] = i - 'A' + 10;
+ for (i='a'; i <= 'z'; i++)
+ hex1[i] = i - 'a' + 36;
+ hex1['$'] = 62;
+ hex1['_'] = 63;
+
+ /* Generate char-to-binary table for the hex2 encoding. */
+ for (i=0; i < 256; i++)
+ hex2[i] = 0177;
+ for (i='0'; i <= '9'; i++)
+ hex2[i] = i - '0';
+ for (i='a'; i <= 'f'; i++)
+ hex2[i] = i - 'a' + 10;
+ for (i='A'; i <= 'F'; i++)
+ hex2[i] = i - 'A' + 10;
+
+ have_tables++;
+ }
+
+ /* Decode the pixel data. */
+ if (!(data = (unsigned char *) malloc (nx * ny)))
+ return (-1);
+ otop = data + nx * ny;
+
+ /* Uncompress the input if RLE compression is indicated. */
+ if (strcmp (&encoding[strlen(encoding)-4], "-rle") == 0) {
+ int buflen = nx * ny * 2;
+ char *ibuf, *op;
+ int ch;
+
+ /* Get buffer to hold the uncompressed pixel data array. */
+ if (!(ibuf = (char *) malloc (buflen + 1)))
+ goto err;
+
+ /* Uncompress the pixel array. */
+ for (ip=pixels, op=ibuf; *ip; ) {
+ while (isspace (*ip))
+ ip++;
+
+ if ((ch = *ip++) == '@') {
+ if ((i = hex1[*ip++]) >= 0x7f)
+ while (*ip && ((i = hex1[*ip++]) >= 0x7f))
+ ;
+ if (op-ibuf + i + 1 > buflen)
+ goto err;
+ for (v = *(op-1), i++; --i >= 0; )
+ *op++ = v;
+
+ } else if (ch == '%') {
+ if ((i = hex2[*ip++]) >= 0x7f)
+ while (*ip && ((i = hex2[*ip++]) >= 0x7f))
+ ;
+ if ((j = hex2[*ip++]) >= 0x7f)
+ while (*ip && ((j = hex2[*ip++]) >= 0x7f))
+ ;
+ i = ((i << 4) | j) + 1;
+ if (op-ibuf + i > buflen)
+ goto err;
+ for (v = *(op-1); --i >= 0; )
+ *op++ = v;
+
+ } else
+ *op++ = ch;
+ }
+
+ *op = '\0';
+ pixels = ibuf;
+ }
+
+ /* Convert the ascii pixels array to a binary data array.
+ */
+ if (strcmp (encoding, "numeric") == 0) {
+ while (isspace (*ip))
+ ip++;
+ for (ip=pixels, op=data; *ip && op < otop; ) {
+ for (v=0; isdigit(*ip); )
+ v = v * 10 + *ip++ - '0';
+ *op++ = v + bias;
+ while (isspace (*ip))
+ ip++;
+ }
+ } else if (strncmp (encoding, "hex1", 4) == 0) {
+ for (ip=pixels, op=data; *ip && op < otop; ) {
+ if ((v = hex1[*ip++]) > 0xf)
+ while (*ip && ((v = hex1[*ip++]) > 0xf))
+ ;
+ *op++ = v + bias;
+ }
+ } else if (strncmp (encoding, "hex2", 4) == 0) {
+ for (ip=pixels, op=data; *ip && op < otop; ) {
+ if ((v = hex2[*ip++]) > 0xf)
+ while (*ip && ((v = hex2[*ip++]) > 0xf))
+ ;
+ if ((i = hex2[*ip++]) > 0xf)
+ while (*ip && ((i = hex2[*ip++]) > 0xf))
+ ;
+ *op++ = ((v << 4) | i) + bias;
+ }
+ } else {
+err: free ((char *)data);
+ if (pixels != sv_pixels)
+ free (pixels);
+ return (-1);
+ }
+
+ /* Write the pixels. */
+ write (out, data, op - data);
+ free ((char *)data);
+ if (pixels != sv_pixels)
+ free (pixels);
+
+ return (0);
+}
diff --git a/vendor/x11iraf/util/ras2text.c b/vendor/x11iraf/util/ras2text.c
new file mode 100644
index 00000000..7e490a82
--- /dev/null
+++ b/vendor/x11iraf/util/ras2text.c
@@ -0,0 +1,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);
+}