Rescaling an image...

Hey... I appear to have a problem.. My program seems to crash everytime I get to a certain point, it runs fine..

This is the code.....

void scaleImage (ICAImage& img)
{
	ICAImage scaled_img;
	RGB* pixels;
	int new_w(0); //New ratios that will store new inputs of user
	int new_h(0); //New ratios that will store new inputs of user
	int x,y;
	float x_ratio, y_ratio;

	do
	{
		cout << "Enter a new width (pixels):";
		cin >> new_w;
		if (cin.fail())
		{
         cin.clear();
         cin.ignore();
         cerr << endl << "Invalid Input" << endl
         << "Please enter value between 1 and 1500" << endl;
         new_w = 0;
         continue;
		}
		cout << "Enter a new height (pixels):";
		cin >> new_h;
		if (cin.fail())
		{
			cin.clear();
			cin.ignore();
			cerr << endl << "Invalid Input" << endl
			<< "Please enter value between 1 and 1500" << endl;
			new_h = 0;
			continue;
		}
	} while (new_w < 1 || new_h < 1);
	
	x_ratio = (img.width)/new_w;
	y_ratio = (img.height)/new_h;

	scaled_img.height = new_h;
	scaled_img.width = new_w;
	


   scaled_img.pixels = new RGB[new_w*new_h]; //sizing 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_img.pixels[(i*(new_w))+j] = img.pixels[(y*(img.width))+x];

      }
	}

   return;
   
}


Structures

struct RGB {
   float r, g, b;
};

struct ICAImage { //Loading //rescaling
   int width, height;
   RGB* pixels;
   Image image;
};



I can run the code and get to entering new inputs for the image but I cannot seem to get past that point it freezes for a second then tells me the exe has stopped working.. I think it may be a problem with the index array and how it goes past the array size???? :SS:


Many thanks.
Last edited on
You allow scaling up, meaning x_ratio and y_ratio can go over 1. If original img size was 10x10, and new size is 1000x1000, the ratios are 100, so values of x and y in this statement:
scaled_img.pixels[(i*(new_w))+j] = img.pixels[(y*(img.width))+x]; reach 1000*100. You are waaaaay over the img.pixels bounds.
Topic archived. No new replies allowed.