Example #1
0
// Append a single slice at the bottom of a 3D image file
void 
	iomanager::tiff3D::appendSlice(
	std::string img_path,			// (INPUT)	image filepath (it includes the file extension)
	unsigned char * raw_img,		// (INPUT)	slice to be saved into the file
	int img_height,					// (INPUT)	slice height (in pixels)
	int img_width,					// (INPUT)	slice width (in pixels)
	int img_bytes_x_chan,			// (INPUT)  number of bytes per channel
	int img_chans,					// (INPUT)	number of channels
	int y0,							// (INPUT)	region of interest [x0,x1)[y0,y1) to be set on the image
	int y1,							// (INPUT)	region of interest [x0,x1)[y0,y1) to be set on the image
	int x0,							// (INPUT)	region of interest [x0,x1)[y0,y1) to be set on the image
	int x1,							// (INPUT)	region of interest [x0,x1)[y0,y1) to be set on the image
	int slice,						// (INPUT)  slice index 
	const std::string & params)		// (INPUT) additional parameters <param1=val, param2=val, ...> 
throw (iom::exception)
{
	//throw iom::exception(iom::strprintf("not implemented yet"), __iom__current__function__);

	/**/iom::debug(iom::LEV3, iom::strprintf("img_path = %s, img_width = %d, img_height = %d, img_bytes_x_chan = %d, img_chans = %d, y0 = %d, y1 = %d, x0 = %d, x1 = %d, params = \"%s\"",
		img_path.c_str(), img_width, img_height, img_bytes_x_chan, img_chans, y0, y1, x0, x1, params.c_str()).c_str(), __iom__current__function__);

	//disable warning handler to avoid messages on unrecognized tags
	TIFFSetWarningHandler(0);

	iim::sint64 stridex  = img_width * img_chans * img_bytes_x_chan;
	unsigned char *buf; // to scan the input buffer

	char *err_Tiff3Dfmt;

	y0 = (y0 < 0) ? 0: y0;
	y1 = (y1 < 0) ? img_height : y1;
	x0 = (x0 < 0) ? 0: x0;
	x1 = (x1 < 0) ? img_width  : x1;

	if ( y0 >= y1 || x0 >= x1 )
		throw iom::exception(iom::strprintf("wrong ROI (y0 = %d, y1 = %d, x0 = %d, x1 = %d)", y0, y1, x0, x1), __iom__current__function__);

	// append a slice

	buf = raw_img + x0*stridex + y0*img_chans*img_bytes_x_chan; // buf points to the first byte to be written

	if ( x0 == 0 && x1 == img_width && y0 == 0 && y1 == img_height ) { // all buffer must be written
		if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),slice,buf,img_width,img_height)) != 0 ) {
			throw iom::exception(iom::strprintf("(%s) unable to write slice %d into file %s",err_Tiff3Dfmt,slice,img_path.c_str()), __iom__current__function__);
		}
	}
	else { // copy to a sub buffer before writing
		iim::sint64 stridex_ROI    = (x1-x0) * img_chans * img_bytes_x_chan;
		iim::sint64 stridexy_ROI   = stridex_ROI * (y1-y0); // just because required by 'copyBlock2SubBuf', not actually used
		iim::sint64 stridexy       = stridex * img_height; // just because required by 'copyBlock2SubBuf', not actually used
		unsigned char *raw_img_ROI = new unsigned char[(y1-y0) * (x1-x0) * img_chans * img_bytes_x_chan];

		iim::VirtualFmtMngr::copyBlock2SubBuf(buf,raw_img_ROI,(y1-y0),(x1-x0),1,img_bytes_x_chan,stridex,stridexy,stridex_ROI,stridexy_ROI);

		if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),slice,raw_img_ROI,(x1-x0),(y1-y0))) != 0 ) {
			throw iom::exception(iom::strprintf("(%s) unable to write slice %d into file %s",err_Tiff3Dfmt,slice,img_path.c_str()), __iom__current__function__);
		}
		delete []raw_img_ROI;
	}
}
Example #2
0
// write 2D image data into a single (2D) image file
void 
	iomanager::tiff2D::writeData(
	std::string img_path,		// (INPUT)	image filepath (it includes the file extension)
	iom::real_t* raw_img,			// (INPUT)	a [0.0,1.0]-valued array storing the 2D image in channel->row order
	int img_height,				// (INPUT)	image height
	int img_width,				// (INPUT)	image width
	int img_chans,				// (INPUT)	number of channels
	int y0,						// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int y1,						// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int x0,						// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int x1,						// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int bpp,					// (INPUT)	color depth (bits per pixel)
	const std::string & params)	// (INPUT)	additional parameters <param1=val, param2=val, ...> 
