Click here to Skip to main content
15,891,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Has any one done Vertical flipping in c++..

here it my code
BOOL BMPFile::VertFlipBuf(unsigned char  * inbuf, 
					   UINT widthBytes, 
					   __int16 height)
{   
	BYTE  *tb1;
	BYTE  *tb2;

	if (inbuf==NULL)
		return FALSE;

	UINT bufsize;

	bufsize=widthBytes;

	tb1= new unsigned char[bufsize];
	if (tb1==NULL) {
		return FALSE;
	}

	tb2= new unsigned char [bufsize];
	if (tb2==NULL) {
		delete [] tb1;
		return FALSE;
	}
	
	__int16 row_cnt;     
	ULONG off1=0;
	ULONG off2=0;

	for (row_cnt=0;row_cnt<(height+1)/2;row_cnt++) 
	{
		off1=row_cnt*bufsize;
		off2=((height-1)-row_cnt)*bufsize;  
		
		memcpy(tb1,inbuf+off1,bufsize*sizeof(unsigned char));	
		memcpy(tb2,inbuf+off2,bufsize*sizeof(unsigned char));	
		memcpy(inbuf+off1,tb2,bufsize*sizeof(unsigned char));
		memcpy(inbuf+off2,tb1,bufsize*sizeof(unsigned char));
	}	

	delete [] tb1;
	delete [] tb2;

	return TRUE;
}        



What's wrong in this ? im getting the out put of 2/3rd image..but the rest part it got flipped by it ownself..


some one can please check the code ,,
An suggestionsss,,,
Posted
Comments
Sergey Chepurin 20-Apr-12 6:38am    
If on Windows, then who handles images nowadays in plain C/C++? Ever heard of Direct2D - http://msdn.microsoft.com/en-us/library/ff934857.aspx?

There are a couple of somewhat odd things in your code, but they are not the cause for the outcome that you have described:

1. You only need one temporary buffer for the exchange of image rows.

copy upper row to temp buffer
copy lower row to upper row
copy temp buffer to lower row


But this is just a matter of convenience. Your code should be working perfectly fine.

2. The row loop needs only run to height/2, and not to (height+1)/2. In case height is odd, the middle row doesn't need to be worked on.

3. The declaration of the height parameter as __int16 is a little odd. On 32-bit architectures it will be forced up to 32-bit value anyhow.

All that said, these things are not an explanation for what you describe. My best guess for the error you are describing is one of the following causes:

- you are giving a wrong value for the height parameter in your call
- the passed pointer value for inbuf is not correct
- both of the above

So I would be looking for a problem in the calling code.
 
Share this answer
 
Hi nv3,

this is the code i am using for loading 8 bit image.
I am using bmpfile.h and .cpp ...


I don know what mistake i have done ,, please someone can help me out from this issue....

Here is the main code ..

C#
int nWidth = 149 ;
    int nHeight = 126;

    unsigned char * pInputBuffer = new unsigned char[nWidth*nHeight]();


C#
Loading 8 bit bmp image ..

pInputBuffer = bmp.LoadBMP("D:\\MyWorkSpace\\Clipboard01.bmp",nWidth,nHeight,8);
// Calling Vertical Flipping here
bmp.VertFlipBuf(pInputBuffer,nWidth,nHeight);





