1. 程式人生 > >24位bmp影象旋轉

24位bmp影象旋轉

當前僅實現了bmp影象的90°旋轉

以下為旋轉的關鍵程式碼:

void rotate_90_shun(char *data_original, FILE *bfOut1, int l_width1, int l_width2, int original_height, int original_width)
{
	int width_after90 = original_height;
	int height_after90 = original_width;

	fseek(bfOut1, 54L, SEEK_SET);

	char *target1 = (char*)malloc(sizeof(char)*height_after90*l_width2);
	if (target1 == NULL) {
		printf("malloc failed\n");
	}
	memset(target1, 0, height_after90*l_width2);

	int i, j;
	int p = 0, p1 = 0;
	
	for (i = 0; i < original_height; i++)
	{
		for (j = 0; j < original_width; j++)
		{
			p = i * l_width1 + j * 3;
			p1 = j * l_width2 + (width_after90 - 1 - i) * 3;
			
			target1[p1] = data_original[p];
			target1[p1 + 1] = data_original[p + 1];
			target1[p1 + 2] = data_original[p + 2];	
		}
	}
	fwrite(target1, sizeof(char), height_after90*l_width2, bfOut1);

	free(target1);
	target1 = NULL;
}
void rotate_90_ni(char *data_original, FILE *bfOut2, int l_width1, int l_width2, int original_width, int original_height)
{
	int width_after90 = original_height;
	int height_after90 = original_width;

	fseek(bfOut2, 54L, SEEK_SET);

	char *target2 = (char*)malloc(sizeof(char)*height_after90*l_width2);
	if (target2 == NULL) {
		printf("malloc failed\n");
	}
	memset(target2, 0, height_after90*l_width2);
	
	int i, j;
	int p = 0,p2 = 0;
	for (i = 0; i < original_height; i++) {
		for (j = 0; j < original_width; j++) {
			p = i * l_width1 + j * 3;
			p2 = (height_after90 - 1 - j)*l_width2 + i * 3;
			
			target2[p2] = data_original[p];
			target2[p2 + 1] = data_original[p + 1];
			target2[p2 + 2] = data_original[p + 2];			
		}
	}
	fwrite(target2, sizeof(char), height_after90*l_width2, bfOut2);

	free(target2);
	target2 = NULL;
}
void rotate_180(char *data_original, FILE *bfOut3, int l_width1, int l_width2, int original_width, int original_height)
{
	int width_after90 = original_height;
	int height_after90 = original_width;

	fseek(bfOut3, 54L, SEEK_SET);
	char *target3 = (char*)malloc(sizeof(char)*original_height*l_width1);
	if (target3 == NULL) {
		printf("malloc failed\n");
	}
	memset(target3, 0, original_height*l_width1);
	
	int i, j;
	int p = 0,p3 = 0;
	for (i = 0; i < original_height; i++) {
		for (j = 0; j < original_width; j++) {
			
			p = i * l_width1 + j * 3;
			p3 = (original_height - 1 - i)*l_width1 + (original_width - 1 - j) * 3;

			target3[p3] = data_original[p];
			target3[p3 + 1] = data_original[p + 1];
			target3[p3 + 2] = data_original[p + 2];
		}
	}
	fwrite(target3, sizeof(char), original_height*l_width1, bfOut3);

	free(target3);
	target3 = NULL;
}