composite on x86_64 is strange

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
AndreyKl

composite on x86_64 is strange

Post by AndreyKl »

Hello.

I faced problem with composite.
I have very small test code:

Code: Select all

#include <Magick++.h>
#include <iostream>
using namespace std;
using namespace Magick;
int main(/*int argc,char **argv*/)
{
  Image top("top.png"), bottom("bottom.png");

  try {
    bottom.composite(top, CenterGravity, OverCompositeOp);
    bottom.write("top-over-bottom.png");
  }
  catch( Exception &error_ ) {
      cout << "Caught exception: " << error_.what() << endl;
      return 1;
  }
  return 0;
}
files:

top.png: http://pic.ipicture.ru/uploads/090403/TN42cTRVy3.png
Image

bottom.png, just transparent image 110px × 130px with blue border: http://pic.ipicture.ru/uploads/090403/WgaekwSWht.png
Image

top-over-bottom.png, right version: http://pic.ipicture.ru/uploads/090403/oVY7gt7W93.png
Image

top-over-bottom.png, not right version: http://pic.ipicture.ru/uploads/090403/4qTnz3iQOg.png
Image

on my home pc i have right version. but on server i have image that is very similar to bottom.png.
but

Code: Select all

/usr/local/bin/composite -gravity center top.png bottom.png top-over-bottom.png 
works perfect on both, my home pc and server

Info about server:

cat /etc/redhat-release
CentOS release 5.2 (Final)

gcc --version
gcc (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)

/usr/local/bin/Magick++-config --version
6.4.2 Q8

ImageMagick configure string
./configure --with-quantum-depth=8 --without-perl

uname -a
Linux xxxx.net 2.6.18-92.1.17.el5 #1 SMP Tue Nov 4 13:43:30 EST 2008 x86_64 x86_64 x86_64 GNU/Linux

Looks like its bug.

Could anybody approve or/and fix, pls.

And if it will possible, post patch here.

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite on x86_64 is strange

Post by fmw42 »

to ensure it is not an IM version issue, be sure to specify what -compose method you want. See http://www.imagemagick.org/Usage/compose/
AndreyKl

Re: composite on x86_64 is strange

Post by AndreyKl »

fmw42 wrote:to ensure it is not an IM version issue, be sure to specify what -compose method you want. See http://www.imagemagick.org/Usage/compose/

Code: Select all

/usr/local/bin/composite -compose Over  -gravity center top.png bottom.png top-over-bottom3.png
the same result.

also, i'm compiling 6.5.1-0 right now, actually, i checked several versions more then 6.4.2 - it was the same for all. But now i'll check last one to be absolutly sure.
AndreyKl

Re: composite on x86_64 is strange

Post by AndreyKl »

/usr/local/bin/Magick++-config --version
6.5.1 Q8

the same results

thanks for anser
AndreyKl

Re: composite on x86_64 is strange

Post by AndreyKl »

it was problem in file Magick++/lib/Image.cpp

there are strings like this:

Code: Select all

long x;

unsigned int ...columns() { ...

x = static_cast<long>(columns() - compositeImage_.columns()) >> 1;
if columns() was less then compositeImage_.columns() (by 1), it looks like 0xffffffff on 32 bit machine, what is exactly -1. After sar 1, it is still -1 (again 0xffffffff).

Problems is that on 64bit platform long is 8bytes but unsigned int is only 4 bytes, so, we have after substract 0xffffffff , that is -1 for 4 bytes int, but it is 0x00000000ffffffff for 8 bytes).

more correct code will be

Code: Select all

x = (static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns())) >> 1;
i could not find bugzilla... how could i submit patch?

bellow is patch for Magick++/lib/Image.cpp

Code: Select all

672c672
<               x = static_cast<long>(columns() - compositeImage_.columns()) >> 1;
---
>               x = (static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns())) >> 1;
678c678
<               x = static_cast<long>(columns() - compositeImage_.columns());
---
>               x = static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns());
685c685
<               y = static_cast<long>(rows() - compositeImage_.rows()) >> 1;
---
>               y = (static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows())) >> 1;
693,694c693,694
<               x = static_cast<long>(columns() - compositeImage_.columns()) >> 1;
<               y = static_cast<long>(rows() - compositeImage_.rows()) >> 1;
---
>               x = (static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns())) >> 1;
>               y = (static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows())) >> 1;
699,700c699,700
<               x = static_cast<long>(columns() - compositeImage_.columns());
<               y = static_cast<long>(rows() - compositeImage_.rows()) >> 1;
---
>               x = static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns());
>               y = (static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows())) >> 1;
706c706
<               y = static_cast<long>(rows() - compositeImage_.rows());
---
>               y = static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows());
711,712c711,712
<               x =  static_cast<long>(columns() - compositeImage_.columns()) >> 1;
<               y = static_cast<long>(rows() - compositeImage_.rows());
---
>               x = (static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns())) >> 1;
>               y = static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows());
717,718c717,718
<               x = static_cast<long>(columns() - compositeImage_.columns());
<               y = static_cast<long>(rows() - compositeImage_.rows());
---
>               x = static_cast<long>(columns()) - static_cast<long>(compositeImage_.columns());
>               y = static_cast<long>(rows()) - static_cast<long>(compositeImage_.rows());
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: composite on x86_64 is strange

Post by magick »

Thanks for the patch. We took a slightly different approach using the MagickCore GravityAdjustGeometry() method but it gives the desired result.
Post Reply