about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-05-25 03:02:46 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-05-25 03:02:46 +0000
commitbe68d56e96c48d548b33b487f12bf7850e9cb92c (patch)
tree86a0d5e5940df9f32ee6e00664edf6b20df1cdf8
parentf621ebcdaa736bed6cbb37ca28082e722398249b (diff)
downloadnetpbm-mirror-be68d56e96c48d548b33b487f12bf7850e9cb92c.tar.gz
netpbm-mirror-be68d56e96c48d548b33b487f12bf7850e9cb92c.tar.xz
netpbm-mirror-be68d56e96c48d548b33b487f12bf7850e9cb92c.zip
Make one PAM image per SRF image
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1490 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/other/pamtosrf.c108
-rw-r--r--converter/other/srf.c45
-rw-r--r--converter/other/srf.h12
-rw-r--r--converter/other/srftopam.c42
4 files changed, 103 insertions, 104 deletions
diff --git a/converter/other/pamtosrf.c b/converter/other/pamtosrf.c
index a65fe39e..a92be8b0 100644
--- a/converter/other/pamtosrf.c
+++ b/converter/other/pamtosrf.c
@@ -130,78 +130,47 @@ srfAlphaFromTuple(tuple              const t,
 
 
 static void
-producepam(struct cmdlineInfo const cmdline,
-           struct pam *       const pamP,
-           FILE *             const ofP) {
-/*----------------------------------------------------------------------------
-   Design note:  It's is really a modularity violation that we have
-   all the command line parameters as an argument.  We do it because we're
-   lazy -- it takes a great deal of work to carry all that information as
-   separate arguments -- and it's only a very small violation.
------------------------------------------------------------------------------*/
-    uint16_t width3d, height3d;
-    uint16_t widthOv, heightOv;
-    unsigned int row;
-    unsigned int imgCt;
-    unsigned int i;
-    struct srf srf;
+convertRaster(struct pam *     const pamP,
+              struct srf_img * const imgP) {
+
     tuple * tuplerow;
+    unsigned int row;
 
-    if (verbose)
-        pm_message("reading %ux%u image", pamP->width, pamP->height);
+    tuplerow = pnm_allocpamrow(pamP);
 
-    /* Figure out the dimensions.  The frame series should come in pairs,
-       each series should contain 36 frames, the first set should never
-       be smaller than the 2nd set, the sets should have the same dimension
-       combos as other sets, and each frame is square.
+    for (row = 0; row < pamP->height; ++row) {
+        uint32_t        const off   = row * pamP->height;
+        uint16_t *      const data  = &imgP->data.data[off];
+        unsigned char * const alpha = &imgP->alpha.data[off];
 
-       So if we have two frame series with the first being 80px tall and
-       the second is 60px tall, we can figure out things from there.
-    */
-    height3d = pamP->width / SRF_NUM_FRAMES;
-    for (row = 1; row <= pamP->height / height3d; ++row) {
-        heightOv = (pamP->height - (height3d * row)) / row;
-        if (heightOv <= height3d) {
-            if ((heightOv + height3d) * row == pamP->height)
-                break;
+        unsigned int col;
+
+        pnm_readpamrow(pamP, tuplerow);
+
+        for (col = 0; col < pamP->width; ++col) {
+            alpha[col] = srfAlphaFromTuple(tuplerow[col], pamP);
+            data[col]  = srfColorFromTuple(tuplerow[col], pamP);
         }
     }
-    imgCt = row * 2;
-    width3d = height3d * SRF_NUM_FRAMES;
-    widthOv = heightOv * SRF_NUM_FRAMES;
-
-    if (verbose)
-        pm_message("detected %u sets of 16-bit RGB images (%ux%u and %ux%u)",
-                   imgCt, width3d, height3d, widthOv, heightOv);
+    pnm_freepamrow(tuplerow);
+}
 
-    srf_init(&srf, imgCt, width3d, height3d, widthOv, heightOv);
 
-    /* Scan out each frame series */
-    tuplerow = pnm_allocpamrow(pamP);
-    for (i = 0; i < srf.header.img_cnt; ++i) {
-        struct srf_img * const imgP = &srf.imgs[i];
 
-        unsigned int row;
+static void
+convertImage(FILE *       const ifP,
+             struct srf * const srfP) {
 
-        for (row = 0; row < imgP->header.height; ++row) {
-            uint32_t        const off   = row * imgP->header.width;
-            uint16_t *      const data  = &imgP->data.data[off];
-            unsigned char * const alpha = &imgP->alpha.data[off];
+    struct pam inpam;
 
-            unsigned int col;
+    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
 
-            pnm_readpamrow(pamP, tuplerow);
-            for (col = 0; col < imgP->header.width; ++col) {
-                alpha[col] = srfAlphaFromTuple(tuplerow[col], pamP);
-                data[col]  = srfColorFromTuple(tuplerow[col], pamP);
-            }
-        }
-    }
-    pnm_freepamrow(tuplerow);
+    if (verbose)
+        pm_message("reading %ux%u image", inpam.width, inpam.height);
 
-    srf_write(ofP, &srf);
+    srf_create_img(srfP, inpam.width, inpam.height);
 
-    srf_term(&srf);
+    convertRaster(&inpam, &srfP->imgs[srfP->header.img_cnt-1]);
 }
 
 
@@ -211,7 +180,10 @@ main(int argc, const char * argv[]) {
 
   struct cmdlineInfo cmdline;
   FILE *             ifP;
-  struct pam         inPam;
+  struct srf         srf;
+  bool               eof;   /* No more images in input */
+  unsigned int       imageSeq;
+      /* Sequence of current image in input file.  First = 0 */
 
   pm_proginit(&argc, argv);
 
@@ -221,11 +193,25 @@ main(int argc, const char * argv[]) {
 
   ifP = pm_openr(cmdline.inputFileName);
 
-  pnm_readpaminit(ifP, &inPam, PAM_STRUCT_SIZE(tuple_type));
+  srf_init(&srf);
+
+  eof = FALSE;
+  for (imageSeq = 0; !eof; ++imageSeq) {
+      if (verbose)
+          pm_message("Converting Image %u", imageSeq);
+
+      convertImage(ifP, &srf);
 
-  producepam(cmdline, &inPam, stdout);
+      pnm_nextimage(ifP, &eof);
+  }
 
+  srf_write(stdout, &srf);
+    
+  srf_term(&srf);
   pm_closer(ifP);
 
   return 0;
 }
+
+
+
diff --git a/converter/other/srf.c b/converter/other/srf.c
index 508d40a0..ded049ac 100644
--- a/converter/other/srf.c
+++ b/converter/other/srf.c
@@ -593,7 +593,7 @@ srf_img_init(struct srf_img * const imgP,
 
 static void
 initPstring(struct srf_pstring * const pstringP,
-                 const char *         const s) {
+            const char *         const s) {
 
     pstringP->len = strlen(s);
 
@@ -610,23 +610,14 @@ initPstring(struct srf_pstring * const pstringP,
 
 
 void
-srf_init(struct srf * const srfP,
-         uint32_t     const imgCnt,
-         uint16_t     const width3d,
-         uint16_t     const height3d,
-         uint16_t     const widthOv,
-         uint16_t     const heightOv) {
+srf_init(struct srf * const srfP) {
 
     struct srf_header * const headerP = &srfP->header;
-    unsigned int i;
-
-    if (imgCnt == 0 || imgCnt % 2 != 0)
-        pm_error("invalid image count");
 
     strcpy(headerP->magic, SRF_MAGIC);
     headerP->_int4[0] = 4;
     headerP->_int4[1] = 4;
-    headerP->img_cnt = imgCnt;
+    headerP->img_cnt = 0;
     headerP->_int5 = 5;
     initPstring(&headerP->s578, "578");
     headerP->_int6 = 6;
@@ -634,16 +625,30 @@ srf_init(struct srf * const srfP,
     headerP->_int7 = 7;
     initPstring(&headerP->prod, "006-D0578-XX");
 
-    MALLOCARRAY(srfP->imgs, headerP->img_cnt);
+    srfP->imgs = NULL;
+}
 
-    if (!srfP->imgs)
-        pm_error("Could not allocate memory for %u images", headerP->img_cnt);
 
-    for (i = 0; i < headerP->img_cnt; i += 2) {
-        srf_img_init(&srfP->imgs[i], width3d, height3d);
-        srf_img_init(&srfP->imgs[i + 1], widthOv, heightOv);
-    }
-}
 
+void
+srf_create_img(struct srf * const srfP,
+               uint16_t     const width,
+               uint16_t     const height) {
+/*----------------------------------------------------------------------------
+   Add an "image" to the SRF.  An image is a horizontal series of 36 
+   square frames, each showing a different angle view of an object, 10
+   degrees about.  At least that's what it's supposed to be.  We don't
+   really care -- it's just an arbitrary rectangular raster image to us.
+-----------------------------------------------------------------------------*/
+    
+    ++srfP->header.img_cnt;
 
+    REALLOCARRAY(srfP->imgs, srfP->header.img_cnt);
 
+    if (!srfP->imgs)
+        pm_error("Could not allocate memory for %u images",
+                 srfP->header.img_cnt);
+
+    srf_img_init(&srfP->imgs[srfP->header.img_cnt-1], width, height);
+}
+                 
diff --git a/converter/other/srf.h b/converter/other/srf.h
index 1821f305..e06355a4 100644
--- a/converter/other/srf.h
+++ b/converter/other/srf.h
@@ -159,12 +159,12 @@ void
 srf_term(struct srf * const srfP);
 
 void
-srf_init(struct srf * const srfP,
-         uint32_t     const imgCnt,
-         uint16_t     const width3d,
-         uint16_t     const height3d,
-         uint16_t     const widthOv,
-         uint16_t     const heightOv);
+srf_init(struct srf * const srfP);
+
+void
+srf_create_img(struct srf * const srfP,
+               uint16_t     const width,
+               uint16_t     const height);
 
 #endif
 
diff --git a/converter/other/srftopam.c b/converter/other/srftopam.c
index 398077f3..efe55253 100644
--- a/converter/other/srftopam.c
+++ b/converter/other/srftopam.c
@@ -152,30 +152,20 @@ writeRaster(struct pam *     const pamP,
 
 
 static void
-srftopam(struct cmdlineInfo const cmdline,
-         FILE *             const ifP,
-         FILE *             const ofP) {
+convertOneImage(struct srf_img * const imgP,
+                FILE *           const ofP) {
 
     const char * comment = "Produced by srftopam";  /* constant */
-    long         width, height;
-    unsigned int i;
-    struct srf   srf;
-    struct pam   outPam;
-
-    srf_read(ifP, verbose, &srf);
 
-    for (i = 0, width = 0, height = 0; i < srf.header.img_cnt; ++i) {
-        width = MAX(width, srf.imgs[i].header.width);
-        height += srf.imgs[i].header.height;
-    }
+    struct pam   outPam;
 
     outPam.size             = sizeof(struct pam);
     outPam.len              = PAM_STRUCT_SIZE(comment_p);
     outPam.file             = ofP;
     outPam.format           = PAM_FORMAT;
     outPam.plainformat      = 0;
-    outPam.width            = width;
-    outPam.height           = height;
+    outPam.width            = imgP->header.width;
+    outPam.height           = imgP->header.height;
     outPam.depth            = 4;
     outPam.maxval           = 255;
     outPam.bytes_per_sample = 1;
@@ -185,8 +175,26 @@ srftopam(struct cmdlineInfo const cmdline,
 
     pnm_writepaminit(&outPam);
 
-    for (i = 0; i < srf.header.img_cnt; ++i)
-        writeRaster(&outPam, &srf.imgs[i]);
+    writeRaster(&outPam, imgP);
+}
+
+
+
+static void
+srftopam(struct cmdlineInfo const cmdline,
+         FILE *             const ifP,
+         FILE *             const ofP) {
+
+    unsigned int imgSeq;
+    struct srf   srf;
+
+    srf_read(ifP, verbose, &srf);
+
+    for (imgSeq = 0; imgSeq < srf.header.img_cnt; ++imgSeq) {
+        if (verbose)
+            pm_message("Converting Image %u", imgSeq);
+        convertOneImage(&srf.imgs[imgSeq], ofP);
+    }
 
     srf_term(&srf);
 }