ome / omero-insight

Gradle project containing insight java client for OMERO
https://www.openmicroscopy.org/omero/
GNU General Public License v2.0
7 stars 14 forks source link

Cast may be needed #411

Open scuniff opened 12 months ago

scuniff commented 12 months ago

In file:

https://github.com/ome/omero-insight/blob/40566b97d28f9caf5720a55faf15c324a8c6e5de/src/main/java/org/openmicroscopy/shoola/agents/imviewer/browser/BrowserUI.java#L134

Regarding this code:

        int sizeX = rl.width;
    int sizeY = rl.height;
    double vx = sizeX/d.width;
    double vy = sizeY/d.height;
    int x = (int) (vx*region.x);
    int y = (int) (vy*region.y);

As is, the division is being done in integer precision. If double precision is really wanted then sizeX and sizeY need to be cast to double:

    int sizeX = rl.width;
    int sizeY = rl.height;
    double vx = (double)sizeX/d.width;
    double vy = (double)sizeY/d.height;
    int x = (int) (vx*region.x);
    int y = (int) (vy*region.y);

Example snippet:

       double d;

    int a=5;
    int b = 2;

    d = a/b;        
    System.out.println("no cast, d is " + d);

    d = (double) a/b;
    System.out.println("with cast, d is " + d);

Output:

no cast, d is 2.0 with cast, d is 2.5

dominikl commented 11 months ago

I don't think this matters. Close?

scuniff commented 11 months ago

Sounds ok to me .

I did though look into the code some and got a basic understanding. The following example shows what the width of the new rectangle would be with and without double precision.

Referring to lines 130 – 138 and just focusing on the width calculation:

    Dimension d = birdEyeView.getImageSize();
    Rectangle rl = canvas.getBounds();
    int sizeX = rl.width;
    int sizeY = rl.height;
    double vx = sizeX/d.width;
    double vy = sizeY/d.height;
    int x = (int) (vx*region.x);
    int y = (int) (vy*region.y);
    int w = (int) (vx*region.width);

Assume:

Canvas is 500x500 Image is 260x260 Region is 200x200

Then……

sizeX=500 // canvas d.width = 260 // image

then vx would be: double vx = sizeX/d.width;

vx = 1.9 // double precsion vx=1 // int precision

Assume: region.width=200

then width of new Rectangle would be: int w = (int) (vx*region.width);

w = 380 // with double precision w = 200 // with int precision

Is this difference between the 2 precisions ok??

scuniff commented 11 months ago

Sorry, closed this by accident....