[Liblas-commits] laszip: refactor to remove triplicated code paths

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Jan 13 14:21:14 EST 2011


details:   http://hg.liblas.orglaszip/rev/d2d8b98c7960
changeset: 144:d2d8b98c7960
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Jan 13 10:51:08 2011 -0800
description:
refactor to remove triplicated code paths
Subject: laszip: consolidate still more, so can add infinite loop

details:   http://hg.liblas.orglaszip/rev/e3ff0a074d82
changeset: 145:e3ff0a074d82
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Thu Jan 13 11:20:44 2011 -0800
description:
consolidate still more, so can add infinite loop

diffstat:

 tools/laszippertest.cpp |  774 ++++++++++++++++++++++-------------------------
 1 files changed, 356 insertions(+), 418 deletions(-)

diffs (truncated from 794 to 300 lines):

diff -r d464a9150b13 -r e3ff0a074d82 tools/laszippertest.cpp
--- a/tools/laszippertest.cpp	Wed Jan 12 13:55:19 2011 -0800
+++ b/tools/laszippertest.cpp	Thu Jan 13 11:20:44 2011 -0800
@@ -59,434 +59,372 @@
 #include <time.h>
 #include <stdio.h>
 
+//#define LASZIP_HAVE_RANGECODER
+
+
+//---------------------------------------------------------------------------
+
 static double taketime()
 {
   return (double)(clock())/CLOCKS_PER_SEC;
 }
 
