The PNG Guide is an eBook based on Greg Roelofs' book, originally published by O'Reilly. |
![]() |
Home ![]() ![]() |
|
![]() ![]() ![]() ![]() ![]() ![]() |
|
Compositing and Displaying the Imagehmax = (bgscale-1)/2; /* half the max weight of a color */ max = 2*hmax; /* the max weight of a color */ for (row = 0; row < rpng2_info.height; ++row) { yidx = row % bgscale; if (yidx > hmax) yidx = bgscale-1 - yidx; dest = bg_data + row*bg_rowbytes; for (i = 0; i < rpng2_info.width; ++i) { xidx = i % bgscale; if (xidx > hmax) xidx = bgscale-1 - xidx; k = xidx + yidx; *dest++ = (k*r1 + (max-k)*r2) / max; *dest++ = (k*g1 + (max-k)*g2) / max; *dest++ = (k*b1 + (max-k)*b2) / max; } } With this approach, the inner display loop requires only a tiny change to support the background image instead of just a background color: r = *src++; g = *src++; b = *src++; a = *src++; if (bg_image) { /* NEW */ bg_red = *src2++; /* NEW */ bg_green = *src2++; /* NEW */ bg_blue = *src2++; /* NEW */ } /* NEW */ if (a == 255) { red = r; green = g; blue = b; } else if (a == 0) { red = bg_red; green = bg_green; blue = bg_blue; } else { /* this macro (copied from png.h) composites * the foreground and background values and * puts the result into the first argument */ alpha_composite(red, r, a, bg_red); alpha_composite(green, g, a, bg_green); alpha_composite(blue, b, a, bg_blue); } In other words, the background color used for compositing is now changed
once per pixel. (Note that the src2 pointer is initialized just
once per call. That's the only other change to the display routine to
support the background image.) The cases in which the alpha component is either
255 or 0 are handled separately for performance reasons only; using the
The results, using one of the more exotic radial-wave patterns as the background, are shown in Figure C-1 in the color insert. The base image consists of partially transparent icicles hanging from opaque tree branches, seen against a completely transparent sky. The figure is a composite of the appearance after the first PNG pass (left half) and the final pass (right half).
|
|
Home ![]() ![]() |