throw (iom::exception)
{
	throw iom::exception(iom::strprintf("no more available"), __iom__current__function__);

	/**/iom::debug(iom::LEV3, iom::strprintf("img_path = %s, img_height = %d, img_width = %d, y0 = %d, y1 = %d, x0 = %d, x1 = %d, bpp = %d, params = \"%s\"", 
		img_path.c_str(), img_height, img_width, y0, y1, x0, x1, bpp, params.c_str()).c_str(), __iom__current__function__);


	// correct default parameters
	y1 = (y1 == -1 ? img_height - 1 : y1);
	x1 = (x1 == -1 ? img_width  - 1 : x1 );

	// compute ROI dimensions
	int ROI_height = y1 - y0 + 1;
	int ROI_width  = x1 - x0 + 1;

	// precondition checks
	if(! (y0>=0 && y1>y0 && y1<img_height && x0>=0 && x1>x0 && x1<img_width) )
		throw iom::exception(iom::strprintf("invalid ROI [%d,%d](X) x [%d,%d](Y) on image %d(X) x %d(Y)", x0, x1, y0, y1, img_width, img_height), __iom__current__function__);
	if(bpp != 8 && bpp != 16)
		throw iom::exception(iom::strprintf("unsupported bitdepth %d\n", bpp), __iom__current__function__);
	if(img_chans != 1)
		throw iom::exception(iom::strprintf("unsupported number of channels = %d\n. Only single-channel images are supported", img_chans), __iom__current__function__);

	// convert raw data to image data
	double proctime = -TIME(0);

	char *err_Tiff3Dfmt;

	// creates the file (2D image: depth is 1)
	if ( (err_Tiff3Dfmt = initTiff3DFile((char *)img_path.c_str(),ROI_width,ROI_height,1,img_chans,bpp/8)) != 0 ) {
		throw iom::exception(iom::strprintf("unable to create tiff file (%s)",err_Tiff3Dfmt), __iom__current__function__);
	}

	unsigned char *buffer = new unsigned char[ROI_height * ROI_width * img_chans * (bpp/8)];

	if(bpp == 8)
	{
		for(int i = 0; i <ROI_height; i++)
		{
			uint8* img_data = buffer + i*ROI_width*img_chans;
			for(int j = 0; j < ROI_width; j++)
				img_data[j] = static_cast<uint8>(raw_img[(i+y0)*img_width+j+x0] * 255.0f + 0.5f);
		}
	}
	else // bpp == 16
	{
		for(int i = 0; i <ROI_height; i++)
		{
			uint16* img_data = ((uint16 *)buffer) + i*ROI_width*img_chans; // the cast to uint16* guarantees the right offset
			for(int j = 0; j < ROI_width; j++)
				img_data[j] = static_cast<uint16>(raw_img[(i+y0)*img_width+j+x0] * 65535.0f + 0.5f);
		}
	}

	// update conversion time
	if(TIME_CALC)
	{
		proctime += TIME(0);
		time_conversions+=proctime;
		proctime = -TIME(0);
	}

	// save image
	if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),0,buffer,ROI_width,ROI_height)) != 0 ) {
		throw iom::exception(iom::strprintf("unable to save image at \"%s\". Unsupported format or wrong path.\n", img_path.c_str()), __iom__current__function__);
	}

	delete []buffer;

	// update IO time
	if(TIME_CALC)
	{
		proctime += TIME(0);
		time_IO+=proctime;
	}
}
Example #3
0
// Write 3D image data into a single (3D) image file
void 
	iomanager::tiff3D::writeData(
	std::string img_path,			// (INPUT)	image filepath (it includes the file extension)
	unsigned char * raw_img,		// (INPUT)	image data to be saved into the file
	int img_height,					// (INPUT)	image height (in pixels)
	int img_width,					// (INPUT)	image width (in pixels)
	int img_depth,					// (INPUT)  image depth (in pixels)
	int img_bytes_x_chan,			// (INPUT)  number of bytes per channel
	int img_chans,					// (INPUT)	number of channels
	int z0,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	int z1,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	int y0,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	int y1,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	int x0,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	int x1,							// (INPUT)	region of interest [x0,x1)[y0,y1)[z0,z1) to be set on the image
	const std::string & params)		// (INPUT) additional parameters <param1=val, param2=val, ...> 
