rtsEnvi.h 15 KB
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
#ifndef RTS_ENVI_H
#define RTS_ENVI_H

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

/*	This is a class for reading and writing ENVI binary files. This class will be updated as needed.

What this class CAN currently do:
	*)  Write band images to a BSQ file.
	*)  Append band images to a BSQ file.

*/

//information from an ENVI header file
//A good resource can be found here: http://www.exelisvis.com/docs/enviheaderfiles.html
struct EnviHeader
{
	std::string name;

	std::string description;

	unsigned int samples;	//x-axis
	unsigned int lines;		//y-axis
	unsigned int bands;		//spectral axis
	unsigned int header_offset;		//header offset for binary file (in bytes)
	std::string file_type;			//should be "ENVI Standard"
	unsigned int data_type;			//data representation; common value is 4 (32-bit float)

	enum interleaveType {BIP, BIL, BSQ};	//bip = Z,X,Y; bil = X,Z,Y; bsq = X,Y,Z
	interleaveType interleave;

	std::string sensor_type;		//not really used

	enum endianType {endianLittle, endianBig};
	endianType byte_order;			//little = least significant bit first (most systems)

	double x_start, y_start;		//coordinates of the upper-left corner of the image
	std::string wavelength_units;	//stored wavelength units
	std::string z_plot_titles[2];

	double pixel_size[2];			//pixel size along X and Y

	std::vector<std::string> band_names;	//name for each band in the image
	std::vector<double> wavelength;		//wavelength for each band

	EnviHeader()
	{
        name = "";

		//specify default values for a new or empty ENVI file
		samples = 0;
		lines = 0;
		bands = 0;
		header_offset = 0;
		data_type = 4;
		interleave = BSQ;
		byte_order = endianLittle;
		x_start = y_start = 0;
		pixel_size[0] = pixel_size[1] = 1;

		//strings
		file_type = "ENVI Standard";
		sensor_type = "Unknown";
		wavelength_units = "Unknown";
		z_plot_titles[0] = z_plot_titles[1] = "Unknown";
	}

	std::string trim(std::string line)
	{
		//trims whitespace from the beginning and end of line
		int start_i, end_i;
		for(start_i=0; start_i < line.length(); start_i++)
			if(line[start_i] != 32)
			{
				break;
			}

		for(end_i = line.length()-1; end_i >= start_i; end_i--)
			if(line[end_i] != ' ')
			{
				break;
			}

		return line.substr(start_i, end_i - start_i+1);
	}


	std::string get_token(std::string line)
	{
		//returns a variable name; in this case we look for the '=' sign
		size_t i = line.find_first_of('=');

		std::string result;
		if(i != std::string::npos)
			result = trim(line.substr(0, i-1));

		return result;
	}

	std::string get_data_str(std::string line)
	{
		size_t i = line.find_first_of('=');

		std::string result;
		if(i != std::string::npos)
			result = trim(line.substr(i+1));
		else
		{
			std::cout<<"ENVI Header error - data not found for token: "<<get_token(line)<<std::endl;
			exit(1);
		}
		return result;
	}

	std::string get_brace_str(std::string token, std::string line, std::ifstream &file)
	{
		//this function assembles all of the characters between curly braces
		//this is how strings are defined within an ENVI file

		std::string result;

		//first, find the starting brace
		size_t i;
		do
		{
			i = line.find_first_of('{');
			if(i != std::string::npos)
				break;
		}while(file);

		//if i is still npos, we have reached the end of the file without a brace...something is wrong
		if(i == std::string::npos)
		{
			std::cout<<"ENVI Header error - string token declared without being defined: "<<token<<std::endl;
			exit(1);
		}
		line = line.substr(i+1);

		//copy character data into the result string until we find a closing brace
		while(file)
		{
			i = line.find_first_of('}');


			if(i != std::string::npos)
			{
				result += line.substr(0, i);
				break;
			}
			else
				result += line;

			getline(file, line);
		}

		if(i == std::string::npos)
		{
			std::cout<<"ENVI Header error - string token declared without a terminating '}': "<<token<<std::endl;
			exit(1);
		}

		return trim(result);
	}

