about summary refs log tree commit diff
path: root/converter/other
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-02-09 03:34:23 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2010-02-09 03:34:23 +0000
commit524a88e2da289050397373b2f013b84593f99750 (patch)
treeb9763c8a4945218c71f0113059703563ff97fd9d /converter/other
parent403e5c481906869d0e9b16382d08aa0c42400e83 (diff)
downloadnetpbm-mirror-524a88e2da289050397373b2f013b84593f99750.tar.gz
netpbm-mirror-524a88e2da289050397373b2f013b84593f99750.tar.xz
netpbm-mirror-524a88e2da289050397373b2f013b84593f99750.zip
Add pamtoavs, avstopam
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1119 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other')
-rw-r--r--converter/other/Makefile4
-rw-r--r--converter/other/avstopam.c110
-rw-r--r--converter/other/pamtoavs.c152
3 files changed, 264 insertions, 2 deletions
diff --git a/converter/other/Makefile b/converter/other/Makefile
index 83676aaa..1417cd3a 100644
--- a/converter/other/Makefile
+++ b/converter/other/Makefile
@@ -77,9 +77,9 @@ ifeq ($(TIFFLIB_NEEDS_Z),Y)
   endif
 endif
 
-PORTBINARIES =  bmptopnm fitstopnm \
+PORTBINARIES =  avstopam bmptopnm fitstopnm \
 		gemtopnm giftopnm hdifftopam infotopam \
-		pamtodjvurle pamtofits pamtogif \
+		pamtoavs pamtodjvurle pamtofits pamtogif \
 		pamtohdiff pamtohtmltbl pamtompfont pamtooctaveimg \
 		pamtopam pamtopfm pamtopnm pamtouil \
 		pamtoxvmini \
