From ce08ed8af438892cf78323b45269ac5ac6684923 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Mon, 19 Jan 2015 03:22:59 +0000 Subject: Add pgmtosbig git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2389 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- converter/pgm/Makefile | 2 +- converter/pgm/pgmtosbig.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++ doc/HISTORY | 2 + 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 converter/pgm/pgmtosbig.c 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 + +#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; +} diff --git a/doc/HISTORY b/doc/HISTORY index b93186e1..a1cbe530 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -6,6 +6,8 @@ CHANGE HISTORY not yet BJH Release 10.70.00 + Add pgmtosbig. Mainly a test tool for sbigtopgm. + tifftopnm: allow input file to be nonseekable. Thanks Ludolf Holzheid . -- cgit 1.4.1