	std::vector<std::string> get_string_seq(std::string token, std::string sequence)
	{
		//this function returns a sequence of comma-delimited strings
		std::vector<std::string> result;

		std::string entry;
		size_t i;
		do
		{
			i = sequence.find_first_of(',');
			entry = sequence.substr(0, i);
			sequence = sequence.substr(i+1);
			result.push_back(trim(entry));
		}while(i != std::string::npos);

		return result;
	}

	std::vector<double> get_double_seq(std::string token, std::string sequence)
	{
		//this function returns a sequence of comma-delimited strings
		std::vector<double> result;

		std::string entry;
		size_t i;
		do
		{
			i = sequence.find_first_of(',');
			entry = sequence.substr(0, i);
			sequence = sequence.substr(i+1);
			result.push_back(atof(entry.c_str()));
		}while(i != std::string::npos);

		return result;
	}

	void load(std::string filename)
	{
		name = filename;

		//open the header file
		std::ifstream file(name.c_str());

		if(!file)
		{
			std::cout<<"ENVI header file not found: "<<name<<std::endl;
			exit(1);
		}

		//the first line should just be "ENVI"
		std::string line;
		getline(file, line);
		if(line != "ENVI")
		{
			std::cout<<"The header doesn't appear to be an ENVI file. The first line should be 'ENVI'."<<std::endl;
			exit(1);
		}

		//for each line in the file, get the token
		std::string token;
		while(file)
		{

			//get a line
			getline(file, line);

			//get the token
			token = get_token(line);

			if(token == "description")
				description = get_brace_str(token, line, file);
			else if(token == "band names")
			{
				std::string string_sequence = get_brace_str(token, line, file);
				band_names = get_string_seq(token, string_sequence);
			}
			else if(token == "wavelength")
			{
				std::string string_sequence = get_brace_str(token, line, file);
				wavelength = get_double_seq(token, string_sequence);
			}
			else if(token == "pixel size")
			{
				std::string string_sequence = get_brace_str(token, line, file);
				std::vector<double> pxsize = get_double_seq(token, string_sequence);
				pixel_size[0] = pxsize[0];
				pixel_size[1] = pxsize[1];
			}
			else if(token == "z plot titles")
			{
				std::string string_sequence = get_brace_str(token, line, file);
				std::vector<std::string> titles = get_string_seq(token, string_sequence);
				z_plot_titles[0] = titles[0];
				z_plot_titles[1] = titles[1];
			}

			else if(token == "samples")
				samples = atoi(get_data_str(line).c_str());
			else if(token == "lines")
				lines = atoi(get_data_str(line).c_str());
			else if(token == "bands")
				bands = atoi(get_data_str(line).c_str());
			else if(token == "header offset")
				header_offset = atoi(get_data_str(line).c_str());
			else if(token == "file type")
				file_type = get_data_str(line);
			else if(token == "data type")
				data_type = atoi(get_data_str(line).c_str());
			else if(token == "interleave")
			{
				std::string interleave_str = get_data_str(line);
				if(interleave_str == "bip")
					interleave = BIP;
				else if(interleave_str == "bil")
					interleave = BIL;
				else if(interleave_str == "bsq")
					interleave = BSQ;
			}
			else if(token == "sensor type")
				sensor_type = get_data_str(line);
			else if(token == "byte order")
				byte_order = (endianType)atoi(get_data_str(line).c_str());
			else if(token == "x start")
				x_start = atof(get_data_str(line).c_str());
			else if(token == "y start")
				y_start = atof(get_data_str(line).c_str());
			else if(token == "wavelength units")
				wavelength_units = get_data_str(line);
		}

		//close the file
		file.close();
	}

	void save(std::string filename)
	{
		//open a file
		std::ofstream outfile(filename.c_str());

		//write the ENVI type identifier
		outfile<<"ENVI"<<std::endl;

		//output all of the data
		outfile<<"description = {"<<std::endl;
		outfile<<"  "<<description<<"}"<<std::endl;

		outfile<<"samples = "<<samples<<std::endl;
		outfile<<"lines = "<<lines<<std::endl;
		outfile<<"bands = "<<bands<<std::endl;
		outfile<<"header offset = "<<header_offset<<std::endl;
		outfile<<"file type = "<<file_type<<std::endl;
		outfile<<"data type = "<<data_type<<std::endl;
		outfile<<"interleave = ";
		if(interleave == BIP)
			outfile<<"bip";
		if(interleave == BIL)
			outfile<<"bil";
		if(interleave == BSQ)
			outfile<<"bsq";
			outfile<<std::endl;
		outfile<<"sensor type = "<<sensor_type<<std::endl;
		outfile<<"byte order = "<<byte_order<<std::endl;
		outfile<<"x start = "<<x_start<<std::endl;
		outfile<<"y start = "<<y_start<<std::endl;
		outfile<<"wavelength units = "<<wavelength_units<<std::endl;
		outfile<<"z plot titles = {";
			outfile<<z_plot_titles[0]<<", "<<z_plot_titles[1]<<"}"<<std::endl;
		outfile<<"pixel size = {"<<pixel_size[0]<<", "<<pixel_size[1]<<", units=Meters}"<<std::endl;
		if(band_names.size() > 0)
		{
			outfile<<"band names = {"<<std::endl;
			for(int i=0; i<band_names.size(); i++)
			{
				outfile<<band_names[i];
				if(i < band_names.size() - 1)
					outfile<<", ";
			}
			outfile<<"}"<<std::endl;
		}
		outfile<<"wavelength = {"<<std::endl;
			for(int i=0; i<wavelength.size()-1; i++)
				outfile<<wavelength[i]<<", ";
			outfile<<wavelength.back()<<"}"<<std::endl;

		outfile.close();
	}

	void save()
	{
        std::cout<<"ENVI Header Name: "<<name<<std::endl;
		save(name);
	}

	//returns the size of a single value (in bytes)
	unsigned int valsize()
	{
		switch(data_type)
		{
		case 1:			//1 = 8-bit byte
			return 1;
		case 2:			//16-bit signed integer
		case 12:		//16-bit unsigned integer
			return 2;
		case 3:			//32-bit signed long integer
		case 4:			//32-bit floating point
		case 13:		//32-bit unsigned long integer
			return 4;
		case 5:			//64-bit double-precision floating point
		case 6:			//32-bit complex value
		case 14:		//64-bit signed long integer
		case 15:		//64-bit unsigned long integer
			return 8;
		case 9:			//64-bit complex value
			return 16;
		default:
			return 0;
		}

	}
};		//end EnviHeader

class EnviFile
{
	EnviHeader header;
	FILE* data;
	std::string mode;

	//a locked file has alread had data written..certain things can't be changed
	//		accessor functions deal with these issues
	bool locked;

	void init()
	{
		locked = false;
		data = NULL;
	}

	void readonly()
	{
		if(mode == "r")
		{
			std::cout<<"ENVI Error: changes cannot be made to a read-only file."<<std::endl;
			exit(1);
		}
	}

