More Pixel Art: Run Length Encoding
by sonone, May 14, 2021, 10:04 PM
I have made a new pixel art function using Run Length Encoding (I more used the Run Length without the encoding
). This is the source code: Click for reveal hidden code
The first input is the width of your drawing. The second is the string coding your picture (explained below). The third is either true or false, giving you the option to show a palette. This is optional, with the default being no palette. The last inputs are your color palette, the first being a, the second b, and so on (you can go as far as z).
For the actual coding, you put the number of pixels you want, followed by the letter corresponding to a color on your palette. Like the previous versions, a dash (-) is an invisible mark.
Example
![[asy]
//Made by sonone
int parse_digit(string n) {
if (n == "0")
return 0;
else if (n == "1")
return 1;
else if (n == "2")
return 2;
else if (n == "3")
return 3;
else if (n == "4")
return 4;
else if (n == "5")
return 5;
else if (n == "6")
return 6;
else if (n == "7")
return 7;
else if (n == "8")
return 8;
else if (n == "9")
return 9;
else
return -1;
}
bool is_int(string idk) {
bool yes = true;
string n;
for (int i = 0; i < length(idk); ++i) {
n = substr(idk, i, 1);
if (parse_digit(n) == -1) {
yes = false;
break;
}
}
return yes;
}
int parse_int(string n) {
if (!is_int(n)) {
return -1;
}
else {
int result = 0;
for (int i = 0; i < length(n); ++i) {
result += parse_digit(substr(n,i,1))*10^(length(n) - i - 1);
}
return result;
}
}
void draw_RLE_string(int width, string px, bool palette = false...pen[] colors) { // converts a rle string into a pixel drawing with specified demensions and color palette
string letters = "abcdefghijklmnopqrstuvwxyz-";
int x = 0, y = 0;
string bit;
int curr_int;
int num_digits = 0;
string color_letter = "a";
for (int i = 0; i < length(px); ++i) {
bit = substr(px, i, 1);
if (is_int(bit) && num_digits == 0) {
curr_int = parse_int(substr(px, i, 1));
++num_digits;
continue;;
}
else if (is_int(bit)) {
curr_int = parse_int(substr(px, i - num_digits, num_digits + 1));
++num_digits;
continue;
}
else if (find(letters,bit) == -1) {
continue;
}
else {
num_digits = 0;
color_letter = substr(px, i, 1);
}
for (int i = 0; i < curr_int; ++i) {
if (x >= width) {
x = 0;
y -= 1;
}
if (color_letter == "-")
fill(shift(x,y)*unitsquare, invisible);
else
fill(shift(x,y)*unitsquare, colors[find(letters, color_letter)]);
++x;
}
}
if (palette) {
for (int i = 0; i < colors.length; ++i) {
label(scale(.7)*substr(letters,i,1), (i,2),2*N);
filldraw(shift(i-.5,1.5)*scale(.8)*unitsquare, colors[i],linewidth(.3));
}
}
}
draw_RLE_string(12,"
3-5a6-9a3-3d2b1d1b4-
1d1b1d3b1d3b2-1d1b2d
3b1d3b1-2d4b4d4-7b4-
2c1a3c5-3c1a2c1a3c1-
4c4a4c2b1c1a1e2a1e1a
1c5b6a5b8a2b2-3a2-3a
3-3c4-3c1-4c4-4c
", springgreen,olive+brown,brown,black, yellow);
[/asy]](//latex.artofproblemsolving.com/c/9/f/c9ffb2e52d28d31aea5cf573e286dfeb6bc61f17.png)

//Made by sonone int parse_digit(string n) { if (n == "0") return 0; else if (n == "1") return 1; else if (n == "2") return 2; else if (n == "3") return 3; else if (n == "4") return 4; else if (n == "5") return 5; else if (n == "6") return 6; else if (n == "7") return 7; else if (n == "8") return 8; else if (n == "9") return 9; else return -1; } bool is_int(string idk) { bool yes = true; string n; for (int i = 0; i < length(idk); ++i) { n = substr(idk, i, 1); if (parse_digit(n) == -1) { yes = false; break; } } return yes; } int parse_int(string n) { if (!is_int(n)) { return -1; } else { int result = 0; for (int i = 0; i < length(n); ++i) { result += parse_digit(substr(n,i,1))*10^(length(n) - i - 1); } return result; } } void draw_RLE_string(int width, string px, bool palette = false...pen[] colors) { // converts a rle string into a pixel drawing with specified demensions and color palette string letters = "abcdefghijklmnopqrstuvwxyz-"; int x = 0, y = 0; string bit; int curr_int; int num_digits = 0; string color_letter = "a"; for (int i = 0; i < length(px); ++i) { bit = substr(px, i, 1); if (is_int(bit) && num_digits == 0) { curr_int = parse_int(substr(px, i, 1)); ++num_digits; continue;; } else if (is_int(bit)) { curr_int = parse_int(substr(px, i - num_digits, num_digits + 1)); ++num_digits; continue; } else if (find(letters,bit) == -1) { continue; } else { num_digits = 0; color_letter = substr(px, i, 1); } for (int i = 0; i < curr_int; ++i) { if (x >= width) { x = 0; y -= 1; } if (color_letter == "-") fill(shift(x,y)*unitsquare, invisible); else fill(shift(x,y)*unitsquare, colors[find(letters, color_letter)]); ++x; } } if (palette) { for (int i = 0; i < colors.length; ++i) { label(scale(.7)*substr(letters,i,1), (i,2),2*N); filldraw(shift(i-.5,1.5)*scale(.8)*unitsquare, colors[i],linewidth(.3)); } } }
The first input is the width of your drawing. The second is the string coding your picture (explained below). The third is either true or false, giving you the option to show a palette. This is optional, with the default being no palette. The last inputs are your color palette, the first being a, the second b, and so on (you can go as far as z).
For the actual coding, you put the number of pixels you want, followed by the letter corresponding to a color on your palette. Like the previous versions, a dash (-) is an invisible mark.
Example
![[asy]
//Made by sonone
int parse_digit(string n) {
if (n == "0")
return 0;
else if (n == "1")
return 1;
else if (n == "2")
return 2;
else if (n == "3")
return 3;
else if (n == "4")
return 4;
else if (n == "5")
return 5;
else if (n == "6")
return 6;
else if (n == "7")
return 7;
else if (n == "8")
return 8;
else if (n == "9")
return 9;
else
return -1;
}
bool is_int(string idk) {
bool yes = true;
string n;
for (int i = 0; i < length(idk); ++i) {
n = substr(idk, i, 1);
if (parse_digit(n) == -1) {
yes = false;
break;
}
}
return yes;
}
int parse_int(string n) {
if (!is_int(n)) {
return -1;
}
else {
int result = 0;
for (int i = 0; i < length(n); ++i) {
result += parse_digit(substr(n,i,1))*10^(length(n) - i - 1);
}
return result;
}
}
void draw_RLE_string(int width, string px, bool palette = false...pen[] colors) { // converts a rle string into a pixel drawing with specified demensions and color palette
string letters = "abcdefghijklmnopqrstuvwxyz-";
int x = 0, y = 0;
string bit;
int curr_int;
int num_digits = 0;
string color_letter = "a";
for (int i = 0; i < length(px); ++i) {
bit = substr(px, i, 1);
if (is_int(bit) && num_digits == 0) {
curr_int = parse_int(substr(px, i, 1));
++num_digits;
continue;;
}
else if (is_int(bit)) {
curr_int = parse_int(substr(px, i - num_digits, num_digits + 1));
++num_digits;
continue;
}
else if (find(letters,bit) == -1) {
continue;
}
else {
num_digits = 0;
color_letter = substr(px, i, 1);
}
for (int i = 0; i < curr_int; ++i) {
if (x >= width) {
x = 0;
y -= 1;
}
if (color_letter == "-")
fill(shift(x,y)*unitsquare, invisible);
else
fill(shift(x,y)*unitsquare, colors[find(letters, color_letter)]);
++x;
}
}
if (palette) {
for (int i = 0; i < colors.length; ++i) {
label(scale(.7)*substr(letters,i,1), (i,2),2*N);
filldraw(shift(i-.5,1.5)*scale(.8)*unitsquare, colors[i],linewidth(.3));
}
}
}
draw_RLE_string(12,"
3-5a6-9a3-3d2b1d1b4-
1d1b1d3b1d3b2-1d1b2d
3b1d3b1-2d4b4d4-7b4-
2c1a3c5-3c1a2c1a3c1-
4c4a4c2b1c1a1e2a1e1a
1c5b6a5b8a2b2-3a2-3a
3-3c4-3c1-4c4-4c
", springgreen,olive+brown,brown,black, yellow);
[/asy]](http://latex.artofproblemsolving.com/c/9/f/c9ffb2e52d28d31aea5cf573e286dfeb6bc61f17.png)
This post has been edited 3 times. Last edited by sonone, May 14, 2021, 11:28 PM