Code: Select all
int main( int argc, char *argv[] )
{
if ( 3 > argc )
{
return 1;
}
const char* infilename = argv[1];
const char* outfilename = argv[2];
// Read image
ImageInfo* pimageinfo = AcquireImageInfo();
GetImageInfo( pimageinfo );
strncpy( pimageinfo->filename, infilename, MaxTextExtent - 1 );
ExceptionInfo* pexceptioninfo = AcquireExceptionInfo();
Image* pimage = ReadImage( pimageinfo, pexceptioninfo );
// Image buffer
const size_t rows = pimage->rows;
const size_t columns = pimage->columns;
uint16_t* pData = (uint16_t*) malloc( rows * columns * 2 );
// Copy ImageMagick image to the destination buffer
QuantumInfo quantuminfo;
GetQuantumInfo( pimageinfo, &quantuminfo );
quantuminfo.depth = 16;
quantuminfo.endian = LSBEndian;
const Quantum* pquantum = 0;
for ( int y = 0; y < rows; ++y )
{
pquantum = GetVirtualPixels( pimage, 0, y, columns, 1, pexceptioninfo );
ExportQuantumPixels( pimage, 0, &quantuminfo, GrayQuantum,
(unsigned char*)( pData + y * columns ), pexceptioninfo );
}
// Completely clean up
DestroyImage( pimage );
pimage = 0;
DestroyExceptionInfo( pexceptioninfo );
pexceptioninfo = 0;
DestroyImageInfo( pimageinfo );
pimageinfo = 0;
// Write image
pimageinfo = AcquireImageInfo();
GetImageInfo( pimageinfo );
strncpy( pimageinfo->filename, outfilename, MaxTextExtent - 1 );
pimageinfo->depth = 16;
pimageinfo->endian = LSBEndian;
pimageinfo->type = GrayscaleType;
pexceptioninfo = AcquireExceptionInfo();
pimage = AcquireImage( pimageinfo, pexceptioninfo );
pimage->columns = columns;
pimage->rows = rows;
pimage->depth = 16;
SetImageType( pimage, pimageinfo->type, pexceptioninfo );
// Copy source buffer to the ImageMagick image
QuantumInfo* pquantuminfo = AcquireQuantumInfo( pimageinfo, pimage );
pquantum = 0;
for ( int y = 0; y < rows; ++y )
{
pquantum = GetAuthenticPixels( pimage, 0, y, columns, 1,
pexceptioninfo );
ImportQuantumPixels( pimage, 0, pquantuminfo, GrayQuantum,
(unsigned char*)( pData + y * columns ), pexceptioninfo );
}
DestroyQuantumInfo( pquantuminfo );
WriteImage( pimageinfo, pimage, pexceptioninfo );
DestroyImage( pimage );
DestroyExceptionInfo( pexceptioninfo );
DestroyImageInfo( pimageinfo );
free( pData );
return 0;
}