24位bmp顺时针旋转/逆时针旋转90度C语言

借鉴我的代码给个赞再走呗!,我瞅瞅有几个人借鉴

#include 
#include 
#include 
#define BYTE1  __attribute__((packed, aligned(1)))

//跨度应该4字节对齐
int main()
{
	typedef struct FILEHEADER 
	{ 
		short    bfType;   //位图类型
		int      bfSize;   //bmp图像文件的大小 
		short    bfReserved1;  //0
		short    bfReserved2;  //0
		int      bfOffBits;    //BMP图像数据的偏移地址   54
	} BYTE1 BITMAPFILEHEADER;

	typedef struct INFOHEADER
	{ 
		unsigned int      biSize;    //本结构的大小
		unsigned int     biWidth;   //BMP图像的宽度,单位像素
		unsigned int     biHeight;  //BMP图像的高度,单位像素
		short    biPlanes;     //1
		short    biBitCount;   //BMP图像的色深,即一个像素用多少位表示
		unsigned int      biCompression;  //压缩方式,0表示不压缩
		unsigned int      biSizeImage;    //BMP图像数据大小,单位字节
		unsigned int     biXPelsPerMeter;  //水平分辨率,单位像素  (还没搞清楚是啥)
		unsigned int     biYPelsPerMeter;  //垂直分辨率,单位像素  (还没搞清楚是啥)
		unsigned int      biClrUsed;    //BMP图像使用的颜色,0表示使用全部颜色
		unsigned int      biClrImportant;   //重要的颜色数,此值为0时所有颜色都重要,
	} BITMAPINFOHEADER;

	int ret = 0;
	FILE * fp = NULL;

	fp = fopen("yes.bmp", "rb+");
	if(NULL == fp)
	{
		perror("fopen");
		printf("fp is null \n");
		return -1;
	}

	BITMAPFILEHEADER bmpFileHeader = {0};
	BITMAPINFOHEADER bmpInfo = {0};

	ret = fread(&bmpFileHeader, 1, sizeof(BITMAPFILEHEADER), fp);
	if(ret <= 0)
	{
		perror("fread");
		return -1;
	}
	printf("1 ret = %d sizeof(BITMAPFILEHEADER) = %ld\n", ret, sizeof(BITMAPFILEHEADER));
	
	ret = fread(&bmpInfo, 1, sizeof(BITMAPINFOHEADER), fp);
	if(ret <= 0)
	{
		perror("fread");
		return -1;
	}
	printf("2 ret = %d sizeof(BITMAPINFOHEADER) = %ld\n\n", ret, sizeof(BITMAPINFOHEADER));
	
#if 1	
	printf("bmpFileHeader.bfType %x\n",bmpFileHeader.bfType );
	printf("bmpFileHeader.bfSize %d \n",bmpFileHeader.bfSize );
	printf("bmpFileHeader.bfReserved1 %d\n",bmpFileHeader.bfReserved1 );
	printf("bmpFileHeader.bfReserved2  %d\n",bmpFileHeader.bfReserved2 );
	printf("bmpFileHeader.bfOffBits %d\n\n",bmpFileHeader.bfOffBits );
	
	printf("bmpInfo.biSize %d\n", bmpInfo.biSize);
	printf("bmpInfo.biWidth  %d\n",bmpInfo.biWidth);
	printf("bmpInfo.biHeight  %d\n",bmpInfo.biHeight );
	printf("bmpInfo.biPlanes  %d\n",bmpInfo.biPlanes );
	printf("bmpInfo.biBitCount  %d\n",bmpInfo.biBitCount );
	printf("bmpInfo.biCompression  %d\n",bmpInfo.biCompression );
	printf("bmpInfo.biSizeImage  %d\n",bmpInfo.biSizeImage );
	printf("bmpInfo.biXPelsPerMeter  %d\n",bmpInfo.biXPelsPerMeter );
	printf("bmpInfo.biYPelsPerMeter  %d\n",bmpInfo.biYPelsPerMeter );
	printf("bmpInfo.biClrUsed  %d\n",bmpInfo.biClrUsed );
	printf("bmpInfo.biClrImportant  %d\n\n",bmpInfo.biClrImportant );	
#endif
	int b = bmpInfo.biBitCount / 8;
	int w = bmpInfo.biWidth;
	int h = bmpInfo.biHeight;
	int stride = w*3;
	int stridew = (w*3+3)/4*4;  //跨度应该4对其,原跨度
	int strideh = (h*3+3)/4*4;  //跨度应该4对其,旋转后的跨度
	
	if(b != 3)
	{
		printf("b != 3\n");
		fclose(fp);
		fp = NULL;
		return -1;
	}
	
	int sizesrc = stridew * h;
	int sizetar = strideh * w;
	
	char *gPBmpBuf = (char *)malloc(sizesrc);
	if(NULL == gPBmpBuf)
	{
		printf("gPBmpBuf is NULL\n");
		fclose(fp);
		fp = NULL;
		return -1;
	}
	
	char *arr = (char *)malloc(sizetar);
	if(NULL == arr)
	{
		printf("arr is NULL\n");
		free(gPBmpBuf);
		gPBmpBuf = NULL;
		fclose(fp);
		fp = NULL;
		return -1;
	}
	
	ret = fread(gPBmpBuf, 1, sizesrc, fp);    
	if(ret <= 0)
	{
		printf("ret <= 0 \n");
		free(gPBmpBuf);
		gPBmpBuf = NULL;
		free(arr);
		arr = NULL;
		fclose(fp);
		fp = NULL;
		return -1;
	}
	
	
#if 1	
	for(int j=0; j<h; j++)  //高
	{
		for(int i=0; i<stride; i=i+3) //宽
		{				
		#if 0
			/* 顺时针旋转90°,但是我认为我的逻辑是逆时针旋转的 */
			memcpy(arr+(3*j+(w-1-i/3)*strideh), gPBmpBuf+(i+j*stridew), 3);
		
		#else
			/* 逆时针旋90° */
			memcpy(arr+(3*(h-1-j)+(i/3)*strideh), gPBmpBuf+i+j*stridew, 3);
		#endif	
		}
	}	
#endif	
	
	bmpInfo.biWidth = h;
	bmpInfo.biHeight = w;
	bmpInfo.biSizeImage = sizetar;
	bmpFileHeader.bfSize = 54 + bmpInfo.biSizeImage;
	
	fseek(fp, 0, SEEK_SET);
	fwrite(&bmpFileHeader, 1, sizeof(BITMAPFILEHEADER), fp);
	fwrite(&bmpInfo, 1, sizeof(BITMAPINFOHEADER),  fp);
	fwrite(arr, 1, bmpInfo.biSizeImage, fp);

#if 1	
	printf("bmpFileHeader.bfType %x\n",bmpFileHeader.bfType );
	printf("bmpFileHeader.bfSize %d \n",bmpFileHeader.bfSize );
	printf("bmpFileHeader.bfReserved1 %d\n",bmpFileHeader.bfReserved1 );
	printf("bmpFileHeader.bfReserved2  %d\n",bmpFileHeader.bfReserved2 );
	printf("bmpFileHeader.bfOffBits %d\n\n",bmpFileHeader.bfOffBits );
	
	printf("bmpInfo.biSize %d\n", bmpInfo.biSize);
	printf("bmpInfo.biWidth  %d\n",bmpInfo.biWidth);
	printf("bmpInfo.biHeight  %d\n",bmpInfo.biHeight );
	printf("bmpInfo.biPlanes  %d\n",bmpInfo.biPlanes );
	printf("bmpInfo.biBitCount  %d\n",bmpInfo.biBitCount );
	printf("bmpInfo.biCompression  %d\n",bmpInfo.biCompression );
	printf("bmpInfo.biSizeImage  %d\n",bmpInfo.biSizeImage );
	printf("bmpInfo.biXPelsPerMeter  %d\n",bmpInfo.biXPelsPerMeter );
	printf("bmpInfo.biYPelsPerMeter  %d\n",bmpInfo.biYPelsPerMeter );
	printf("bmpInfo.biClrUsed  %d\n",bmpInfo.biClrUsed );
	printf("bmpInfo.biClrImportant  %d\n\n",bmpInfo.biClrImportant );	
#endif

	free(gPBmpBuf);
	gPBmpBuf = NULL;
	free(arr); 
	arr = NULL;
	fclose(fp);
	fp = NULL;
}

你可能感兴趣的:(linux_C语言)