+
+//---------------------------------------------------------------------------
+// abstractions for doing I/O, which support VC6 streams, modern streams, and FILE*
+
+struct OStream
+{
+public:
+    OStream(bool use_iostream, const char* filename) :
+      m_use_iostream(use_iostream),
+      m_filename(filename),
+      ofile(NULL),
+      ostream(NULL)
+    {
+        if (m_use_iostream)
+        {
+#ifdef LZ_WIN32_VC6 
+            ofb.open(filename, ios::out);
+            ofb.setmode(filebuf::binary);
+            ostream = new ostream(&ofb);
+#else
+            ostream = new ofstream();
+            ostream->open(filename, std::ios::out | std::ios::binary );
+#endif 
+        }
+        else
+        {
+            ofile = fopen(filename, "wb");
+        }
+
+        return;
+    }
+
+      ~OStream()
+      {
+          if (m_use_iostream)
+          {
+              delete ostream;
+#ifdef LZ_WIN32_VC6 
+              ofb.close();
+#endif
+          }
+          else
+          {
+              if (ofile)
+                  fclose(ofile);
+          }
+      }
+
+public:
+    bool m_use_iostream;
+    const char* m_filename;
+    FILE* ofile;
+    filebuf ofb;
+#ifdef LZ_WIN32_VC6 
+    ostream* ostream;
+#else
+    ofstream* ostream;
+#endif
+};
+
+
+//---------------------------------------------------------------------------
+
+struct IStream
+{
+public:
+    IStream(bool use_iostream, const char* filename) :
+      m_use_iostream(use_iostream),
+      m_filename(filename),
+      ifile(NULL),
+      istream(NULL)
+    {
+        if (m_use_iostream)
+        {
+#ifdef LZ_WIN32_VC6 
+            ifb.open(filename, ios::in);
+            ifb.setmode(filebuf::binary);
+            istream = new istream(&ifb);
+#else
+            istream = new ifstream();
+            istream->open(filename, std::ios::in | std::ios::binary );
+#endif 
+        }
+        else
+        {
+            ifile = fopen(filename, "rb");
+        }
+      }
+
+      ~IStream()
+      {
+          if (m_use_iostream)
+          {
+              delete istream;
+#ifdef LZ_WIN32_VC6 
+              ifb.close();
+#endif
+          }
+          else
+          {
+              if (ifile)
+                  fclose(ifile);
+          }
+      }
+
+public:
+    const char* m_filename;
+    bool m_use_iostream;
+    FILE* ifile;
+    filebuf ifb;
+#ifdef LZ_WIN32_VC6 
+    istream* istream;
+#else
+    ifstream* istream;
+#endif
+};
+
+
+//---------------------------------------------------------------------------
+
+class Data
+{
+public:
+    Data(unsigned int num_pts, bool random) :
+      num_points(num_pts),
+      use_random(random)
+    {
+        items[0].type = LASitem::POINT10;
+        items[0].size = 20;
+        items[0].version = 0;
+
+        items[1].type = LASitem::GPSTIME11;
+        items[1].size = 8;
+        items[1].version = 0;
+
+        items[2].type = LASitem::RGB12;
+        items[2].size = 6;
+        items[2].version = 0;
+
+        items[3].type = LASitem::WAVEPACKET13;
+        items[3].size = 29;
+        items[3].version = 0;
+
+        items[4].type = LASitem::BYTE;
+        items[4].size = 7;
+        items[4].version = 0;
+
+        unsigned int i;
+
+        // compute the point size
+        point_size = 0;
+        for (i = 0; i < num_items; i++) point_size += items[i].size;
+
+        // create the point data
+        unsigned int point_offset = 0;
+        point = new unsigned char*[num_items];
+        point_data = new unsigned char[point_size];
+        for (i = 0; i < num_items; i++)
+        {
+            point[i] = &(point_data[point_offset]);
+            point_offset += items[i].size;
+        }
+
+        return;
+    }
+
+    ~Data()
+    {
+        delete[] point;
+        delete[] point_data;
+        return;
+    }
+
+    static const unsigned int num_items = 5;
+    LASitem items[num_items];
+    unsigned int point_size;
+    unsigned char* point_data;
+    unsigned char** point;
+    unsigned num_points;
+    bool use_random;
+};
+
+
+//---------------------------------------------------------------------------
+
+static LASzipper* make_zipper(OStream* ost, Data& data, LASzip::Algorithm alg)
+{
+#ifndef LASZIP_HAVE_RANGECODER
+    if (alg == LASzip::POINT_BY_POINT_RANGE)
+    {
+        fprintf(stderr, "(skipping range encoder test)\n");
+        return NULL;
+    }
+#endif
+
+    LASzipper* zipper = new LASzipper();
+
+    int stat = 0;
+    if (ost->m_use_iostream)
+        stat = zipper->open(*ost->ostream, data.num_items, data.items, alg);
+    else
+        stat = zipper->open(ost->ofile, data.num_items, data.items, alg);
+
+    if (stat != 0)
+    {
+      fprintf(stderr, "ERROR: could not open laszipper with %s\n", ost->m_filename);
+      exit(1);
+    }
+
+    return zipper;
+}
+
+
+//---------------------------------------------------------------------------
+
+static LASunzipper* make_unzipper(IStream* ist, Data& data, LASzip::Algorithm alg)
+{
+#ifndef LASZIP_HAVE_RANGECODER
+    if (alg == LASzip::POINT_BY_POINT_RANGE)
+    {
+        return NULL;
+    }
+#endif
+
+    LASunzipper* unzipper = new LASunzipper();
+
+    int stat = 0;
+    if (ist->m_use_iostream)
+        stat = unzipper->open(*ist->istream, data.num_items, data.items, alg);
+    else
+        stat = unzipper->open(ist->ifile, data.num_items, data.items, alg);
+
+    if (stat != 0)
+    {
+      fprintf(stderr, "ERROR: could not open lasunzipper with %s\n", ist->m_filename);
+      exit(1);
+    }
+
+    return unzipper;
+}
+
+
+//---------------------------------------------------------------------------
+
+static void write_points(LASzipper* zipper, Data& data)
+{
+    if (zipper==NULL) // range coder test
+        return;
+
+    double start_time, end_time;
+    unsigned char c;
+    unsigned int i,j;
+
+    start_time = taketime();
+    c = 0;
+    for (i = 0; i < data.num_points; i++)
+    {
+        for (j = 0; j < data.point_size; j++)
+        {
+            data.point_data[j] = c;
+            c++;
+        }
+        zipper->write(data.point);
+    }
+
+    unsigned int num_bytes = zipper->close();
+    end_time = taketime();
+    fprintf(stderr, "laszipper wrote %d bytes in %g seconds\n", num_bytes, end_time-start_time);
+
+    return;
+}
+
+
+//---------------------------------------------------------------------------
+
+static void read_points(LASunzipper* unzipper, Data& data)
+{
+    if (unzipper==NULL) // range coder test
+        return;
+
+    unsigned char c;
+    unsigned int i,j;
+    unsigned int num_errors, num_bytes;


More information about the Liblas-commits mailing list