Skip to content

Commit 6adc5a9

Browse files
author
Zahl
committed
option to load biome colors from external pre-rendered png files
1 parent 7c4471d commit 6adc5a9

20 files changed

+4156
-53
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CFLAGS=-O2 -c -Wall -fomit-frame-pointer -pedantic -DWITHPNG -I/usr/local/includ
44
LDFLAGS=-O2 -lz -lpng -fomit-frame-pointer -L/usr/local/lib
55
DCFLAGS=-g -O0 -c -Wall -D_DEBUG -DWITHPNG -I/usr/local/include
66
DLDFLAGS=-g -O0 -lz -lpng -L/usr/local/lib
7-
SOURCES=main.cpp helper.cpp nbt.cpp draw.cpp colors.cpp worldloader.cpp filesystem.cpp globals.cpp draw_png.cpp extractcolors.cpp
7+
SOURCES=main.cpp helper.cpp nbt.cpp draw.cpp colors.cpp worldloader.cpp filesystem.cpp globals.cpp draw_png.cpp extractcolors.cpp pngreader.cpp
88
OBJECTS=$(SOURCES:.cpp=.default.o)
99
OBJECTS_TURBO=$(SOURCES:.cpp=.turbo.o)
1010
DOBJECTS=$(SOURCES:.cpp=.debug.o)

colors.cpp

+283-1
Large diffs are not rendered by default.

colors.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#include <cmath>
66

77
#define GETBRIGHTNESS(c) (uint8_t)sqrt( \
8-
double(2[c] * 2[c]) * .236 + \
9-
double(1[c] * 1[c]) * .601 + \
10-
double(0[c] * 0[c]) * .163)
8+
double(2[c] * 2[c]) * .236 + \
9+
double(1[c] * 1[c]) * .601 + \
10+
double(0[c] * 0[c]) * .163)
1111

1212
// Byte order see below. Colors aligned to word boundaries for some speedup
1313
// Brightness is precalculated to speed up calculations later
@@ -26,9 +26,10 @@ extern uint8_t colors[256][16];
2626
#define PALPHA 11
2727

2828
void loadColors();
29-
bool loadColorsFromFile(const char* file);
30-
bool dumpColorsToFile(const char* file);
31-
bool extractColors(const char* file);
29+
bool loadColorsFromFile(const char *file);
30+
bool dumpColorsToFile(const char *file);
31+
bool extractColors(const char *file);
32+
bool loadBiomeColors(const char* path, int fromXZ);
3233

3334
#define AIR 0
3435
#define STONE 1

draw.cpp

+542-1
Large diffs are not rendered by default.

draw.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
#include "helper.h"
55

6-
bool createImageBmp(FILE* fh, size_t width, size_t height, bool splitUp);
7-
bool saveImageBmp(FILE* fh);
8-
bool loadImagePartBmp(FILE* fh, int startx, int starty, int width, int height);
6+
bool createImageBmp(FILE *fh, size_t width, size_t height, bool splitUp);
7+
bool saveImageBmp(FILE *fh);
8+
bool loadImagePartBmp(FILE *fh, int startx, int starty, int width, int height);
99
void setPixelBmp(size_t x, size_t y, uint8_t color, float fsub);
1010
void blendPixelBmp(size_t x, size_t y, uint8_t color, float fsub);
11-
bool saveImagePartBmp(FILE* fh);
11+
bool saveImagePartBmp(FILE *fh);
1212
uint64_t calcImageSizeBmp(int mapChunksX, int mapChunksZ, size_t mapHeight, int &pixelsX, int &pixelsY, bool tight = false);
1313

1414
#endif

draw_png.cpp

+799-1
Large diffs are not rendered by default.

draw_png.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
#include "helper.h"
55

