home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / bomb.tar.gz / bomb.tar / bomb / image2cmap.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  3KB  |  121 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.  
  23. /* simple hill climber, circular topology */
  24.  
  25. int dist(Pixel p0, Pixel p1) {
  26.    int d;
  27.    int r;
  28.    d = p0.r - p1.r;
  29.    r = d * d;
  30.    d = p0.g - p1.g;
  31.    r += d * d;
  32.    d = p0.b - p1.b;
  33.    r += d * d;
  34.    return r;
  35. }
  36.  
  37. double total_length(Pixel *cmap, int n) {
  38.    int i;
  39.    double t = dist(cmap[0], cmap[n-1]);
  40.    for (i = 1; i < n; i++)
  41.       t += dist(cmap[i-1], cmap[i]);
  42.    return t / 1000.0;
  43. }
  44.  
  45. void improve_cmap_order(Pixel *cmap, int n) {
  46.    int i, i0, i1, niters;
  47.    int total_d = 0;
  48.    Pixel t;
  49.  
  50.    niters = argd("niters", 100) * 200000;
  51.    for (i = 0; i < niters; i++) {
  52.       int as_is, swapped;
  53.       i0 = R%n;
  54.       i1 = R%n;
  55.       as_is = (dist(cmap[(i0-1)%n], cmap[i0]) +
  56.            dist(cmap[i0], cmap[(i0+1)%n]) +
  57.            dist(cmap[(i1-1)%n], cmap[i1]) +
  58.            dist(cmap[i1], cmap[(i1+1)%n]));
  59.       swapped = (dist(cmap[(i0-1)%n], cmap[i1]) +
  60.          dist(cmap[i1], cmap[(i0+1)%n]) +
  61.          dist(cmap[(i1-1)%n], cmap[i0]) +
  62.          dist(cmap[i0], cmap[(i1+1)%n]));
  63.       if (swapped < as_is) {
  64.      total_d += as_is - swapped;
  65.      if (total_d > 20000) {
  66.         total_d = 0;
  67.         fprintf(stderr, ".");
  68.      }
  69.      t = cmap[i0];
  70.      cmap[i0] = cmap[i1];
  71.      cmap[i1] = t;
  72.       }
  73.    }
  74.    fprintf(stderr, "\n");
  75.  
  76. }
  77.  
  78. int main(int argc, char **argv) {
  79.    char *fname = args("in", "in.ppm");
  80.    int nentries = argd("nentries", 256);
  81.    int box_size = argd("box_size", 4);
  82.    Pixel *cmap = malloc(sizeof(Pixel) * nentries);
  83.    int t, i;
  84.    Image in;
  85.    Image box;
  86.  
  87.    image_init(&in);
  88.    image_init(&box);
  89.  
  90.    if (NULL == cmap) {
  91.       fprintf(stderr, "could not allocate %d entry cmap\n", nentries);
  92.       exit(1);
  93.    }
  94.  
  95.    if (TCL_ERROR == image_read(&in, fname))
  96.       exit(1);
  97.  
  98.    t = time(NULL);
  99.    srandom(argd("seed", t));
  100.    
  101.    for (i = 0; i < nentries; i++) {
  102.       Pixel p;      
  103.       image_random_tile(&box, &in, box_size);
  104.       image_mean_pixel(&box, &p);
  105.       cmap[i] = p;
  106.    }
  107.  
  108.    fprintf(stderr, "length = %g\n", total_length(cmap, nentries));
  109.    improve_cmap_order(cmap, nentries);
  110.    fprintf(stderr, "length = %g\n", total_length(cmap, nentries));
  111.  
  112.    printf("(comment %s)\n(cmap\n", fname);
  113.    for (i = 0; i < nentries; i++) {
  114.       printf(" (%d %d %d)", cmap[i].r, cmap[i].g, cmap[i].b);
  115.       if (3 == i%4)
  116.      printf("\n");
  117.    }
  118.    printf(")\n");
  119. }
  120.    
  121.