CSP 字符画

#include <iostream>

using namespace std;

const int M = 2000, N = 1200;

struct Color24 {
    unsigned char R, G, B;

    Color24() {
        R = 0;
        G = 0;
        B = 0;
    }

    Color24(int r, int g, int b) : R((unsigned char) r), G((unsigned char) g), B((unsigned char) b) {}

    bool operator==(const Color24 &rhs) const {
        return R == rhs.R &&
               G == rhs.G &&
               B == rhs.B;
    }

    bool operator!=(const Color24 &rhs) const {
        return !(rhs == *this);
    }

    void SetColor(string htmlColor) {
        switch (htmlColor.size()) {
            case 7:  /// #010203
                sscanf(("0x" + htmlColor.substr(1, 2)).c_str(), "%x", &R);
                sscanf(("0x" + htmlColor.substr(3, 2)).c_str(), "%x", &G);
                sscanf(("0x" + htmlColor.substr(5, 2)).c_str(), "%x", &B);
                break;
            case 4:  /// #123
                sscanf(("0x" + string(2, htmlColor[1])).c_str(), "%x", &R);
                sscanf(("0x" + string(2, htmlColor[2])).c_str(), "%x", &G);
                sscanf(("0x" + string(2, htmlColor[3])).c_str(), "%x", &B);
                break;
            case 2:  /// #1
                sscanf(("0x" + string(2, htmlColor[1])).c_str(), "%x", &R);
                sscanf(("0x" + string(2, htmlColor[1])).c_str(), "%x", &G);
                sscanf(("0x" + string(2, htmlColor[1])).c_str(), "%x", &B);
                break;
            default:
                cout << "ERROR!!!Color convert: unexpect html type" << endl;
        }
    }
} Image[M * N];

string color2Format(Color24 color) {
    char formatString[100];
    sprintf(formatString, "\033[48;2;%d;%d;%dm", color.R, color.G, color.B);
    return formatString;
}


int w, h;
int pixel_w, pixel_h;

Color24 getMeanColor(int row, int col) {
    int R = 0, G = 0, B = 0;
    int count = pixel_w * pixel_h;
    int startX = col * pixel_w, startY = row * pixel_h;
    for (int i = 0; i < pixel_h; ++i) {
        for (int j = 0; j < pixel_w; ++j) {
            R += Image[(i + startY) * w + (j + startX)].R;
            G += Image[(i + startY) * w + (j + startX)].G;
            B += Image[(i + startY) * w + (j + startX)].B;
        }
    }
    return {R / count, G / count, B / count};
}

void convertToASCII(string raw) {
    for (char c: raw) {
        printf("\\x%.2X", c);
    }
}

int main() {
    cin >> w >> h;
    cin >> pixel_w >> pixel_h;
    string colorStr;

    /// input image
    for (int row = 0; row < h; ++row) {
        for (int col = 0; col < w; ++col) {
            cin >> colorStr;
            Image[row * w + col].SetColor(colorStr);
        }
    }
    string finalStr = "";
    /// solve color setting
    Color24 preColor;
    for (int row = 0; row < h / pixel_h; ++row) {
        for (int col = 0; col < w / pixel_w; ++col) {
            Color24 meanColor = getMeanColor(row, col);
            if (meanColor == preColor) {
                finalStr += " ";
            } else {
                if (meanColor == Color24()) {
                    finalStr += "\033[0m ";
                } else {
                    finalStr += color2Format(meanColor) + " ";
                }
                preColor = meanColor;
            }

        }
        if (preColor != Color24())
            finalStr += "\033[0m";
        finalStr += "\n";
        preColor = Color24();
    }
//    printf("%s", finalStr.c_str());

    convertToASCII(finalStr);

    /*printf("\\x%.2X \\x%.2X \\x%.2X", color.R, color.G, color.B);
    char output[10]{};
    itoa((int) color.R, output, 16);*/

    return 0;
}


对象写的,我现在还在debug中,掉了太多的坑了。因为转化的原因

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值