home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / envtools / frommerc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.6 KB  |  109 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    frommerc -
  19.  *        Convert from a mercator projection to an environment map.
  20.  *
  21.  *                Paul Haeberli - 1990
  22.  */
  23. #include "texture.h"
  24.  
  25. #define XSIZE    (3*size)
  26. #define YSIZE    (2*size)
  27. #define HACK    (0.001)    
  28.  
  29. TEXTURE *tm;
  30. int size, samples;
  31.  
  32. main(argc,argv)
  33. int argc;
  34. char **argv;
  35. {
  36.     float vr, vg, vb;
  37.     float tr, tg, tb;
  38.     int x, y;
  39.     IMAGE *image;
  40.     int i;
  41.     vect c, pos, dir, merc;
  42.     float temp;
  43.     short *rbuf, *gbuf, *bbuf;
  44.  
  45.     if(argc<5) {
  46.     fprintf(stderr,"usage: frommerc size samples file.rgb file.env\n"); 
  47.     exit(1);
  48.     }
  49.     size = atoi(argv[1]);
  50.     samples = atoi(argv[2]);
  51.     tm = tmopen(argv[3]);
  52.     if(!tm) {
  53.     fprintf(stderr,"can't open input image.\n");
  54.     exit(1);
  55.     }
  56.     rbuf = (short *)malloc(XSIZE*sizeof(short));
  57.     gbuf = (short *)malloc(XSIZE*sizeof(short));
  58.     bbuf = (short *)malloc(XSIZE*sizeof(short));
  59.     image = iopen(argv[4],"w",RLE(1),3,XSIZE,YSIZE,3);
  60.     for(y=0; y<YSIZE; y++) {
  61.     for(x=0; x<XSIZE; x++) {
  62.         tr = tg = tb = 0.0;
  63.         for(i=0; i<samples; i++) {
  64.         if(samples>1) {
  65.             pos.x = (x+frand())/(XSIZE+HACK);
  66.             pos.y = (y+frand())/(YSIZE+HACK);
  67.             pos.z = 0;
  68.         } else {
  69.             pos.x = (x+0.5)/XSIZE;
  70.             pos.y = (y+0.5)/YSIZE;
  71.             pos.z = 0.0;
  72.         }
  73.         envtovect(&pos,&dir);
  74. #ifdef HORIZON 
  75.         temp = dir.x;
  76.         dir.x = dir.y;
  77.         dir.y = dir.z;
  78.         dir.z = temp;
  79. #endif
  80.         dirtomerc(&dir,&merc);
  81.         tmsample(tm,&merc,&c);
  82.         tr += c.x;
  83.         tg += c.y;
  84.         tb += c.z;
  85.         }
  86.         rbuf[x] = 255*tr/samples;
  87.         gbuf[x] = 255*tg/samples;
  88.         bbuf[x] = 255*tb/samples;
  89.     }
  90.     putrow(image,rbuf,y,0);
  91.     putrow(image,gbuf,y,1);
  92.     putrow(image,bbuf,y,2);
  93.     tpercentdone(100.0*y/(YSIZE-1));
  94.     }
  95.     iclose(image);
  96. }
  97.  
  98. dirtomerc(dir,merc)
  99. vect *dir, *merc;
  100. {
  101.     float magout, r;
  102.  
  103.     r = sqrt(dir->x*dir->x+dir->y*dir->y);
  104.     merc->x = (atan2(dir->x,dir->y)/(2*M_PI))+0.5;
  105.     merc->y = (atan2(r,dir->z)/M_PI);
  106.     merc->z = 0.0;
  107.     return 1;
  108. }
  109.