Created
March 20, 2013 01:20
-
-
Save mkosler/5201600 to your computer and use it in GitHub Desktop.
Bad downsampling! Bad!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Canvas::downsample(float *pixels, unsigned side) | |
{ | |
unsigned nside = side / 2; | |
for (unsigned i = 0; i < nside * nside; i++) { | |
unsigned row = i / nside; | |
unsigned x = (i * 2) + (row * side); | |
unsigned y = x + 1; | |
unsigned z = x + side; | |
unsigned w = z + 1; | |
unsigned count = 0; | |
if (!isBlack(pixels[(3 * x)], pixels[(3 * x) + 1], pixels[(3 * x) + 2])) { | |
count++; | |
if (x != 0) { | |
pixels[(3 * i)] += pixels[(3 * x)]; | |
pixels[(3 * i) + 1] += pixels[(3 * x) + 1]; | |
pixels[(3 * i) + 2] += pixels[(3 * x) + 2]; | |
} | |
} | |
if (!isBlack(pixels[(3 * y)], pixels[(3 * y) + 1], pixels[(3 * y) + 2])) { | |
count++; | |
pixels[(3 * i)] += pixels[(3 * y)]; | |
pixels[(3 * i) + 1] += pixels[(3 * y) + 1]; | |
pixels[(3 * i) + 2] += pixels[(3 * y) + 2]; | |
} | |
if (!isBlack(pixels[(3 * z)], pixels[(3 * z) + 1], pixels[(3 * z) + 2])) { | |
count++; | |
pixels[(3 * i)] += pixels[(3 * z)]; | |
pixels[(3 * i) + 1] += pixels[(3 * z) + 1]; | |
pixels[(3 * i) + 2] += pixels[(3 * z) + 2]; | |
} | |
if (!isBlack(pixels[(3 * w)], pixels[(3 * w) + 1], pixels[(3 * w) + 2])) { | |
count++; | |
pixels[(3 * i)] += pixels[(3 * w)]; | |
pixels[(3 * i) + 1] += pixels[(3 * w) + 1]; | |
pixels[(3 * i) + 2] += pixels[(3 * w) + 2]; | |
} | |
if (count > 0) { | |
pixels[(3 * i)] /= count; | |
pixels[(3 * i) + 1] /= count; | |
pixels[(3 * i) + 2] /= count; | |
} | |
} | |
} | |
void Canvas::draw() | |
{ | |
for (size_t i = 0; i < _curves.size(); i++) { | |
if (_arePointsVisible) { | |
_curves[i]->drawControlPoints(); | |
} | |
_curves[i]->drawCurve(); | |
} | |
glFlush(); | |
if (_isFinalized) { | |
_isFinalized = false; | |
for (unsigned size = _width; size > 2; size /= 2) { | |
std::ostringstream oss; | |
oss << "test_" << size << ".bmp"; | |
screenshot(oss.str(), size, size); | |
float *pixels = new float[size * size * 3]; | |
glReadPixels(0, 0, size, size, GL_RGB, GL_FLOAT, pixels); | |
downsample(pixels, size); | |
glDrawPixels(size / 2, size / 2, GL_RGB, GL_FLOAT, pixels); | |
glFlush(); | |
delete[] pixels; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment