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.cpp
2025-11-09 14:10:48 +08:00

387 lines
8.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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 "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>һ<EFBFBD>£<EFBFBD>";
// }
// 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 "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>һ<EFBFBD>£<EFBFBD>";
// }
// 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 "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>һ<EFBFBD>£<EFBFBD>";
// }
// 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 "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1>һ<EFBFBD>£<EFBFBD>";
// }
// 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;
//}
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, CntFin;
static unsigned Interval = 0;
static char FramePath[50] = { 0 };
static unsigned FrontPathLen = 0;
static COORD LineXY;
static BYTE * * ResDat;
static FastPrinter * FramePrinter;
Timer t;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
void PlayerInit(const char * FilePath) {
std::ifstream inf(FilePath);
inf >> Vcol >> Vline;
inf >> CntLen >> CntFin;
inf >> Interval;
inf >> FramePath;
inf.close();
FrameLoader = new PicReader;
FramePrinter = new FastPrinter(Vcol * 2, Vline, 8);
while (FramePath[++FrontPathLen]);
FramePath[FrontPathLen] = ' ';
FramePath[FrontPathLen + CntLen + 1] = '.';
FramePath[FrontPathLen + CntLen + 2] = 'j';
FramePath[FrontPathLen + CntLen + 3] = 'p';
FramePath[FrontPathLen + CntLen + 4] = 'g';
LineXY.X = 0;
FramePrinter->cleanSrceen();
}
void PlayFinish() {
delete FrameLoader;
delete FramePrinter;
}
// <20><>Ⱦ<EFBFBD><C8BE>ǰ֡<C7B0><D6A1>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>һ֡
void FrameEvent() {
static unsigned FrameCounter = 0;
FramePrinter->draw(true);
FrameCounter++;
if (FrameCounter >= CntFin) {
t.Expire();
PlayFinish();
return;
}
int TmpCnt = FrameCounter;
for (int i = CntLen; i > 0; i--) {
FramePath[FrontPathLen + i] = '0' + TmpCnt % 10;
TmpCnt /= 10;
}
FrameLoader->readPic(FramePath);
FrameLoader->testReader(FrameData, Vcol, Vline);
Array pic(FrameData, Vline, Vcol, 4);
delete[] FrameData;
ResDat = Grey2BYTE(RGB2Grey(pic), Vline, Vcol);
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[i][j];
Line[j * 2 + 1] = ResDat[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[i];
}
delete[] ResDat;
delete[] Line;
}
int main(int argc, char * * argv) {
if (argc == 1) {
printf("<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>Ϣ<EFBFBD>ļ<EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD>ţ<EFBFBD>лл<EFBFBD><EFBFBD>");
getchar();
return 0;
}
PlayerInit(argv[1]);
t.StartTimer(Interval, std::bind(FrameEvent));
getchar();
t.Expire();
PlayFinish();
return 0;
}
#endif