私は ImageMagick/Magick++ の開発者/保守者ではないので、これがバグなのか「機能」なのかについてこれ以上コメントすることはできません。ただし、同じ問題があり、回避策としてこの関数を作成しました (ピクセル キャッシュが配置されていても、ピクセルを手動で反復するよりもはるかに高速であることに注意してください)。
Magick::Geometry CalculateImageMagickBoundingBox( const Magick::Image & image, const Magick::Color & borderColor )
{
// Clone input image.
Magick::Image clone( image );
// Remember original image size.
const Magick::Geometry originalSize( image.columns( ), image.rows( ) );
// Extend geometry by two in width and height (one pixel border).
Magick::Geometry extendedSize( originalSize.width( ) + 2, originalSize.height( ) + 2 );
// Extend cloned canvas (center gravity so 1 pixel border of user specified colour).
clone.extent( extendedSize, borderColor, Magick::CenterGravity );
// Calculate bounding box (will use border colour, which we have set above).
Magick::Geometry boundingBox = clone.boundingBox( );
// We added 1 pixel border, so subtract this now.
boundingBox.xOff( boundingBox.xOff( ) - 1 );
boundingBox.yOff( boundingBox.yOff( ) - 1 );
// Clamp (required for cases where entire image is border colour, and therefore the right/top borders
// that we added are taken into account).
boundingBox.width( std::min( boundingBox.width( ), originalSize.width( ) ) );
boundingBox.height( std::min( boundingBox.height( ), originalSize.height( ) ) );
// Return bounding box.
return boundingBox;
}
特定のケースでは、この関数を使用して、返されたジオメトリに基づいてキャンバス サイズを設定できます。