[opengtl-commits] [295] * load/save image in the shiva tool

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 295
Author:   cyrille
Date:     2008-07-02 00:24:38 +0200 (Wed, 02 Jul 2008)

Log Message:
-----------
* load/save image in the shiva tool
* shivatester can be used to compare the output of a kernel with an image

Modified Paths:
--------------
    trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt
    trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp
    trunk/OpenGTL/OpenShiva/tools/tester/CMakeLists.txt

Added Paths:
-----------
    trunk/OpenGTL/OpenShiva/tools/tester/ShivaTester.cpp

Removed Paths:
-------------
    trunk/OpenGTL/OpenShiva/tools/tester/Shiva.cpp


Modified: trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt	2008-07-01 22:23:37 UTC (rev 294)
+++ trunk/OpenGTL/OpenShiva/tools/interpreter/CMakeLists.txt	2008-07-01 22:24:38 UTC (rev 295)
@@ -7,5 +7,5 @@
 add_definitions( -DCOUMPONENT_NAME=\"\\\"Shiva\\\"\" )
 
 add_executable(shiva ${shiva_SRCS})
-target_link_libraries(shiva OpenShiva )
+target_link_libraries(shiva OpenShiva GTLImageIO )
 install( TARGETS shiva DESTINATION ${BIN_INSTALL_DIR} )

Modified: trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp	2008-07-01 22:23:37 UTC (rev 294)
+++ trunk/OpenGTL/OpenShiva/tools/interpreter/Shiva.cpp	2008-07-01 22:24:38 UTC (rev 295)
@@ -25,9 +25,14 @@
 
 // GTLCore Headers
 #include <GTLCore/PixelDescription.h>
+#include <GTLCore/Debug.h>
 #include <GTLCore/Region.h>
 #include <GTLCore/Type.h>
 
+// GTLImageIO Headers
+#include <GTLImageIO/ImageDC.h>
+#include <GTLImageIO/ImageDCRegistry.h>
+
 // OpenShiva Headers
 #include <OpenShiva/Debug.h>
 #include <GTLCore/Image.h>
@@ -46,6 +51,8 @@
   std::cout << std::endl;
   std::cout << "Options : " << std::endl;
   std::cout << "  -S --asm-source         print the assembly source code generated after the execution of the kernel" << std::endl;
+  std::cout << "  -w --width [w]          define the width of the output" << std::endl;
+  std::cout << "  -h --height [h]         define the height of the output" << std::endl;
 //   std::cout << "  -L --module-dir   add a location where to find modules" << std::endl;
   std::cout << std::endl;
   std::cout << "  -h --help               print this message" << std::endl;
@@ -56,8 +63,12 @@
 int main(int argc, char** argv)
 {
   GTLCore::String fileName = "";
+  GTLCore::String outputFileName = "";
   GTLCore::String output = "";
+  std::list< GTLCore::String > fileNames;
   bool showAssembly = false;
+  int width = 800;
+  int height = 600;
   for(int ai = 1; ai < argc; ai++)
   {
     if(ARG_IS("-h","--help"))
@@ -79,19 +90,38 @@
         ++ai;
 //         OpenShiva::ModulesManager::instance()->addDirectory(argv[ai]);
       }
-    } else {
-      SHIVA_DEBUG( ai << " " << (int)argc << " " << ( ai > argc - 2) );
-      if( ai > argc - 2)
+    } else if(ARG_IS("-w", "--width")) {
+      if( ai == argc )
       {
-        std::cerr << "Invalid command line parameters." << std::endl;
+        std::cerr << "Expected width after -w --width." << std::endl;
+        return EXIT_FAILURE;
       } else {
-        fileName = argv[ai];
+        ++ai;
+        width = GTLCore::String( argv[ai] ).toInt();
       }
-      break;
+    } else if(ARG_IS("-h", "--height")) {
+      if( ai == argc )
+      {
+        std::cerr << "Expected width after -h --height." << std::endl;
+        return EXIT_FAILURE;
+      } else {
+        ++ai;
+        height = GTLCore::String( argv[ai] ).toInt();
+      }
+    } else {
+      fileNames.push_back( argv[ai] );
     }
   }
