home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / info / prgramer / edmi / issue_1 / ugpm / scale.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  7.3 KB  |  203 lines

  1. /* SCALE.C -- Program to display grayscale (or redscale, or cyanscale, or...)
  2.    Version 0.96 */
  3.  
  4. /* This program is copyright (c) 1993 by Raja Thiagarajan. However, you
  5.    may freely use it for any non-commercial purpose. You can contact me
  6.    at sthiagar@bronze.ucs.indiana.edu */
  7. /* Thanks to Peter Nielsen (pnielsen@finabo.abo.fi) for some clever ideas. */
  8.  
  9. #include <stdlib.h>         /* include atoi(), ldiv(), max(), min() */
  10. #define INCL_GPI
  11. #define INCL_WIN
  12. #include <os2.h>
  13.  
  14. MRESULT EXPENTRY ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  15.  
  16. static USHORT redTarg;
  17. static USHORT greenTarg;
  18. static USHORT blueTarg;
  19.  
  20. /* Read redTarg, greenTarg, and blueTarg from the command line */
  21. VOID ParseArgs (INT ac, CHAR * av[])
  22. {
  23.    USHORT j;
  24.    redTarg = greenTarg = blueTarg = 0;
  25.    for (j = 1; j < ac; j++) {
  26.       CHAR a = av[j][0];
  27.       if ((a == '-') || (a == '/')) {
  28.          CHAR a = av[j][1];
  29.          SHORT v = atoi (&av[j][2]);
  30. #define MIN_VAL 0
  31. #define MAX_VAL 255
  32.          v = min (max (v, MIN_VAL), MAX_VAL);
  33.          if (a == 'r') {
  34.             redTarg = v;
  35.          } else if (a == 'g') {
  36.             greenTarg = v;
  37.          } else if (a == 'b') {
  38.             blueTarg = v;
  39.          }
  40.       }
  41.    }
  42.    if (!(redTarg | greenTarg | blueTarg)) {
  43.       redTarg = greenTarg = blueTarg = MAX_VAL;
  44.    }
  45. }
  46.  
  47. static LONG   hasPalMan;      /* Whether the Palette Manager is available */
  48. static LONG   colorsToShow;   /* How many colored rectangles to draw */
  49. static HAB    hab;            /* This program's hab */
  50.  
  51. INT main (INT argc, CHAR * argv [])
  52. {
  53.    HMQ   hmq;         /* The usual bunch of variables for PM programs */
  54.    QMSG  qmsg;
  55.    HWND  hwnd,
  56.          hwndClient;
  57.  
  58.    ULONG createFlags  =  FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER
  59.                                       | FCF_MINMAX | FCF_SHELLPOSITION
  60.                                       | FCF_TASKLIST;
  61.  
  62. #define clientClass "scale"
  63.  
  64.    ParseArgs (argc, argv);
  65.  
  66.    {
  67.       LONG sColors; /* How many colors are available */
  68.       HDC hdcScr;   /* Used to query the screen driver */
  69.       hdcScr = GpiQueryDevice (WinGetPS (HWND_DESKTOP));
  70.       DevQueryCaps (hdcScr, CAPS_ADDITIONAL_GRAPHICS, 1, &hasPalMan);
  71.       hasPalMan &= CAPS_PALETTE_MANAGER;
  72.       DevQueryCaps (hdcScr, CAPS_COLORS, 1L, &sColors);
  73.       colorsToShow = sColors;
  74. #define MAX_SHADES 64
  75.       if (colorsToShow > MAX_SHADES) {
  76.         colorsToShow = MAX_SHADES;
  77.       }
  78.    }
  79.  
  80.    hab = WinInitialize (0);  /* initialize PM usage */
  81.  
  82.    hmq = WinCreateMsgQueue (hab, 0);   /* create message queue */
  83.  
  84.    WinRegisterClass (hab, clientClass, (PFNWP) ClientWinProc, CS_SIZEREDRAW, 0);
  85.  
  86.       /* Create standard window and client. Note that the title message
  87.          depends on whether the Palette Manager is available or not */
  88.    if (hasPalMan) {
  89.       hwnd = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE, &createFlags,
  90.                                  clientClass, "Palette-Managed Color Scale", 0L,
  91.                                  0UL, 0, &hwndClient);
  92.    } else {
  93.       hwnd = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE, &createFlags,
  94.                                  clientClass, "Color Scale", 0L, 0UL, 0,
  95.                                  &hwndClient);
  96.    }
  97.  
  98.    while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0)) { /*  msg dispatch loop */
  99.       WinDispatchMsg (hab, &qmsg);
  100.    }
  101.  
  102.    WinDestroyWindow (hwnd);           /* destroy frame window */
  103.    WinDestroyMsgQueue (hmq);          /* destroy message queue */
  104.    WinTerminate (hab);                /* terminate PM usage */
  105.  
  106.    return 0;
  107. }
  108.  
  109. MRESULT EXPENTRY ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  110. {
  111.    static HPS    hps;
  112.    static HDC    hdc;
  113.    static HPAL   hpal;
  114.    static USHORT cWid, cHi; /* client area width and height */
  115.    static BOOL   firstPaint = TRUE; /* Whether this is the first response
  116.                                        to WM_PAINT */
  117.  
  118.    switch (msg)
  119.       {
  120.          case WM_CREATE:
  121.             {
  122.                SIZEL sizl = {0L, 0L};
  123.                hdc = WinOpenWindowDC (hwnd);
  124.                hps = GpiCreatePS (hab, hdc, &sizl, PU_PELS | GPIF_DEFAULT
  125.                         | GPIT_MICRO | GPIA_ASSOC);
  126.                if (hasPalMan) {
  127.                   ULONG  tbl [MAX_SHADES];
  128.                   INT    j;
  129.                   for (j = 0; j < colorsToShow; j++) {
  130.                      USHORT rj = redTarg * j / (colorsToShow - 1);
  131.                      USHORT gj = greenTarg * j / (colorsToShow - 1);
  132.                      USHORT bj = blueTarg * j / (colorsToShow - 1);
  133.                      tbl [j] = 65536 * rj + 256 * gj + bj;
  134.                   }
  135.                   hpal = GpiCreatePalette (hab, 0L, LCOLF_CONSECRGB,
  136.                                            colorsToShow, tbl);
  137.                   GpiSelectPalette (hps, hpal);
  138.                }
  139.             }
  140.             return (MRESULT) FALSE;
  141.          case WM_DESTROY:
  142.             if (hasPalMan) {
  143.                GpiSelectPalette (hps, NULLHANDLE);
  144.                GpiDeletePalette (hpal);
  145.             }
  146.             GpiDestroyPS (hps);
  147.             return (MRESULT) FALSE;
  148.          case WM_ERASEBACKGROUND:
  149.             return (MRESULT) TRUE;
  150.          case WM_PAINT:
  151.             {
  152.                POINTL ptl;
  153.                LONG   j;
  154.  
  155.                WinBeginPaint (hwnd, hps, NULL);
  156.                if (hasPalMan && firstPaint) {
  157.                  /* Realize the palette when you paint for the first time.
  158.                     This is necessary in case the program doesn't start
  159.                     in the foreground. */
  160.                  ULONG palSize = colorsToShow;
  161.                  WinRealizePalette (hwnd, hps, &palSize);
  162.                  firstPaint = FALSE;
  163.                }
  164.                for (j = 0; j < colorsToShow; j++) {
  165.                   if (hasPalMan) { /* use the exact color */
  166.                      GpiSetColor (hps, j);
  167.                   } else { /* find a reasonable facsimile */
  168.                      USHORT rj = redTarg * j / (colorsToShow - 1);
  169.                      USHORT gj = greenTarg * j / (colorsToShow - 1);
  170.                      USHORT bj = blueTarg * j / (colorsToShow - 1);
  171.                      LONG i = GpiQueryColorIndex (hps, 0, 65536 * rj
  172.                                                           + 256 * gj + bj);
  173.                      GpiSetColor (hps, i);
  174.                   }
  175.                   ptl.y = 0;
  176.                   ptl.x = j * cWid / colorsToShow;
  177.                   GpiMove (hps, &ptl);
  178.                   ptl.y = cHi;
  179.                   ptl.x += cWid / colorsToShow;
  180.                   GpiBox (hps, DRO_FILL, &ptl, 0L, 0L);
  181.                }
  182.                WinEndPaint (hps);
  183.             }
  184.             return (MRESULT) FALSE;
  185.          case WM_REALIZEPALETTE:
  186.             {
  187.                ULONG palSize = colorsToShow;
  188.                if (WinRealizePalette (hwnd, hps, &palSize)) {
  189.                   WinInvalidateRect (hwnd, NULL, FALSE);
  190.                }
  191.             }
  192.             return (MRESULT) FALSE;
  193.          case WM_SIZE:
  194.             cWid = SHORT1FROMMP (mp2);
  195.             cHi = SHORT2FROMMP (mp2);
  196.             return (MRESULT) FALSE;
  197.          default:
  198.             return (WinDefWindowProc (hwnd, msg, mp1, mp2));
  199.             break;
  200.       }  /*end switch*/
  201.    return (MRESULT) FALSE;
  202. }
  203.