	//this function determines if the current software is capable of the requested change
	bool caps(EnviHeader new_header)
	{
		readonly();		//if the file is read-only, throw an exception

		//we currently only support BSQ
		if(new_header.interleave == EnviHeader::BIP || new_header.interleave == EnviHeader::BIL)
		{
			std::cout<<"This software only supports BSQ.";
			return false;
		}

		//if the number of bands has changed
		if(header.bands != new_header.bands)
		{
			//the new header must be a BSQ file
			if(new_header.interleave != EnviHeader::BSQ)
			{
				std::cout<<"ENVI Error: can only add bands to a BSQ file."<<std::endl;
				return false;
			}
		}

		//disallow anything that can't be done when the file is locked
		if(locked)
		{
			if(header.lines != new_header.lines)
			{
				std::cout<<"ENVI Error: cannot change the number of lines in a locked BSQ file."<<std::endl;
				return false;
			}
			if(header.samples != new_header.samples)
			{
				std::cout<<"ENVI Error: cannot change the number of samples in a locked BSQ file."<<std::endl;
				return false;
			}
			if(header.data_type != new_header.data_type)
			{
				std::cout<<"ENVI Error: cannot change the data type of a locked file."<<std::endl;
				return false;
			}
			if(header.byte_order != new_header.byte_order)
			{
				std::cout<<"ENVI Error: cannot change the byte order of a locked file."<<std::endl;
				return false;
			}
		}

		return true;

	}

	EnviHeader load_header(std::string header_file)
	{
		EnviHeader new_header;
		new_header.load(header_file);
		return new_header;
	}

	void set_header(EnviHeader new_header)
	{
		if(caps(new_header))
			header = new_header;
		else
			exit(1);
	}

public:

	EnviFile()
	{
		init();
	}

	EnviFile(std::string filename, std::string file_mode = "r")
	{
		init();
		open(filename, filename + ".hdr", file_mode);
	}

	void open(std::string file_name, std::string header_name, std::string file_mode = "r")
	{
		mode = file_mode;

		header.name = header_name;

		//open the data file
		data = fopen(file_name.c_str(), mode.c_str());

		//lock the file if we are loading or appending
		if(mode == "r" || mode == "a" || mode == "r+" || mode == "a+")
		{
			locked = true;
			//load the ENVI header - failure will cause an exit
			set_header(load_header(header_name));
		}
		else
		{
			header.name = header_name;
		}


	}

	void setDescription(std::string desc)
	{
		readonly();		//if the file is read-only, throw an exception
		header.description = desc;
	}

	void setZPlotTitles(std::string spectrum, std::string value)
	{
		readonly();		//if the file is read-only, throw an exception
		header.z_plot_titles[0] = spectrum;
		header.z_plot_titles[1] = value;
	}

	void setPixelSize(double sx, double sy)
	{
		readonly();		//if the file is read-only, throw an exception
		header.pixel_size[0] = sx;
		header.pixel_size[1] = sy;
	}

	void setWavelengthUnits(std::string units)
	{
		readonly();		//if the file is read-only, throw an exception
		header.wavelength_units = units;
	}

	void setCoordinates(double x, double y)
	{
		readonly();		//if the file is read-only, throw an exception
		header.x_start = x;
		header.y_start = y;
	}

	//FUNCTIONS THAT MAY BE DISALLOWED IN A LOCKED FILE
	void setSamples(unsigned int samples)
	{
		EnviHeader newHeader = header;
		newHeader.samples = samples;
		set_header(newHeader);
	}

	void setLines(unsigned int lines)
	{
		EnviHeader newHeader = header;
		newHeader.lines = lines;
		set_header(newHeader);
	}

	void setBands(unsigned int bands)
	{
		EnviHeader newHeader = header;
		newHeader.bands = bands;
		set_header(newHeader);
	}

	//DATA MANIPULATION
	void addBand(void* band, unsigned int samples, unsigned int lines, double wavelength, std::string band_name ="")
	{
		//add a band to the file

		EnviHeader newHeader = header;
		newHeader.bands++;
		newHeader.samples = samples;
		newHeader.lines = lines;
		newHeader.wavelength.push_back(wavelength);
		if(band_name != "")
			newHeader.band_names.push_back(band_name);
		set_header(newHeader);

		//copy the data to the file
		fwrite(band, header.valsize(), samples * lines, data);

		//since data was written, the file must be locked
		locked = true;
	}

	//close file
	void close()
	{
        std::cout<<"ENVI File Mode: "<<mode<<std::endl;

		//close the data stream
		fclose(data);

		//close the header
		if(mode != "r")
			header.save();
	}

};		//end EnviFile


#endif