Commits

Ben Bass committed bc5904d

factor out colourmap; fix bug on repeated run

  • Participants
  • Parent commits 003572e

Comments (0)

Files changed (7)

 CC=g++ --std=c++11 -Werror -O3 -I$(SRC_DIR)
 
 BROTMAP_FILES = brotmap.cc worker.cc evaluate.cc
-MAKE_PPM_FILES = make_ppm.cc
-INCLUDE_FILES = brotmap.h
+BROTMAP_HEADERS = brotmap.h
+MAKE_PPM_FILES = make_ppm.cc colourmap.cc
+MAKE_PPM_HEADERS = brotmap.h colourmap.h
 
 BROTMAP_SRC = $(addprefix $(SRC_DIR)/,$(BROTMAP_FILES))
 MAKE_PPM_SRC = $(addprefix $(SRC_DIR)/,$(MAKE_PPM_FILES))
-INCLUDE_SRC = $(addprefix $(SRC_DIR)/,$(INCLUDE_FILES))
+BROTMAP_INCLUDE = $(addprefix $(SRC_DIR)/,$(BROTMAP_HEADERS))
+MAKE_PPM_INCLUDE = $(addprefix $(SRC_DIR)/,$(MAKE_PPM_HEADERS))
 
 all: $(BUILD_DIR)/make_ppm $(BUILD_DIR)/brotmap
 
 	@echo "Running brotmap to create a 1024x1024 test image..."
 	@time $(BUILD_DIR)/brotmap $(OUTPUT_DIR)/mandel.dat 10
 
+test: $(OUTPUT_DIR)/mandel.dat
+	@echo "Running brotmap to update test image..."
+	@time $(BUILD_DIR)/brotmap $(OUTPUT_DIR)/mandel.dat 10
+
+show: $(OUTPUT_DIR)/out_image
+	@touch $(OUTPUT_DIR)/out_image
+	@which xdg-open > /dev/null && xdg-open $(OUTPUT_DIR)/out_image || open $(OUTPUT_DIR)/out_image
 
 $(BUILD_DIR):
 	@mkdir -p $(BUILD_DIR)
 
-$(BUILD_DIR)/make_ppm: $(MAKE_PPM_SRC) $(INCLUDE_SRC)
+$(BUILD_DIR)/make_ppm: $(MAKE_PPM_SRC) $(MAKE_PPM_INCLUDE)
 	$(CC) $(MAKE_PPM_SRC) -o $(BUILD_DIR)/make_ppm
 
-$(BUILD_DIR)/brotmap: $(BROTMAP_SRC) $(INCLUDE_SRC)
+$(BUILD_DIR)/brotmap: $(BROTMAP_SRC) $(BROTMAP_INCLUDE)
 	$(CC) $(BROTMAP_SRC) -lc -lpthread -o $(BUILD_DIR)/brotmap
 
 clean:
 	rm -f $(BUILD_DIR)/*
 
+imageclean:
+	rm -f $(OUTPUT_DIR)/{*.ppm,out_image}
+
 superclean: clean
 	rm -f $(OUTPUT_DIR)/*
     printf("mapped file %d at %p %llu bytes\n", fd, fptr, mapsize);
     printf("Current MAX_ITER: %d\n", fheader->max_iter);
 
-    worker_run(fptr, step);
+    worker_run(fptr, step, fheader->max_iter);
 
     fheader->max_iter += MAX_ITER;
     fheader->binary_digits = BINARY_DIGITS;
-// MandelMap - Ben Bass 2010-2012
+// MandelMap - Ben Bass 2010-2014
 
 #ifndef MANDEL_MAP_H
 #define MANDEL_MAP_H
     };
 };
 
+// check that y and itercount are the same size
+static_assert(sizeof(FLOAT) == sizeof(long), "Invalid sizeof spec");
 
 const int HEADER_LEN=4096;  // probable page size for happiness
 struct brotfile_header
 long mpoint(FLOAT r,
             FLOAT i,
             pinfo* p,
-            int old_max_iter=0);
+            int old_max_iter);
 void* worker_start(void* arg);
-void worker_run(pinfo* fptr, const FLOAT step);
+void worker_run(pinfo* fptr, const FLOAT step, int old_max_iter);
 
 #endif
+#include "colourmap.h"
+
+Colour colourIter(long iterCount, int max_iter, bool non_mset) {
+    if (non_mset)
+    {
+        unsigned char zz = iterCount % 255;
+        return Colour({.r=zz, .g=zz, .b=zz});
+    }
+    else
+    {
+        return Colour({.r=0, .g=0, .b=0});
+    }
+}
+#ifndef COLOURMAP_H
+#define COLOURMAP_H
+
+struct Colour {
+    unsigned char r, g, b;
+};
+
+Colour colourIter(long iterCount, int max_iter, bool non_mset);
+
+#endif
 #include "brotmap.h"
+#include "colourmap.h"
 #include <cstdio>
 #include <cmath>
 #include <string>
     for (int pix=0; pix < (image_size * image_size); pix++)
     {
         indata = bmf.nextPoint();
-        if (isnan(indata.x))
-        {
-            unsigned char zz = (max_iter - indata.itercount) % 256;
-            putc(zz, outf);
-            putc(zz, outf);
-            putc(zz, outf);
-        }
-        else
-        {
-            putc(0, outf);
-            putc(0, outf);
-            putc(0, outf);
-        }
+        Colour col = colourIter(indata.itercount, max_iter, isnan(indata.x));
+        putc(col.r, outf);
+        putc(col.g, outf);
+        putc(col.b, outf);
     }
 }
 
 pinfo* FPTR_START;
 FLOAT STEP_SIZE;
+int OLD_MAX_ITER;
 
 pthread_mutex_t acc_lock;
 
         for (FLOAT x=MIN_X; x<MAX_X; x+=STEP_SIZE)
         {
             pinfo* p=local_fptr++;
-            local_inside += mpoint(x, y, p, MAX_ITER);
+            local_inside += mpoint(x, y, p, OLD_MAX_ITER);
         }
         // skip ahead the relevant number of lines
         local_fptr += (row_skip * (NUM_THREADS-1));
     return 0;
 }
 
-void worker_run(pinfo* fptr, const FLOAT step) {
+void worker_run(pinfo* fptr, const FLOAT step, int old_max_iter) {
     pthread_t t_handle[NUM_THREADS];
     int t_num[NUM_THREADS];
 
     // setup global read-only vars //BADBADBAD (but could be worse)
     FPTR_START = fptr;
     STEP_SIZE = step;
+    OLD_MAX_ITER = old_max_iter;
 
     std::cout << "Using " << NUM_THREADS << " threads" << std::endl;