forked from emilk/sproxel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SproxelProject.cpp
205 lines (172 loc) · 5.19 KB
/
SproxelProject.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <QImage>
#include "SproxelProject.h"
static SproxelColor colorFromHSV(float h, float s, float v)
{
if (s == 0) {
return SproxelColor(v, v, v, 1);
}
float part = h / 60.f;
int i = floor(part);
float fraction = part - (float)i;
float c0 = v * (1 - s );
float c1 = v * (1 - s * fraction);
float c2 = v * (1 - s * (1-fraction));
if (i == 0) {
return SproxelColor(v, c2, c0, 1);
} else if (i == 1) {
return SproxelColor(c1, v, c0, 1);
} else if (i == 2) {
return SproxelColor(c0, v, c2, 1);
} else if (i == 3) {
return SproxelColor(c0, c1, v, 1);
} else if (i == 4) {
return SproxelColor(c2, c0, v, 1);
} else {
return SproxelColor(v, c0, c1, 1);
}
}
SproxelProject::SproxelProject()
{
mainPalette=new ColorPalette();
mainPalette->resize(256);
mainPalette->setName("main");
for (int y=0; y<15; ++y)
{
float sat = 1.f - (y / 16.f)*0.5f;
float value = 1.f - (y / 16.f);
for (int x=0; x<16; ++x)
{
float hue = x * 22.5f;
mainPalette->setColor(y*16+x, colorFromHSV(hue, sat, value));
}
}
for (int x=0; x<16; ++x)
{
float value = x / 16.f;
mainPalette->setColor(240+x, colorFromHSV(0, 0, value));
}
palettes.push_back(mainPalette);
}
VoxelGridLayerPtr VoxelGridLayer::fromQImage(QImage readMe, ColorPalettePtr pal)
{
QString tempStr;
tempStr = readMe.text("VoxelGridDimX");
int sizeX = tempStr.toInt();
tempStr = readMe.text("VoxelGridDimY");
int sizeY = tempStr.toInt();
tempStr = readMe.text("VoxelGridDimZ");
int sizeZ = tempStr.toInt();
if (sizeX == 0 || sizeY == 0 || sizeZ == 0) return VoxelGridLayerPtr();
readMe = readMe.mirrored();
VoxelGridLayerPtr layer(new VoxelGridLayer());
bool indexed=false;
if (readMe.colorCount()>0)
{
indexed=true;
layer->setPalette(pal);
}
layer->resize(Imath::Box3i(Imath::V3i(0), Imath::V3i(sizeX, sizeY, sizeZ)-Imath::V3i(1)));
for (int slice = 0; slice < sizeZ; slice++)
{
const int sliceOffset = slice * sizeX;
for (int y = 0; y < sizeY; y++)
{
for (int x = 0; x < sizeX; x++)
{
int index=-1;
if (indexed) index=readMe.pixelIndex(x+sliceOffset, y);
QRgb pixelValue = readMe.pixel(x+sliceOffset, y);
Imath::Color4f color(
(float)qRed (pixelValue) / 255.0f,
(float)qGreen(pixelValue) / 255.0f,
(float)qBlue (pixelValue) / 255.0f,
(float)qAlpha(pixelValue) / 255.0f);
layer->set(Imath::V3i(x, y, slice), color, index);
}
}
}
return layer;
}
QImage VoxelGridLayer::makeQImage() const
{
const Imath::Box3i dim=bounds();
const Imath::V3i cellDim = dim.size()+Imath::V3i(1);
// TODO: Offer other options besides XY slices? Directionality? Ordering?
const int height = cellDim.y;
const int width = cellDim.x * cellDim.z;
QImage writeMe(QSize(width, height), m_ind?QImage::Format_Indexed8:QImage::Format_ARGB32);
if (m_ind)
{
if (m_palette)
{
writeMe.setColorCount(m_palette->numColors());
for (int i=0; i<m_palette->numColors(); ++i)
{
SproxelColor c=m_palette->color(i)*255.0f;
writeMe.setColor(i, qRgba(int(c.r), int(c.g), int(c.b), int(c.a)));
}
}
for (int slice = 0; slice < cellDim.z; slice++)
{
const int sliceOffset = slice * cellDim.x;
for (int y = 0; y < cellDim.y; y++)
{
for (int x = 0; x < cellDim.x; x++)
writeMe.setPixel(x+sliceOffset, y, getInd(Imath::V3i(x, y, slice)+dim.min));
}
}
}
else
{
for (int slice = 0; slice < cellDim.z; slice++)
{
const int sliceOffset = slice * cellDim.x;
for (int y = 0; y < cellDim.y; y++)
{
for (int x = 0; x < cellDim.x; x++)
{
const Imath::Color4f colorScaled = getColor(Imath::V3i(x, y, slice)+dim.min) * 255.0f;
writeMe.setPixel(x+sliceOffset, y, qRgba(
(int)colorScaled.r,
(int)colorScaled.g,
(int)colorScaled.b,
(int)colorScaled.a));
}
}
}
}
writeMe = writeMe.mirrored(); // QT Bug: mirrored() doesn't preserve text.
QString tempStr;
writeMe.setText("SproxelFileVersion", "1");
writeMe.setText("VoxelGridDimX", tempStr.setNum(cellDim.x));
writeMe.setText("VoxelGridDimY", tempStr.setNum(cellDim.y));
writeMe.setText("VoxelGridDimZ", tempStr.setNum(cellDim.z));
return writeMe;
}
//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//
VoxelGridLayerPtr VoxelGridGroup::bakeLayers() const
{
VoxelGridLayerPtr grid(new VoxelGridLayer());
ColorPalettePtr palette;
bool hasRgb=false, hasMultiPal=false;
foreach (VoxelGridLayerPtr layer, m_layers)
if (layer->palette())
{
if (!palette) palette=layer->palette();
else if (layer->palette()!=palette) hasMultiPal=true;
}
else
hasRgb=true;
if (hasRgb || hasMultiPal) palette=NULL;
grid->setPalette(palette);
Imath::Box3i ext=bounds();
grid->resize(ext);
for (int z=ext.min.z; z<=ext.max.z; ++z)
for (int y=ext.min.y; y<=ext.max.y; ++y)
for (int x=ext.min.x; x<=ext.max.x; ++x)
{
Imath::V3i at(x, y, z);
grid->set(at, get(at), getInd(at));
}
return grid;
}