+  if( fileNames.size() > 1 )
+  {
+    fileName = *( fileNames.begin() );
+    fileNames.pop_front();
+    outputFileName = *( --fileNames.end() );
+    fileNames.pop_back();
+  }
   if( fileName == "")
   {
+    std::cerr << "Invalid command line parameters." << std::endl;
     printHelp();
   } else {
     GTLCore::String source;
@@ -114,21 +144,46 @@
     OpenShiva::Kernel p(fileName, channelsCount);
     p.setSource( source );
     p.compile();
+    GTLCore::String errMsg;
     if(not p.isCompiled())
     {
-      std::cout << "Error: " << std::endl << p.compilationErrorsMessage() << std::endl;
+      std::cerr << "Error: " << std::endl << p.compilationErrorsMessage() << std::endl;
       return EXIT_FAILURE;
     }
-    GTLCore::Image image(200,300, pixel );
-//     memset( image.data(0, 0), 1, 200 * 300 );
+    GTLCore::Image image(width, height, pixel );
     std::list<GTLCore::AbstractImage*> images;
-    images.push_back( new GTLCore::Image(0,0, pixel ) );
-    p.evaluatePixeles( GTLCore::Region(0,0, 200, 300), images, &image );
-    std::cout << *((char*)image.data( 0, 0));
+    for( std::list< GTLCore::String >::iterator it = fileNames.begin();
+         it != fileNames.end(); ++it )
+    {
+      const GTLImageIO::ImageDC* decoder = GTLImageIO::ImageDCRegistry::instance()->decoder( *it );
+      if( not decoder )
+      {
+        std::cerr << "Can't find decoder for " << *it << std::endl;
+        return EXIT_FAILURE;
+      }
+      GTLCore::AbstractImage* image = decoder->decode( *it, 0, &errMsg );
+      if( not image )
+      {
+        std::cerr << "Can't read image " << *it << " : " << errMsg << std::endl;
+      }
+      images.push_back( image );
+    }
+    p.evaluatePixeles( GTLCore::Region(0,0, width, height), images, &image );
     if( showAssembly )
     {
       std::cout << p.asmSourceCode();
     }
+    const GTLImageIO::ImageDC* encoder = GTLImageIO::ImageDCRegistry::instance()->encoder( outputFileName );
+    if( not encoder )
+    {
+      std::cerr << "Can't find encoder for " << outputFileName << std::endl;
+      return EXIT_FAILURE;
+    }
+    if( not encoder->encode( &image, GTLCore::Region(0, 0, width, height), outputFileName, &errMsg ) )
+    {
+      std::cerr << "Can't encode " << outputFileName << " : " << errMsg << std::endl;
+      return EXIT_FAILURE;
+    }
   }
   return EXIT_SUCCESS;
 }

Modified: trunk/OpenGTL/OpenShiva/tools/tester/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/tester/CMakeLists.txt	2008-07-01 22:23:37 UTC (rev 294)
+++ trunk/OpenGTL/OpenShiva/tools/tester/CMakeLists.txt	2008-07-01 22:24:38 UTC (rev 295)
@@ -1,9 +1,9 @@
 include_directories( ${CMAKE_SOURCE_DIR} )
 
 set(shivatester_SRCS
-  Shiva.cpp
+  ShivaTester.cpp
     )
 
 add_executable(shivatester ${shivatester_SRCS})
-target_link_libraries(shivatester OpenShiva )
+target_link_libraries(shivatester OpenShiva GTLImageIO )
 install( TARGETS shivatester DESTINATION ${BIN_INSTALL_DIR} )