throw (iom::exception) 
{
	//throw iom::exception(iom::strprintf("not implemented yet"), __iom__current__function__);

	/**/iom::debug(iom::LEV3, iom::strprintf("img_path = %s, img_width = %d, img_height = %d, img_depth = %d, img_bytes_x_chan = %d, img_chans = %d, z0 = %d, z1 = %d, y0 = %d, y1 = %d, x0 = %d, x1 = %d, params = \"%s\"",
		img_path.c_str(), img_width, img_height, img_depth,	img_bytes_x_chan, img_chans, z0, z1, y0, y1, x0, x1, params.c_str()).c_str(), __iom__current__function__);

	//disable warning handler to avoid messages on unrecognized tags
	TIFFSetWarningHandler(0);

	iim::sint64 stridex  = img_width * img_chans * img_bytes_x_chan;
	iim::sint64 stridexy = stridex * img_height;
	unsigned char *buf; // to scan the input buffer

	char *err_Tiff3Dfmt;

	z0 = (z0 < 0) ? 0: z0;
	z1 = (z1 < 0) ? img_depth  : z1;
	y0 = (y0 < 0) ? 0: y0;
	y1 = (y1 < 0) ? img_height : y1;
	x0 = (x0 < 0) ? 0: x0;
	x1 = (x1 < 0) ? img_width  : x1;

	if ( z0 >= z1 || y0 >= y1 || x0 >= x1 )
		throw iom::exception(iom::strprintf("wrong ROI (z0 = %d, z1 = %d, y0 = %d, y1 = %d, x0 = %d, x1 = %d)",z0, z1, y0, y1, x0, x1), __iom__current__function__);

	// creates an empty file
	if ( (err_Tiff3Dfmt = initTiff3DFile((char *)img_path.c_str(),(x1-x0),(y1-y0),(z1-z0),img_chans,img_bytes_x_chan)) != 0 ) {
		throw iom::exception(iom::strprintf("(%s) unable to create an empty tiff file %s",err_Tiff3Dfmt,img_path.c_str()), __iom__current__function__);
	}

	// append slice by slice

	buf = raw_img + z0*stridexy + x0*stridex + y0*img_chans*img_bytes_x_chan; // buf points to the first byte to be written

	if ( x0 == 0 && x1 == img_width && y0 == 0 && y1 == img_height ) { // all buffer must be written
		for ( int i=0; i<(z1-z0); i++, buf += stridexy ) {
			if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),i,buf,img_width,img_height)) != 0 ) {
				throw iom::exception(iom::strprintf("(%s) unable to write slice %d into file %s",err_Tiff3Dfmt,z0+i,img_path.c_str()), __iom__current__function__);
			}
		}
	}
	else { // copy to a sub buffer before writing
		iim::sint64 stridex_ROI    = (x1-x0) * img_chans * img_bytes_x_chan;
		iim::sint64 stridexy_ROI   = stridex_ROI * (y1-y0);
		unsigned char *raw_img_ROI = new unsigned char[(z1-z0) * (y1-y0) * (x1-x0) * img_chans * img_bytes_x_chan];
		unsigned char *buf_ROI;

		iim::VirtualFmtMngr::copyBlock2SubBuf(buf,raw_img_ROI,(y1-y0),(x1-x0),(z1-z0),img_bytes_x_chan,stridex,stridexy,stridex_ROI,stridexy_ROI);

		buf_ROI = raw_img_ROI;
		for ( int i=0; i<(z1-z0); i++, buf_ROI += stridexy_ROI ) {
			if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),i,buf_ROI,(x1-x0),(y1-y0))) != 0 ) {
				throw iom::exception(iom::strprintf("(%s) unable to write slice %d into file %s",err_Tiff3Dfmt,z0+i,img_path.c_str()), __iom__current__function__);
			}
		}
		delete []raw_img_ROI;
	}
}
Example #4
0
// Write 2D image data into a single (2D) image file
void 
	iomanager::tiff2D::writeData(
	std::string img_path,			// (INPUT)	image filepath (it includes the file extension)
	unsigned char *raw_img,			// (INPUT)	image data to be saved into the file
	int img_height,					// (INPUT)	image height
	int img_width,					// (INPUT)	image width
	int img_bytes_x_chan,			// (INPUT)  number of bytes per channel
	int img_chans,					// (INPUT)	number of channels
	int y0,							// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int y1,							// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int x0,							// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	int x1,							// (INPUT)	region of interest [x0,x1][y0,y1] to be set on the image
	const std::string & params)		// (INPUT) additional parameters <param1=val, param2=val, ...> 
