about summary refs log tree commit diff
path: root/editor/pamenlarge.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-08-19 03:12:28 +0000
commit1fd361a1ea06e44286c213ca1f814f49306fdc43 (patch)
tree64c8c96cf54d8718847339a403e5e67b922e8c3f /editor/pamenlarge.c
downloadnetpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.gz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.tar.xz
netpbm-mirror-1fd361a1ea06e44286c213ca1f814f49306fdc43.zip
Create Subversion repository
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'editor/pamenlarge.c')
-rw-r--r--editor/pamenlarge.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/editor/pamenlarge.c b/editor/pamenlarge.c
new file mode 100644
index 00000000..15b91b4f
--- /dev/null
+++ b/editor/pamenlarge.c
@@ -0,0 +1,117 @@
+/*=============================================================================
+                             pamenlarge
+===============================================================================
+  By Bryan Henderson 2004.09.26.  Contributed to the public domain by its
+  author.
+=============================================================================*/
+
+#include "pam.h"
+#include "mallocvar.h"
+
+struct cmdlineInfo {
+    /* All the information the user supplied in the command line,
+       in a form easy for the program to use.
+    */
+    const char *inputFilespec;  
+    unsigned int scaleFactor;
+};
+
+
+
+static void
+parseCommandLine(int argc, char ** const argv,
+                 struct cmdlineInfo * const cmdlineP) {
+/*----------------------------------------------------------------------------
+   Note that the file spec array we return is stored in the storage that
+   was passed to us as the argv array.
+-----------------------------------------------------------------------------*/
+    if (argc-1 < 1)
+        pm_error("You must specify at least one argument:  The scale factor");
+    else {
+        cmdlineP->scaleFactor = atoi(argv[1]);
+        
+        if (cmdlineP->scaleFactor < 1)
+            pm_error("Scale factor must be an integer at least 1.  "
+                     "You specified '%s'", argv[1]);
+
+        if (argc-1 >= 2)
+            cmdlineP->inputFilespec = argv[2];
+        else
+            cmdlineP->inputFilespec = "-";
+    }
+}        
+
+
+
+static void
+makeOutputRowMap(tuple **     const outTupleRowP,
+                 struct pam * const outpamP,
+                 struct pam * const inpamP,
+                 tuple *      const inTuplerow) {
+/*----------------------------------------------------------------------------
+   Create a tuple *outTupleRowP which is actually a row of pointers into
+   inTupleRow[], so as to map input pixels to output pixels by stretching.
+-----------------------------------------------------------------------------*/
+    tuple * newtuplerow;
+    int col;
+
+    MALLOCARRAY_NOFAIL(newtuplerow, outpamP->width);
+
+    for (col = 0 ; col < inpamP->width; ++col) {
+        unsigned int const scaleFactor = outpamP->width / inpamP->width;
+        unsigned int subcol;
+
+        for (subcol = 0; subcol < scaleFactor; ++subcol)
+            newtuplerow[col * scaleFactor + subcol] = inTuplerow[col];
+    }
+    *outTupleRowP = newtuplerow;
+}
+
+
+
+int
+main(int    argc, 
+     char * argv[]) {
+
+    struct cmdlineInfo cmdline;
+    FILE * ifP;
+    struct pam inpam;
+    struct pam outpam; 
+    tuple * tuplerow;
+    tuple * newtuplerow;
+    int row;
+
+    pnm_init(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFilespec);
+ 
+    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
+
+    outpam = inpam; 
+    outpam.file   = stdout;
+    outpam.width  = inpam.width * cmdline.scaleFactor;
+    outpam.height = inpam.height * cmdline.scaleFactor; 
+
+    pnm_writepaminit(&outpam);
+
+    tuplerow = pnm_allocpamrow(&inpam);
+
+    makeOutputRowMap(&newtuplerow, &outpam, &inpam, tuplerow);
+
+    for (row = 0; row < inpam.height; ++row) {
+        pnm_readpamrow(&inpam, tuplerow);
+        pnm_writepamrowmult(&outpam, newtuplerow, cmdline.scaleFactor);
+    }
+
+    free(newtuplerow);
+
+    pnm_freepamrow(tuplerow);
+
+    pm_close(ifP);
+    pm_close(stdout);
+
+    return 0;
+}
+