Deleted: trunk/OpenGTL/OpenShiva/tools/tester/Shiva.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/tester/Shiva.cpp	2008-07-01 22:23:37 UTC (rev 294)
+++ trunk/OpenGTL/OpenShiva/tools/tester/Shiva.cpp	2008-07-01 22:24:38 UTC (rev 295)
@@ -1,111 +0,0 @@
-/*
- *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation;
- * version 2 of the License.
- *
- * This library 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; see the file COPYING.  If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-// C++ Headers
-#include <iostream>
-#include <fstream>
-#include <cstdlib>
-
-// OpenShiva Headers
-#include <OpenShiva/Debug.h>
-#include <OpenShiva/Kernel.h>
-#include <OpenShiva/Version.h>
-
-void printVersion()
-{
-  std::cout << OpenShiva::LibraryShortName << " - " << OpenShiva::LibraryName << " - " << OpenShiva::LibraryVersionString << std::endl;
-  std::cout << OpenShiva::LibraryCopyright << std::endl;
-  std::cout << "Shiva Version : " << OpenShiva::LanguageVersion << std::endl;
-}
-void printHelp()
-{
-  std::cout << "Usage : shivatester [option] fileName.shiva" << std::endl;
-  std::cout << std::endl;
-  std::cout << "Options : " << std::endl;
-//   std::cout << "  -L --module-dir   add a location where to find modules" << std::endl;
-  std::cout << std::endl;
-  std::cout << "  -h --help               print this message" << std::endl;
-  std::cout << "  -v --version            print the version information" << std::endl;
-}
-#define ARG_IS(a,b) argv[ai] == GTLCore::String(a) or argv[ai] == GTLCore::String(b)
-
-int main(int argc, char** argv)
-{
-  GTLCore::String fileName = "";
-  GTLCore::String output = "";
-  for(int ai = 1; ai < argc; ai++)
-  {
-    if(ARG_IS("-h","--help"))
-    {
-      printHelp();
-      return EXIT_SUCCESS;
-    } else if(ARG_IS("-v","--version"))
-    {
-      printVersion();
-      return EXIT_SUCCESS;
-    } else if(ARG_IS("-L", "--module-dir")) {
-      if( ai == argc )
-      {
-        std::cerr << "Expected directory after -L --module-dir." << std::endl;
-        return EXIT_FAILURE;
-      } else {
-        ++ai;
-//         OpenShiva::ModulesManager::instance()->addDirectory(argv[ai]);
-      }
-    } else {
-      if( ai != argc - 1)
-      {
-        std::cerr << "Invalid command line parameters." << std::endl;
-      } else {
-        fileName = argv[ai];
-      }
-      break;
-    }
-  }
-  if( fileName == "")
-  {
-    printHelp();
-  } else {
-    GTLCore::String source;
-    std::ifstream in;
-    in.open(fileName.c_str() );
-    if(not in)
-    {
-      std::cerr << "Impossible to open file " << fileName << std::endl;
-      return EXIT_FAILURE;
-    }
-    GTLCore::String str;
-    std::getline(in,str);
-    while ( in ) {
-      source += str;
-      source += "\n";
-      std::getline(in,str);
-    }
-    OpenShiva::Kernel p(fileName);
-    p.setSource( source );
-    p.compile();
-    if(not p.isCompiled())
-    {
-      std::cout << "Error: " << std::endl << p.compilationErrorsMessage() << std::endl;
-      return EXIT_FAILURE;
-    }
-    return p.runTest();
-  }
-  return EXIT_SUCCESS;
-}

Copied: trunk/OpenGTL/OpenShiva/tools/tester/ShivaTester.cpp (from rev 192, trunk/OpenGTL/OpenShiva/tools/tester/Shiva.cpp)
===================================================================
--- trunk/OpenGTL/OpenShiva/tools/tester/ShivaTester.cpp	                        (rev 0)
+++ trunk/OpenGTL/OpenShiva/tools/tester/ShivaTester.cpp	2008-07-01 22:24:38 UTC (rev 295)
@@ -0,0 +1,179 @@
+/*
+ *  Copyright (c) 2008 Cyrille Berger <cberger@xxxxxxxxxxx>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2 of the License.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+// C++ Headers
+#include <iostream>
+#include <fstream>
+#include <cstdlib>
+
+// GTLCore Headers
+#include <GTLCore/AbstractImage.h>
+#include <GTLCore/Debug.h>
+#include <GTLCore/Image.h>
+#include <GTLCore/PixelDescription.h>
+#include <GTLCore/Region.h>
+
+// OpenShiva Headers
+#include <OpenShiva/Debug.h>
+#include <OpenShiva/Kernel.h>
+#include <OpenShiva/Version.h>
+
+// GTLImageIO Headers
+#include <GTLImageIO/ImageDC.h>
+#include <GTLImageIO/ImageDCRegistry.h>
+
+void printVersion()
+{
+  std::cout << OpenShiva::LibraryShortName << " - " << OpenShiva::LibraryName << " - " << OpenShiva::LibraryVersionString << std::endl;
+  std::cout << OpenShiva::LibraryCopyright << std::endl;
+  std::cout << "Shiva Version : " << OpenShiva::LanguageVersion << std::endl;
+}
+void printHelp()
+{
+  std::cout << "Usage : shivatester [option] fileName.shiva" << std::endl;
+  std::cout << std::endl;
+  std::cout << "Options : " << std::endl;
+  std::cout << "  -r --run-test           run the function 'run-test'" << std::endl;
+  std::cout << "  -c --compare            compare the result of the program with a file stored on disk" << std::endl;
+//   std::cout << "  -L --module-dir   add a location where to find modules" << std::endl;
+  std::cout << std::endl;
+  std::cout << "  -h --help               print this message" << std::endl;
+  std::cout << "  -v --version            print the version information" << std::endl;
+}
+#define ARG_IS(a,b) argv[ai] == GTLCore::String(a) or argv[ai] == GTLCore::String(b)
+
+enum ShivaTesterMode {
+  STM_DEFAULT,
+  STM_RUN_TEST,
+  STM_COMPARE_RESULT
+};
+
+int main(int argc, char** argv)
+{
+  GTLCore::String fileName = "";
+  std::list< GTLCore::String > fileNames;
+  ShivaTesterMode mode = STM_DEFAULT;
+  for(int ai = 1; ai < argc; ai++)
+  {
+    if(ARG_IS("-h","--help"))
+    {
+      printHelp();
+      return EXIT_SUCCESS;
+    } else if(ARG_IS("-c","--compare") ) {
+      mode = STM_COMPARE_RESULT;
+    } else if(ARG_IS("-v","--version"))
+    {
+      printVersion();
+      return EXIT_SUCCESS;
+    } else if(ARG_IS("-L", "--module-dir")) {
+      if( ai == argc )
+      {
+        std::cerr << "Expected directory after -L --module-dir." << std::endl;
+        return EXIT_FAILURE;
+      } else {
+        ++ai;
+//         OpenShiva::ModulesManager::instance()->addDirectory(argv[ai]);
+      }
+    } else {
+      fileNames.push_back( argv[ai] );
+    }
+  }
+  if( fileNames.size() > 0 )
+  {
+    fileName = *( fileNames.begin() );
+    fileNames.pop_front();
+  }
+  if( fileName == "" or (fileNames.size() < 1 and mode == STM_COMPARE_RESULT) )
+  {
+    std::cerr << "Invalid command line parameters." << std::endl;
+    printHelp();
+  } else {
+    GTLCore::String source;
+    std::ifstream in;
+    in.open(fileName.c_str() );
+    if(not in)
+    {
+      std::cerr << "Impossible to open file " << fileName << std::endl;
+      return EXIT_FAILURE;
+    }
+    GTLCore::String str;
+    std::getline(in,str);
+    while ( in ) {
+      source += str;
+      source += "\n";
+      std::getline(in,str);
+    }
+    OpenShiva::Kernel p(fileName);
+    p.setSource( source );
+    p.compile();
+    if(not p.isCompiled())
+    {
+      std::cout << "Error: " << std::endl << p.compilationErrorsMessage() << std::endl;
+      return EXIT_FAILURE;
+    }
+    switch( mode )
+    {
+      case STM_COMPARE_RESULT:
+      {
+        GTLCore::String resultFileName = *( --fileNames.end() );
+        fileNames.pop_back();
+        const GTLImageIO::ImageDC* decoder = GTLImageIO::ImageDCRegistry::instance()->decoder( resultFileName );
+        if( not decoder )
+        {
+          std::cerr << "Can't find decoder for " << resultFileName << std::endl;
+          return EXIT_FAILURE;
+        }
+        GTLCore::Region region;
+        GTLCore::String errMsg;
+        GTLCore::AbstractImage* resultImage = decoder->decode( resultFileName, &region, &errMsg );
+        GTLCore::Image image(region.width(), region.height(), resultImage->pixelDescription() );
+        std::list<GTLCore::AbstractImage*> images;
+        for( std::list< GTLCore::String >::iterator it = fileNames.begin();
+            it != fileNames.end(); ++it )
+        {
+          const GTLImageIO::ImageDC* decoder = GTLImageIO::ImageDCRegistry::instance()->decoder( *it );
+          if( not decoder )
+          {
+            std::cerr << "Can't find decoder for " << *it << std::endl;
+            return EXIT_FAILURE;
+          }
+          GTLCore::AbstractImage* image = decoder->decode( *it, 0, &errMsg );
+          if( not image )
+          {
+            std::cerr << "Can't read image " << *it << " : " << errMsg << std::endl;
+            return EXIT_FAILURE;
+          }
+          images.push_back( image );
+        }
+        p.evaluatePixeles( region, images, &image );
+        int errorCount = image.compare( resultImage, region );
+        if( errorCount != 0 )
+        {
+          std::cout << errorCount << " different pixels" << std::endl;
+          return EXIT_FAILURE;
+        }
+        break;
+      }
+      case STM_RUN_TEST:
+      case STM_DEFAULT:
+        return p.runTest();
+    }
+  }
+  return EXIT_SUCCESS;
+}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/