home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / bomb.tar.gz / bomb.tar / bomb / image_rule.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  6KB  |  262 lines

  1. /*
  2.     bomb - automatic interactive visual stimulation
  3.     Copyright (C) 1994  Scott Draves <spot@cs.cmu.edu>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "defs.h"
  21. #include "image.h"
  22. #include "image_db.h"
  23. #include "bomb.h"
  24.  
  25. void
  26. step_rule_rug_image(int frame, rule_t *p, image8_t *fb) {
  27.    int x,y,bx,by;    
  28.    board_t *s_heat_board, *d_heat_board;
  29.    board_t *s_image_board, *d_image_board;
  30.    u_char *lp;
  31.  
  32.    s_heat_board=&board[dbuf];
  33.    d_heat_board=&board[1-dbuf];
  34.    s_image_board=&board2[0];   
  35.    dbuf = 1-dbuf;
  36.  
  37.    for (y=1;y<=YSIZE;y++) {
  38.       lp = fb->p + (fb->stride * (y - 1));
  39.       for (x=1;x<=XSIZE;x++) {
  40.      int heat, t;
  41.      heat = ((((*s_heat_board)[x  ][y-1])+
  42.           ((*s_heat_board)[x-1][y  ]<<1)+
  43.           ((*s_heat_board)[x  ][y  ]<<1)+
  44.           ((*s_heat_board)[x+1][y  ]<<1)+
  45.           ((*s_heat_board)[x  ][y+1])) >> 3);
  46.      if (heat < 0) {
  47.         if (p->floor)
  48.            heat = 0;     /* hm, just &mask? */
  49.         else if (p->randomized_underflow)
  50.            heat = R;
  51.      }
  52.      t = (*s_image_board)[x][y];
  53.      heat += t;
  54.      heat += p->speed;
  55.      heat &= p->mask;
  56.      (*d_heat_board)[x][y]= heat;
  57.      // if (p->remap)
  58.         heat = remap[heat];
  59.      *(lp++) = heat;
  60. #if 0
  61.      /* some kind of non-linearity could be cool, adjusted to mask,
  62.         what is the distribution of heat values in a rug? */
  63.      *(lp++) = 20 * log(1+t);
  64. #endif
  65.       }
  66.    }
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. int current_image = 0;
  78. int image_dir_len;
  79. Image global_images[N_RAM_IMAGES];
  80. Image global_images_small[N_RAM_IMAGES];
  81. Image *global_image;
  82. image8_t small_pattern;
  83. u_char sd_pix[SMALL_SIZE * SMALL_SIZE];
  84.  
  85. int
  86. gray(Pixel p)
  87. {
  88.    return (p.r + p.g + p.g);
  89. }
  90.  
  91.  
  92. void
  93. drive_with_image(int image_num)
  94. {
  95.    int i, j;
  96.    board_t *s_image_board, *d_image_board;
  97.    int w, h, s;
  98.    Pixel *p;
  99.    int scale = (rule.rule == rule_rd) ? 3 : 255;
  100.  
  101.    grad_state = 0;
  102.  
  103.    image_num = iclamp(image_num, N_RAM_IMAGES);
  104.  
  105.    s_image_board=&board2[0];
  106.    d_image_board=&board2[1];
  107.  
  108.    global_image = &global_images[image_num];
  109.  
  110.    w = global_image->width;
  111.    h = global_image->height;
  112.    p = global_image->pixels;
  113.    s = global_image->stride;
  114.    for (i = 0; i < YSIZE+2; i++) {
  115.       for (j =  0; j < XSIZE+2; j++) {
  116.      int g = gray(p[(i * h / (YSIZE+2)) * s + (j * w / (XSIZE+2))]);
  117.      ((*s_image_board)[j][i]) = g / scale;
  118.      ((*d_image_board)[j][i]) = g / scale;
  119.       }
  120.    }
  121. }
  122.  
  123. void
  124. invert_board()
  125. {
  126.    board_t *s_image_board, *d_image_board;
  127.    int i, j;
  128.    s_image_board=&board2[0];
  129.    d_image_board=&board2[1];
  130.  
  131.    for (i = 0; i < YSIZE+2; i++) {
  132.       for (j =  0; j < XSIZE+2; j++) {
  133.      (*s_image_board)[j][i] *=  -1;
  134.      (*d_image_board)[j][i] *=  -1;
  135.       }
  136.    }
  137. }
  138.  
  139. void rotate_images()
  140. {
  141.    int i;
  142.    Image bt;
  143.    Image st;
  144.    int last = N_RAM_IMAGES - 1;
  145.  
  146.    bt = global_images[last];
  147.    st = global_images_small[last];
  148.  
  149.    for (i = last; i > 0; i--) {
  150.       global_images[i] = global_images[i - 1];
  151.       global_images_small[i] = global_images_small[i - 1];
  152.    }
  153.    global_images[0] = bt;
  154.    global_images_small[0] = st;
  155. }
  156.  
  157. #if use_suck_dir
  158. #  define MAXPATHLEN 1030
  159. char image_names[max_images][max_image_name];
  160. #else
  161. #  include <dirent.h>
  162. #  include <sys/types.h>
  163. #  include <sys/param.h>
  164. struct dirent **image_dir;
  165. int isimage(const struct dirent *d) {
  166.    return d->d_name[0] != '.';
  167. }
  168. #endif
  169.  
  170. void file_to_image(int file_num, int image_num)
  171. {
  172. #if 1
  173.    char buf[MAXPATHLEN];
  174. #else
  175.    char buf[PATH_MAX];
  176. #endif
  177.    extern int tiles_ready;
  178.    tiles_ready = 0;
  179.    image_num = iclamp(image_num, N_RAM_IMAGES);
  180.    if (1 > image_dir_len) {
  181.       int x, y;
  182.       Image *im = &global_images[image_num];
  183.       image_allocate(im, 100, 100);
  184.       for (x = 0; x < 100; x++)
  185.      for (y = 0; y < 100; y++) {
  186.         int v = (((x>>3) + (y>>3)) & 1) ? 0 : -1;
  187.         Pixel p;
  188.         p.r = p.g = p.b = v;
  189.         im->pixels[im->stride * y + x] = p;
  190.      }
  191.    } else {
  192.       file_num = iclamp(file_num, image_dir_len);
  193. #if mac_bomb
  194.       sprintf(buf, ":suck:%s", image_names[file_num]);
  195. #elif win_bomb
  196.       sprintf(buf, "%ssuck\\%s", DATA_DIR, image_names[file_num]);
  197. #else
  198.       sprintf(buf, "%ssuck/%s", DATA_DIR, image_dir[file_num]->d_name);
  199. #endif
  200.       if (TCL_ERROR == image_read(global_images + image_num, buf)) {
  201.          file_to_image(file_num + 1, image_num);
  202.          return;
  203.       }
  204.    }
  205.    image_allocate(global_images_small + image_num,
  206.           global_images[image_num].width / SMALL_FACTOR,
  207.           global_images[image_num].height / SMALL_FACTOR);
  208.    image_filter_down(global_images + image_num,
  209.              global_images_small + image_num);
  210. }
  211.  
  212. void random_image_set()
  213. {
  214.    int i;
  215.    for (i = 0; i < N_RAM_IMAGES; i++)
  216.       file_to_image(R, i);
  217. }
  218.  
  219.  
  220. void init_images()
  221. {
  222.    int i;
  223.    char buf[1000];
  224.  
  225.    small_pattern.width = SMALL_SIZE;
  226.    small_pattern.height = SMALL_SIZE;
  227.    small_pattern.stride = SMALL_SIZE;
  228.    small_pattern.p = sd_pix;
  229. #if use_suck_dir
  230.    image_dir_len = 0;
  231.       sprintf(buf, "%ssuck-dir", DATA_DIR);
  232.    if (1) {
  233.      FILE *dir = fopen(buf, "r");
  234.     if (NULL == dir) {
  235.         printf("couldnt open suck-dir\n");
  236.         message("open suck-dir failed");
  237.      } else while (1) {
  238.         image_names[image_dir_len][0] = 0;
  239.         fscanf(dir, "%s", image_names[image_dir_len]);
  240.  
  241.         if ('.' != image_names[image_dir_len][0])
  242.           image_dir_len++;
  243.         else
  244.           break;
  245.      }
  246.    }
  247. #else
  248.    sprintf(buf, "%ssuck", DATA_DIR);
  249.    image_dir_len = scandir(buf, &image_dir, isimage, alphasort);
  250. #endif
  251.    if (1 > image_dir_len)
  252.       fprintf(stderr, "no images found, using checker-board\n");
  253.  
  254.    for (i = 0; i < N_RAM_IMAGES; i++) {
  255.       image_init(&global_images[i]);
  256.       image_init(&global_images_small[i]);
  257.    }
  258.  
  259.    if (!getenv("quick"))
  260.       random_image_set(); // XXXX
  261. }
  262.