Image Manipulation - is this a for-loop issue?
===

I have an image that I'm trying to blur out, but it ends up looking very weird indeed:

![enter image description here][1]


What I DO is this: I take each pixel, and I average its value on a color-by-color basis with the values of all adjacent pixels.

Or so I think. But there's clearly an error in there, and I suspect there's a problem with my for-loops but for the life of me I cannot find out what is actually going wrong.

In particular, step five (the output step) shows that the image is still ordered - if I output the left image on the right side, rather than the blur-masked image, the pixels are still in the correct order.


    	try
	{
		// STEP ONE: MAKE MEMORY AVAILABLE FOR IMAGE
		int ***image;
		image = new int**[m_nSizeX];
		for(int i = 0; i < m_nSizeX; ++i)
		{
			image[i] = new int*[m_nSizeY];
			for(int j = 0; j < m_nSizeY; ++j)
			{
				image[i][j] = new int[nrPixels];// image[x][y][z] is now a pointer to an int
				
			}
		}
		
		// STEP TWO: MAKE MEMORY AVAILABLE FOR IMAGE MASK
		int ***mask;
		mask = new int**[m_nSizeX];
		for(int i = 0; i < m_nSizeX; ++i)
		{
			mask[i] = new int*[m_nSizeY];
			for(int j = 0; j < m_nSizeY; ++j)
			{
				mask[i][j] = new int[nrPixels];// mask[x][y][z] is now a pointer to an int
				
			}
		}
		
		//STEP THREE: COPY IMAGE INTO MEMORY

		unsigned long lOffset = 0;
		for(long i=0; i<m_nSizeX ; i++)
		{
			for(long j=0; j<m_nSizeY ; j++)
			{
				for(int k=0; k<(nrPixels) ; k++)
				{
					image[i][j][k] = *(reinterpret_cast<unsigned char*>(m_pcMemOrg + lOffset) );
					lOffset++;
				}
			}
		}

		// STEP FOUR: BLUR IMAGE

		for(long i=0; i<m_nSizeX ; i++)
		{
			for(long j=0; j<m_nSizeY ; j++)
			{
				for(int k=0; k<(nrPixels) ; k++)
				{
					// INSERT BLURRING FUNCTION HERE (New value = Old value averaged with adjacent pixels)

					if(k != 2) // 0 = blue, 1 = green, 2 = red;
					{
						mask[i][j][k] = 0;
					}
					else


					if(i==0 && j==0)// (0,0) Corner Pixel
					{
						mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j+1][k]+image[i+1][j+1][k])/4;
					}
					else if(i==0 && j==(m_nSizeY-1))// (0,yMax) Corner Pixel
					{
						mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i][j-1][k]+image[i+1][j-1][k])/4;
					}
					else if(i==(m_nSizeX-1) && j==0)// (xMax,0) Corner Pixel
					{
						mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j+1][k]+image[i-1][j+1][k])/4;
					}
					else if(i==(m_nSizeX-1) && j==(m_nSizeY-1))// (xMax,yMax) Corner Pixel
					{
						mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i][j-1][k]+image[i-1][j-1][k])/4;
					}
					else if(i==0)// (0,---) Edge Pixels
					{
						mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k])/6;
					}
					else if(j==0)// (---,0) Edge Pixels
					{
						mask[i][j][k] = (image[i][j][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k])/6;					
					}
					else if(i==(m_nSizeX-1))// (xMax,---) Edge Pixels
					{
						mask[i][j][k] = (image[i][j][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k]+image[i][j+1][k])/6;
					}
					else if(j==(m_nSizeY-1))// (---,yMax) Edge Pixels
					{
						mask[i][j][k] = (image[i][j][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k])/6;
					}
					else // Mid-Image Pixels
					{
						mask[i][j][k] = (image[i][j][k]+image[i][j+1][k]+image[i+1][j+1][k]+image[i+1][j][k]+image[i+1][j-1][k]+image[i][j-1][k]+image[i-1][j-1][k]+image[i-1][j][k]+image[i-1][j+1][k])/9;
					}
				}
			}
		}

		//STEP FIVE: OUTPUT BLURRED IMAGE
		lOffset = 0;

		for(long i=0; i<m_nSizeX ; i++)
		{
			for(long j=0; j<m_nSizeY ; j++)
			{
				for(int k=0; k<(nrPixels) ; k++)
				{
					*(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = mask[i][j][k];
					//*(reinterpret_cast<unsigned char*>(m_pcMemInv + lOffset) ) = image[i][j][k];
					lOffset++;
				}
			}
		}


		// STOP USING IMAGE MEMORY NOW

		for (int i = 0; i < m_nSizeX; ++i) {
			for (int j = 0; j < m_nSizeY; ++j)
				delete [] image[i][j];

			delete [] image[i];
		}
		delete [] image;

		// STOP USING MASK MEMORY NOW
		for (int i = 0; i < m_nSizeX; ++i) {
			for (int j = 0; j < m_nSizeY; ++j)
				delete [] mask[i][j];

			delete [] mask[i];
		}
		delete [] mask;
	}
	catch( ... )
	{

	}


  [1]: http://i.stack.imgur.com/kGpSf.jpg