BYTE * BMPFile::LoadBMP(char* fileName, 
						__int16 width, 
						__int16 height, int bitdepth)
{

	BITMAP inBM;

	BYTE m1,m2;
    long filesize;
    short res1,res2;
    long pixoff;
    long bmisize;                    
    long compression;
    unsigned long sizeimage;
    long xscale, yscale;
    long colors;
    long impcol;
    

	BYTE *outBuf=NULL;
	
	// for safety
	width=0; height=0;
	bitdepth=0;

	// init
	m_bytesRead=0;

	FILE *fp;
	
	fp=fopen(fileName,"rb");
	if (fp==NULL) 
	{
		return NULL;
	} 
	else
	{
	    long rc;
		rc=fread((BYTE  *)&(m1),1,1,fp); m_bytesRead+=1;
		if (rc==-1) {fclose(fp); return NULL;}

		rc=fread((BYTE  *)&(m2),1,1,fp); m_bytesRead+=1;
		if (rc==-1) 
		if ((m1!='B') || (m2!='M'))
		{
			fclose(fp);
			return NULL;
        }
        
		////////////////////////////////////////////////////////////////////////////
		//
		//	read a ton of header stuff

		rc=fread((long *)&(filesize),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((int  *)&(res1),2,1,fp); m_bytesRead+=2;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((int  *)&(res2),2,1,fp); m_bytesRead+=2;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(pixoff),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(bmisize),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(inBM.bmWidth),4,1,fp);	 m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(inBM.bmHeight),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((int  *)&(inBM.bmPlanes),2,1,fp); m_bytesRead+=2;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((int  *)&(inBM.bmBitsPixel),2,1,fp); m_bytesRead+=2;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(compression),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(sizeimage),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(xscale),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(yscale),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(colors),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		rc=fread((long  *)&(impcol),4,1,fp); m_bytesRead+=4;
		if (rc!=1) { fclose(fp); return NULL;}

		bitdepth = inBM.bmBitsPixel;
		////////////////////////////////////////////////////////////////////////////
		//	i don't do RLE files

		if (compression!=BI_RGB) 
		{
	    	
	    	fclose(fp);
	    	return NULL;
	    }

		if (colors == 0) 
		{
			colors = 1 << inBM.bmBitsPixel;
		}


		////////////////////////////////////////////////////////////////////////////
		// read colormap

		//RGBQUAD *colormap = NULL;

		switch (inBM.bmBitsPixel) 
		{
		case 24:
			break;
			// read pallete 
		case 1:
		case 4:
		case 8:
			colormap = new RGBQUAD[colors];
			if (colormap==NULL) 
			{
				fclose(fp);
				return NULL;
			}

			int i;
			for (i=0;i<colors;i++)>
			{
				BYTE r,g,b, dummy;

				rc=fread((BYTE *)&(b),1,1,fp);
				m_bytesRead++;
				if (rc!=1) 
				{
						
					delete [] colormap;
					fclose(fp);
					return NULL;
				}

				rc=fread((BYTE  *)&(g),1,1,fp); 
				m_bytesRead++;
				if (rc!=1) 
				{
						
					delete [] colormap;
					fclose(fp);
					return NULL;
				}

				rc=fread((BYTE  *)&(r),1,1,fp); 
				m_bytesRead++;
				if (rc!=1)
				{
						
					delete [] colormap;
					fclose(fp);
					return NULL;
				}


				rc=fread((BYTE  *)&(dummy),1,1,fp); 
				m_bytesRead++;
				if (rc!=1) 
				{
						
					delete [] colormap;
					fclose(fp);
					return NULL;
				}

				colormap[i].rgbRed=r;
				colormap[i].rgbGreen=g;
				colormap[i].rgbBlue=b;
			}
			break;
		}


		if ((long)m_bytesRead>pixoff) 
		{
			fclose(fp);
			delete [] colormap;
			fclose(fp);
			return NULL;
		}

		while ((long)m_bytesRead<pixoff)>
		{
			char dummy;
			fread(&dummy,1,1,fp);
			m_bytesRead++;
		}

		int w=inBM.bmWidth;
		int h=inBM.bmHeight;

		// set the output params
		width=w;
		height=h;

		long row_size = w * 3;

		long bufsize = (long)w * 3 * (long)h;

		////////////////////////////////////////////////////////////////////////////
		// alloc our buffer

		outBuf = new BYTE[bufsize];
		if (outBuf==NULL) 
		{
		} 
		else 
		{
			////////////////////////////////////////////////////////////////////////////
			//	read it

			long row=0;
			long rowOffset=0;

			// read rows in reverse order
			for (row=inBM.bmHeight-1;row>=0;row--) 
			{

				// which row are we working on?
				rowOffset=(long unsigned)row*row_size;						      

				if (inBM.bmBitsPixel==24) 
				{
					for (int col=0;col<w;col++)>
					{
						long offset = col * 3;
						char pixel[3];

						if (fread((void  *)(pixel),1,3,fp)==3) 
						{
							// we swap red and blue here
							*(outBuf + rowOffset + offset + 0)=pixel[2];		// r
							*(outBuf + rowOffset + offset + 1)=pixel[1];		// g
							*(outBuf + rowOffset + offset + 2)=pixel[0];		// b
						}

					}

					m_bytesRead+=row_size;
					
					// read DWORD padding
					while ((m_bytesRead-pixoff)&3) 
					{
						char dummy;
						if (fread(&dummy,1,1,fp)!=1) 
						{
							delete [] outBuf;
							fclose(fp);
							return NULL;
						}
						m_bytesRead++;
					}					
				} 
				else
				{	// 1, 4, or 8 bit image

					////////////////////////////////////////////////////////////////
					// pixels are packed as 1 , 4 or 8 bit vals. need to unpack them

					int bit_count = 0;
					UINT mask = (1 << inBM.bmBitsPixel) - 1;

					BYTE inbyte=0;

					for (int col=0;col<w;col++)>
					{						
						int pix=0;

						// if we need another byte
						if (bit_count <= 0) 
						{
							bit_count = 8;
							if (fread(&inbyte,1,1,fp)!=1) 
							{
								delete [] outBuf;
								delete [] colormap;
								fclose(fp);
								return NULL;
							}
							m_bytesRead++;
						}

						// keep track of where we are in the bytes
						bit_count -= inBM.bmBitsPixel;
						pix = ( inbyte >> bit_count) & mask;

						// lookup the color from the colormap - stuff it in our buffer
						// swap red and blue
						*(outBuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
						*(outBuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
						*(outBuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
					}

					// read DWORD padding
					while ((m_bytesRead-pixoff)&3) 
					{
						char dummy;
						if (fread(&dummy,1,1,fp)!=1) 
						{
							delete [] outBuf;
							if (colormap)
								delete [] colormap;
							fclose(fp);
							return NULL;
						}
						m_bytesRead++;
					}
				}
			}
		
		}

	/*	if (colormap) 
		{
			delete [] colormap;
		}*/
		fclose(fp);

    }

	return outBuf;
}
 
Share this answer
 
Comments
BillW33 23-May-13 11:58am    
You should not post a comment to a solution or an addition to your question as a "Solution". Either add a comment to a previous solution by pressing the "Have a Question or Comment?" button or update your question by using the "Improve question" button. I did not vote, but I thought you should know why this might be downvoted.
The mistake is in the size of a pixel.
Here BMPFile::loadBMP creates bitmap in Width*Height*3 bytes. But you expect it as Width*Height bytes.
Code from BMPFile::LoadBMP

VB
// set the output params
width=w;
height=h;

long row_size = w * 3;

long bufsize = (long)w * 3 * (long)h;

////////////////////////////////////////////////////////////////////////////
// alloc our buffer

outBuf = new BYTE[bufsize];


Please change size in VertFlipBuf()

Please change
bufsize=widthBytes;

to
C++
bufsize=widthBytes * 3;
 
Share this answer
 
Comments
Santhosh G_ 23-May-13 20:41pm    
Here one member down voted this answer. Please give a reason for the same, then I can correct the same.
JackDingler 24-May-13 15:10pm    
Your comment fits the symptoms. I was going to suggest something similar...
C#
apmatrix<int> flip (apmatrix<int> img)
{
    int space;//for the sort
    for (int x = 0; x<img.numcols()/2; x++)// divide by 2 so it doesnt switch back
    {
        for (int y=0; y< img.numrows() ; y++)
        {
            space = img[y][x];
            img[y][x] = img[y][img.numcols()-1-x];//switches an x coordinate with x - 1
            img[y][img.numcols()-1 -x] = space; //puts others in place
        }

    }
    return img;
}



//NOTE: "img" is the matrix I downloaded in earlier code to get the picture I have. I created a new matrix, "flip" to be the new flipped matrix, because we are creating a new matrix that has different numbers in different places.
~ Kailyn
 
Share this answer
 
Comments
kailyn 136 23-May-13 13:40pm    
i meant the same numbers in different places*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900