申请内存过大,堆栈溢出,哪位高手帮忙改一下。。
这是对Lena.bmp图片加上高斯噪声和椒盐噪声的程序,但是运行时不对,应该是溢出问题。哪位高手帮忙修改一下~
一些定义如下:
typedef struct
{
char id[2];
unsigned long fileSize;
unsigned long reserved0;
unsigned long bitmapDataOffset;
unsigned long bitmapHeaderSize;
unsigned long width;
unsigned long height;
unsigned short planes;
unsigned short bitsPerPixel;
unsigned long compression;
unsigned long bitmapDataSize;
unsigned long hRes;
unsigned long vRes;
unsigned long colors;
unsigned long importantColors;
} BMPHeader_t;
typedef struct
{
unsigned char Red;
unsigned char Green;
unsigned char Blue;
} Pixel_t;
class BmpImageUtil
{
public:
BMPHeader_t header;
BmpImageUtil(void);
~BmpImageUtil(void);
void ParseBmpHeader(ifstream & fin);
void ZoomIn(ofstream & fout, int multiple, bool useZero);
void ZoomOut(ofstream & fout, int multiple, bool useZero);
void GaussianNoise(ofstream & fout);
void GaussianFilter(ofstream & fout);
void GaussianFilter3(ofstream & fout);
void SaltPepperNoise(ofstream & fout);
void MedianFilter(ofstream & fout);
void verifyLoad(ofstream & fout);
private:
Pixel_t matrix[512][512];
Pixel_t buff[3072];
void loadMatrix(ifstream & fin);
void writeFileHeader(BMPHeader_t header, ofstream & fout);
void zero_matrix_out(ofstream & fout, int multiple);
void zero_matrix_operation(int multiple, ofstream & fout);
void writeFileContent(ofstream & fout, unsigned long pixel_number, Pixel_t * martix_new);
double Gaussian(double expection, double sigma);
unsigned char MidPixel(unsigned char * buff);
};
void BmpImageUtil::GaussianFilter3(ofstream & fout)
{
int templates[25] = { 1,1,1,
1,1,1,
1,1,1};
Pixel_t smooth[512][512]; ////////////////////////////////////这里溢出
memcpy ( smooth, this->matrix, sizeof(this->matrix));
for (int j=2;j<this->header.width-1;j++)
{
for (int i=2;i<this->header.height-1;i++)
{
int sum = 0;
int index = 0;
for ( int m=j-1; m<j+2; m++)
{
for (int n=i-1; n<i+2; n++)
{
sum += this->matrix[m][n].Blue * templates[index++] ;
}
}
sum /= 9;
if (sum > 255)
sum = 255;
smooth [j][i].Blue =
smooth [j][i].Green =
smooth [j][i].Red = sum;
}
}
this->writeFileHeader(this->header, fout);
for(int i = 0;i < this->header.width;i++)
for(int j = 0;j < this->header.height;j++)
fout.write((char *)&(smooth[i][j]), sizeof(Pixel_t));
fout.flush();
}
有问题的函数部分如下:
void BmpImageUtil::GaussianFilter(ofstream & fout)
{
int templates[25] = { 1, 4, 7, 4, 1,
4, 16, 26, 16, 4,
7, 26, 41, 26, 7,
4, 16, 26, 16, 4,
1, 4, 7, 4, 1 };
Pixel_t smooth[512][512];////////////////////////////////////这里溢出
memcpy ( smooth, this->matrix, sizeof(this->matrix));
for (int j=2;j<this->header.width-2;j++)
{
for (int i=2;i<this->header.height-2;i++)
{
int sum = 0;
int index = 0;
for ( int m=j-2; m<j+3; m++)
{
for (int n=i-2; n<i+3; n++)
{
sum += this->matrix[m][n].Blue * templates[index++] ;
}
}
sum /= 273;
if (sum > 255)
sum = 255;
smooth [j][i].Blue =
smooth [j][i].Green =
smooth [j][i].Red = sum;
}
}
this->writeFileHeader(this->header, fout);
for(int i = 0;i < this->header.width;i++)
for(int j = 0;j < this->header.height;j++)
fout.write((char *)&(smooth[i][j]), sizeof(Pixel_t));
fout.flush();
}
void BmpImageUtil::MedianFilter(std::ofstream & fout)
{
unsigned char square[9];
Pixel_t p[512][512];////////////////////////////////////这里溢出
this->writeFileHeader(this->header,fout);
memcpy(p, this->matrix, sizeof(this->matrix));
for(int i = 1;i < this->header.width - 1;i++)
{
for(int j = 1;j < this->header.height - 1;j++)
{
int index = 0;
for(int m = i - 1; m < i + 2;m++)
{
for(int n = j - 1;n < j + 2;n++)
{
square[index] = this->matrix[m][n].Blue;
index++;
}
}
p[i][j].Red = p[i][j].Blue = p[i][j].Green = this->MidPixel(square);
}
}
for( i = 0;i < this->header.width;i++)
{
for(int j = 0;j < this->header.height;j++)
{
fout.write((char *)&(p[i][j]), sizeof(Pixel_t));
}
}
fout.flush();
}