Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r1217 - trunk/Image


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r1217 - trunk/Image
  • Date: Tue, 10 Oct 2006 16:59:25 -0600 (MDT)

Author: bigler
Date: Tue Oct 10 16:59:23 2006
New Revision: 1217

Modified:
   trunk/Image/SimpleImage.h
   trunk/Image/TGAFile.cc
Log:

Image/SimpleImage.h

  I'm not sure why these functions were declared virtual.  They
  compiler must have just figured it out considering the parent
  class's function were pure virtual.

Image/TGAFile.cc

  Cleaned up and refactored code for writing images.  Added support
  for all currently defined pixel types, plus a new function that does
  lots of conversions to work with any future pixel types that will
  come down the pipe (well, any pixel format that works with
  Fragment::setColor and getColor).


Modified: trunk/Image/SimpleImage.h
==============================================================================
--- trunk/Image/SimpleImage.h   (original)
+++ trunk/Image/SimpleImage.h   Tue Oct 10 16:59:23 2006
@@ -46,8 +46,8 @@
     virtual ~SimpleImage();
     static Image* create(const std::vector<std::string>& args, bool stereo,
                          int xres, int yres);
-    void set(const Fragment& fragment);
-    void get(Fragment& fragment) const;
+    virtual void set(const Fragment& fragment);
+    virtual void get(Fragment& fragment) const;
 
     Pixel* getRawPixels(int which_eye) const;
     void* getRawData(int which_eye) const;

Modified: trunk/Image/TGAFile.cc
==============================================================================
--- trunk/Image/TGAFile.cc      (original)
+++ trunk/Image/TGAFile.cc      Tue Oct 10 16:59:23 2006
@@ -41,9 +41,97 @@
 using namespace std;
 using SCIRun::IllegalValue;
 using SCIRun::InternalError;
+using namespace Manta;
 
-namespace Manta
+static void writePixel(ofstream& out, unsigned char r, unsigned char g,
+                       unsigned char b)
 {
+  out.put( b );
+  out.put( g );
+  out.put( r );
+}
+
+static void writePixel(ofstream& out, unsigned char r, unsigned char g,
+                       unsigned char b, unsigned char a)
+{
+  out.put( b );
+  out.put( g );
+  out.put( r );
+  out.put( a );
+}
+
+static void writePixel(ofstream& out, float r, float g, float b)
+{
+  b = b < 0.0f ? 0.0f : b;
+  b = b > 1.0f ? 1.0f : b;
+  out.put( static_cast< unsigned char >( b * 255.0f ) );
+  g = g < 0.0f ? 0.0f : g;
+  g = g > 1.0f ? 1.0f : g;
+  out.put( static_cast< unsigned char >( g * 255.0f ) );
+  r = r < 0.0f ? 0.0f : r;
+  r = r > 1.0f ? 1.0f : r;
+  out.put( static_cast< unsigned char >( r * 255.0f ) );
+}
+
+static void writePixel(ofstream& out, float r, float g, float b, float a)
+{
+  b = b < 0.0f ? 0.0f : b;
+  b = b > 1.0f ? 1.0f : b;
+  out.put( static_cast< unsigned char >( b * 255.0f ) );
+  g = g < 0.0f ? 0.0f : g;
+  g = g > 1.0f ? 1.0f : g;
+  out.put( static_cast< unsigned char >( g * 255.0f ) );
+  r = r < 0.0f ? 0.0f : r;
+  r = r > 1.0f ? 1.0f : r;
+  out.put( static_cast< unsigned char >( r * 255.0f ) );
+  a = a < 0.0f ? 0.0f : a;
+  a = a > 1.0f ? 1.0f : a;
+  out.put( static_cast< unsigned char >( a * 255.0f ) );
+}
+
+template<class PType>
+void writeAllPixels(ofstream& out, Image const* image, int eye)
+{
+  bool stereo;
+  int xres, yres;
+  image->getResolution( stereo, xres, yres );
+  
+  const SimpleImage< PType >* si =
+    dynamic_cast< const SimpleImage< PType > * >( image );
+  PType const *buffer = si->getRawPixels( eye );
+  for ( int y = 0; y < yres; ++y )
+    for ( int x = 0; x < xres; ++x ) {
+      writePixel(out, buffer->r, buffer->g, buffer->b);
+      ++buffer;
+    }
+}
+
+template<class PType>
+void writeAllPixelsAlpha(ofstream& out, Image const* image, int eye)
+{
+  bool stereo;
+  int xres, yres;
+  image->getResolution( stereo, xres, yres );
+  
+  const SimpleImage< PType >* si =
+    dynamic_cast< const SimpleImage< PType > * >( image );
+  PType const *buffer = si->getRawPixels( eye );
+  for ( int y = 0; y < yres; ++y )
+    for ( int x = 0; x < xres; ++x ) {
+      writePixel(out, buffer->r, buffer->g, buffer->b, buffer->a);
+      ++buffer;
+    }
+}
+
+void writePixelDescription(ofstream& out) {
+  out.put( 24 );  // Pixel depth: 24bpp
+  out.put( 0 );   // Image descriptor: 0 bits of alpha, bottom-left origin
+}
+
+void writePixelDescriptionAlpha(ofstream& out) {
+  out.put( 32 );  // Pixel depth: 32bpp
+  out.put( 8 );   // Image descriptor: 8 bits of alpha, bottom-left origin
+}
 
   static inline void write16bit(
     ofstream &out,
@@ -64,7 +152,7 @@
     out.put( ( value >> 24 ) & 0xFF );
   }
 
