728x90
반응형
<설명>
<전체코드>
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main()
{
FILE* FpJsfInput; // 입력 이미지 파일 포인터.
FILE* FpJsfOutput; // 출력 이미지 파일 포인터.
short Widthtmp[2], Heighttmp[2]; // 가로, 세로 해상도 임시 저장.
long width, height; // 가로, 세로 해상도.
short LineIDtmp[2]; // 라인 식별 번호 임시 저장.
long LineIDNum; // 라인 식별 번호.
short BgPixeltmp[2], ImgPixeltmp[2]; // 배경, 이미지 픽셀 개수 임시 저장.
long BgPixelNum, ImgPixelNum; // 배경, 이미지 픽셀 개수.
short RemainingPixels; // 세트로 채워지지 않은 나머지 픽셀.
int JumpingPixel; // 다음 라인 식별 번호까지 건너뛰기 위한 변수.
char dot = '.'; // 배경 부분 출력 문자.
char circle = 'o'; // 이미지 부분 출력 문자.
char NextLine = '\n'; // 줄 바꿈 문자.
// 0000.JSF 파일을 읽기 모드로 open.
FpJsfInput = fopen("C:\\workspace\\practice\\Project1\\Project1\\0000.JSF", "rb");
// 파일열기에 실패하면 프로그램 종료.
if (FpJsfInput == NULL)
{
perror("읽기 모드 열기 실패.\n");
return 0;
}
printf("읽기 모드 열기 성공.\n");
// 가로, 세로 해상도 정보 위치 찾기
fseek(FpJsfInput, 0x11c, SEEK_SET);
// 가로, 세로 해상도 초기화.
for (int i = 0; i < 2; i++)
Widthtmp[i] = fgetc(FpJsfInput);
width = long(Widthtmp[0] + (Widthtmp[1] << 8));
for (int i = 0; i < 2; i++)
Heighttmp[i] = fgetc(FpJsfInput);
height = long(Heighttmp[0] + (Heighttmp[1] << 8));
printf("가로: %d, 세로: %d\n\n", width, height);
// 쓰기 모드로 파일 open
FpJsfOutput = fopen("C:\\workspace\\practice\\Project1\\Project1\\ASCII_image.txt", "wt");
if (FpJsfOutput == NULL)
{
perror("쓰기 모드 열기 실패.\n");
return 0;
}
printf("쓰기 모드 열기 성공.\n");
// 픽셀 데이터 접근.
fseek(FpJsfInput, 0x120, SEEK_SET);
//printf("픽셀 데이터 시작: %x\\n", ftell(FpJsfInput));
// 파일에 배경, 이미지에 따른 문자 저장.
for (int i = 0; i < height; i++)
{
// 라인 식별 번호 초기화.
for (int i = 0; i < 2; i++)
LineIDtmp[i] = fgetc(FpJsfInput);
LineIDNum = long(LineIDtmp[0] + (LineIDtmp[1] << 8));
// 라인 식별 번호에 따른 3가지 케이스.
// 1. All 배경.
// 2. All 이미지.
// 3. 배경 & 이미지.
switch (LineIDNum)
{
// 1. All 배경. (00 00)
// 라인 식별 번호(2) → 라인 식별 번호(2) → ...
case 0:
{
// 가로 해상도만큼 '.' 저장.
for (int i = 0; i < width; i++)
fputc(dot, FpJsfOutput);
fputc(NextLine, FpJsfOutput);
}break;
// 2. All 이미지. (01 00)
// 라인 식별 번호(2) → 색상 정보(가로 해상도 * 2) → 라인 식별 번호(2) → ...
case 1:
{
// 가로 해상도만큼 'o' 저장.
for (int i = 0; i < width; i++)
fputc(circle, FpJsfOutput);
fputc(NextLine, FpJsfOutput);
// 색상 정보 건너 뛰기.
JumpingPixel = width * 2;
for (int i = 0; i < JumpingPixel; i++)
fgetc(FpJsfInput);
}break;
// 3. 배경 & 이미지. (0n 00)
// 라인 식별 번호(2) → 배경 개수(2) → 이미지 개수(2)
// → 색상 정보(이미지 개수 * 2) → 배경 개수 → 이미지 개수 → 색상 정보 → ...
default:
{
// 배경 & 이미지 셋에 따른 문자 저장 후,
// 나머지 픽셀에 배경을 채우기 위한 변수.
int BgPixelCnt = 0; // 총 배경 픽셀 개수.
int ImgPixelCnt = 0; // 총 이미지 픽셀 개수.
// 세트 수 만큼 반복. (n - 1)
for (int i = 0; i < LineIDNum - 1; i++)
{
// 배경 픽셀 개수 초기화.
for (int i = 0; i < 2; i++)
BgPixeltmp[i] = fgetc(FpJsfInput);
BgPixelNum = long(BgPixeltmp[0] + (BgPixeltmp[1] << 8));
// 이미지 픽셀 개수 초기화.
for (int i = 0; i < 2; i++)
ImgPixeltmp[i] = fgetc(FpJsfInput);
ImgPixelNum = long(ImgPixeltmp[0] + (ImgPixeltmp[1] << 8));
// 배경 픽셀 수만큼 '.' 채우기.
for (int i = 0; i < BgPixelNum; i++)
fputc(dot, FpJsfOutput);
// 이미지 픽셀 수만큼 'o' 채우기.
for (int i = 0; i < ImgPixelNum; i++)
fputc(circle, FpJsfOutput);
// 색상 정보 건너 뛰기.
JumpingPixel = ImgPixelNum * 2;
for (int i = 0; i < JumpingPixel; i++)
fgetc(FpJsfInput);
//printf("배경: %x\\n", ftell(FpJsfInput));
// 총 배경, 이미지 픽셀 수.
BgPixelCnt += BgPixelNum;
ImgPixelCnt += ImgPixelNum;
}
// 나머지 픽셀 배경으로 채우기
//printf("나머지");
RemainingPixels = (short)(width - BgPixelCnt - ImgPixelCnt);
for (int i = 0; i < RemainingPixels; i++)
fputc(dot, FpJsfOutput);
fputc(NextLine, FpJsfOutput);
}break;
}
}
// 파일 닫기.
fclose(FpJsfOutput);
fclose(FpJsfInput);
return 0;
}
<수정 코드>
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
short BgPixelCnt, ImgPixelCnt;
short BgPixeltmp[2], ImgPixeltmp[2];
short RemainingPixels;
int nextPixel;
char dot = '.';
char c = '@';
char nl = '\n';
int main()
{
FILE* rf = fopen("0000.JSF", "rb"); // input image fp
if (rf == NULL)
{
printf("Open Error.\n");
return 0;
}
printf("읽기 성공\n");
fseek(rf, 0x11c, SEEK_SET); //0x11c
WORD width, height;
fread(&width, sizeof(WORD), 1, rf); //73 가로 (점의 개수)
fread(&height, sizeof(WORD), 1, rf); //56 세로 (점의 개수)
printf("width: %d height: %d\n", width, height);
FILE* wf = fopen("image.txt", "wt");
if (wf == NULL)
{
printf("Write Error.\n");
return 0;
}
printf("쓰기 성공.\n");
fseek(wf, 0x120, SEEK_SET); // 0x120
for (int i = 0; i < height; i++)
{
WORD idNum = 0;
fread(&idNum, 2, 1, rf); // 2바이트씩 1번 읽기
printf("idNum: %d\n", idNum);
DWORD _OFFSET = (height - 1 - i) * width * 2;
switch (idNum)
{
case 0:
{
}
break;
case 1:
{
for (int i = 0; i < width; i++)
{
fputc(c, wf);
}
fputc(nl, wf);
WORD width16;
width16 = width * 2;
for (int i = 0; i < width16; i++)
{
fgetc(rf);
}
}break;
default:
{
WORD bgpNum, imgpNum;
for (int i = 0; i < idNum - 1; i++)
{
for (int i = 0; i < 2; i++)
{
BgPixeltmp[i] = fgetc(rf);
}
bgpNum = long(BgPixeltmp[0] + (BgPixeltmp[1] << 8));
for (int i = 0; i < 2; i++)
ImgPixeltmp[i] = fgetc(rf);
imgpNum = long(ImgPixeltmp[0] + (ImgPixeltmp[1] << 8));
printf("bgpNum: %d\n", bgpNum);
printf("imgNum: %d\n", imgpNum);
for (int i = 0; i < bgpNum; i++)
{
fputc(dot, wf);
}
for (int i = 0; i < imgpNum; i++)
{
fputc(c, wf);
}
nextPixel = imgpNum * 2;
for (int i = 0; i < nextPixel; i++)
{
fgetc(rf);
}
//printf("bgpNum: %d\n", bgpNum);
//printf("bgpNum: %d\n", imgpNum);
BgPixelCnt += bgpNum;
ImgPixelCnt += imgpNum;
}
RemainingPixels = (short)(width - BgPixelCnt - ImgPixelCnt);
for (int i = 0; i < RemainingPixels; i++)
{
fputc(dot, wf);
}
fputc(nl, wf);
}
break;
}
}
fclose(wf);
fclose(rf);
}
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 4) [C/C++] 문제풀이_1 (0) | 2021.01.08 |
---|---|
# 3.2) [C/C++] JSF 확장자 파일 입출력 (bmp 이미지 출력) (0) | 2021.01.07 |
# 2.9) [Win32API] 도형그리기, 색 채우기 (0) | 2021.01.04 |
# 2.8) [Win32API] 그래픽처리 (bitmap 출력하기) (0) | 2021.01.04 |
# 2.7) [Win32API] 메세지 띄우는 법 (0) | 2021.01.04 |