home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / OCOLMAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  4.3 KB  |  173 lines

  1. /*
  2.     ocolmap.c    10/19/88
  3.  
  4.     % color map related utility functions.
  5.     by Ted.
  6.  
  7.     OWL 1.1
  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.  
  18. #include "oakhead.h"
  19. #include "disppriv.h"
  20. /* -------------------------------------------------------------------------- */
  21.  
  22. ocolmap_type ocolmap_Open(firstpix, nentries)
  23.     opixval firstpix;
  24.     unsigned nentries;
  25. {
  26.     ocolmap_type cmap, dcmap;
  27.     opixval lastentry, dlastentry;
  28.     unsigned size;
  29.  
  30.     if ((dcmap = disp_GetDefColMapp()) == NULL) {
  31.         return(NULL);
  32.     }
  33.     lastentry  = firstpix + nentries - 1;
  34.     dlastentry = dcmap->firstpix + dcmap->nentries - 1;
  35.  
  36.     /* Don't allow any cmap to be allocated outside the bounds of the */
  37.     /*  display color map */
  38.     if (firstpix > dlastentry || lastentry < dcmap->firstpix) {
  39.         return(NULL);
  40.     }
  41.     if (firstpix < dcmap->firstpix) {
  42.         nentries -= dcmap->firstpix - firstpix;
  43.         firstpix = dcmap->firstpix;
  44.     }
  45.     if (firstpix + nentries - 1 > dlastentry) {
  46.         nentries = dlastentry - firstpix + 1;
  47.     }
  48.     size = ocolmap_GetSize(nentries);
  49.     if ((cmap = (ocolmap_type) ocalloc(OA_OCOLMAP, size, sizeof(byte))) == NULL) {
  50.         return(NULL);
  51.     }
  52.     cmap->firstpix = firstpix;
  53.     cmap->nentries = nentries;
  54.  
  55.     return(cmap);
  56. }
  57. /* -------------------------------------------------------------------------- */
  58.  
  59. void ocolmap_Close(cmap)
  60.     ocolmap_type cmap;
  61. {
  62.     if (cmap != NULL) {
  63.         ofree(OA_OCOLMAP, (VOID *) cmap);
  64.     }
  65. }
  66. /* -------------------------------------------------------------------------- */
  67.  
  68. void ocolmap_set(dcmap, scmap)
  69.     ocolmap_type dcmap;
  70.     ocolmap_type scmap;
  71. {
  72.     opixval firstpix, lastpix;
  73.     unsigned nentries;
  74.  
  75.     if (scmap == NULL || dcmap == NULL) return;
  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) return;
  84.     nentries = (unsigned)(lastpix + 1 - firstpix);
  85.  
  86.     if (nentries != 0) {
  87.         memmove(ocolmap_entry(dcmap, firstpix),
  88.                 ocolmap_entry(scmap, firstpix),
  89.                 nentries * sizeof(orgb_struct));
  90.     }
  91. }
  92. /* -------------------------------------------------------------------------- */
  93.  
  94. orgb_struct *ocolmap_entry(colmap, opix)
  95.     ocolmap_type colmap;
  96.     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(cmap, ipix, red, green, blue)
  113.     ocolmap_type cmap;
  114.     opixval ipix;
  115.     olevel red;
  116.     olevel green;
  117.     olevel blue;
  118. {
  119.     orgb_struct *colrgb;
  120.  
  121.     if ((colrgb = ocolmap_entry(cmap, ipix)) != NULL) {
  122.         colrgb->rgb[ORED] = red;
  123.         colrgb->rgb[OGREEN] = green;
  124.         colrgb->rgb[OBLUE] = blue;
  125.     }
  126. }
  127. /* -------------------------------------------------------------------------- */
  128.  
  129. boolean ocolmap_samecolor(scmap, scolor, dcmap, dcolor)
  130.     ocolmap_type scmap;
  131.     opixval scolor;
  132.     ocolmap_type dcmap;
  133.     opixval dcolor;
  134. /*
  135.     Return TRUE if the given colors in the given cmaps have the same rgb values.
  136. */
  137. {
  138.     orgb_struct *srgb, *drgb;
  139.  
  140.     srgb = ocolmap_entry(scmap, scolor);
  141.     drgb = ocolmap_entry(dcmap, dcolor);
  142.  
  143.     return( srgb->rgb[ORED] == drgb->rgb[ORED] &&
  144.             srgb->rgb[OGREEN] == drgb->rgb[OGREEN] &&
  145.             srgb->rgb[OBLUE] == drgb->rgb[OBLUE]);
  146. }
  147. /* -------------------------------------------------------------------------- */
  148.  
  149. boolean ocolmap_getcolor(scmap, scolor, dcmap, dcolor)
  150.     ocolmap_type scmap;
  151.     opixval scolor;
  152.     ocolmap_type dcmap;
  153.     opixval dcolor;
  154. /*
  155.     Copy the given color in scmap to the given color in dcmap.
  156.     Return FALSE if the given color is out of range in the given cmap.
  157. */
  158. {
  159.     orgb_struct *srgb, *drgb;
  160.  
  161.     srgb = ocolmap_entry(scmap, scolor);
  162.     drgb = ocolmap_entry(dcmap, dcolor);
  163.  
  164.     if (srgb == NULL || drgb == NULL) {
  165.         return(FALSE);
  166.     }
  167.     memcpy(drgb, srgb, sizeof(orgb_struct));
  168.  
  169.     return(TRUE);
  170. }
  171. /* -------------------------------------------------------------------------- */
  172.  
  173.