0

The following image of size 1x9 is being trimmed to 1x6 because presumably the pixel at the top is the same color as the pixel at the bottom and in the trim function, these pixels are being identified as the background color, even though the backgroundColor being reported before the execution of the trim function is #FFFFFF.

http://s1.postimage.org/a7r69yxsr/m_medium_bc.png

The only thing I am doing is executing trim on the Image. Explicitly setting backgroundColor and/or transparent() makes no difference.

  1. Why is this occurring and is this the expected behavior?
  2. Can this be fixed by configuration/property setting/without changing Graphicsk library code?
  3. If not, when can this bug be fixed? Do you expect a bug of this nature to be fixed in the next few days?

Here is the code:

Magick::Image tempImage;
tempImage.read(name);
std::cout<<"size:"<<tempImage.columns()<<","<<tempImage.rows()<<std::endl;
temp=tempImage.backgroundColor();
std::cout<<"bg:"<<(std::string)temp<<std::endl;
tempImage.trim();
std::cout<<"size:"<<tempImage.columns()<<","<<tempImage.rows()<<std::endl;
Andrea
  • 11,801
  • 17
  • 65
  • 72
MetaChrome
  • 3,210
  • 6
  • 31
  • 48
  • Can you post the exact command you are using? – PinnyM Jan 14 '13 at 22:30
  • This occurs in the GetBoundingBox(name of the top of my head?) function of GraphicsMagick and can be fixed relatively easily. The results of that function are peculiar with regards to it's intentions/usefulness. It treats the colors at the corners of the image as a background color. – MetaChrome Jan 21 '13 at 23:26

1 Answers1

0

I agree that this behaviour is strange, I am not a developer/maintainer of ImageMagick/Magick++ so can't comment further as to whether this is a bug or 'feature'. However I had the same issue and created this function as a workaround (note this is much faster than manually iterating the pixels, even with a pixel cache in place):

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;
}

In your particular case, you could use this function and then set the canvas size based on the geometry returned.

Steven Craft
  • 1,243
  • 11
  • 15