home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / OCOLMAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-28  |  4.3 KB  |  160 lines

  1. /*
  2.     ocolmap.c    10/19/88
  3.  
  4.     % color map related utility functions.
  5.     by Ted.
  6.  
  7.     OWL 1.2
  8.     Copyright (c) 1988, by Oakland Group, Inc.
  9.     ALL RIGHTS RESERVED.
  10.  
  11.     Revision History:
  12.     -----------------
  13.      5/09/89 ted    Changed Alloc/Free terms to Open/Close.
  14.      6/27/89 ted    Removed references to min and max.
  15.      8/09/89 ted    Fixed nentries bug in ocolmap_Open, made it use ocalloc.
  16.  
  17.     11/29/89 jmd    added casts for DG
  18.      3/28/90 jmd    ansi-fied
  19. */
  20.  
  21. #include "oakhead.h"
  22. #include "disppriv.h"
  23. /* -------------------------------------------------------------------------- */
  24.  
  25. ocolmap_type ocolmap_Open(opixval firstpix, unsigned nentries)
  26. {
  27.     ocolmap_type cmap, dcmap;
  28.     opixval lastentry, dlastentry;
  29.     unsigned size;
  30.  
  31.     if ((dcmap = disp_GetDefColMapp()) == NULL) {
  32.         return(NULL);
  33.     }
  34.     lastentry  = firstpix + nentries - 1;
  35.     dlastentry = dcmap->firstpix + dcmap->nentries - 1;
  36.  
  37.     /* Don't allow any cmap to be allocated outside the bounds of the */
  38.     /*  display color map */
  39.     if (firstpix > dlastentry || lastentry < dcmap->firstpix) {
  40.         return(NULL);
  41.     }
  42.     if (firstpix < dcmap->firstpix) {
  43.         nentries -= dcmap->firstpix - firstpix;
  44.         firstpix = dcmap->firstpix;
  45.     }
  46.     if (firstpix + nentries - 1 > dlastentry) {
  47.         nentries = dlastentry - firstpix + 1;
  48.     }
  49.     size = ocolmap_GetSize(nentries);
  50.     if ((cmap = (ocolmap_type) ocalloc(OA_OCOLMAP, size, sizeof(byte))) == NULL) {
  51.         return(NULL);
  52.     }
  53.     cmap->firstpix = firstpix;
  54.     cmap->nentries = nentries;
  55.  
  56.     return(cmap);
  57. }
  58. /* -------------------------------------------------------------------------- */
  59.  
  60. void ocolmap_Close(ocolmap_type cmap)
  61. {
  62.     if (cmap != NULL) {
  63.         ofree(OA_OCOLMAP, (VOID *) cmap);
  64.     }
  65. }
  66. /* -------------------------------------------------------------------------- */
  67.  
  68. void ocolmap_set(ocolmap_type dcmap, ocolmap_type scmap)
  69. {
  70.     opixval firstpix, lastpix;
  71.     unsigned nentries;
  72.  
  73.     if (scmap == NULL || dcmap == NULL) {
  74.         return;
  75.     }
  76.  
  77.     firstpix = (scmap->firstpix > dcmap->firstpix) ? scmap->firstpix :
  78.                                                      dcmap->firstpix;
  79.  
  80.     lastpix = scmap->firstpix + scmap->nentries  - 1;
  81.     lastpix = (lastpix < dcmap->firstpix + dcmap->nentries  - 1) ? lastpix :
  82.                                     dcmap->firstpix + dcmap->nentries  - 1;
  83.     if (lastpix < firstpix) {
  84.         return;
  85.     }
  86.     nentries = (unsigned)(lastpix + 1 - firstpix);
  87.  
  88.     if (nentries != 0) {
  89.         memmove((VOID *) ocolmap_entry(dcmap, firstpix),
  90.                 (VOID *) ocolmap_entry(scmap, firstpix),
  91.                 nentries * sizeof(orgb_struct));
  92.     }
  93. }
  94. /* -------------------------------------------------------------------------- */
  95.  
  96. orgb_struct *ocolmap_entry(ocolmap_type colmap, opixval opix)
  97. {
  98.     unsigned irgb;
  99.  
  100.     if (opix < colmap->firstpix) {
  101.         return(NULL);
  102.     }
  103.     irgb = (unsigned)(opix - colmap->firstpix);
  104.  
  105.     if (irgb >= colmap->nentries) {
  106.         return(NULL);
  107.     }
  108.     return(&colmap->rgbs[irgb]);
  109. }
  110. /* -------------------------------------------------------------------------- */
  111.  
  112. void ocolmap_setpixrgb(ocolmap_type cmap, opixval ipix, olevel red, olevel green, olevel blue)
  113. {
  114.     orgb_struct *colrgb;
  115.  
  116.     if ((colrgb = ocolmap_entry(cmap, ipix)) != NULL) {
  117.         colrgb->rgb[ORED] = red;
  118.         colrgb->rgb[OGREEN] = green;
  119.         colrgb->rgb[OBLUE] = blue;
  120.     }
  121. }
  122. /* -------------------------------------------------------------------------- */
  123.  
  124. boolean ocolmap_samecolor(ocolmap_type scmap, opixval scolor, ocolmap_type dcmap, opixval dcolor)
  125. /*
  126.     Return TRUE if the given colors in the given cmaps have the same rgb values.
  127. */
  128. {
  129.     orgb_struct *srgb, *drgb;
  130.  
  131.     srgb = ocolmap_entry(scmap, scolor);
  132.     drgb = ocolmap_entry(dcmap, dcolor);
  133.  
  134.     return( srgb->rgb[ORED] == drgb->rgb[ORED] &&
  135.             srgb->rgb[OGREEN] == drgb->rgb[OGREEN] &&
  136.             srgb->rgb[OBLUE] == drgb->rgb[OBLUE]);
  137. }
  138. /* -------------------------------------------------------------------------- */
  139.  
  140. boolean ocolmap_getcolor(ocolmap_type scmap, opixval scolor, ocolmap_type dcmap, opixval dcolor)
  141. /*
  142.     Copy the given color in scmap to the given color in dcmap.
  143.     Return FALSE if the given color is out of range in the given cmap.
  144. */
  145. {
  146.     orgb_struct *srgb, *drgb;
  147.  
  148.     srgb = ocolmap_entry(scmap, scolor);
  149.     drgb = ocolmap_entry(dcmap, dcolor);
  150.  
  151.     if (srgb == NULL || drgb == NULL) {
  152.         return(FALSE);
  153.     }
  154.     memcpy((VOID *) drgb, (VOID *) srgb, sizeof(orgb_struct));
  155.  
  156.     return(TRUE);
  157. }
  158. /* -------------------------------------------------------------------------- */
  159.  
  160.