My_Image scale_image(My_Image& image)
{
My_Image scaled_image;
int new_w(0);
int new_h(0);
int x,y;
float x_ratio, y_ratio;
do
{
cout<<" enter the new width amount of pixles:";
cin>>new_w;
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX,'\n');
cerr << endl <<endl<< "**DATA NOT VALID**"<<endl<< "enter a value between 1 and 2000"<<endl<<endl;
new_w = 0;
continue;
}
cout<<" enter the new height amount of pixles:";
cin>>new_h;
if (cin.fail())
{
cin.clear();
cin.ignore(INT_MAX,'\n');
cerr << endl <<endl<< "**DATA NOT VALID**"<<endl<< "enter a value between 1 and 2000"<<endl<<endl;
new_h = 0;
continue;
}
}while(new_w<1||new_h<1);
x_ratio = (image.width)/new_w;
y_ratio = (image.height)/new_h;
scaled_image.height = new_h;
scaled_image.width = new_w;
scaled_image.pixel = new Pixel[new_w*new_h];//sets size of array
for(int i = 0; i<new_h;i++)
{
for (int j=0;j<new_w;j++)
{
x = (j*x_ratio);
y = (i*y_ratio);
scaled_image.pixel[(i*(new_w))+j] = image.pixel[(y*(image.width))+x];
}
}
return scaled_image;
}
it runs but the image that is saved is all distorted, the save and load functions work properly.
Lines 34 and 35. The result is a floating point value, but this looks like an integer division. Try casting at least one of the values to a type float, to force a floating-point divide.
One other thing. I usually try to preserve the aspect ratio of the image. If the height and width supplied by the user don't match the image proportions, the result will be squashed or stretched.
It may be better to use just one of those inputs values (for example just use the user-supplied width) and derive the other by a simple calculation,
The algorithm works fine, so your bitmap layout probably isn't what you expect it to be.
This looks as if you're treating a 32-bit bitmap as a 24-bit bitmap or vice versa.
got it working. after what athar said i look at the other parts of my code again and it was loading and saving in the array using pixel[y*image.height+x] so swapped height for width and it is working fine now.