1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int m, n, p, q, u, v;
unsigned char arr[2000][2000][3];
unsigned char arrAvg[2000][2000][3];
char strs[1000];
char chColor[] = "\x1b[48;2;%d;%d;%dm";
char rstColor[] = "\x1b[0m";
int transNum1(char c) {
if ('0' <= c && c <= '9')return c - '0';
else if ('a' <= c && c <= 'f')return c - 'a' + 10;
else if ('A' <= c && c <= 'F')return c - 'A' + 10;
else return -1;
}
int transNum2(char c1, char c2) {
return 16 * transNum1(c1) + transNum1(c2);
}
void input() {
scanf("%d %d %d %d", &m, &n, &p, &q);
u = m / p;
v = n / q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf(" #");
scanf("%s", strs);
int len = strlen(strs);
int result = 0;
if (len == 1) {
int low4 = transNum1(strs[0]);
arr[i][j][0] = arr[i][j][1] = arr[i][j][2] = low4 * 0x11;
}
else if (len == 3) {
int low4 = transNum1(strs[0]);
int low8 = transNum1(strs[1]);
int low12 = transNum1(strs[2]);
arr[i][j][0] = low4 * 0x11;
arr[i][j][1] = low8 * 0x11;
arr[i][j][2] = low12 * 0x11;
}
else {
arr[i][j][0] = transNum2(strs[0], strs[1]);
arr[i][j][1] = transNum2(strs[2], strs[3]);
arr[i][j][2] = transNum2(strs[4], strs[5]);
}
}
}
}
inline void calculateAvgSub(int blockX, int blockY) {
unsigned long long x = 0, y = 0, z = 0;
for (int i = blockX * q; i < (blockX + 1) * q; i++) {
for (int j = blockY * p; j < (blockY + 1) * p; j++) {
x += arr[i][j][0];
y += arr[i][j][1];
z += arr[i][j][2];
}
}
arrAvg[blockX][blockY][0] = x / q / p;
arrAvg[blockX][blockY][1] = y / q / p;
arrAvg[blockX][blockY][2] = z / q / p;
}
void calculateAvg() {
for (int i = 0; i < v; i++) {
for (int j = 0; j < u; j++) {
calculateAvgSub(i, j);
}
}
}
inline void printChar(int ch) {
printf("\\x%02X", ch);
}
inline void printStr(const char* s) {
while (*s) {
printChar(*s);
s++;
}
}
inline void setColor(unsigned char* color1, unsigned char* color2) {
color1[0] = color2[0];
color1[1] = color2[1];
color1[2] = color2[2];
}
inline bool cmpColor(unsigned char* color1, unsigned char* color2) {
return color1[0] == color2[0] && color1[1] == color2[1] && color1[2] == color2[2];
}
void output() {
unsigned char blackColor[3] = { 0, 0, 0 };
unsigned char color[3] = { 0, 0, 0 };
for (int i = 0; i < v; i++) {
for (int j = 0; j < u; j++) {
unsigned char* curColor = arrAvg[i][j];
if (!cmpColor(color, curColor)) {
if (cmpColor(blackColor, curColor))
sprintf(strs, rstColor);
else
sprintf(strs, chColor, curColor[0], curColor[1], curColor[2]);
setColor(color, curColor);
printStr(strs);
}
printChar(' ');
}
if (!cmpColor(color, blackColor)) {
setColor(color, blackColor);
printStr(rstColor);
}
printChar('\n');
}
}
int main() {
input();
calculateAvg();
output();
} |