JustPaste.it

A critical distinction in 2D and 3D computer graphics pipelines is how an image format stores RGB color values in the presence of an opacity channel (Alpha). The Portable Network Graphics (ISO/IEC 15948) specification mandates in Section 6.2 that all PNG alpha channels are stored as unassociated (straight / unpremultiplied) alpha. That means a semi-transparent pixel with pure red chrominance at 50% opacity is encoded on disk as (R=255, G=0, B=0, A=128), rather than premultiplied (R=128, G=0, B=0, A=128).

While straight alpha preserves full 8-bit chrominance precision inside semi-transparent anti-aliased contours—which is essential when designers download and re-composite transparent raster cutouts from TopPNG—it introduces subtle mathematical pitfalls during GPU bilinear texture filtering, mipmap downsampling, and DEFLATE scanline compression if zero-alpha pixels are not properly sanitized.

1. Porter-Duff Source-Over Compositing in Straight vs. Associated Alpha

Under Thomas Porter and Tom Duff's 1984 Compositing Digital Images algebra, placing a foreground pixel (C_fg, alpha_fg) over a background pixel (C_bg, alpha_bg) in straight-alpha space requires dividing by the composite output alpha alpha_out = alpha_fg + alpha_bg * (1 - alpha_fg):

export function compositeStraightAlphaOver(fgR, fgG, fgB, fgA, bgR, bgG, bgB, bgA) {
  const aF = fgA / 255;
  const aB = bgA / 255;
  const outA = aF + aB * (1 - aF);
  if (outA === 0) return [0, 0, 0, 0];

  const outR = Math.round(((fgR * aF + bgR * aB * (1 - aF)) / outA));
  const outG = Math.round(((fgG * aF + bgG * aB * (1 - aF)) / outA));
  const outB = Math.round(((fgB * aF + bgB * aB * (1 - aF)) / outB));
  return [outR, outG, outB, Math.round(outA * 255)];
}

2. Why Bilinear Interpolation on Straight RGBA Causes Dark or Colored Edge Fringing

Suppose a pixel at coordinate x = 10 lies on the visible edge of a white icon (255, 255, 255, 255), and its immediate neighbor at x = 11 is completely transparent (0, 0, 0, 0). If a GPU sampler or Canvas 2D transformation interpolates halfway between x = 10 and x = 11 in straight RGBA space without premultiplying first, it averages the channels independently to (127, 127, 127, 127)—a 50% opaque dark gray pixel instead of a 50% opaque pure white pixel! Converting straight PNG scanlines to premultiplied RGBA prior to bilinear resampling eliminates this halo artifact completely:

export function premultiplyRgbaScanline(rgba) {
  const out = new Uint8Array(rgba.length);
  for (let i = 0; i < rgba.length; i += 4) {
    const a = rgba[i + 3];
    if (a === 0) {
      out[i] = 0;
      out[i + 1] = 0;
      out[i + 2] = 0;
      out[i + 3] = 0;
    } else if (a === 255) {
      out[i] = rgba[i];
      out[i + 1] = rgba[i + 1];
      out[i + 2] = rgba[i + 2];
      out[i + 3] = 255;
    } else {
      // Integer rounding division by 255: (c * a + 127) / 255
      out[i] = ((rgba[i] * a + 127) / 255) | 0;
      out[i + 1] = ((rgba[i + 1] * a + 127) / 255) | 0;
      out[i + 2] = ((rgba[i + 2] * a + 127) / 255) | 0;
      out[i + 3] = a;
    }
  }
  return out;
}

3. Reconciling Edge Bleed Padding with Zero-Alpha DEFLATE Compression

In lossless PNG encoding, interior background regions where Alpha === 0 should have their RGB channels zeroed to (0, 0, 0) so the ISO/IEC 15948 Paeth and Sub scanline predictors emit long runs of zero residuals. At the 1-pixel outer boundary immediately adjacent to non-zero alpha pixels (1 <= Alpha <= 255), GPU texture pipelines either switch to premultiplied texture upload (gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true)) or dilate edge RGB by 1 pixel while zeroing all remaining exterior transparent regions.


About the Author: Graphic Systems & Raster Compression Engineering Notes — Open-source research into ISO/IEC 15948 PNG encoding, alpha-channel compositing mathematics, and lossless scanline filtering.