about summary refs log tree commit diff
path: root/converter/pgm
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-01-19 03:22:59 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-01-19 03:22:59 +0000
commitce08ed8af438892cf78323b45269ac5ac6684923 (patch)
tree1c21a7e43684c26afaec7d0ea8a94b4afcb02d28 /converter/pgm
parent4ccc95b66dfd0cd2d38d710c22d3233fc19b37c1 (diff)
downloadnetpbm-mirror-ce08ed8af438892cf78323b45269ac5ac6684923.tar.gz
netpbm-mirror-ce08ed8af438892cf78323b45269ac5ac6684923.tar.xz
netpbm-mirror-ce08ed8af438892cf78323b45269ac5ac6684923.zip
Add pgmtosbig
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2389 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/pgm')
-rw-r--r--converter/pgm/Makefile2
-rw-r--r--converter/pgm/pgmtosbig.c130
2 files changed, 131 insertions, 1 deletions
diff --git a/converter/pgm/Makefile b/converter/pgm/Makefile
index b109683b..2668a2a0 100644
--- a/converter/pgm/Makefile
+++ b/converter/pgm/Makefile
@@ -8,7 +8,7 @@ VPATH=.:$(SRCDIR)/$(SUBDIR)
 include $(BUILDDIR)/config.mk
 
 PORTBINARIES =	asciitopgm bioradtopgm fstopgm hipstopgm \
-		lispmtopgm pgmtofs pgmtolispm pgmtopgm \
+		lispmtopgm pgmtofs pgmtolispm pgmtopgm pgmtosbig \
 	        psidtopgm spottopgm sbigtopgm
 MATHBINARIES =	rawtopgm
 BINARIES =	$(PORTBINARIES) $(MATHBINARIES)
diff --git a/converter/pgm/pgmtosbig.c b/converter/pgm/pgmtosbig.c
new file mode 100644
index 00000000..0a302dd8
--- /dev/null
+++ b/converter/pgm/pgmtosbig.c
@@ -0,0 +1,130 @@
+/*=============================================================================
+                                 pgmtosbig
+===============================================================================
+
+  This program converts from PGM to a simple subset of SBIG.
+
+  By Bryan Henderson January 19, 2015.
+
+  Contributed to the public domain by its author.
+=============================================================================*/
+#include <string.h>
+
+#include "pm.h"
+#include "nstring.h"
+#include "pgm.h"
+
+
+
+#define SBIG_HEADER_LENGTH  2048      /* File header length */
+
+#define CTLZ "\x1A"
+
+
+struct SbigHeader {
+/*----------------------------------------------------------------------------
+   The information in an SBIG file header.
+
+   This is only the information this program cares about; the header
+   may have much more information in it.
+-----------------------------------------------------------------------------*/
+    unsigned int height;
+    unsigned int width;
+    unsigned int saturationLevel;
+};
+
+
+
+static void
+addUintParm(char *       const buffer,
+            const char * const name,
+            unsigned int const value) {
+
+    const char * line;
+
+    pm_asprintf(&line, "%s=%u\n\r", name, value);
+
+    strcat(buffer, line);
+
+    pm_strfree(line);
+}
+
+
+
+static void
+writeSbigHeader(FILE *            const ofP,
+                struct SbigHeader const hdr) {
+
+    char buffer[SBIG_HEADER_LENGTH];
+
+    memset(&buffer[0], 0x00, sizeof(buffer));
+
+    buffer[0] = '\0';
+
+    /* N.B. LF-CR instead of CRLF.  That's what the spec says. */
+
+    strcat(buffer, "ST-6 Image\n\r" );
+
+    addUintParm(buffer, "Height", hdr.height);
+
+    addUintParm(buffer, "Width", hdr.width);
+
+    addUintParm(buffer, "Sat_level", hdr.saturationLevel);
+
+    strcat(buffer, "End\n\r" CTLZ);
+
+    fwrite(buffer, 1, sizeof(buffer), ofP);
+}
+
+
+
+int
+main(int argc, const char * argv[]) {
+
+    FILE * ifP;
+    gray * grayrow;
+    int rows;
+    int cols;
+    int format;
+    struct SbigHeader hdr;
+    unsigned int row;
+    gray maxval;
+    const char * inputFile;
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 < 1)
+        inputFile = "-";
+    else {
+        inputFile = argv[1];
+
+        if (argc-1 > 2)
+            pm_error("Too many arguments.  The only argument is the optional "
+                     "input file name");
+    }
+
+    ifP = pm_openr(inputFile);
+
+    pgm_readpgminit(ifP, &cols, &rows, &maxval, &format);
+
+    grayrow = pgm_allocrow(cols);
+
+    hdr.height = rows;
+    hdr.width = cols;
+    hdr.saturationLevel = maxval;
+
+    writeSbigHeader(stdout, hdr);
+
+    for (row = 0; row < rows; ++row) {
+        unsigned int col;
+
+        pgm_readpgmrow(ifP, grayrow, cols, maxval, format);
+
+        for (col = 0; col < cols; ++col)
+            pm_writelittleshort(stdout, grayrow[col]);
+    }
+
+    pm_close(ifP);
+
+    return 0;
+}