-  void writeTGA(
+  void Manta::writeTGA(
     Image const *image,
     string const &filename,
     int const eye )
@@ -85,79 +173,54 @@
     image->getResolution( stereo, xres, yres );
     write16bit( out, xres );      // Image width
     write16bit( out, yres );      // Image height
+
     if ( typeid( *image ) == typeid( SimpleImage< RGBA8Pixel > ) ) {
-      out.put( 32 );              // Pixel depth: 32bpp
-      out.put( 8 );               // Image descriptor: 8 bits of alpha, 
bottom-left origin
-      const SimpleImage< RGBA8Pixel > *si = dynamic_cast< const SimpleImage< 
RGBA8Pixel > * >( image );
-      RGBA8Pixel const *buffer = si->getRawPixels( eye );
-      for ( int y = 0; y < yres; ++y )
-        for ( int x = 0; x < xres; ++x ) {
-          out.put( buffer->b );
-          out.put( buffer->g );
-          out.put( buffer->r );
-          out.put( buffer->a );
-          ++buffer;
-        }
+      // RGBA8
+      writePixelDescriptionAlpha(out);
+      writeAllPixelsAlpha<RGBA8Pixel>(out, image, eye);
+    } else if ( typeid( *image ) == typeid( SimpleImage< ABGR8Pixel > ) ) {
+      // ABGR8
+      writePixelDescriptionAlpha(out);
+      writeAllPixelsAlpha<ABGR8Pixel>(out, image, eye);
+    } else if ( typeid( *image ) == typeid( SimpleImage< ARGB8Pixel > ) ) {
+      // ARGB8
+      writePixelDescriptionAlpha(out);
+      writeAllPixelsAlpha<ARGB8Pixel>(out, image, eye);
+    } else if ( typeid( *image ) == typeid( SimpleImage< BGRA8Pixel > ) ) {
+      // BGRA8
+      writePixelDescriptionAlpha(out);
+      writeAllPixelsAlpha<BGRA8Pixel>(out, image, eye);
     } else if ( typeid( *image ) == typeid( SimpleImage< RGB8Pixel > ) ) {
-      out.put( 24 );              // Pixel depth: 24bpp
-      out.put( 0 );               // Image descriptor: 0 bits of alpha, 
bottom-left origin
-      const SimpleImage< RGB8Pixel > *si = dynamic_cast< const SimpleImage< 
RGB8Pixel > * >( image );
-      RGB8Pixel const *buffer = si->getRawPixels( eye );
-      for ( int y = 0; y < yres; ++y )
-        for ( int x = 0; x < xres; ++x ) {
-          out.put( buffer->b );
-          out.put( buffer->g );
-          out.put( buffer->r );
-          ++buffer;
-        }
-    } else if ( typeid( *image ) == typeid( SimpleImage< RGBAfloatPixel > ) 
) {
-      out.put( 32 );              // Pixel depth: 32bpp
-      out.put( 8 );               // Image descriptor: 8 bits of alpha, 
bottom-left origin
-      const SimpleImage< RGBAfloatPixel > *si = dynamic_cast< const 
SimpleImage< RGBAfloatPixel > * >( image );
-      RGBAfloatPixel const *buffer = si->getRawPixels( eye );
-      for ( int y = 0; y < yres; ++y )
-        for ( int x = 0; x < xres; ++x ) {
-          float b = buffer->b;
-          b = b < 0.0f ? 0.0f : b;
-          b = b > 1.0f ? 1.0f : b;
-          out.put( static_cast< unsigned char >( b * 255.0f ) );
-          float g = buffer->g;
-          g = g < 0.0f ? 0.0f : g;
-          g = g > 1.0f ? 1.0f : g;
-          out.put( static_cast< unsigned char >( g * 255.0f ) );
-          float r = buffer->r;
-          r = r < 0.0f ? 0.0f : r;
-          r = r > 1.0f ? 1.0f : r;
-          out.put( static_cast< unsigned char >( r * 255.0f ) );
-          float a = buffer->a;
-          a = a < 0.0f ? 0.0f : a;
-          a = a > 1.0f ? 1.0f : a;
-          out.put( static_cast< unsigned char >( a * 255.0f ) );
-          ++buffer;
-        }
+      // RGB8
+      writePixelDescription(out);
+      writeAllPixels<RGB8Pixel>(out, image, eye);
     } else if ( typeid( *image ) == typeid( SimpleImage< RGBfloatPixel > ) ) 
{
-      out.put( 24 );              // Pixel depth: 24bpp
-      out.put( 0 );               // Image descriptor: 0 bits of alpha, 
bottom-left origin
-      const SimpleImage< RGBfloatPixel > *si = dynamic_cast< const 
SimpleImage< RGBfloatPixel > * >( image );
-      RGBfloatPixel const *buffer = si->getRawPixels( eye );
+      // RGBfloat
+      writePixelDescription(out);
+      writeAllPixels<RGBfloatPixel>(out, image, eye);
+    } else if ( typeid( *image ) == typeid( SimpleImage< RGBAfloatPixel > ) 
) {
+      // RGBAfloat
+      writePixelDescriptionAlpha(out);
+      writeAllPixelsAlpha<RGBAfloatPixel>(out, image, eye);
+    } else
+    {
+      writePixelDescription(out);
+      // Do slow crappy conversion
+      cerr << __FILE__ <<":"<<__LINE__<<":Unknown image/pixel type in 
writeTGA(), doing slow conversion.\n";
+      Fragment fragment(Fragment::UnknownShape);
       for ( int y = 0; y < yres; ++y )
-        for ( int x = 0; x < xres; ++x ) {
-          float b = buffer->b;
-          b = b < 0.0f ? 0.0f : b;
-          b = b > 1.0f ? 1.0f : b;
-          out.put( static_cast< unsigned char >( b * 255.0f ) );
-          float g = buffer->g;
-          g = g < 0.0f ? 0.0f : g;
-          g = g > 1.0f ? 1.0f : g;
-          out.put( static_cast< unsigned char >( g * 255.0f ) );
-          float r = buffer->r;
-          r = r < 0.0f ? 0.0f : r;
-          r = r > 1.0f ? 1.0f : r;
-          out.put( static_cast< unsigned char >( r * 255.0f ) );
-          ++buffer;
+        for ( int x = 0; x < xres; x+=Fragment::MaxSize ) {
+          int xend = x + Fragment::MaxSize;
+          if (xend > xres) xend = xres;
+          fragment.setConsecutiveX(x, xend, y, eye);
+          image->get(fragment);
+          // Now write out the fragments
+          for(int frag = fragment.begin(); frag < fragment.end(); ++frag) {
+            RGBColor pixel(fragment.getColor(frag).convertRGB());
+            writePixel(out, pixel.r(), pixel.g(), pixel.b());
+          }
         }
-    } else {
-      throw InternalError( "Unknown image/pixel type in writeTGA()", 
__FILE__, __LINE__  );
+      //      throw InternalError( "Unknown image/pixel type in writeTGA()", 
__FILE__, __LINE__  );
     }
   }
 
@@ -180,7 +243,7 @@
     return byte_1 | ( byte_2 << 8 ) | ( byte_3 << 16 ) | ( byte_4 << 24 );
   }
 
-  Image *readTGA(
+  Image* Manta::readTGA(
     string const &filename )
   {
     ifstream in( filename.c_str(), ios::in | ios::binary );
@@ -234,4 +297,3 @@
     throw InternalError( "Unhandled pixel depth and alpha for TGA files", 
__FILE__, __LINE__  );
   }
 
-}




  • [MANTA] r1217 - trunk/Image, bigler, 10/10/2006

Archive powered by MHonArc 2.6.16.

Top of page