Text archives Help
- From: roni@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [Manta] r1822 - trunk/Image
- Date: Mon, 5 Nov 2007 22:28:04 -0700 (MST)
Author: roni
Date: Mon Nov 5 22:28:04 2007
New Revision: 1822
Modified:
trunk/Image/NRRDFile.cc
Log:
Image/NRRDFile.cc
Fixed nrrd-formatted image saving to handle padded images.
NOTE: NRRD will not save rgbfloat or rgbafloat images. I
don't know why.
Modified: trunk/Image/NRRDFile.cc
==============================================================================
--- trunk/Image/NRRDFile.cc (original)
+++ trunk/Image/NRRDFile.cc Mon Nov 5 22:28:04 2007
@@ -41,10 +41,9 @@
using namespace Manta;
using namespace SCIRun;
-extern "C" void writeNRRD( Image const *image, std::string const &file_name,
int which ) {
-
+extern "C" void writeNRRD( Image const *image, std::string const &file_name,
int eye ) {
// Create a nrrd to save the image.
- Nrrd *out_nrrd = nrrdNew();
+ Nrrd *out_nrrd = nrrdNew();
// Determine the resolution.
bool stereo;
@@ -52,38 +51,96 @@
image->getResolution( stereo, width, height );
// Three dimensions for images
- out_nrrd->dim = 3;
+ out_nrrd->dim = 3;
int pixel_width;
/////////////////////////////////////////////////////////////////////////////
- // Determine the pixel type and size.
- if (typeid(*image) == typeid(SimpleImage<RGB8Pixel>)) {
+ // Determine the pixel type and size. Also swizzle the pixel data
+ // into a new buffer, eliminating padding and rearranging the
+ // color/alpha channels into a nrrd-friendly order.
+ if (typeid(*image) == typeid(SimpleImage<RGB8Pixel>)) {
+ const SimpleImage<RGB8Pixel> *si = dynamic_cast<const
SimpleImage<RGB8Pixel> *>(image);
+
pixel_width = 3;
out_nrrd->type = nrrdTypeUChar;
- out_nrrd->data =
- dynamic_cast<SimpleImage<RGB8Pixel> const *>( image )->getRawData(
which );
+
+ unsigned char *data = new unsigned char[pixel_width*width*height];
+ for(int i=0; i<height; i++){
+ for(int j=0; j<width; j++){
+ const RGB8Pixel p = si->get(j,i,eye);
+
+ data[pixel_width*width*i + pixel_width*j + 0] = p.r;
+ data[pixel_width*width*i + pixel_width*j + 1] = p.g;
+ data[pixel_width*width*i + pixel_width*j + 2] = p.b;
+ }
+ }
+
+ out_nrrd->data = data;
}
else if (typeid(*image) == typeid(SimpleImage<RGBA8Pixel>)) {
+ const SimpleImage<RGBA8Pixel> *si = dynamic_cast<const
SimpleImage<RGBA8Pixel> *>(image);
+
pixel_width = 4;
out_nrrd->type = nrrdTypeUChar;
- out_nrrd->data =
- dynamic_cast<SimpleImage<RGBA8Pixel> const *>( image )->getRawData(
which );
+
+ unsigned char *data = new unsigned char[pixel_width*width*height];
+ for(int i=0; i<height; i++){
+ for(int j=0; j<width; j++){
+ const RGBA8Pixel p = si->get(j,i,eye);
+
+ data[pixel_width*width*i + pixel_width*j + 0] = p.r;
+ data[pixel_width*width*i + pixel_width*j + 1] = p.g;
+ data[pixel_width*width*i + pixel_width*j + 2] = p.b;
+ data[pixel_width*width*i + pixel_width*j + 3] = p.a;
+ }
+ }
+
+ out_nrrd->data = data;
}
else if (typeid(*image) == typeid(SimpleImage<RGBfloatPixel>)) {
+ const SimpleImage<RGBfloatPixel> *si = dynamic_cast<const
SimpleImage<RGBfloatPixel> *>(image);
+
pixel_width = 3;
out_nrrd->type = nrrdTypeFloat;
out_nrrd->data =
- dynamic_cast<SimpleImage<RGBfloatPixel> const *>( image )->getRawData(
which );
+ dynamic_cast<SimpleImage<RGBfloatPixel> const *>( image )->getRawData(
which );
+
+// float *data = new float[pixel_width*width*height];
+// for(int i=0; i<height; i++){
+// for(int j=0; j<width; j++){
+// const RGBfloatPixel p = si->get(j,i,eye);
+
+// data[pixel_width*width*i + pixel_width*j + 0] = p.r;
+// data[pixel_width*width*i + pixel_width*j + 1] = p.g;
+// data[pixel_width*width*i + pixel_width*j + 2] = p.b;
+// }
+// }
+
+// out_nrrd->data = data;
}
else if (typeid(*image) == typeid(SimpleImage<RGBAfloatPixel>)) {
+ const SimpleImage<RGBAfloatPixel> *si = dynamic_cast<const
SimpleImage<RGBAfloatPixel> *>(image);
+
pixel_width = 4;
out_nrrd->type = nrrdTypeFloat;
- out_nrrd->data =
- dynamic_cast<SimpleImage<RGBAfloatPixel> const *>( image
)->getRawData( which );
+
+ float *data = new float[pixel_width*width*height];
+ for(int i=0; i<height; i++){
+ for(int j=0; j<width; j++){
+ const RGBAfloatPixel p = si->get(j,i,eye);
+
+ data[pixel_width*width*i + pixel_width*j + 0] = p.r;
+ data[pixel_width*width*i + pixel_width*j + 1] = p.g;
+ data[pixel_width*width*i + pixel_width*j + 2] = p.b;
+ data[pixel_width*width*i + pixel_width*j + 3] = p.a;
+ }
+ }
+
+ out_nrrd->data = data;
}
else {
nrrdNix( out_nrrd );
@@ -101,22 +158,22 @@
if (nrrdFlip( flipped, out_nrrd, 2 )) {
const char *reason = biffGetDone( NRRD );
- nrrdNix( out_nrrd );
+ nrrdNuke( out_nrrd );
nrrdNix( flipped );
throw OutputError( "Could not flip nrrd image: " + string( reason ) );
}
/////////////////////////////////////////////////////////////////////////////
- // Write the nrrd to disk.
- if (nrrdSave( file_name.c_str(), flipped, 0 )) {
- const char *reason = biffGetDone( NRRD );
+ // Write the nrrd to disk.
+ if (nrrdSave( file_name.c_str(), flipped, 0 )) {
+ const char *reason = biffGetDone( NRRD );
- nrrdNix( out_nrrd );
+ nrrdNuke( out_nrrd );
nrrdNix( flipped );
throw OutputError( "Could not save image as nrrd: " + string( reason ) );
- }
+ }
- nrrdNix( out_nrrd );
+ nrrdNuke( out_nrrd );
nrrdNix( flipped );
}
- [Manta] r1822 - trunk/Image, roni, 11/06/2007
Archive powered by MHonArc 2.6.16.