void CDisplaySettingsList::SaveDisplaySettingsModeTable( std::ofstream &stream )
{
CDisplaySettingsList videomodelist;
CDisplaySettingsList pixelsizelist;
CDisplaySettingsList displayfreqlist;
CDisplaySettings temp, *pptr;
char buffer[128], padline[128], padspace[128], padtop[128];
static int padtable[6] = { 0, 28, 14, 9, 5 };
static char title[] = "Display Frequencies";

int newfreq, freq, len;
unsigned int padlen;

for (unsigned int pn = 0; pn < size(); pn++ )
	{
	pptr = &at(pn);
	newfreq = at(pn).m_pelswidth *
		at(pn).m_pelsheight *
	   	at(pn).m_displayvertfreq;

	if ( newfreq > m_maxfreq )
		{
		m_maxfreq = newfreq;
		}

	// Generate list of unique video resolutions

	if ( videomodelist.FindDisplaySetting( 
		 pptr->m_pelswidth, pptr->m_pelsheight, -1, -1 ) == NULL )
		{
		videomodelist.push_back( *pptr );
		}

	// Generate list of pixel sizes

	if ( pixelsizelist.FindDisplaySetting( 
		 -1, -1, pptr->m_bitsperpel, -1 ) == NULL )
		{
		pixelsizelist.push_back( *pptr );
		}

	// Generate list of display frequencies

	if ( displayfreqlist.FindDisplaySetting(
		 -1, -1, -1, pptr->m_displayvertfreq ) == NULL )
		{
		displayfreqlist.push_back( *pptr );
		}
	}

pixelsizelist.SetSortMode( LIST_SORTBITSPERPEL );
pixelsizelist.Sort();

stream << std::endl << std::endl;
stream << "Display modes" << std::endl;
stream << "-------------" << std::endl << std::endl;

stream << "Maximum bandwidth: " << m_maxfreq << " Hertz" << std::endl;

if ( displayfreqlist.size() < 5)
	{
	padlen = padtable[ displayfreqlist.size()];
	}
else
	{
	padlen = 5;
	}
		
for (unsigned int pn = 0; pn < padlen; pn++ )
	{
	padline[pn]  = '-';
	padtop[pn] = '-';
	padspace[pn] = ' ';			
	}
	 
padline[padlen]   = '+';
padline[padlen+1] = '\0';
padspace[padlen]  = '\0';
padtop[padlen]	= '-';
padtop[padlen+1] = '\0';

for (unsigned int qn = 0; qn < pixelsizelist.size(); qn++ )
	{
	stream << std::endl << std::endl;
	stream << "Pixel size: " << pixelsizelist[qn].m_bitsperpel << " bits" << std::endl;
	stream << "-------------------" << std::endl << std::endl;

	// ------ Top line

	stream << "			   +";

	for (unsigned int pn = 0; pn < displayfreqlist.size()-1; pn++ )
		{
		stream << padtop;
		}

	stream << padline;
	stream << std::endl;
  
	// ------ Display frequencies

	stream << "			   | ";
	stream.width( padlen-1);
	stream << title;
	stream.width( 0 );

	len = padlen * displayfreqlist.size() - strlen( title );

	while ( len % padlen )	
		{
		stream << " ";
		len--;
		}

	stream << " ";

	padspace[2] = ' ';

	while ( len > 0 )
		{			
		stream << padspace;
		stream << " ";
		len -= padlen;
		}

	stream << " |" << std::endl;

	// Top line for resolutions

	stream << "+--------------+";

	for (unsigned int pn = 0; pn < displayfreqlist.size(); pn++ )
		{
		stream << padline;
		}
					
	stream << std::endl;
	stream << "| Resolution   |";

	// List of frequencies

	for (unsigned int pn = 0; pn < displayfreqlist.size(); pn++ )
		{
		stream.width( padlen-1 );
	
		freq = displayfreqlist[pn].m_displayvertfreq;

		if (  freq == 0 || freq == 1 )
			{
			stream << "VGA";
			}
		else
			{
			stream << freq;

			stream.width(0);
			stream << " |";
			}

		stream << std::endl;

		// Bottom of resolutions

		stream << "+--------------+";

		for ( pn = 0; pn < displayfreqlist.size(); pn++ )
			{
			stream << padline;
			}
					
		stream << std::endl;

		// For each display resolution

		for ( pn = 0; pn < videomodelist.size(); pn++ )
			{
			int empty = TRUE;

			for (unsigned int vn = 0; vn < displayfreqlist.size(); vn++ )
				{
				if ( FindDisplaySetting( 
					videomodelist[pn].m_pelswidth,
					videomodelist[pn].m_pelsheight,
					pixelsizelist[qn].m_bitsperpel,
					displayfreqlist[vn].m_displayvertfreq ) )
					{
					empty = FALSE;
					}
				}
					
			if ( !empty )
				{
				stream.width(4);
				stream << videomodelist[pn].m_pelswidth;
				stream << " x ";
				stream.width(4);
				stream << videomodelist[pn].m_pelsheight;

				stream << "|";
				stream.width(13);

				stream << buffer;	
				stream.width(0);

				stream << " |";

				for (unsigned int vn = 0; vn < displayfreqlist.size(); vn++ )
					{
					pptr = FindDisplaySetting( 
						videomodelist[pn].m_pelswidth,
						videomodelist[pn].m_pelsheight,
						pixelsizelist[qn].m_bitsperpel,
						displayfreqlist[vn].m_displayvertfreq );
						
					newfreq =
						videomodelist[pn].m_pelswidth *
						videomodelist[pn].m_pelsheight *
						displayfreqlist[vn].m_displayvertfreq;

					if ( newfreq < m_maxfreq )
						{
						padspace[2] = '?';
						}
					else
						{
						padspace[2] = ' ';
						}
						
					if ( !pptr )
						{
						stream << padspace << "|";
						}
					else
						{
						stream.width( padlen-1 );
						stream << pptr->m_index << " |";
						stream.width( 0 );
						}
					}

				stream << std::endl;
				}

			if ( pn == videomodelist.size()-1 )
				{
				stream << "+--------------+";

				for (unsigned int vn = 0; vn < displayfreqlist.size(); vn++ )
					{
					stream << padline;
					}
					
				stream << std::endl;
				}		   
			}	
		}
	}
}
    void saveImagesToDisk(ros::NodeHandle& nh, ros::Subscriber& sub)
    {	 
    	//pcl::ScopeTime t1 ("save images");
    	  {  
          boost::mutex::scoped_lock lock(rgb_mutex_);   
          memcpy( cv_rgb_.data, &rgb_data_[0], 3*rows_*cols_*sizeof(unsigned char));
          
          new_rgb_ = false;
        }
        
        capture_->stop();        
        sub.shutdown();        
        sub = nh.subscribe("/camera/depth/image_raw", 1, &OpenNIShoter::frameDepthCallback, this);        
        new_depth_ = false;
        capture_->start();
        
        do
        {         
          if (new_depth_ == true )
          {
            boost::mutex::scoped_lock lock(depth_mutex_);   
            memcpy( cv_depth_.data, &depth_data_[0], rows_*cols_*sizeof(uint16_t));
          
            new_depth_ = false;
            break;
          }
          
          boost::this_thread::sleep (boost::posix_time::millisec (10)); 
             
        } while (1);
        
        
        capture_->stop();        
        sub.shutdown();        
        sub = nh.subscribe("/camera/ir/image", 1, &OpenNIShoter::frameIRCallback, this);   
        new_ir_ = false;         
        capture_->start();
        
        do
        {         
          if (new_ir_ == true )
          {            
            boost::mutex::scoped_lock lock(ir_mutex_);   
            memcpy( cv_ir_raw_.data, &ir_data_[0], rows_*cols_*sizeof(uint16_t));            
            
            cv_ir_raw_.convertTo(cv_ir_, CV_8U); 
            cv::equalizeHist( cv_ir_, cv_ir_ );
            
            int max = 0;
            
            for (int i=0; i< rows_; i++)
            {
              for (int j=0; j< cols_; j++)
              {
               if (ir_data_[i+j*rows_] > max)
                max = ir_data_[i+j*rows_];
              }
            }
            
            std::cout << "max IR val: " << max << std::endl;
            
          
            new_ir_ = false;
            break;
          }
          
          boost::this_thread::sleep (boost::posix_time::millisec (10)); 
             
        } while (1);
        
        capture_->stop();        
        sub.shutdown();        
        sub = nh.subscribe("/camera/rgb/image_raw", 1, &OpenNIShoter::frameRGBCallback, this);         
        capture_->start();

	      sprintf(depth_file, "/Depth/%018ld.png", timestamp_depth_ );
	      sprintf(rgb_file, "/RGB/%018ld.png", timestamp_rgb_);  
        sprintf(ir_file, "/IR/%018ld.png", timestamp_ir_);  
        
	      cv::imwrite( write_folder_ + depth_file, cv_depth_);
	      cv::imwrite( write_folder_ + rgb_file, cv_rgb_);
        cv::imwrite( write_folder_ + ir_file, cv_ir_);
	      
	      sprintf(depth_file_PNG, "/Depth/%018ld.png", timestamp_depth_);
	      sprintf(rgb_file_PNG, "/RGB/%018ld.png", timestamp_rgb_);
        sprintf(ir_file_PNG, "/IR/%018ld.png", timestamp_ir_);

	      off_rgb_.width(18);
	      off_rgb_.fill('0');
	      off_rgb_ << timestamp_rgb_;
	      off_rgb_ <<  " " << rgb_file_PNG << std::endl;
        
        off_depth_.width(18);
	      off_depth_.fill('0');
	      off_depth_ << timestamp_depth_;
	      off_depth_ <<  " " << depth_file_PNG << std::endl;
        
        off_ir_.width(18);
	      off_ir_.fill('0');
	      off_ir_ << timestamp_ir_;
	      off_ir_ <<  " " << ir_file_PNG << std::endl;
	  }
