home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * canmap -
- * Support arbitrary color transforms on canvases
- *
- * Paul Haeberli - 1991
- *
- * exports
- *
- canmap *makecanmap(transfunc,levels);
- void freecanmap(map);
- void applycanmap(c,map);
- *
- */
- #include "stdio.h"
- #include "canvas.h"
- #include "math.h"
- #include "izoom.h"
- #include "vect.h"
- #include "lum.h"
-
-
- static unsigned char *randbase;
-
- canmap *makecanmap(transfunc,levels)
- int (*transfunc)();
- int levels;
- {
- canmap *m;
- pixel *pptr;
- int r, g, b, i;
- float div;
- vect in, out;
-
- m = (canmap *)mymalloc(sizeof(canmap));
- m->levels = levels;
- m->table = (pixel *)mymalloc(levels*levels*levels*sizeof(pixel));
- div = levels-1.0;
- pptr = m->table;
- for(b=0; b<levels; b++) {
- in.z = b/div;
- for(g=0; g<levels; g++) {
- in.y = g/div;
- for(r=0; r<levels; r++) {
- in.x = r/div;
- transfunc(&in,&out);
-
- i = 255*out.x;
- if(i&0xffffff00) {
- if(i<0) i = 0; else i = 255;
- }
- pptr->r = i;
- i = 255*out.y;
- if(i&0xffffff00) {
- if(i<0) i = 0; else i = 255;
- }
- pptr->g = i;
- i = 255*out.z;
- if(i&0xffffff00) {
- if(i<0) i = 0; else i = 255;
- }
- pptr->b = i;
-
- pptr++;
- }
- }
- }
- return m;
- }
-
- void freecanmap(map)
- canmap *map;
- {
- myfree(map->table);
- myfree(map);
- }
-
- void applycanmap(c,map)
- canvas *c;
- canmap *map;
- {
- int ir, ig, ib;
- int n, i, l1, l2, sc;
- pixel *pptr, *pos, *base;
- unsigned char *rptr;
- int rmag;
-
- base = map->table;
- l1 = map->levels;
- l2 = l1*l1;
- sc = l1-1;
-
- if(!randbase)
- randbase = (unsigned char *)mymalloc(256);
- rptr = randbase;
-
- rmag = 255/sc;
- for(i=0; i<256; i++)
- *rptr++ = random()%rmag;
-
- n = c->xsize*c->ysize;
- pptr = (pixel *)c->data;
- while(n) {
- i = n;
- if(i>(128/3))
- i = 128/3;
- n = n-i;
- rptr = randbase+(random()%128);
- while(i--) {
- ir = ((pptr->r+(*rptr))*sc)/256;
- ig = ((pptr->g+(*rptr))*sc)/256;
- ib = ((pptr->b+(*rptr++))*sc)/256;
- pos = base+(ir+ig*l1+ib*l2);
- *pptr++ = *pos;
- }
- }
- }
-