马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个图片是百度来的,有没有大神能编写一个程序出来跟这个图片差不多
- #include "ximage.h"
- #include <iostream>
- #include <string>
- static void HideCursor()
- {
- CONSOLE_CURSOR_INFO cursor_info = {100, false};
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
- }
- static void GotoXY(SHORT x, SHORT y)
- {
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x, y});
- }
- // 字符与RGB的对应的映射关系
- static char RgbToChar(uint8_t r, uint8_t g, uint8_t b)
- {
- static const std::string ascii_char = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,"^`'. ";
- int gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b);
- double unit = (256.0 + 1) / ascii_char.size();
- return ascii_char[int(gray / unit)];
- }
- class AsciiGif
- {
- public:
- AsciiGif()
- {
- this->image = nullptr;
- }
- ~AsciiGif()
- {
- delete this->image;
- }
- bool Load(const std::string &filename)
- {
- if(this->image)
- delete this->image;
- this->image = new CxImage;
- this->image->SetRetreiveAllFrames(true);
- this->image->Load(filename.c_str(), CXIMAGE_FORMAT_GIF);
- if(!this->image->IsValid())
- return false;
- return true;
- }
- bool Scale(double w_scale, double h_scale)
- {
- if(!this->image)
- return false;
- for(int32_t i = 0; i < this->image->GetNumFrames(); ++i)
- {
- CxImage *frame = this->image->GetFrame(i);
- frame->Resample(int32_t(frame->GetWidth() * w_scale), int32_t(frame->GetHeight() * h_scale));
- }
- return true;
- }
- std::string GetFrame(int32_t index) const
- {
- if(!this->image)
- return std::string();
- if(index >= this->image->GetNumFrames())
- return std::string();
- CxImage *frame = this->image->GetFrame(index);
- uint32_t width = frame->GetWidth();
- uint32_t height = frame->GetHeight();
- std::string buf;
- for(int32_t h = height - 1; h >= 0; --h)
- {
- for(uint32_t w = 0; w < width; ++w)
- {
- RGBQUAD color = frame->GetPixelColor(w, h);
- buf += RgbToChar(color.rgbRed, color.rgbGreen, color.rgbBlue);
- }
- buf += '\n';
- }
- return buf;
- }
- int32_t GetNumFrames() const
- {
- return this->image->GetNumFrames();
- }
- uint32_t GetFrameDelay() const
- {
- return this->image->GetFrameDelay();
- }
- private:
- CxImage *image;
- };
- int main()
- {
- AsciiGif gif;
- gif.Load("src\\1.gif");
- gif.Scale(0.5 * 1.4, 0.25 * 1.4);
-
- HideCursor(); // CxImage内部设置过显示光标,在这里覆盖这个设置
- while(1)
- {
- for(int32_t i = 0; i < gif.GetNumFrames(); ++i)
- {
- std::string frame = gif.GetFrame(i);
- puts(frame.c_str()); // std::cout太慢了,这里用puts
- Sleep(gif.GetFrameDelay() * 10);
- GotoXY(0, 0);
- }
- }
- return 0;
- }
复制代码
完整源码
链接: https://pan.baidu.com/s/1Hkv1bl010J8Yb_5r1CMQyQ
提取码:ys82
|