Doubeling the size of an image

I'd like to double the size of an image without the need to return a completely new array. I have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pixel* doublesize(pixel image[],int height,int width,int& nw,int& nh)
{	
	nw=width*2;
	nh=height*2;
	pixel* doubleimage=new pixel[nw*nh];
	for (int i=0; i<nh; i++)
		for (int j=0; j<nw; j++)
			doubleimage[i*nw+j] = image[(i/2)*width+(j/2)];

	return doubleimage;
}
void main()
{
	int height,width,numLevels;
	pixel* image;
	pixel* nimage;
	int nw,nh;
	image=readfile(height,width,numLevels);
	nimage=doublesize(image,height,width,nw,nh);
}

I'd like to avoid having nimage in the main function and just increase the size of the image array. How do I do that?
You can't really 'just increase the size of the image array', but a minor change to your function can get the same effect.

After creating the doubleimage, delete the old image and assign image to the new doubleimage. Get rid of the return value entirely. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
void doublesize(pixel image[],int height,int width,int& nw,int& nh)
{	
	nw=width*2;
	nh=height*2;
	pixel* doubleimage=new pixel[nw*nh];
	for (int i=0; i<nh; i++)
		for (int j=0; j<nw; j++)
			doubleimage[i*nw+j] = image[(i/2)*width+(j/2)];
	delete[] image; //I'm assuming readfile() is using new.  If it isn't, this may not work
	image = doubleimage;
}
void main()
{
	int height,width,numLevels;
	pixel* image;
	pixel* nimage;
	int nw,nh;
	image=readfile(height,width,numLevels);
	doublesize(image,height,width,nw,nh);
	//now image points to the doublesized image, which replaced the original
}
Thanks
Topic archived. No new replies allowed.