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.
- */
- /*
- * envreduce -
- * Reduce an environment map by a factor of 2.
- *
- * Paul Haeberli - 1987
- */
- #include "texture.h"
-
- TEXTURE *tm;
- int doreflect;
- int xsize, ysize;
-
- float kernel[4][4] = {
- 1, 2, 2, 1,
- 2, 4, 4, 2,
- 2, 4, 4, 2,
- 1, 2, 2, 1,
- };
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- float tr, tg, tb;
- int x, y, dx, dy;
- IMAGE *image;
- int dir, i;
- vect pos, v, vv, c;
- float delta, w;
- short *rbuf, *gbuf, *bbuf;
-
- if(argc<3) {
- fprintf(stderr,"usage: envreduce in.env out.env\n");
- exit(1);
- }
- tm = tmopen(argv[1]);
- if(!tm) {
- printf("can't open environment map.\n");
- exit(1);
- }
- xsize = tm->xsize;
- ysize = tm->ysize;
- if(xsize&1 || ysize&1) {
- printf("envreduce: bad poop\n");
- exit(1);
- }
- delta = 1.0/(xsize/2);
- xsize = xsize/2;
- ysize = ysize/2;
- rbuf = (short *)malloc(xsize*sizeof(short));
- gbuf = (short *)malloc(xsize*sizeof(short));
- bbuf = (short *)malloc(xsize*sizeof(short));
- image = iopen(argv[2],"w",RLE(1),3,xsize,ysize,3);
- for(y=0; y<ysize; y++) {
- for(x=0; x<xsize; x++) {
- pos.x = (x+0.5)/xsize;
- pos.y = (y+0.5)/ysize;
- pos.z = 0.0;
- dir = envtodir(&pos,&v);
- switch(dir) {
- case 1:
- v.y -= 1.5*delta;
- v.z -= 1.5*delta;
- break;
- case 2:
- v.x -= 1.5*delta;
- v.z -= 1.5*delta;
- break;
- case 3:
- v.x -= 1.5*delta;
- v.y -= 1.5*delta;
- break;
- }
- tr = tg = tb = 0.0;
- for(dy=0; dy<4; dy++) {
- for(dx=0; dx<4; dx++) {
- switch(dir) {
- case 1:
- vv.x = v.x;
- vv.y = v.y+delta*dx;
- vv.z = v.z+delta*dy;
- break;
- case 2:
- vv.y = v.y;
- vv.x = v.x+delta*dx;
- vv.z = v.z+delta*dy;
- break;
- case 3:
- vv.z = v.z;
- vv.x = v.x+delta*dx;
- vv.y = v.y+delta*dy;
- break;
- }
- envsample(tm,&vv,&c);
- w = kernel[dy][dx];
- tr += w*c.x;
- tg += w*c.y;
- tb += w*c.z;
- }
- }
- rbuf[x] = 255*tr/36;
- gbuf[x] = 255*tg/36;
- bbuf[x] = 255*tb/36;
- }
- putrow(image,rbuf,y,0);
- putrow(image,gbuf,y,1);
- putrow(image,bbuf,y,2);
- tpercentdone(100.0*y/(ysize-1));
- }
- iclose(image);
- }
-
- envtodir(e,v)
- vect *e, *v;
- {
- int seg;
- float tx, ty;
-
- tx = 3.0*e->x;
- ty = 2.0*e->y;
- seg = 0;
- if(ty>1.0) {
- seg = 3;
- ty -= 1.0;
- } else
- seg = 0;
- if(tx>2.0) {
- seg += 2;
- tx -= 2.0;
- } else if(tx>1.0) {
- seg += 1;
- tx -= 1.0;
- }
- tx -= 0.5;
- ty -= 0.5;
- switch(seg) {
- case 0:
- v->x = -0.5;
- v->y = tx;
- v->z = ty;
- return 1;
- break;
- case 1:
- v->y = -0.5;
- v->z = tx;
- v->x = ty;
- return 2;
- break;
- case 2:
- v->z = -0.5;
- v->x = tx;
- v->y = ty;
- return 3;
- break;
- case 3:
- v->x = 0.5;
- v->y = tx;
- v->z = ty;
- return 1;
- break;
- case 4:
- v->y = 0.5;
- v->z = tx;
- v->x = ty;
- return 2;
- break;
- case 5:
- v->z = 0.5;
- v->x = tx;
- v->y = ty;
- return 3;
- break;
- }
- }
-