
Originally Posted by
spawnwj
I have updated the code as shown below...
I can see the full image now but it is rotated anticlockwise 90 degree....
Any idea why?
I'm surprised with this behaviour. Do you sure that it happened because of the function of convert?
I want to explain a bit. This function was made only for display of images, not for processing. This algorithm does scale 16bit values to the 8bit range and considers min and max values of pixels at the same time. If you will processing 8bit image and you need to keep the source image as far as possible you should change the algorithm of convert. It should be such as:
template<typename T>
bool conv16to8bit(const T * const data, unsigned char * const dest, unsigned len)
{
if ( !data || !dest || !len ) return false;
size_t type_size = sizeof(T);
if ( type_size != 2 ) return false;
bool isSigned = ( typeid(T) == typeid(signed short) );
T maxValue = ( isSigned ?
static_cast<T>(::pow(2, type_size * 8 - 1)/2)-1 :
static_cast<T>(::pow(2, type_size * 8 - 1)) );
unsigned char * dst = dest;
for (const T * pix = data; pix < data+len; pix++)
{
int c = static_cast<int>( 0.5f + (::abs(*pix) / maxValue * 255));
if ( c < 0 ) c = 0;
if ( c > 255 ) c = 255;
*dst++ = static_cast<unsigned char>(c);
}
return true;
};
template<typename T>
bool conv16to8bit(const T * const data, unsigned char * const dest, unsigned len)
{
if ( !data || !dest || !len ) return false;
size_t type_size = sizeof(T);
if ( type_size != 2 ) return false;
bool isSigned = ( typeid(T) == typeid(signed short) );
T maxValue = ( isSigned ?
static_cast<T>(::pow(2, type_size * 8 - 1)/2)-1 :
static_cast<T>(::pow(2, type_size * 8 - 1)) );
unsigned char * dst = dest;
for (const T * pix = data; pix < data+len; pix++)
{
int c = static_cast<int>( 0.5f + (::abs(*pix) / maxValue * 255));
if ( c < 0 ) c = 0;
if ( c > 255 ) c = 255;
*dst++ = static_cast<unsigned char>(c);
}
return true;
};
To copy to clipboard, switch view to plain text mode
I didn't check this code. May be it has a couple errors.
Bookmarks