C 语言图像翻转程序:垂直翻转、水平翻转和双向翻转
#include <stdlib.h> #include <stdio.h> #include <string.h>
#define HEIGHT 256 #define WIDTH 256
void flipVertical(unsigned char srcimg[HEIGHT][WIDTH], unsigned char newimg[HEIGHT][WIDTH]) { int i, j; for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { newimg[i][j] = srcimg[HEIGHT - i - 1][j]; } } }
void flipHorizontal(unsigned char srcimg[HEIGHT][WIDTH], unsigned char newimg[HEIGHT][WIDTH]) { int i, j; for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { newimg[i][j] = srcimg[i][WIDTH - j - 1]; } } }
void flipBoth(unsigned char srcimg[HEIGHT][WIDTH], unsigned char newimg[HEIGHT][WIDTH]) { int i, j; for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { newimg[i][j] = srcimg[HEIGHT - i - 1][WIDTH - j - 1]; } } }
int main() { FILE* fpin; FILE* fpout; unsigned char srcimg[HEIGHT][WIDTH]; unsigned char newimg[HEIGHT][WIDTH]; int j, i;
fpin = fopen("C:/barbara512.raw", "rb");
if (fpin == NULL) {
printf("Failed to open input file.\n");
return 1;
}
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
fread(&srcimg[i][j], 1, 1, fpin);
printf("%d ", (int)srcimg[i][j]);
}
printf("\n");
}
fclose(fpin);
flipVertical(srcimg, newimg);
fpout = fopen("C:/barbara512_vertical.raw", "wb+");
if (fpout == NULL) {
printf("Failed to open output file.\n");
return 1;
}
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
fwrite(&newimg[i][j], 1, 1, fpout);
}
}
fclose(fpout);
flipHorizontal(srcimg, newimg);
fpout = fopen("C:/barbara512_horizontal.raw", "wb+");
if (fpout == NULL) {
printf("Failed to open output file.\n");
return 1;
}
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
fwrite(&newimg[i][j], 1, 1, fpout);
}
}
fclose(fpout);
flipBoth(srcimg, newimg);
fpout = fopen("C:/barbara512_both.raw", "wb+");
if (fpout == NULL) {
printf("Failed to open output file.\n");
return 1;
}
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
fwrite(&newimg[i][j], 1, 1, fpout);
}
}
fclose(fpout);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pwcX 著作权归作者所有。请勿转载和采集!