home *** CD-ROM | disk | FTP | other *** search
- /*
- ocolmap.c 10/19/88
-
- % color map related utility functions.
- by Ted.
-
- OWL 1.2
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 5/09/89 ted Changed Alloc/Free terms to Open/Close.
- 6/27/89 ted Removed references to min and max.
- 8/09/89 ted Fixed nentries bug in ocolmap_Open, made it use ocalloc.
-
- 11/29/89 jmd added casts for DG
- 3/28/90 jmd ansi-fied
- */
-
- #include "oakhead.h"
- #include "disppriv.h"
- /* -------------------------------------------------------------------------- */
-
- ocolmap_type ocolmap_Open(opixval firstpix, unsigned nentries)
- {
- ocolmap_type cmap, dcmap;
- opixval lastentry, dlastentry;
- unsigned size;
-
- if ((dcmap = disp_GetDefColMapp()) == NULL) {
- return(NULL);
- }
- lastentry = firstpix + nentries - 1;
- dlastentry = dcmap->firstpix + dcmap->nentries - 1;
-
- /* Don't allow any cmap to be allocated outside the bounds of the */
- /* display color map */
- if (firstpix > dlastentry || lastentry < dcmap->firstpix) {
- return(NULL);
- }
- if (firstpix < dcmap->firstpix) {
- nentries -= dcmap->firstpix - firstpix;
- firstpix = dcmap->firstpix;
- }
- if (firstpix + nentries - 1 > dlastentry) {
- nentries = dlastentry - firstpix + 1;
- }
- size = ocolmap_GetSize(nentries);
- if ((cmap = (ocolmap_type) ocalloc(OA_OCOLMAP, size, sizeof(byte))) == NULL) {
- return(NULL);
- }
- cmap->firstpix = firstpix;
- cmap->nentries = nentries;
-
- return(cmap);
- }
- /* -------------------------------------------------------------------------- */
-
- void ocolmap_Close(ocolmap_type cmap)
- {
- if (cmap != NULL) {
- ofree(OA_OCOLMAP, (VOID *) cmap);
- }
- }
- /* -------------------------------------------------------------------------- */
-
- void ocolmap_set(ocolmap_type dcmap, ocolmap_type scmap)
- {
- opixval firstpix, lastpix;
- unsigned nentries;
-
- if (scmap == NULL || dcmap == NULL) {
- return;
- }
-
- firstpix = (scmap->firstpix > dcmap->firstpix) ? scmap->firstpix :
- dcmap->firstpix;
-
- lastpix = scmap->firstpix + scmap->nentries - 1;
- lastpix = (lastpix < dcmap->firstpix + dcmap->nentries - 1) ? lastpix :
- dcmap->firstpix + dcmap->nentries - 1;
- if (lastpix < firstpix) {
- return;
- }
- nentries = (unsigned)(lastpix + 1 - firstpix);
-
- if (nentries != 0) {
- memmove((VOID *) ocolmap_entry(dcmap, firstpix),
- (VOID *) ocolmap_entry(scmap, firstpix),
- nentries * sizeof(orgb_struct));
- }
- }
- /* -------------------------------------------------------------------------- */
-
- orgb_struct *ocolmap_entry(ocolmap_type colmap, opixval opix)
- {
- unsigned irgb;
-
- if (opix < colmap->firstpix) {
- return(NULL);
- }
- irgb = (unsigned)(opix - colmap->firstpix);
-
- if (irgb >= colmap->nentries) {
- return(NULL);
- }
- return(&colmap->rgbs[irgb]);
- }
- /* -------------------------------------------------------------------------- */
-
- void ocolmap_setpixrgb(ocolmap_type cmap, opixval ipix, olevel red, olevel green, olevel blue)
- {
- orgb_struct *colrgb;
-
- if ((colrgb = ocolmap_entry(cmap, ipix)) != NULL) {
- colrgb->rgb[ORED] = red;
- colrgb->rgb[OGREEN] = green;
- colrgb->rgb[OBLUE] = blue;
- }
- }
- /* -------------------------------------------------------------------------- */
-
- boolean ocolmap_samecolor(ocolmap_type scmap, opixval scolor, ocolmap_type dcmap, opixval dcolor)
- /*
- Return TRUE if the given colors in the given cmaps have the same rgb values.
- */
- {
- orgb_struct *srgb, *drgb;
-
- srgb = ocolmap_entry(scmap, scolor);
- drgb = ocolmap_entry(dcmap, dcolor);
-
- return( srgb->rgb[ORED] == drgb->rgb[ORED] &&
- srgb->rgb[OGREEN] == drgb->rgb[OGREEN] &&
- srgb->rgb[OBLUE] == drgb->rgb[OBLUE]);
- }
- /* -------------------------------------------------------------------------- */
-
- boolean ocolmap_getcolor(ocolmap_type scmap, opixval scolor, ocolmap_type dcmap, opixval dcolor)
- /*
- Copy the given color in scmap to the given color in dcmap.
- Return FALSE if the given color is out of range in the given cmap.
- */
- {
- orgb_struct *srgb, *drgb;
-
- srgb = ocolmap_entry(scmap, scolor);
- drgb = ocolmap_entry(dcmap, dcolor);
-
- if (srgb == NULL || drgb == NULL) {
- return(FALSE);
- }
- memcpy((VOID *) drgb, (VOID *) srgb, sizeof(orgb_struct));
-
- return(TRUE);
- }
- /* -------------------------------------------------------------------------- */
-
-