C语言实现旋转一个HWC的图像

c 复制代码
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

static int rotate_left90(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = width;
		dst_w = height;

		for (h = 0; h < dst_h; h++) {
			for (w = 0; w < dst_w; w++) {
				for (c = 0; c < channel; c++) {
					org_img[h * dst_w *channel + w * channel + c] = \
						tmp_img[w * width *channel + (width - 1 - h) * channel + c];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

static int rotate_right90(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = width;
		dst_w = height;

		for (h = 0; h < dst_h; h++) {
			for (w = 0; w < dst_w; w++) {
				for (c = 0; c < channel; c++) {
					org_img[h * dst_w *channel + w * channel + c] = \
						tmp_img[(height - 1 - w) * width * channel + h * channel + c];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

static int rotate_right180(uint8_t *org_img, int height, int width, int channel)
{
	int rval = 0;
	int h = 0, w = 0, c = 0;
	int dst_h = 0, dst_w = 0;
	uint8_t *tmp_img = NULL;

	do {
		tmp_img = (uint8_t *)malloc(height * width * channel * sizeof(uint8_t));
		if (tmp_img == NULL) {
			rval = -1;
			break;
		}
		memcpy(tmp_img, org_img, height * width * channel * sizeof(uint8_t));

		dst_h = height;
		dst_w = width;

		for (h = 0; h < dst_h; h++) {
			for (w = 0; w < dst_w; w++) {
				for (c = 0; c < channel; c++) {
					org_img[h * dst_w *channel + w * channel + c] = \
						tmp_img[(dst_h - 1 - h) * dst_w *channel + (dst_w - 1 - w) * channel + c];
				}
			}
		}
	} while(0);

	if (tmp_img) {
		free(tmp_img);
		tmp_img = NULL;
	}

	return rval;
}

static void fill_test_img(uint8_t *test_img, int height, int width, int channel)
{
	int i = 0, j = 0, c = 0;

	for (i = 0; i < height * width * channel; i++) {
		test_img[i] = i;
	}

	for (i = 0; i < height; i++) {
		for (j = 0; j < width; j++) {
			printf("(");
			for (c = 0; c < channel; c++) {
				printf("%d,", test_img[i * width * channel + j * channel + c]);
			}
			printf("), ");
		}
		printf("\n");
	}
}

static void print_result(uint8_t *result_img, int height, int width, int channel)
{
	int i = 0, j = 0, c = 0;

	for (i = 0; i < height; i++) {
		for (j = 0; j < width; j++) {
			printf("(");
			for (c = 0; c < channel; c++) {
				printf("%d,", result_img[i * width * channel + j * channel + c]);
			}
			printf("), ");
		}
		printf("\n");
	}
}

static int test_channel_3(void)
{
	// hwc; for test, h=2, w=3, c=3
	int height = 2;
	int width = 3;
	int channel = 3;
	uint8_t test_img[18];
	int i = 0, j = 0, k = 0;
	int new_w = 0, new_h = 0;
	int ret = 0;

	printf("\n\n=========== start rotate_left90 for channel_3 ===========\n");
	/*
		(0, 1, 2), (3, 4, 5), (6, 7, 8),               (6, 7, 8), (15, 16, 17),
		(9, 10, 11), (12, 13, 14), (15, 16, 17),  ===> (3, 4, 5), (12, 13, 14),
		                                               (0, 1, 2), (9, 10, 11),
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_left90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_left90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);


	printf("\n\n=========== start rotate_right90 for channel_3 ===========\n");
	/*
		(0, 1, 2), (3, 4, 5), (6, 7, 8),               (9, 10, 11), (0, 1, 2),
		(9, 10, 11), (12, 13, 14), (15, 16, 17),  ===> (12, 13, 14), (3, 4, 5),
		                                               (15, 16, 17), (6, 7, 8),
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right90: \n");
	new_w = 2; new_h = 3;
	print_result(test_img, new_h, new_w, channel);

	printf("\n\n=========== start rotate_right180 for channel_3 ===========\n");
	/*
		(0, 1, 2), (3, 4, 5), (6, 7, 8),               (15, 16, 17), (12, 13, 14), (9, 10, 11)
		(9, 10, 11), (12, 13, 14), (15, 16, 17),  ===> (6, 7, 8), (3, 4, 5), (0, 1, 2)
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right180(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right180: \n");
	new_w = 3; new_h = 2;
	print_result(test_img, new_h, new_w, channel);

	return ret;
}

static int test_channel_1(void)
{
	// hwc; for test, h=3, w=4, c=1
	int height = 3;
	int width = 4;
	int channel = 1;
	uint8_t test_img[12];
	int new_w = 0, new_h = 0;
	int ret = 0;

	printf("\n\n=========== start rotate_left90 for channel_1 ===========\n");
	/*
		0, 1, 2,  3,          3, 7, 11
		4, 5, 6,  7,  ===>    2, 6, 10
		8, 9, 10, 11,         1, 5, 9
		                      0, 4, 8
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_left90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_left90: \n");
	new_w = 3; new_h = 4;
	print_result(test_img, new_h, new_w, channel);


	printf("\n\n=========== start rotate_right90 for channel_1 ===========\n");
	/*
		0, 1, 2,  3,          8,  4, 0
		4, 5, 6,  7,  ===>    9,  5, 1
		8, 9, 10, 11,         10, 6, 2
		                      11, 7, 3
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right90(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right90: \n");
	new_w = 3; new_h = 4;
	print_result(test_img, new_h, new_w, channel);

	printf("\n\n=========== start rotate_right180 for channel_1 ===========\n");
	/*
		0, 1, 2,  3,          11, 10, 9, 8
		4, 5, 6,  7,  ===>    7, 6, 5, 4
		8, 9, 10, 11,         3, 2, 1, 0
	};*/
	fill_test_img(test_img, height, width, channel);
	ret = rotate_right180(test_img, height, width, channel);
	assert(ret == 0);
	printf("After rotate_right180: \n");
	new_w = 4; new_h = 3;
	print_result(test_img, new_h, new_w, channel);

	return ret;
}

int main()
{
	test_channel_1();

	test_channel_3();

	return 0;
}

结果

c 复制代码
=========== start rotate_left90 for channel_1 ===========
(0,), (1,), (2,), (3,), 
(4,), (5,), (6,), (7,), 
(8,), (9,), (10,), (11,), 
After rotate_left90: 
(3,), (7,), (11,), 
(2,), (6,), (10,), 
(1,), (5,), (9,), 
(0,), (4,), (8,), 


=========== start rotate_right90 for channel_1 ===========
(0,), (1,), (2,), (3,), 
(4,), (5,), (6,), (7,), 
(8,), (9,), (10,), (11,), 
After rotate_right90: 
(8,), (4,), (0,), 
(9,), (5,), (1,), 
(10,), (6,), (2,), 
(11,), (7,), (3,), 


=========== start rotate_right180 for channel_1 ===========
(0,), (1,), (2,), (3,), 
(4,), (5,), (6,), (7,), 
(8,), (9,), (10,), (11,), 
After rotate_right180: 
(11,), (10,), (9,), (8,), 
(7,), (6,), (5,), (4,), 
(3,), (2,), (1,), (0,), 


=========== start rotate_left90 for channel_3 ===========
(0,1,2,), (3,4,5,), (6,7,8,), 
(9,10,11,), (12,13,14,), (15,16,17,), 
After rotate_left90: 
(6,7,8,), (15,16,17,), 
(3,4,5,), (12,13,14,), 
(0,1,2,), (9,10,11,), 


=========== start rotate_right90 for channel_3 ===========
(0,1,2,), (3,4,5,), (6,7,8,), 
(9,10,11,), (12,13,14,), (15,16,17,), 
After rotate_right90: 
(9,10,11,), (0,1,2,), 
(12,13,14,), (3,4,5,), 
(15,16,17,), (6,7,8,), 


=========== start rotate_right180 for channel_3 ===========
(0,1,2,), (3,4,5,), (6,7,8,), 
(9,10,11,), (12,13,14,), (15,16,17,), 
After rotate_right180: 
(15,16,17,), (12,13,14,), (9,10,11,), 
(6,7,8,), (3,4,5,), (0,1,2,), 
相关推荐
炸膛坦客9 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
程序员Rock16 小时前
上位机开发-MODBUS面试常见问题
c语言·c++·面试·职场和发展·上位机
blevoice21 小时前
抖抖燃脂机单 AC6966B 主控调试踩坑:Type-C 整机供电喇叭无声排障
c语言·开发语言·单片机
小小晓.1 天前
C++小白记:C风格字符串和数组用法
c语言·开发语言·c++
深念Y1 天前
开发者如何清理和迁移C盘堆积的垃圾
c语言·开发语言
十月的皮皮1 天前
STM32从零到量产开发:四路继电器工业控制模块开发实战笔记(小白视角)
c语言·笔记·stm32·stm32cubemx·hal库
不正经学生1 天前
C 语言函数深入剖析(基础篇)—— 从零理解函数的每一个细节
c语言·开发语言·数据结构·算法·c#
天空'之城1 天前
C 语言工业级通用组件手写 10:字节序转换
c语言·字节序·大小端·嵌入式通信·工业级组件
C++ 老炮儿的技术栈1 天前
OpenCV 实现社保卡透视矫正:Hough 直线提取四角并转正证件图像
c语言·c++·人工智能·opencv·算法·计算机视觉·c
我叫洋洋1 天前
C ++ [ hello world ]
c语言·c++·算法