This repository has been archived on 2025-11-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cpp-AsciiArtPlayer/Z_3/core-bkup.cpp
2025-11-09 14:10:48 +08:00

400 lines
8.8 KiB
C++

//#define _DEBUG_MODE
#include "PicReader.h"
#include "FastPrinter.h"
class Array {
private:
BYTE * * * arr_header;
UINT line;
UINT col;
UINT depth;
public:
//Array(const UINT, const UINT, const UINT);
Array(BYTE * data, UINT l, UINT c, UINT d) : line(l), col(c), depth(d) {
arr_header = new BYTE * * [l];
for (UINT i = 0; i < l; i++) {
arr_header[i] = new BYTE * [c];
for (UINT j = 0; j < c; j++) {
arr_header[i][j] = new BYTE[d];
}
}
for (UINT i = 0; i < l; i++) {
for (UINT j = 0; j < c; j++) {
for (UINT k = 0; k < d; k++) {
arr_header[i][j][k] = data[(i * c + j) * d + k];
}
}
}
}
//Array(Array &);
~Array();
BYTE * * operator [] (UINT & line_no) const {
return *(arr_header + line_no);
}
/*BYTE * operator () (UINT & line_no, UINT & col_no) const {
return *(*(arr_header + line_no) + col_no);
}
BYTE & operator () (UINT & line_no, UINT & col_no, UINT & dep_no) const {
return *(*(*(arr_header + line_no) + col_no) + dep_no);
}*/
//friend Array operator + (Array &, Array &);
//friend Array operator - (Array &, Array &);
//friend Array operator * (Array &, Array &);
//friend Array operator / (Array &, Array &);
//Array & operator = (const Array &);
/*void reshape(const UINT l = 0, const UINT c = 1, const UINT d = 1) {
if (arr_header != nullptr) {
delete[] arr_header;
}
line = l; col = c; depth = d;
if (l * c * d != 0) {
arr_header = (BYTE * * *) new BYTE[l*c*d];
} else {
arr_header = nullptr;
}
}*/
friend BYTE * * * RGB2Grey(Array &);
};
//Array::Array(const UINT l = 0, const UINT c = 1, const UINT d = 1) : line(l), col(c), depth(d) {
// if (l * c * d != 0) {
// arr_header = new BYTE * * [l];
// for (UINT i = 0; i < l; i++) {
// arr_header[i] = new BYTE * [c];
// for (UINT j = 0; j < c; j++) {
// arr_header[i][j] = new BYTE[d];
// }
// }
// } else {
// arr_header = nullptr;
// }
//}
//Array::Array(Array & A0) {
// line = A0.line; col = A0.col; depth = A0.depth;
// reshape(line, col, depth);
// for (UINT i = 0; i < A0.line && i < line; i++) {
// for (UINT j = 0; j < A0.col && i < col; j++) {
// for (UINT k = 0; k < A0.depth && i < depth; k++) {
// *(*(*(arr_header + i) + j) + k) = A0[i][j][k];
// }
// }
// }
//}
Array::~Array() {
for (UINT i = 0; i < line; i++) {
for (UINT j = 0; j < col; j++) {
delete[] arr_header[i][j];
}
delete[] arr_header[i];
}
delete[] arr_header;
}
//Array operator + (Array & A1, Array & A2) {
// if (A1.line != A2.line || A1.col != A2.col || A1.depth != A2.depth) {
// throw "矩阵大小不一致!";
// }
// Array ans(A1.line, A1.col, A1.depth);
// for (UINT i = 0; i < A1.line; i++) {
// for (UINT j = 0; j < A1.col; j++) {
// for (UINT k = 0; k < A1.depth; k++) {
// ans[i][j][k] = A1[i][j][k] + A2[i][j][k];
// }
// }
// }
// return ans;
//}
//
//Array operator - (Array & A1, Array & A2) {
// if (A1.line != A2.line || A1.col != A2.col || A1.depth != A2.depth) {
// throw "矩阵大小不一致!";
// }
// Array ans(A1.line, A1.col, A1.depth);
// for (UINT i = 0; i < A1.line; i++) {
// for (UINT j = 0; j < A1.col; j++) {
// for (UINT k = 0; k < A1.depth; k++) {
// ans[i][j][k] = A1[i][j][k] - A2[i][j][k];
// }
// }
// }
// return ans;
//}
//
//Array operator * (Array & A1, Array & A2) {
// if (A1.line != A2.line || A1.col != A2.col || A1.depth != A2.depth) {
// throw "矩阵大小不一致!";
// }
// Array ans(A1.line, A1.col, A1.depth);
// for (UINT i = 0; i < A1.line; i++) {
// for (UINT j = 0; j < A1.col; j++) {
// for (UINT k = 0; k < A1.depth; k++) {
// ans[i][j][k] = A1[i][j][k] * A2[i][j][k];
// }
// }
// }
// return ans;
//}
//
//Array operator / (Array & A1, Array & A2) {
// if (A1.line != A2.line || A1.col != A2.col || A1.depth != A2.depth) {
// throw "矩阵大小不一致!";
// }
// Array ans(A1.line, A1.col, A1.depth);
// for (UINT i = 0; i < A1.line; i++) {
// for (UINT j = 0; j < A1.col; j++) {
// for (UINT k = 0; k < A1.depth; k++) {
// ans[i][j][k] = A1[i][j][k] / A2[i][j][k];
// }
// }
// }
// return ans;
//}
//Array & Array::operator = (const Array & A0) {
// line = A0.line; col = A0.col; depth = A0.depth;
// reshape(line, col, depth);
// for (UINT i = 0; i < A0.line && i < line; i++) {
// for (UINT j = 0; j < A0.col && i < col; j++) {
// for (UINT k = 0; k < A0.depth && i < depth; k++) {
// *(*(*(arr_header + i) + j) + k) = A0[i][j][k];
// }
// }
// }
// return *this;
//}
// Specified functions for this project only
BYTE * * * RGB2Grey(Array & RGB_Arr) {
for (UINT i = 0; i < RGB_Arr.line; i++) {
for (UINT j = 0; j < RGB_Arr.col; j++) {
RGB_Arr[i][j][0] = BYTE((RGB_Arr[i][j][0] * 299 + RGB_Arr[i][j][1] * 587 + RGB_Arr[i][j][2] * 114) / 1100);
}
}
//RGB.reshape(RGB.line, RGB.col, 1);
return RGB_Arr.arr_header;
}
BYTE * * Grey2BYTE(BYTE * * * From, UINT line, UINT col) {
const BYTE asciiStrength[] = { 'M','N','H','Q','$','O','C','?','!',':','-','.' };
BYTE * * Result = new BYTE * [line];
for (UINT i = 0; i < line; i++) {
Result[i] = new BYTE[col];
}
for (UINT i = 0; i < line; i++) {
for (UINT j = 0; j < col; j++) {
Result[i][j] = asciiStrength[From[i][j][0] / 21];
}
//Result[i][col] = '\0';
}
return Result;
}
#include <cstdio>
#define _DBL_CHR
#ifdef _DEBUG_MODE
int main(int argc, char * * argv) {
PicReader imread;
BYTE *data = nullptr;
UINT line, col;
if (argc == 1) {
imread.readPic("test.png");
} else if (argc == 2) {
imread.readPic(argv[1]);
}
imread.testReader(data, col, line);
Array pic(data, line, col, 4);
delete[] data;
BYTE * * ResDat = Grey2BYTE(RGB2Grey(pic), line, col);
#ifdef _DBL_CHR
FastPrinter printer(col * 2, line, 1);
#else
FastPrinter printer(col, line, 1);
#endif
COORD picXY;
printer.cleanSrceen();
picXY.X = 0;
#ifdef _DBL_CHR
char * Line = new char[col * 2];
#endif
for (unsigned i = 0; i < line; i++) {
picXY.Y = i;
#ifdef _DBL_CHR
for (UINT j = 0; j < col; j++) {
Line[j * 2] = ResDat[i][j];
Line[j * 2 + 1] = ResDat[i][j];
}
printer.setText(picXY, Line, (fp_color::f_black | fp_color::b_l_white), col * 2);
#else
printer.setText(picXY, (char *)(ResDat[i]), (fp_color::f_black | fp_color::b_l_white), col);
#endif
}
#ifdef _DBL_CHR
delete[] Line;
#endif
printer.draw(true);
for (UINT i = 0; i < line; i++) {
delete[] ResDat[i];
}
delete[] ResDat;
getchar();
return 0;
}
#else
#include "timer.h"
#include <fstream>
static PicReader * FrameLoader;
static BYTE *FrameData = nullptr;
static UINT Vline, Vcol;
static unsigned CntLen, FrameNum;
static unsigned Interval = 0;
static char FramePath[50] = { 0 };
static unsigned FrontPathLen = 0;
static BYTE * * * ResDat;
static COORD LineXY;
static FastPrinter * FramePrinter;
Timer t;
void ResDatInit() {
printf("按下回车开始读取资源文件…\n");
getchar();
for (UINT i = 1; i <= FrameNum; i++) {
int TmpCnt = i;
for (int j = CntLen; j > 0; j--) {
FramePath[FrontPathLen + j] = '0' + TmpCnt % 10;
TmpCnt /= 10;
}
FrameLoader->readPic(FramePath);
FrameLoader->testReader(FrameData, Vcol, Vline);
Array pic(FrameData, Vline, Vcol, 4);
delete[] FrameData;
ResDat[i] = Grey2BYTE(RGB2Grey(pic), Vline, Vcol);
printf("资源文件%u/%u读取完成\n", i, FrameNum);
}
}
// 播放器初始化
void PlayerInit(const char * FilePath) {
std::ifstream inf(FilePath);
inf >> Vcol >> Vline;
inf >> CntLen >> FrameNum;
inf >> Interval;
inf >> FramePath;
inf.close();
while (FramePath[++FrontPathLen]);
FramePath[FrontPathLen] = ' ';
FramePath[FrontPathLen + CntLen + 1] = '.';
FramePath[FrontPathLen + CntLen + 2] = 'j';
FramePath[FrontPathLen + CntLen + 3] = 'p';
FramePath[FrontPathLen + CntLen + 4] = 'g';
FrameLoader = new PicReader;
ResDat = new BYTE * * [FrameNum];
ResDatInit();
FramePrinter = new FastPrinter(Vcol * 2, Vline, 1);
LineXY.X = 0;
FramePrinter->cleanSrceen();
}
void PlayFinish() {
delete[] ResDat;
delete FrameLoader;
delete FramePrinter;
}
// 渲染当前帧并准备下一帧
void FrameEvent() {
static unsigned FrameCounter = 0;
FramePrinter->draw(true);
FrameCounter++;
if (FrameCounter >= FrameNum) {
t.Expire();
PlayFinish();
return;
}
FramePrinter->cleanSrceen();
char * Line = new char[Vcol * 2];
for (unsigned i = 0; i < Vline; i++) {
LineXY.Y = i;
for (UINT j = 0; j < Vcol; j++) {
Line[j * 2] = ResDat[FrameCounter][i][j];
Line[j * 2 + 1] = ResDat[FrameCounter][i][j];
}
FramePrinter->setText(LineXY, Line, (fp_color::f_black | fp_color::b_l_white), Vcol * 2);
}
for (UINT i = 0; i < Vline; i++) {
delete[] ResDat[FrameCounter][i];
}
delete[] ResDat[FrameCounter];
delete[] Line;
}
int main(int argc, char * * argv) {
if (argc == 1) {
printf("请将视频信息文件拖放至此可执行文件以进行播放,谢谢!");
getchar();
return 0;
}
PlayerInit(argv[1]);
t.StartTimer(Interval, std::bind(FrameEvent));
getchar();
return 0;
}
#endif