home *** CD-ROM | disk | FTP | other *** search
/ DTP Toolbox / DTPToolbox.iso / utilities / graphics / wmf_convert / src / window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-05  |  5.4 KB  |  248 lines

  1. /*
  2. * Virtual Window for Windows Metafile (WMF).
  3. *
  4. * Need to put more thought into clipping...
  5. *
  6. * $Log: window.c,v $
  7. *  Revision 1.7  1995/12/04  19:24:33  JFusco
  8. *  #ifdef'd out some debug messages.
  9. *
  10. *  Revision 1.6  1994/11/08  22:10:52  JFusco
  11. *  Added iff save.
  12. *
  13. *  Revision 1.5  1994/11/04  20:51:43  JFusco
  14. *  Added some speedup code.
  15. *
  16. *  Revision 1.4  1994/06/20  23:23:06  JFusco
  17. *  Added polygon fill routines
  18. *
  19. *  Revision 1.3  1994/06/19  21:59:45  JFusco
  20. *  Fixed scaling problem when scaling images down.
  21. *  Scaling up has not been tested.
  22. *
  23. *  Revision 1.2  1994/06/19  10:48:43  JFusco
  24. *  Fixed scaling problems.  Need to watch out for possible
  25. *  problems with coodinate systems.  Do all WMF files use the
  26. *  same convention?
  27. *
  28. *  Revision 1.1  94/06/18  02:45:07  JFusco
  29. *  Initial revision
  30. *
  31. */
  32.  
  33. #include <stdio.h>
  34. #include "wmf.h"   /* WMF Structures and types.   */
  35. #include "amiga.h" /* Amiga<->WMF Specific stuff. */
  36.  
  37. static char RCS_ID[] = "$Id: window.c,v 1.7 1995/12/04 19:24:33 JFusco Exp $";
  38.  
  39. /* Virtual window origin and extent. */
  40. yx_pair_t WindowOrg={0,0}, WindowExt={0,0};
  41. short Top, Left, Bottom, Right;
  42.  
  43. /* Integer scaling. */
  44. struct ratio { long num, den; };
  45. struct scale { struct ratio X, Y; } Scale;
  46.  
  47. void InitVirtualWindow( wmf_place_hdr_t *PlaceHdr )
  48. {
  49.    ULONG xfactor, yfactor;
  50.    int aspectx, aspecty;
  51.    struct xy_pair display_dim, extent;
  52.  
  53.    Top    = PlaceHdr->Top;
  54.    Left   = PlaceHdr->Left;
  55.    Bottom = PlaceHdr->Bottom;
  56.    Right  = PlaceHdr->Right;
  57.  
  58.    /* This corrects for non-square pixels. */
  59.    GetPixelAspect( &aspectx, &aspecty );
  60.  
  61.    display_dim.X = WINDOWX-1;
  62.    display_dim.Y = WINDOWY-1;
  63.    extent.X = abs(WindowExt.X);
  64.    extent.Y = abs(WindowExt.Y);
  65.  
  66.    /* Determine scale factor - up or down. */
  67.    if (extent.X > display_dim.X * aspectx || extent.Y > display_dim.Y * aspecty)
  68.       /* Scaling down. */
  69.       {
  70.       /* Is it tall or wide? */
  71.       xfactor = extent.X / (display_dim.X * aspectx);
  72.       yfactor = extent.Y / (display_dim.Y * aspecty);
  73.       if ( abs(yfactor>xfactor) ) 
  74.          { /* Tall - scale to the Y dimension */
  75.          Scale.X.num = display_dim.X;
  76.          Scale.Y.num = display_dim.Y;
  77.          Scale.X.den = extent.Y;
  78.          Scale.Y.den = extent.Y;
  79.          }
  80.       else
  81.          { /* Wide - scale to the X dimension */
  82.          Scale.X.num = display_dim.X;
  83.          Scale.Y.num = display_dim.Y;
  84.          Scale.X.den = extent.X;
  85.          Scale.Y.den = extent.X;
  86.          }
  87.       }
  88.    else
  89.       /* Scaling up. */
  90.       {
  91.       /* Is it tall or wide? */
  92.       xfactor = (display_dim.X * aspecty) / extent.Y;
  93.       yfactor = (display_dim.Y * aspectx) / extent.X;
  94.       if ( abs(yfactor>xfactor) )
  95.          { /* Tall - scale to the Y dimension */
  96.          Scale.X.num = extent.Y;
  97.          Scale.Y.num = extent.Y;
  98.          Scale.X.den = display_dim.X;
  99.          Scale.Y.den = display_dim.Y;
  100.          }
  101.       else
  102.          { /* Wide - scale to the X dimension */
  103.          Scale.X.num = extent.X;
  104.          Scale.Y.num = extent.X;
  105.          Scale.X.den = display_dim.X;
  106.          Scale.Y.den = display_dim.Y;
  107.          }
  108.       };
  109.  
  110. #ifdef DEBUG
  111.    printf("Output window will be %d x %d\n", display_dim.X, display_dim.Y);
  112.    printf("X Scale factor is %d/%d\n",Scale.X.num, Scale.X.den);
  113.    printf("Y Scale factor is %d/%d\n",Scale.X.num, Scale.X.den);
  114. #endif
  115. }
  116.  
  117. /* WC stands for Window Coordinates. */
  118.  
  119. MoveWC( short x, short y )
  120. {
  121.    register long rx, ry;
  122.  
  123.    /* Need to do real clipping! */
  124.    x = (x>Right)  ? Right  : x;
  125.    x = (x<Left)   ? Left   : x;
  126.    y = (y<Top)    ? Top    : y;
  127.    y = (y>Bottom) ? Bottom : y;
  128.  
  129.    y -= WindowOrg.Y;
  130.    y = -y;
  131.    x -= WindowOrg.X;
  132.  
  133.    rx = x;
  134.    ry = y;
  135.  
  136.    rx *= Scale.X.num;
  137.    rx /= Scale.X.den;
  138.    x = (short) rx;
  139.    ry *= Scale.Y.num;
  140.    ry /= Scale.Y.den;
  141.    y = (short) ry;
  142.    MoveAmiga( x, y );
  143. }
  144.  
  145. DrawWC( short x, short y )
  146. {
  147.    register long rx, ry;
  148.    static short lastx, lasty;
  149.  
  150.    /* Need to do real clipping! */
  151.    x = (x>Right)  ? Right  : x;
  152.    x = (x<Left)   ? Left   : x;
  153.    y = (y<Top)    ? Top    : y;
  154.    y = (y>Bottom) ? Bottom : y;
  155.  
  156.    y -= WindowOrg.Y;
  157.    y = -y;
  158.    x -= WindowOrg.X;
  159.  
  160.    if (x==lastx && y==lasty) {
  161.       return;
  162.    }
  163.    else {
  164.       lastx = x;
  165.       lasty = y;
  166.    }
  167.  
  168.    rx = x;
  169.    ry = y;
  170.  
  171.    rx *= Scale.X.num;
  172.    rx /= Scale.X.den;
  173.    x = (short) rx;
  174.    ry *= Scale.Y.num;
  175.    ry /= Scale.Y.den;
  176.    y = (short) ry;
  177.    DrawAmiga( x, y );
  178. }
  179.  
  180. PolygonMoveWC( short x, short y )
  181. {
  182.    register long rx, ry;
  183.  
  184.    /* Need to do real clipping! */
  185.    x = (x>Right)  ? Right  : x;
  186.    x = (x<Left)   ? Left   : x;
  187.    y = (y<Top)    ? Top    : y;
  188.    y = (y>Bottom) ? Bottom : y;
  189.  
  190.    y -= WindowOrg.Y;
  191.    y = -y;
  192.    x -= WindowOrg.X;
  193.  
  194.    rx = x;
  195.    ry = y;
  196.  
  197.    rx *= Scale.X.num;
  198.    rx /= Scale.X.den;
  199.    x = (short) rx;
  200.    ry *= Scale.Y.num;
  201.    ry /= Scale.Y.den;
  202.    y = (short) ry;
  203.    PolygonMoveAmiga( x, y );
  204. }
  205.  
  206. PolygonDrawWC( short x, short y )
  207. {
  208.    register long rx, ry;
  209.    static short lastx, lasty;
  210.  
  211.    /* Need to do real clipping! */
  212.    x = (x>Right)  ? Right  : x;
  213.    x = (x<Left)   ? Left   : x;
  214.    y = (y<Top)    ? Top    : y;
  215.    y = (y>Bottom) ? Bottom : y;
  216.  
  217.    y -= WindowOrg.Y;
  218.    y = -y;
  219.    x -= WindowOrg.X;
  220.  
  221.    if (x==lastx && y==lasty) {
  222.       return;
  223.    }
  224.    else {
  225.       lastx = x;
  226.       lasty = y;
  227.    }
  228.  
  229.    rx = x;
  230.    ry = y;
  231.  
  232.    rx *= Scale.X.num;
  233.    rx /= Scale.X.den;
  234.    x = (short) rx;
  235.    ry *= Scale.Y.num;
  236.    ry /= Scale.Y.den;
  237.    y = (short) ry;
  238.    PolygonDrawAmiga( x, y );
  239. }
  240.  
  241. PolygonEndWC( )
  242. {
  243.    PolygonEndAmiga( );
  244. }
  245.  
  246.  
  247.