private final int blend_color(int p1, int p2) {
      int a2 = (p2 >>> 24);

      if (a2 == 0xff) {
        // full replacement
        return p2;

      } else {
        int a1 = a2 ^ 0xff;
        int r = (a1 * ((p1 >> 16) & 0xff) + a2 * ((p2 >> 16) & 0xff)) & 0xff00;
        int g = (a1 * ((p1 >>  8) & 0xff) + a2 * ((p2 >>  8) & 0xff)) & 0xff00;
        int b = (a1 * ( p1        & 0xff) + a2 * ( p2        & 0xff)) >> 8;

        return 0xff000000 | (r << 8) | g | b;
      }
    }