home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / envtools / fromfish.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  134 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.  *    fromfish -
  19.  *        Convert from a fisheye projection to an environment map.
  20.  *
  21.  *                Paul Haeberli - 1987
  22.  */
  23. #include "texture.h"
  24. #define HORIZON 
  25. #define TWOSIDES 
  26.  
  27. #define XSIZE    (3*size)
  28. #define YSIZE    (2*size)
  29. #define RAD    (0.98)    /* fish eye image is inset 2% */
  30. #define HACK    (0.001)    
  31.  
  32. TEXTURE *tm;
  33. int doreflect;
  34. int size, samples;
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char **argv;
  39. {
  40.     float vr, vg, vb;
  41.     float tr, tg, tb;
  42.     int x, y;
  43.     IMAGE *image;
  44.     int i;
  45.     vect c, pos, dir, fish;
  46.     float temp;
  47.     short *rbuf, *gbuf, *bbuf;
  48.  
  49.     if(argc<5) {
  50.     fprintf(stderr,"usage: fromfish size samples file.rgb file.env\n"); 
  51.     exit(1);
  52.     }
  53.     size = atoi(argv[1]);
  54.     samples = atoi(argv[2]);
  55.     tm = tmopen(argv[3]);
  56.     if(!tm) {
  57.     fprintf(stderr,"can't open input image.\n");
  58.     exit(1);
  59.     }
  60.     rbuf = (short *)malloc(XSIZE*sizeof(short));
  61.     gbuf = (short *)malloc(XSIZE*sizeof(short));
  62.     bbuf = (short *)malloc(XSIZE*sizeof(short));
  63.     image = iopen(argv[4],"w",RLE(1),3,XSIZE,YSIZE,3);
  64.     for(y=0; y<YSIZE; y++) {
  65.     for(x=0; x<XSIZE; x++) {
  66.         tr = tg = tb = 0.0;
  67.         for(i=0; i<samples; i++) {
  68.         if(samples>1) {
  69.             pos.x = (x+frand())/(XSIZE+HACK);
  70.             pos.y = (y+frand())/(YSIZE+HACK);
  71.             pos.z = 0;
  72.         } else {
  73.             pos.x = (x+0.5)/XSIZE;
  74.             pos.y = (y+0.5)/YSIZE;
  75.             pos.z = 0.0;
  76.         }
  77.         envtovect(&pos,&dir);
  78. #ifdef HORIZON 
  79.         temp = dir.x;
  80.         dir.x = dir.y;
  81.         dir.y = dir.z;
  82.         dir.z = temp;
  83. #endif
  84. #ifdef TWOSIDES
  85.         if(dir.z<0.0)
  86.             dir.z = -dir.z;
  87. #endif
  88.         if(dirtofish(&dir,&fish))
  89.             tmsample(tm,&fish,&c);
  90.         else
  91.             vzero(&c);
  92.         tr += c.x;
  93.         tg += c.y;
  94.         tb += c.z;
  95.         }
  96.         rbuf[x] = 255*tr/samples;
  97.         gbuf[x] = 255*tg/samples;
  98.         bbuf[x] = 255*tb/samples;
  99.     }
  100.     putrow(image,rbuf,y,0);
  101.     putrow(image,gbuf,y,1);
  102.     putrow(image,bbuf,y,2);
  103.     tpercentdone(100.0*y/(YSIZE-1));
  104.     }
  105.     iclose(image);
  106. }
  107.  
  108. dirtofish(dir,fish)
  109. vect *dir, *fish;
  110. {
  111.     float magout, magup, angout, m;
  112.  
  113.     magout = sqrt(dir->x*dir->x+dir->y*dir->y);
  114.     if(dir->z<0.0)
  115.     return 0;
  116.     magup = dir->z;
  117.     angout = atan2(magout,magup)/3.1415926535;
  118.     if(angout<0.0)
  119.     angout = 0.0;
  120.     if(angout>1.0)
  121.     angout = 1.0;
  122.     if(magout>0.0001) {
  123.     m = RAD*angout/magout;
  124.     fish->x = 0.5 + m*dir->x;
  125.     fish->y = 0.5 + m*dir->y;
  126.     fish->z = 0.0;
  127.     } else {
  128.     fish->x = 0.5;
  129.     fish->y = 0.5; 
  130.     fish->z = 0.0;
  131.     }
  132.     return 1;
  133. }
  134.