Hello:
When I run the attached program, a window with error pops up:
Debug assertion failed:
Program: c:/Windows/system32/MSVCP100.dll/
Line:734
I have no clue as to whats causing this error, anyone can give me a clue?
Thanks
Code:
//BMP.hpp
#ifndef BMP_H
#define BMP_H
#include <iostream>
#include <fstream>
using namespace std;
struct Pixel {
char blue;
char green;
char red;
};
struct color
{
int botrange;
int toprange;
Pixel pixel1;
color()
{
botrange = 0;
toprange = 0;
pixel1.blue = 0;
pixel1.green = 0;
pixel1.red = 0;
}
};
class BMP
{
public:
BMP( string filename, int height, int width );
void writeHeader();
void writeRow( Pixel* );
void close();
private:
void writeShort( short value );
void writeWord( int value );
static const int headersize = 14;
static const int infoheadersize = 40;
static const int offset = 54;
static const int planes = 1;
static const int bits = 24;
static const int verticalresolution = 2835;
static const int horizontalresolution = 2835;
int filesize;
int columns;
int rows;
int rowpadding;
int datasize;
ofstream outfile;
};
#endif
Code:
/BMP.cpp
#include "BMP.hpp"
using namespace std;
// Initialize a BMP file writer by remembering the size of the image and
// opening the file and writing a .bmp format header to the file.
BMP::BMP( string filename, int height, int width )
{
rows = height;
columns = width;
int rowsize = width*bits/8;
rowpadding = ( rowsize % 4 ) ? 4 - ( rowsize % 4 ) : 0;
datasize = headersize + infoheadersize + height*(rowsize + rowpadding);
filesize = datasize;
outfile.open( filename.c_str(), ios::binary | ios::out );
writeHeader();
}
// Write a single 32-bit word to the BMP file
void BMP::writeWord( int value )
{
outfile.write( (char*) &value, 4 );
}
// Write a single 16-bit halfword to the BMP file
void BMP::writeShort( short value )
{
outfile.write( (char*) &value, 2 );
}
// Write one row of Pixels to the BMP file
void BMP::writeRow( Pixel* values )
{
outfile.write( (char*) values, columns*bits/8 );
// Write enough zero bytes to fill out a full 32-bit word for
// the row because the format requires this.
int zeros = 0;
if ( rowpadding ) outfile.write( (char*) &zeros, rowpadding );
}
// Close the BMP file
void BMP::close()
{
outfile.close();
}
// Write a 54-byte header to the BMP file that has the appropriate
// settings for an image of the correct size and 24-bit pixels.
//
// The format is explained in Wikipedia
// http://en.wikipedia.org/wiki/BMP_file_format
void BMP::writeHeader()
{
// Bytes 0-1: Magic word. It contains the letters 'B' and 'M'.
outfile.write( "BM", 2 );
// Bytes 2-5: File size.
writeWord( filesize );
// Bytes 6-7 and 8-9: Application dependent. Leave these zeroed.
writeShort( 0 );
writeShort( 0 );
// Bytes 10-13: Start of the bitmap in the file.
writeWord( offset );
// Bytes 14-17: Size of the Inforheader. Normally set to 40.
// The Inforheader is assumed to start at offset 14.
writeWord( infoheadersize );
// Bytes 18-21: Width.
writeWord( columns );
// Bytes 22-25: Height.
writeWord( rows );
// Bytes 26-27: Word. Planes. Should be 1.
writeShort( planes );
// Bytes 29-30: Word. Bits per pixel. 24 for full RGB.
writeShort( bits );
// Bytes 31-34: Compression. Leave it zero.
writeWord( 0 );
// Bytes 35-38: Compressed size. No compression, leave it zero.
writeWord( 0 );
// Bytes 39-42: Horizontal resolution. Pixels per meter.
writeWord( horizontalresolution );
// Bytes 43-46: Vertical resolution. Pixels per meter.
writeWord( verticalresolution );
// Bytes 47-50: Number of colors in the palette. Leave as zero.
writeWord( 0 );
// Bytes 51-54: Number of important colors. Leave as zero.
writeWord( 0 );
}
Code:
/image.cpp
#define WINDOWS 1
#include "BMP.hpp"
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
string filename;
double lat;
double lon;
static const int height = 16;
static const int width = 16;
void readFile( string filename, short buffer, int rows, int columns )
{
ifstream infile;
infile.open( filename.c_str(), ios::binary | ios::in | ios::ate );
infile.seekg( 0 );
infile.read( (char*) buffer, 2*rows*columns );
infile.close();
}
void readFile2( string filename, short* buffer, int rows, int columns )
{
ifstream infile;
infile.open( filename.c_str(), ios::binary | ios::in | ios::ate );
infile.seekg( 0 );
infile.read( (char*) buffer, 2*rows*columns );
infile.close();
}
vector<color> filereader()
{
short array1[25][5];
readFile2( "LUT.txt", array1[0], 25, 5 );
vector<color>color(25);
for( int i=0; i < 25; i ++ )
{
color[i].botrange = array1[i][0];
color[i].toprange = array1[i][1];
Pixel pixel;
pixel.blue = char(array1[i][2]);
pixel.green = char(array1[i][3]);
pixel.red = char(array1[i][4]);
color[i].pixel1 = pixel;
}
return color;
}
Pixel find(double lattitude, double longitude)
{
vector<short>col(4320);
vector<vector<short>>row(2160);
readFile( "tbase.bin", col[0], 2160, 4320 );
short height = row[int((lattitude + 90)*12)][int((longitude+180)*12)];
for( int i = 0; i < 25; i++ )
{
vector<color> colours = filereader();
if( height >= colours[i].botrange && height <= colours[i].toprange )
{
return (colours[i].pixel1);
}
}
}
int main ( int argc, const char * argv[] )
{
cout<<"Please enter filename(without .bmp): "<<endl;
cin>>filename;
cout<<"Please enter lattitude: "<<endl;
cin>>lat;
cout<<"Please enter longitude: "<<endl;
cin>>lon;
cout<<"Please enter height of file: "<<endl;
cout<<"Please enter width of file: "<<endl;
filename = filename + ".bmp";
// Create a .bmp file for the image by specifying the file name
// and the image size.
BMP imageFile( filename, height, width );
// Reserve storage for the image as an array of Pixels (the BMP
// tools define the way that Pixels are stored).
Pixel image[height][width];;
// Put a pattern into the image
for ( int i = 0; i<width; i++ )
{
for ( int j = 0; j<height; j++ )
{
//check the formula again tomorrow, not sure if its right
image[i][j].red = find((i - width/2.0 + lat), (j - height/2.0 + lon )).red;
image[i][j].green = find((i - width/2.0 + lat), (j - height/2.0 + lon )).green;
image[i][j].blue = find((i - width/2.0 + lat), (j - height/2.0 + lon )).blue;
}
}
// Write the image out to the BMP file, bottom row first
for ( int i=height-1; i>=0; i-- )
{
imageFile.writeRow( image[i] );
}
// Close the file when we are done with it (this happens automatically
// when the program quits).
imageFile.close();
// Display the resulting image by opening the file using the system()
// function.
#if WINDOWS
system( "start image.bmp" );
#else
system( "open image.bmp" );
#endif
}