diff --git a/converter/other/avstopam.c b/converter/other/avstopam.c
new file mode 100644
index 00000000..a35fdcea
--- /dev/null
+++ b/converter/other/avstopam.c
@@ -0,0 +1,110 @@
+/* ----------------------------------------------------------------------
+ *
+ * Convert an AVS X image to a PAM image
+ *
+ * By Scott Pakin <scott+pbm@pakin.org>
+ *
+ * ----------------------------------------------------------------------
+ *
+ * Copyright (C) 2010 Scott Pakin <scott+pbm@pakin.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ * ----------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+
+#include "pm.h"
+#include "pam.h"
+
+
+
+static void
+producePam(FILE *       const avsFileP,
+           struct pam * const pamP) {
+
+    tuple *      tuplerow;
+    unsigned int row;
+
+    tuplerow = pnm_allocpamrow(pamP);
+    for (row = 0; row < pamP->height; ++row) {
+        unsigned int col;
+        for (col = 0; col < pamP->width; ++col) {
+            tuple const thisTuple = tuplerow[col];
+            char c;
+            pm_readchar(avsFileP, &c); thisTuple[3] = c;
+            pm_readchar(avsFileP, &c); thisTuple[0] = c;
+            pm_readchar(avsFileP, &c); thisTuple[1] = c;
+            pm_readchar(avsFileP, &c); thisTuple[2] = c;
+        }
+        pnm_writepamrow(pamP, tuplerow);
+    }
+    pnm_freepamrow(tuplerow);
+}
+
+
+
+int
+main(int argc, const char *argv[]) {
+
+    const char * comment = "Produced by avstopam";  /* constant */
+
+    struct pam   outPam;
+    const char * inputFilename;
+    const char * outputFilename;
+    FILE       * inFileP;
+    FILE       * outFileP;
+    long         width;
+    long         height;
+
+    pm_proginit(&argc, argv);
+
+    inputFilename = (argc > 1) ? argv[1] : "-";
+    outputFilename = (argc > 2) ? argv[2] : "-";
+
+    inFileP = pm_openr(inputFilename);
+
+    pm_readbiglong(inFileP, &width);
+    pm_readbiglong(inFileP, &height);
+
+    outFileP = pm_openw(outputFilename);
+
+    outPam.size             = sizeof(struct pam);
+    outPam.len              = PAM_STRUCT_SIZE(comment_p);
+    outPam.file             = outFileP;
+    outPam.format           = PAM_FORMAT;
+    outPam.plainformat      = 0;
+    outPam.width            = width;
+    outPam.height           = height;
+    outPam.depth            = 4;
+    outPam.maxval           = 255;
+    outPam.bytes_per_sample = 1;
+    sprintf(outPam.tuple_type, "RGB_ALPHA");
+    outPam.allocation_depth = 4;
+    outPam.comment_p        = &comment;
+
+    /* Produce a PAM output header.  Note that AVS files *always*
+       contain four channels with one byte per channel.
+    */
+    pnm_writepaminit(&outPam);
+
+    producePam(inFileP, &outPam);
+
+    pm_closew(outFileP);
+    pm_closer(inFileP);
+
+    return 0;
+}
+
diff --git a/converter/other/pamtoavs.c b/converter/other/pamtoavs.c
new file mode 100644
index 00000000..740042e6
--- /dev/null
+++ b/converter/other/pamtoavs.c
@@ -0,0 +1,152 @@
+/* ----------------------------------------------------------------------
+ *
+ * Convert a PAM image to an AVS X image
+ *
+ * By Scott Pakin <scott+pbm@pakin.org>
+ *
+ * ----------------------------------------------------------------------
+ *
+ * Copyright (C) 2010 Scott Pakin <scott+pbm@pakin.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ * ----------------------------------------------------------------------
+ */
+
+#include <stdio.h>
+#include "pm.h"
+#include "pam.h"
+
+
+
+static char
+sample2char(sample const s,
+            sample const maxval) {
+/* Scale down a sample to a single byte. */
+
+    return maxval==255 ? s : s * 255 / maxval;
+}
+
+
+#define THIS_SAMPLE_CHAR(PLANE) \
+  sample2char(tuplerow[col][PLANE], pamP->maxval)
+
+static void
+produceAvs(struct pam * const pamP,
+           FILE *       const avsFileP) {
+
+    tuple * tuplerow;
+
+    /* Write the AVS header (image width and height as 4-byte
+       big-endian integers).
+    */
+    pm_writebiglong(avsFileP, pamP->width);
+    pm_writebiglong(avsFileP, pamP->height);
+
+    /* Write the AVS data (alpha, red, green, blue -- one byte apiece. */
+    tuplerow = pnm_allocpamrow(pamP);
+    switch (pamP->depth) {
+    case 1: {
+        /* Black-and-white or grayscale, no alpha */
+        unsigned int row;
+        for (row = 0; row < pamP->height; ++row) {
+            unsigned int col;
+            pnm_readpamrow(pamP, tuplerow);
+            for (col = 0; col < pamP->width; ++col) {
+                pm_writechar(avsFileP, (char)255);
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+            }
+        }
+    } break;
+
+    case 2: {
+        /* Black-and-white or grayscale plus alpha */
+        unsigned int row;
+        for (row = 0; row < pamP->height; ++row) {
+            unsigned int col;
+            pnm_readpamrow(pamP, tuplerow);
+            for (col = 0; col < pamP->width; ++col) {
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+            }
+        }
+    } break;
+
+    case 3: {
+        /* RGB, no alpha */
+        unsigned int row;
+        for (row = 0; row < pamP->height; ++row) {
+            unsigned int col;
+            pnm_readpamrow(pamP, tuplerow);
+            for (col = 0; col < pamP->width; ++col) {
+                pm_writechar(avsFileP, (char)255);
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2));
+            }
+        }
+    } break;
+
+    case 4: {
+        /* RGB plus alpha */
+        unsigned int row;
+        for (row = 0; row < pamP->height; ++row) {
+            unsigned int col;
+            pnm_readpamrow( pamP, tuplerow );
+            for (col = 0; col < pamP->width; ++col) {
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(3));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(0));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(1));
+                pm_writechar(avsFileP, THIS_SAMPLE_CHAR(2));
+            }
+        }
+    } break;
+
+    default:
+        pm_error("Unrecognized PAM depth %u.  We understand only "
+                 "1, 2, 3, and 4", pamP->depth);
+        break;
+    }
+    pnm_freepamrow(tuplerow);
+}
+
+
+
+int
+main(int argc, const char *argv[]) {
+    struct pam   inPam;
+    const char * inputFilename;
+    const char * outputFilename;
+    FILE       * inFileP;
+    FILE       * outFileP;
+
+    pm_proginit(&argc, argv);
+    inputFilename = (argc > 1) ? argv[1] : "-";
+    outputFilename = (argc > 2) ? argv[2] : "-";
+    inFileP = pm_openr(inputFilename);
+    pnm_readpaminit(inFileP, &inPam, PAM_STRUCT_SIZE(tuple_type));
+    outFileP = pm_openw(outputFilename);
+
+    produceAvs(&inPam, outFileP);
+
+    pm_closew(outFileP);
+    pm_closer(inFileP);
+
+    return 0;
+}
+