throw (iom::exception)
{
	//throw iom::exception(iom::strprintf("not implemented yet"), __iom__current__function__);

	/**/iom::debug(iom::LEV3, iom::strprintf("img_path = %s, img_height = %d, img_width = %d, img_bytes_x_chan = %d, img_chans = %d, y0 = %d, y1 = %d, x0 = %d, x1 = %d, params = \"%s\"", 
		img_path.c_str(), img_height, img_width, img_bytes_x_chan, img_chans, y0, y1, x0, x1, params.c_str()).c_str(), __iom__current__function__);

	// correct default parameters
	y0 = (y0 < 0) ? 0: y0;
	y1 = (y1 < 0) ? img_height : y1;
	x0 = (x0 < 0) ? 0: x0;
	x1 = (x1 < 0) ? img_width  : x1;

	// compute ROI dimensions
	int ROI_height = y1 - y0;
	int ROI_width  = x1 - x0;

	// precondition checks
	if(! (y0>=0 && y1>y0 && y1<=img_height && x0>=0 && x1>x0 && x1<=img_width) )
		throw iom::exception(iom::strprintf("invalid ROI [%d,%d](X) x [%d,%d](Y) on image %d(X) x %d(Y)", x0, x1, y0, y1, img_width, img_height), __iom__current__function__);
	if(img_bytes_x_chan != 1 && img_bytes_x_chan != 2)
		throw iom::exception(iom::strprintf("unsupported bitdepth %d\n", img_bytes_x_chan*8), __iom__current__function__);
	if(img_chans != 1)
		throw iom::exception(iom::strprintf("unsupported number of channels = %d\n. Only single-channel images are supported", img_chans), __iom__current__function__);

	// convert raw data to image data
	double proctime = -TIME(0);

	char *err_Tiff3Dfmt;

	// creates the file (2D image: depth is 1)
	if ( (err_Tiff3Dfmt = initTiff3DFile((char *)img_path.c_str(),ROI_width,ROI_height,1,img_chans,img_bytes_x_chan)) != 0 ) {
		throw iom::exception(iom::strprintf("unable to create tiff file (%s)",err_Tiff3Dfmt), __iom__current__function__);
	}

	// update conversion time
	if(TIME_CALC)
	{
		proctime += TIME(0);
		time_conversions+=proctime;
		proctime = -TIME(0);
	}

	// save image

	if ( x0 == 0 && x1 == img_width && y0 == 0 && y1 == img_height ) { // all buffer must be written
		if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),0,raw_img,ROI_width,ROI_height)) != 0 ) {
			throw iom::exception(iom::strprintf("unable to save image at \"%s\". Unsupported format or wrong path.\n", img_path.c_str()), __iom__current__function__);
		}
	}
	else { // copy to a sub buffer before writing
		iim::sint64 stridex  = img_width * img_chans * img_bytes_x_chan;
		unsigned char *buf; // to scan the input buffer
		iim::sint64 stridex_ROI    = (x1-x0) * img_chans * img_bytes_x_chan;
		unsigned char *raw_img_ROI = new unsigned char[(y1-y0) * (x1-x0) * img_chans * img_bytes_x_chan];

		buf = raw_img + x0*stridex + y0*img_chans*img_bytes_x_chan; // buf points to the first byte to be written

		iim::VirtualFmtMngr::copyBlock2SubBuf(buf,raw_img_ROI,(y1-y0),(x1-x0),1,img_bytes_x_chan,stridex,0,stridex_ROI,0); // xy strides are 0 since the buffer is 2D

		if ( (err_Tiff3Dfmt = appendSlice2Tiff3DFile((char *)img_path.c_str(),0,raw_img_ROI,(x1-x0),(y1-y0))) != 0 ) {
			throw iom::exception(iom::strprintf("(%s) unable to write 2D image into file %s",err_Tiff3Dfmt,img_path.c_str()), __iom__current__function__);
		}

		delete []raw_img_ROI;
	}

	// update IO time
	if(TIME_CALC)
	{
		proctime += TIME(0);
		time_IO+=proctime;
	}
}