From 8ff3640960d38ba953b48e44d81d4bb532822200 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 27 May 2009 05:04:23 +0000 Subject: [PATCH] Increase img2twit's output contrast when rendering the final image. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3528 92316355-f0b4-4df1-b90c-862c8a59935f --- examples/img2twit.cpp | 52 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/img2twit.cpp b/examples/img2twit.cpp index fccfaaf..ffda4fc 100644 --- a/examples/img2twit.cpp +++ b/examples/img2twit.cpp @@ -465,7 +465,7 @@ static inline void set_point(int index, float x, float y, float r, } static inline void get_point(int index, float *x, float *y, float *r, - float *g, float *b, float *s) + float *g, float *b, float *s, bool final = false) { uint32_t pt = points[index]; @@ -480,9 +480,18 @@ static inline void get_point(int index, float *x, float *y, float *r, *x = (fx + dx) * RANGE_X /*+ 0.5 * (index & 1)*/; *y = (fy + dy) * RANGE_Y /*+ 0.5 * (index & 1)*/; - *b = int2midrange(pt % RANGE_R, RANGE_R); pt /= RANGE_R; - *g = int2midrange(pt % RANGE_G, RANGE_G); pt /= RANGE_G; - *r = int2midrange(pt % RANGE_B, RANGE_B); pt /= RANGE_B; + if(final) + { + *b = int2fullrange(pt % RANGE_R, RANGE_R); pt /= RANGE_R; + *g = int2fullrange(pt % RANGE_G, RANGE_G); pt /= RANGE_G; + *r = int2fullrange(pt % RANGE_B, RANGE_B); pt /= RANGE_B; + } + else + { + *b = int2midrange(pt % RANGE_R, RANGE_R); pt /= RANGE_R; + *g = int2midrange(pt % RANGE_G, RANGE_G); pt /= RANGE_G; + *r = int2midrange(pt % RANGE_B, RANGE_B); pt /= RANGE_B; + } } static inline float clip(float x, int modulo) @@ -625,7 +634,8 @@ static uint32_t apply_op(uint8_t op, uint32_t val) } } -static void render(pipi_image_t *dst, int rx, int ry, int rw, int rh) +static void render(pipi_image_t *dst, + int rx, int ry, int rw, int rh, bool final) { int lookup[dw * RANGE_X * 2 * dh * RANGE_Y * 2]; pipi_pixels_t *p = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); @@ -644,16 +654,17 @@ static void render(pipi_image_t *dst, int rx, int ry, int rw, int rh) } /* Add fake points to close the triangulation */ - dt.insert(K::Point_2(-p->w, -p->h)); - dt.insert(K::Point_2(2 * p->w, -p->h)); - dt.insert(K::Point_2(-p->w, 2 * p->h)); - dt.insert(K::Point_2(2 * p->w, 2 * p->h)); + dt.insert(K::Point_2(-dw * RANGE_X, -dh * RANGE_Y)); + dt.insert(K::Point_2(2 * dw * RANGE_X, -dh * RANGE_Y)); + dt.insert(K::Point_2(-dw * RANGE_X, 2 * dh * RANGE_Y)); + dt.insert(K::Point_2(2 * dw * RANGE_X, 2 * dh * RANGE_Y)); for(y = ry; y < ry + rh; y++) { for(x = rx; x < rx + rw; x++) { - K::Point_2 m(x, y); + K::Point_2 m((float)x * dw * RANGE_X / p->w, + (float)y * dh * RANGE_Y / p->h); Point_coordinate_vector coords; CGAL::Triple< std::back_insert_iterator, @@ -671,13 +682,14 @@ static void render(pipi_image_t *dst, int rx, int ry, int rw, int rh) fx = (*it).first.x(); fy = (*it).first.y(); - if(fx < 0 || fy < 0 || fx > p->w - 1 || fy > p->h - 1) + if(fx < 0 || fy < 0 + || fx > dw * RANGE_X - 1 || fy > dh * RANGE_Y - 1) continue; int index = lookup[(int)(fx * 2) + dw * RANGE_X * 2 * (int)(fy * 2)]; - get_point(index, &fx, &fy, &fr, &fg, &fb, &fs); + get_point(index, &fx, &fy, &fr, &fg, &fb, &fs, final); //float k = pow((*it).second * (1.0 + fs), 1.2); float k = (*it).second * (1.00f + fs); @@ -1054,7 +1066,7 @@ int main(int argc, char *argv[]) /* Render what we just computed */ tmp = pipi_new(dw * RANGE_X, dh * RANGE_Y); - render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y); + render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y, false); error = pipi_measure_rmsd(src, tmp); if(DEBUG_MODE) @@ -1113,7 +1125,7 @@ int main(int argc, char *argv[]) points[pt] = candidates[i]; render(scrap, zonex * RANGE_X, zoney * RANGE_Y, - zonew * RANGE_X, zoneh * RANGE_Y); + zonew * RANGE_X, zoneh * RANGE_Y, false); double newerr = pipi_measure_rmsd(src, scrap); if(newerr < besterr) @@ -1130,9 +1142,9 @@ int main(int argc, char *argv[]) { points[pt] = candidates[bestop]; /* Redraw image if the last check wasn't the best one */ - if(bestop != 2) + if(bestop != 0) render(scrap, zonex * RANGE_X, zoney * RANGE_Y, - zonew * RANGE_X, zoneh * RANGE_Y); + zonew * RANGE_X, zoneh * RANGE_Y, false); pipi_free(tmp); tmp = scrap; @@ -1218,12 +1230,8 @@ int main(int argc, char *argv[]) npoints = dw * dh * POINTS_PER_CELL; /* Render these points to a new image */ - tmp = pipi_new(dw * RANGE_X, dh * RANGE_Y); - render(tmp, 0, 0, dw * RANGE_X, dh * RANGE_Y); - - /* TODO: render directly to the final image; scaling sucks */ - dst = pipi_resize(tmp, width, height); - pipi_free(tmp); + dst = pipi_new(width, height); + render(dst, 0, 0, width, height, true); /* Save image and bail out */ pipi_save(dst, dstname);