resizing images

i am trying to resize an image useing this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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.

the structurs used are:
1
2
3
4
5
6
7
8
9
10
11
12
struct Pixel
{
   byte r,g,b,a;
};

struct My_Image
{
   unsigned int height;
   unsigned int width;
   Pixel * pixel ;
};
 

can any one see any problems with the function?
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.
34
35
   x_ratio = float(image.width)/new_w;
   y_ratio = float(image.height)/new_h;


Last edited on
1
2
x_ratio = (image.width)/new_w;
y_ratio = (image.height)/new_h;

You are dividing two integers so you will not get the fractional part. Make sure at least one of the operands are of type float.

1
2
x_ratio = static_cast<float>(image.width)/new_w;
y_ratio = static_cast<float>(image.height)/new_h;
thanks for your fast reply but it has the same result.
Last edited on
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,
 
    new_h =  image.height * (float) new_w / image.width;
Distorted in what way?
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.
Last edited on
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.

thank you everyone who responded.
Last edited on
Topic archived. No new replies allowed.