1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <string>
#include <iostream>
#include <fstream>
#include "FloodMap.h"
using namespace std;
FloodMap::FloodMap(string infile){
ifstream file;
file.open(infile.c_str());
file >> rows;
file >> cols;
file.get();
map = new pixel*[rows];
for (int i = 0; i < rows; i++) {
map[i] = new pixel[cols];
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
file >> map[i][j].r;
map[i][j].b = map[i][j].r;
map[i][j].g = map[i][j].r;
}
}
file.close();
}
FloodMap::~FloodMap(){
for (int i = 0; i < rows; i++) {
delete[] map[i];
}
delete[] map;
}
void FloodMap::scaleData(){
int old_max = map[0][0].r;
int old_min = map[0][0].r;
double old_range = 0;
for(int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (map[i][j].r < old_min) {
old_min = map[i][j].r;
}
if (map[i][j].r > old_max) {
old_max = map[i][j].r;
}
}
}
old_range = (old_max - old_min);
for(int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int val = ((map[i][j].r - old_min) / old_range) * 255;
map[i][j].r = val;
map[i][j].g = val;
map[i][j].b = val;
}
}
}
void FloodMap::saveToDisk(std::string outfile){
ofstream fOut;
fOut.open(outfile.c_str());
fOut << "P3" << endl;
fOut << cols << " " << rows << endl;
fOut << "255 " << endl;
fOut << " " ;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
fOut << map[i][j].r << " ";
fOut << map[i][j].g << " ";
fOut << map[i][j].b << " ";
}
}
fOut.close();
cout << "saved" << endl;
}
void FloodMap::flood(int r, int c, int height){
int tempVar = 0;
if (r < 0 || r >= rows){
return;
}
if (c < 0 || c >= cols){
return;
}
if(map[r][c].g == 225){
return;
}
if (height < map[r][c].r){
return;
}
tempVar = map[r][c].r;
map[r][c].r = 0;
map[r][c].g = 225;
map[r][c].b = 0;
flood(r+1, c, tempVar);
flood(r-1, c, tempVar);
flood(r, c+1, tempVar);
flood(r, c-1, tempVar);
flood(r+1, c+1, tempVar);
flood(r-1, c+1, tempVar);
flood(r+1, c-1, tempVar);
flood(r-1, c-1, tempVar);
}
|