[Zope] Photo: somes issues and thoughts

Jose' Sebrosa sebrosa@artenumerica.com
Fri, 04 Jan 2002 13:29:45 +0000


J M Cerqueira Esteves wrote:
> 
> * marc lindahl <marc@bowery.com> [2002-01-04 01:54 +0000]:
> > > From: J M Cerqueira Esteves <jmce@artenumerica.com>
> >
> > > while Photo uses (with integer division, therefore with truncation
> > > everywhere):
> > >
> > > if hM > h0 * wM / w0:
> > >   height = h0 * wM / w0
> > >   width = wM
> > > else:
> > >   height = hM
> > >   width =  w0 * hM / h0   [I had a mistake here, /w0 instead of /h0]
> >
> >
> > Look at the order of operations - the truncation doesn't hurt anything.
> 
> I believe you are mentioning that
> 
>    h0*wM/w0 == (h0*wM)/w0 == int (float(h0*wM) / w0)
> 
> instead of the (much worse) h0 * (wM/w0)...  I was aware of that.
> 
> The main difference is that Photo defines new dimensions with integer
> truncation, while Image Magick rounds each to the nearest integer
> (when results of both calculations are different, the aspect ratio is
> closer to the original with Image Magick).


Sometimes, truncation (floor) gives better aspect ratio than round.  I would
even not be surprised if, statistically, none of the methods were better.

Two examples:

1-
Initial size: 51x49
Scale: 1/10
Final size (method=round): 5x5
Final size (method=floor): 5x4
Best method: round

2-
Initial size: 54x56
Scale: 1/10
Final size (method=round): 5x6
Final size (method=floor): 5x5
Best method: floor


If you are *really* concerned about aspect ratio (I mean, if half a pixel does
*really* matter) and have not enough time to prove mathematical theorems on
integer arithmetics, I would suggest you to use brute force and ignorance:
Given a scale factor, choose between the four combinations [(x, y) either
scaled with method (ceil, floor)] which one gives better aspect ratio (paying
attention to boundary conditions such as none of the dimentions being lesser
than 1 (zero height/width is no good at all!) or bigger than some limit --
eventually these boundary conditions will make the size of your code increase
quite a bit, but hey!, life is hard... :^) ).

But if you decide that a difference of half a pixel in the aspect ratio is not
such a big thing for your problem, maybe the better is to obey to the scale
factor, so you should always use the round method (paying attention to boundary
conditions as before).


Sebrosa