/********************************************************************************** * Returns a new, gaussian-noise corrupted image from the given image ***********************************************************************************/ Image4DSimple* CImageUtils::addGaussianNoise( Image4DSimple* im, // input image float w) // gaussian noise weight (1 = only noise, 0 = no noise) throw (tf::RuntimeException) { // checks if(!im || !im->getRawData()) throw tf::RuntimeException("in CImageUtils::addGaussianNoise(): invalid image data"); if(im->getTDim() > 1) throw tf::RuntimeException(tf::strprintf("in CImageUtils::addGaussianNoise(): image has TDim = %d (5D not supported)", im->getTDim())); // allocate new image Image4DSimple* imout = new Image4DSimple(); imout->setXDim(im->getXDim()); imout->setYDim(im->getYDim()); imout->setZDim(im->getZDim()); imout->setCDim(im->getCDim()); imout->setTDim(im->getTDim()); imout->setDatatype(im->getDatatype()); V3DLONG data_size = imout->getTotalBytes(); unsigned char* data = new unsigned char[data_size]; imout->setRawDataPointer(data); // data corruption if(imout->getDatatype() == V3D_UINT8) { unsigned char* data_in = im->getRawData(); for(V3DLONG k=0; k<data_size; k++) data[k] = static_cast<tf::uint8>((1-w)*data_in[k] + w*(rand()%256) +0.5f); } else if(imout->getDatatype() == V3D_UINT16) { unsigned short* data_in = (unsigned short*)(im->getRawData()); for(V3DLONG k=0; k<data_size/2; k++) data[k] = static_cast<tf::uint16>((1-w)*data_in[k] + w*(rand()%65535) +0.5f); } else throw tf::RuntimeException("in CImageUtils::addGaussianNoise(): image is neither UINT8 nor UINT16 (not supported)"); return imout; }