void CDisplaySettingsList::SaveDisplaySettingsList( std::ofstream &stream )
{
char buffer[1024];

//stream.setmode( filebuf::text );

stream << "Display Settings List" << std::endl;
stream << "---------------------" << std::endl << std::endl;

stream << "No. of display settings [" << size() << "]	";
stream << "Sorted by [";

switch (m_sortmode & ~LIST_SORTREVERSED)
	{
	case LIST_SORTINDEX:
		stream << "Index";
		break;

	case LIST_SORTBITSPERPEL:
		stream << "Depth";
		break;

	case LIST_SORTPELSWIDTH:
		stream << "Width";
		break;

	case LIST_SORTPELSHEIGHT:
		stream << "Height";
		break;

	case LIST_SORTDISPLAYFLAGS:
		stream << "Flags";
		break;

	case LIST_SORTDISPLAYVERTFREQ:
		stream << "Vert Refresh";	
		break;

	case LIST_SORTDISPLAYHORIZFREQ:
		stream << "Horiz Refresh";
		break;
		
	case LIST_SORTDISPLAYBANDWIDTH:
		stream << "Bandwidth";
		break;
		
	default:
		break;
	}

stream << "]";
stream << "	Reversed ";

if ( m_sortmode & LIST_SORTREVERSED )			
	{
	stream << "[@]";
	}
else	
	{
	stream << "[ ]";
	}

stream << std::endl << std::endl;

stream << "+-------+-------+--------+--------+-----------+-----------+" << std::endl;
stream << "| Index |  BPP  | Width  | Height | Frequency | Bandwidth |" << std::endl;
stream << "+-------+-------+--------+--------+-----------+-----------+" << std::endl;

for ( unsigned int pn = 0; pn < size(); pn++ )
	{
	stream << "| ";
	
	stream.width( 5 );
	stream << at(pn).m_index << " ";

	stream << " | ";

	stream.width( 5 );
	stream << at(pn).m_bitsperpel;		// Pixel size
	stream << " | ";

	stream.width( 6 );			
	stream << at(pn).m_pelswidth;		// Display width
	stream << " | ";
		
	stream.width( 6 );
	stream << at(pn).m_pelsheight;		// Display height
	stream << " | ";

	stream.width( 8 );
	stream << std::hex; 	
//	stream.pad( '0');
	stream << at(pn).m_displayflags;
	stream << " | ";

	stream.width( 9 );
		
	if ( at(pn).m_displayvertfreq == 0 ||
		 at(pn).m_displayvertfreq == 1 )
		{
		stream << "Default";
		}
	else
		{
		sprintf( buffer, "%d", at(pn).m_displayvertfreq );
		stream << buffer;
		}
		
	stream << " | ";
		
	stream.width( 9 );
	stream << at(pn).m_displaybandwidth;

	stream << " |" << std::endl;			
	}

stream << "+-------+-------+--------+--------+-----------+-----------+" << std::endl;
}