#include <stdio.h> #include <stdlib.h> unsigned char **UCalloc(int width, int height) { int i, j; unsigned char **ptr; if ((ptr = (unsigned char**)malloc(height * sizeof(unsigned char*))) == NULL) { printf("\nMemory allocation failure\n"); exit(1); } for (i = 0; i<height; i++) { if ((ptr[i] = (unsigned char*)malloc(width * sizeof(unsigned char))) == NULL) { printf("\nMemory allocation failure\n"); exit(1); } } for (i = 0; i<height; i++) for (j = 0; j<width; j++) ptr[i][j] = 0; printf("\nMEMORY ALLOCATION(UNSIGNED CHAR) OK!\n"); return ptr; } void UCfree(unsigned char **ptr, int height) { int i; for (i = 0; i<height; i++) free(ptr[i]); free(ptr); printf("\nMEMORY FREE(UNSIGNED CHAR) OK!\n"); } void Readfile(char *filename, unsigned char **source, int width, int height) { int i, j; FILE *readf; if ((readf = fopen(filename, "rb")) == NULL) { fprintf(stderr, "ERROR : File cannot open : %s \n", filename); exit(-1); } for (i = 0; i<height; i++) for (j = 0; j<width; j++) source[i][j] = (unsigned char)getc(readf); printf("\n%sFILE READING OK!\n", filename); fclose(readf); } void Writefile(char *filename, unsigned char **result, int width, int height) { int i, j; FILE *writef; if ((writef = fopen(filename, "wb")) == NULL) { fprintf(stderr, "ERROR : File cannot open : %s \n", filename); exit(-1); } for (i = 0; i<height; i++) for (j = 0; j<width; j++) putc((unsigned char)result[i][j], writef); printf("\n%sFILE WRITING OK!\n", filename); fclose(writef); } int main(void) { unsigned char **image; image = UCalloc(512, 512); Readfile("lena.img", image, 512, 512); Writefile("lena_copy.img", image, 512, 512); UCfree(image, 512); return 0; }
raw 이미지 파일을 복사하는 C 소스입니다. 프로그램에 사용 할 수 있는 이미지파일을 첨부합니다.