6-
bool createImagePng(FILE* fh, size_t width, size_t height, bool splitUp);
7-
bool saveImagePng(FILE* fh);
8-
bool loadImagePartPng(FILE* fh, int startx, int starty, int width, int height);
6+
bool createImagePng(FILE *fh, size_t width, size_t height, bool splitUp);
7+
bool saveImagePng(FILE *fh);
8+
bool loadImagePartPng(FILE *fh, int startx, int starty, int width, int height);
99
void setPixelPng(size_t x, size_t y, uint8_t color, float fsub);
1010
void blendPixelPng(size_t x, size_t y, uint8_t color, float fsub);
11-
bool saveImagePartPng(FILE* fh);
11+
bool saveImagePartPng(FILE *fh);
1212
bool composeFinalImagePng();
1313
uint64_t calcImageSizePng(int mapChunksX, int mapChunksZ, size_t mapHeight, int &pixelsX, int &pixelsY, bool tight = false);
1414

extractcolors.cpp

+177-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,177 @@
1-
/* * extractcolors.h * MCMap Live * * Created by DK on 10/18/10. * Modified for mcmap by Zahl * */#include "extractcolors.h"#include "colors.h"// This beast of an array maps block IDs to tile locations in terrain.png// The tile x and tile y are 0-index. A value of -1,-1 means no tile exists// Extra alpha multiplier is there for textures that are shared and might need to// be lighter for one use than another.// { tile x, tile y, extra alpha multiplier)static const int special_sauce[92][3] = { { -1, -1, 10 }, { 1, 0, 10 }, { 0, 0, 10 }, { 2, 0, 10 }, { 0, 1, 10 }, { 4, 0, 10 }, { 15, 0, 10 }, { 1, 1, 10 }, { 15, 12, 5 }, { 15, 12, 5 }, { 15, 14, 10 }, { 15, 14, 10 }, { 2, 1, 10 }, { 3, 1, 10 }, { 0, 2, 10 }, { 1, 2, 10 }, { 2, 2, 10 }, { 5, 1, 7 }, { 4, 3, 10 }, { 0, 3, 10 }, { 1, 3, 8 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { -1, -1, 10 }, { 0, 4, 10 }, { -1, -1, 10 }, { 13, 0, 10 }, { 12, 0, 10 }, { 13, 1, 10 }, { 12, 1, 10 }, { 7, 2, 10 }, { 6, 2, 10 }, { 5, 0, 10 }, { 5, 0, 10 }, { 7, 0, 10 }, { 8, 0, 10 }, { 3, 2, 10 }, { 4, 2, 10 }, { 5, 2, 10 }, { 0, 5, 30 }, { 15, 15, 3 }, { 1, 4, 10 }, { 4, 0, 10 }, { 11, 1, 10 }, { 4, 6, 10 }, { 2, 3, 10 }, { 8, 2, 10 }, { 12, 3, 10 }, { 15, 5, 10 }, { 7, 5, 10 }, { 12, 2, 10 }, { 13, 3, 10 }, { 0, 0, 10 }, { 1, 6, 10 }, { 3, 5, 10 }, { 0, 8, 10 }, { 0, 1, 10 }, { 4, 0, 10 }, { 3, 6, 10 }, { 0, 6, 10 }, { 2, 6, 10 }, { 4, 0, 10 }, { 3, 3, 10 }, { 3, 3, 10 }, { 3, 7, 10 }, { 3, 6, 10 }, { 2, 6, 1 }, { 2, 4, 10 }, { 3, 4, 10 }, { 2, 4, 10 }, { 6, 4, 10 }, { 8, 4, 10 }, { 9, 4, 10 }, { 10, 4, 10 }, { 5, 1, 9 }, { 6, 7, 10 }, { 7, 6, 10 }, { 8, 6, 10 }, { 9, 6, 10 }, { 14, 0, 5 }, { 8, 7, 10 }};bool getTileRGBA(uint8_t *textures, int tilesize, int sauce_index, int &r, int &g, int &b, int &a, int &noise){ r = 0; g = 0; b = 0; a = 0; noise = 0; const int x = special_sauce[sauce_index][0]; const int y = special_sauce[sauce_index][1]; if (x == -1) { return false; } int n = tilesize * tilesize; int bytesperrow = 16 * tilesize * 4; int sx = x * tilesize * 4; int sy = y * tilesize; for (int j = sy; j < (sy + tilesize); j++) { for (int i = sx; i < (sx + tilesize * 4); i = i + 4) { // If the pixel is entirely transparent if (textures[i+3+j* (bytesperrow) ] == 0) { n--; } else { r = r + textures[i+j* (bytesperrow) ]; g = g + textures[i+1+j* (bytesperrow) ]; b = b + textures[i+2+j* (bytesperrow) ]; a = a + textures[i+3+j* (bytesperrow) ]; } } } double var = 0; if (n == 0) { return false; } r = r / n; g = g / n; b = b / n; a = a / (tilesize * tilesize); // This should divide by the total number of pixels to account for invisible ones imo a = int(float(a) * (float(special_sauce[sauce_index][2]) / 10.0f)); for (int j = sy; j < (sy + tilesize); j++) { for (int i = sx; i < (sx + tilesize * 4); i = i + 4) { // If the pixel is not entirely transparent if (textures[i+3+j* (bytesperrow) ] != 0) { var = var + (pow (textures[i+j* (bytesperrow) ] - r, 2) + pow (textures[i+1+j* (bytesperrow) ] - b, 2) + pow (textures[i+2+j* (bytesperrow) ] - g, 2)) / (3 * n); } } } noise = int (8 * var / ( (n * n - 1) / 12)); if (noise > 255) { noise = 255; } if (a > 255) { a = 255; } return true;}
1+
/*
2+
* extractcolors.h
3+
* MCMap Live
4+
*
5+
* Created by DK on 10/18/10.
6+
* Modified for mcmap by Zahl
7+
*
8+
*/
9+
10+
#include "extractcolors.h"
11+
#include "colors.h"
12+
13+
// This beast of an array maps block IDs to tile locations in terrain.png
14+
// The tile x and tile y are 0-index. A value of -1,-1 means no tile exists
15+
// Extra alpha multiplier is there for textures that are shared and might need to
16+
// be lighter for one use than another.
17+
// { tile x, tile y, extra alpha multiplier)
18+
static const int special_sauce[92][3] = {
19+
{ -1, -1, 10 },
20+
{ 1, 0, 10 },
21+
{ 0, 0, 10 },
22+
{ 2, 0, 10 },
23+
{ 0, 1, 10 },
24+
{ 4, 0, 10 },
25+
{ 15, 0, 10 },
26+
{ 1, 1, 10 },
27+
{ 15, 12, 5 },
28+
{ 15, 12, 5 },
29+
{ 15, 14, 10 },
30+
{ 15, 14, 10 },
31+
{ 2, 1, 10 },
32+
{ 3, 1, 10 },
33+
{ 0, 2, 10 },
34+
{ 1, 2, 10 },
35+
{ 2, 2, 10 },
36+
{ 5, 1, 7 },
37+
{ 4, 3, 10 },
38+
{ 0, 3, 10 },
39+
{ 1, 3, 5 },
40+
{ -1, -1, 10 },
41+
{ -1, -1, 10 },
42+
{ -1, -1, 10 },
43+
{ -1, -1, 10 },
44+
{ -1, -1, 10 },
45+
{ -1, -1, 10 },
46+
{ -1, -1, 10 },
47+
{ -1, -1, 10 },
48+
{ -1, -1, 10 },
49+
{ -1, -1, 10 },
50+
{ -1, -1, 10 },
51+
{ -1, -1, 10 },
52+
{ -1, -1, 10 },
53+
{ -1, -1, 10 },
54+
{ 0, 4, 10 },
55+
{ -1, -1, 10 },
56+
{ 13, 0, 10 },
57+
{ 12, 0, 10 },
58+
{ 13, 1, 10 },
59+
{ 12, 1, 10 },
60+
{ 7, 2, 10 },
61+
{ 6, 2, 10 },
62+
{ 5, 0, 10 },
63+
{ 5, 0, 10 },
64+
{ 7, 0, 10 },
65+
{ 8, 0, 10 },
66+
{ 3, 2, 10 },
67+
{ 4, 2, 10 },
68+
{ 5, 2, 10 },
69+
{ 0, 5, 30 },
70+
{ 15, 15, 3 },
71+
{ 1, 4, 10 },
72+
{ 4, 0, 10 },
73+
{ 11, 1, 10 },
74+
{ 4, 6, 10 },
75+
{ 2, 3, 10 },
76+
{ 8, 2, 10 },
77+
{ 12, 3, 10 },
78+
{ 15, 5, 10 },
79+
{ 7, 5, 10 },
80+
{ 12, 2, 10 },
81+
{ 13, 3, 10 },
82+
{ 0, 0, 10 },
83+
{ 1, 6, 10 },
84+
{ 3, 5, 10 },
85+
{ 0, 8, 10 },
86+
{ 0, 1, 10 },
87+
{ 4, 0, 10 },
88+
{ 3, 6, 10 },
89+
{ 0, 6, 10 },
90+
{ 2, 6, 10 },
91+
{ 4, 0, 10 },
92+
{ 3, 3, 10 },
93+
{ 3, 3, 10 },
94+
{ 3, 7, 10 },
95+
{ 3, 6, 10 },
96+
{ 2, 6, 1 },
97+
{ 2, 4, 10 },
98+
{ 3, 4, 10 },
99+
{ 2, 4, 10 },
100+
{ 6, 4, 10 },
101+
{ 8, 4, 10 },
102+
{ 9, 4, 10 },
103+
{ 10, 4, 10 },
104+
{ 5, 1, 9 },
105+
{ 6, 7, 10 },
106+
{ 7, 6, 10 },
107+
{ 8, 6, 10 },
108+
{ 9, 6, 10 },
109+
{ 14, 0, 5 },
110+
{ 8, 7, 10 }
111+
};
112+
113+
bool getTileRGBA(uint8_t *textures, int tilesize, int sauce_index, int &r, int &g, int &b, int &a, int &noise)
114+
{
115+
r = 0;
116+
g = 0;
117+
b = 0;
118+
a = 0;
119+
noise = 0;
120+
121+
const int x = special_sauce[sauce_index][0];
122+
const int y = special_sauce[sauce_index][1];
123+
124+
if (x == -1) {
125+
return false;
126+
}
127+
128+
int n = tilesize * tilesize;
129+
int bytesperrow = 16 * tilesize * 4;
130+
131+
int sx = x * tilesize * 4;
132+
int sy = y * tilesize;
133+
134+
for (int j = sy; j < (sy + tilesize); j++) {
135+
for (int i = sx; i < (sx + tilesize * 4); i = i + 4) {
136+
// If the pixel is entirely transparent
137+
if (textures[i+3+j* (bytesperrow) ] == 0) {
138+
n--;
139+
} else {
140+
r = r + textures[i+j* (bytesperrow) ];
141+
g = g + textures[i+1+j* (bytesperrow) ];
142+
b = b + textures[i+2+j* (bytesperrow) ];
143+
a = a + textures[i+3+j* (bytesperrow) ];
144+
}
145+
146+
}
147+
}
148+
149+
double var = 0;
150+
151+
if (n == 0) {
152+
return false;
153+
}
154+
r = r / n;
155+
g = g / n;
156+
b = b / n;
157+
a = a / n; // Changed my mind again
158+
a = int(float(a) * (float(special_sauce[sauce_index][2]) / 10.0f));
159+
160+
for (int j = sy; j < (sy + tilesize); j++) {
161+
for (int i = sx; i < (sx + tilesize * 4); i = i + 4) {
162+
// If the pixel is not entirely transparent
163+
if (textures[i+3+j* (bytesperrow) ] != 0) {
164+
var = var + (pow(textures[i+j * (bytesperrow)] - r, 2.0) + pow(textures[i+1+j * (bytesperrow)] - b, 2.0) + pow(textures[i+2+j * (bytesperrow)] - g, 2.0)) / (3.0 * n);
165+
}
166+
}
167+
}
168+
169+
noise = int (8 * var / ( (n * n - 1) / 12));
170+
if (noise > 255) {
171+
noise = 255;
172+
}
173+
if (a > 255) {
174+
a = 255;
175+
}
176+
return true;
177+
}

0 commit comments

Comments
 (0)