|
请问下面的调整大小function哪里错了,输出出来的图片只有一片黑色: void image::scaleImage(int factor)
{
if((factor < 1) || (factor > 10))
{
cout << "Factor should be greater than 1 or less than 10" << endl;
exit(0);
}
pixel *temp = NULL;
temp = new(nothrow) pixel[xdim * factor * ydim * factor];
if(temp == NULL)
{
cout << "Allocated failure." << endl;
exit(1);
}
if(pixmap != NULL)
{
//for(int i = 0; i < xdim * factor * factor * ydim; i++)
//temp[i] = pixmap[i/(xdim * factor * factor) * xdim + i%(xdim * factor)/factor];
int n = 0;
for(int i = 0; i < ydim; i++)
{
for(int j = 0; j < factor; j++)
{
for(int k = 0; k < xdim * ydim; k++)
{
for(int m = 0; m < factor; m++)
{
temp[n++] = pixmap[k];
}
}
}
}
}
xdim *= factor;
ydim *= factor;
delete[] pixmap;
pixmap = temp;
}
这是其他的function #include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstring>
#include "image.h"
using namespace std;
image::image()
{
pixmap = NULL;
}
image::~image()
{
if(pixmap != NULL)
delete[] pixmap;
pixmap = NULL;
}
void image::readImage(const char filename[])
{
ifstream fin(filename, ios::binary);
if(!fin)
{
cerr << "File cannot be opened." << endl;
exit(1);
}
char temp[4];
fin.read(temp,2);
fin.ignore(1,""\n"");
if(strcmp(temp, "P6") != 0)
{
fin.close();
cerr << "There is an error in the file." << endl;
exit(1);
}
char find = fin.peek();
if(find == ""#"")
{
fin.ignore(100,""\n"");
}
fin >> xdim;
fin >> ydim;
fin.ignore(1,""\n"");
fin.read(temp,3);
fin.ignore(""\n"");
pixmap = new(nothrow) pixel[xdim * ydim];
if(pixmap == NULL)
{
fin.close();
cout << "Allocated failure." << endl;
exit(1);
}
else
{
image d;
d.~image();
}
fin.read(reinterpret_cast<char*>(pixmap), xdim * ydim * sizeof(pixmap));
fin.close();
}
void image::writeImage(const char filename[])
{
ofstream fout(filename, ios::binary);
if(!fout)
{
cerr << "File failed to open" << endl;
exit(1);
}
fout.write("P6\n", 3);
fout << xdim;
fout.write("\t",1);
fout << ydim;
fout.write("\n255\n", 5);
fout.write(reinterpret_cast<char*>(pixmap), xdim * ydim * sizeof(pixmap));
fout.close();
}
void image::scaleImage(int factor)
{
if((factor < 1) || (factor > 10))
{
cout << "Factor should be greater than 1 or less than 10" << endl;
exit(0);
}
pixel *temp = NULL;
temp = new(nothrow) pixel[xdim * factor * ydim * factor];
if(temp == NULL)
{
cout << "Allocated failure." << endl;
exit(1);
}
if(pixmap != NULL)
{
//for(int i = 0; i < xdim * factor * factor * ydim; i++)
//temp[i] = pixmap[i/(xdim * factor * factor) * xdim + i%(xdim * factor)/factor];
int n = 0;
for(int i = 0; i < ydim; i++)
{
for(int j = 0; j < factor; j++)
{
for(int k = 0; k < xdim * ydim; k++)
{
for(int m = 0; m < factor; m++)
{
temp[n++] = pixmap[k];
}
}
}
}
}
cout << endl;
xdim *= factor;
ydim *= factor;
delete[] pixmap;
pixmap = temp;
}
main.cpp: #include <iostream>
#include <cstring>
#include "image.h"
using namespace std;
int main()
{
char filename[64];
image img;
cout << "Picture file to read: ";
cin.getline(filename, 64, ""\n"");
img.readImage(filename);
img.scaleImage(2);
cout << "Picture file to write: ";
cin.getline(filename, 64, ""\n"");
img.writeImage(filename);
return 0;
}
|
|
| 30分 |
建议用一个超小的图片比如5×4像素的图片作为测试图片,单步调试你的代码。
|