home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / gimpressionist / plasma.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-10  |  2.0 KB  |  80 lines

  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <math.h>
  4. #include "gimpressionist.h"
  5. #include "ppmtool.h"
  6.  
  7. int pfix(int n)
  8. {
  9.   if(n<1) return 1;
  10.   if(n>255) return 255;
  11.   return n;
  12. }
  13.  
  14. #define PIXEL(y,x,z) p->col[(y)*rowstride+(x)*3+z]
  15.  
  16. void mkplasma_sub(struct ppm *p, int x1, int x2, int y1, int y2, float turb)
  17. {
  18.   int rowstride = p->width * 3;
  19.   int r=0;
  20.   int xr, yr, nx, ny;
  21.   xr = abs(x1-x2);
  22.   yr = abs(y1-y2);
  23.  
  24.   if((xr==0) && (yr==0)) return;
  25.  
  26.   nx = (x1+x2)/2;
  27.   ny = (y1+y2)/2;
  28.   if(!PIXEL(y1,nx,r))
  29.     PIXEL(y1,nx,r) = pfix((PIXEL(y1,x1,r)+PIXEL(y1,x2,r))/2.0+
  30.                            turb*(RAND_FUNC()%xr-xr/2.0));
  31.   if(!PIXEL(y2,nx,r))
  32.     PIXEL(y2,nx,r) = pfix((PIXEL(y2,x1,r)+PIXEL(y2,x2,r))/2.0+
  33.                            turb*(RAND_FUNC()%xr-xr/2.0));
  34.   if(!PIXEL(ny,x1,r))
  35.     PIXEL(ny,x1,r) = pfix((PIXEL(y1,x1,r)+PIXEL(y2,x1,r))/2.0+
  36.                          turb*(RAND_FUNC()%yr-yr/2.0));
  37.   if(!PIXEL(ny,x2,r))
  38.     PIXEL(ny,x2,r) = pfix((PIXEL(y1,x2,r)+PIXEL(y2,x2,r))/2.0+
  39.                          turb*(RAND_FUNC()%yr-yr/2.0));
  40.   if(!PIXEL(ny,nx,r))
  41.     PIXEL(ny,nx,r) = 
  42.       pfix((PIXEL(y1,x1,r)+PIXEL(y1,x2,r)+PIXEL(y2,x1,r)+
  43.             PIXEL(y2,x2,r))/4.0+turb*(RAND_FUNC()%(xr+yr)/2.0-(xr+yr)/4.0));
  44.  
  45.   if(xr>1) {
  46.     mkplasma_sub(p,x1,nx,y1,ny, turb);
  47.     mkplasma_sub(p,nx,x2,y1,ny, turb);
  48.   }
  49.   if(yr>1) {
  50.     mkplasma_sub(p,x1,nx,ny,y2, turb);
  51.     mkplasma_sub(p,nx,x2,ny,y2, turb);
  52.   }
  53. }
  54.  
  55. void mkplasma_red(struct ppm *p, float turb)
  56. {
  57.   int x=0, y=0;
  58.   int rowstride = p->width * 3;
  59.  
  60.   for(x = 0; x < p->width; x++)
  61.     for(y = 0; y < p->height; y++)
  62.       PIXEL(y,x,0) = 0;
  63.   x--; y--;
  64.   PIXEL(0,0,0) = 1+RAND_FUNC()%255;
  65.   PIXEL(y,0,0) = 1+RAND_FUNC()%255;
  66.   PIXEL(0,x,0) = 1+RAND_FUNC()%255;
  67.   PIXEL(y,x,0) = 1+RAND_FUNC()%255;
  68.   mkplasma_sub(p, 0, x, 0, y, turb);
  69. }
  70.  
  71. void mkgrayplasma(struct ppm *p, float turb)
  72. {
  73.   int y,l;
  74.  
  75.   mkplasma_red(p, turb);
  76.   l = p->width * 3 * p->height;
  77.   for(y = 0; y < l; y += 3)
  78.     p->col[y+1] = p->col[y+2] = p->col[y];
  79. }
  80.