#! /usr/bin/gawk -f
# Last edited on 2003-10-31 20:55:18 by stolfi

BEGIN {
  usage = ( ARGV[0] " -v xsize=NNNN -v ysize=NNNN > p.sizes" );
  abort = -1;
  
  # Generates a basic "p.sizes" for a picture whose base version
  # Has size "xsize" by "ysize".
  # Do not use this for mug shots --- they have their own set
  # of standard sizes.
  
  if ((xsize !~ /^[0-9]+$/) || (xsize < 1)) { arg_error(("bad or missing \"xsize\"")); }
  if ((ysize !~ /^[0-9]+$/) || (ysize < 1)) { arg_error(("bad or missing \"ysize\"")); }
  
  # Compute GCD:
  m = xsize; n = ysize;
  while (n > 0) { r = m % n; m = n; n = r; }
  gcd = m;
  
  # Compute aspect ratio:
  xasp = xsize/gcd; yasp = ysize/gcd;
  if ((yasp > 35) && (xasp > 50))
    { arg_error(("bad aspect ratio " xasp ":" yasp)); }
  
  # Generate sizes.
  # The icon (first, smallest) 
  # should be about 70 pixels tall, but at most 128 pixels wide:
  ymin = 70;
  m = int(ymin/yasp + 0.5);
  xmax = 128;
  n = int(xmax/xasp);
  micon = (n < m ? n : m);
  out_size(micon, 2000, 1500);
  
  # Figure out the largest (default) size:
  m = int((xsize <  960 ? xsize : 960)/xasp);
  n = int((ysize <  800 ? ysize : 800)/yasp);
  mdef = (m < n ? m : n);
  
  # Then come various sizes in geometric progression:
  amin = 1.75*micon*micon*xasp*yasp;
  amax = 0.60*mdef*mdef*xasp*yasp;
  
  area = 70*98; sevens = 1;
  while (area <= amax)
    { if (area >= amin) 
        { out_area(area, 800, 600); area = 4*area; }
      else
        { area = int(sevens ? (area*100)/49 : (area*49)/25); }
    }
  
  # Finally, a maximum-size image (last, default)
  out_size(mdef, 2000, 1500)

}

function out_area(a, xmax, ymax, m)
{
  m = int(sqrt((a + (xasp*yasp) - 1)/(xasp*yasp)));
  out_size(m, xmax, ymax);
}

function out_size(m, xmax, ymax)
{
  x = m*xasp; y = m*yasp;
  if ((x <= xmax) && (y <= ymax))
    { printf("%03dx%03d\n", x, y); }
  else
    { printf "size %03dx%03d too big, skipped\n", x, y > "/dev/stderr"; }
}

function arg_error(msg)
{
  printf "*** %s\n", msg > "/dev/stderr";
  printf "usage: %s\n", usage > "/dev/stderr";
  abort = 1;
  exit abort;
}