+ *pad_l = min(*pad_l, pp.padding_l);
+ *pad_r = min(*pad_r, pp.padding_r);
+ *pad_t = min(*pad_t, pp.padding_t);
+ *pad_b = min(*pad_b, pp.padding_b);
+
+ // outline the font (RGBA only)
+ if(bpp == 4 && (pp.outline > 0 || pp.blur > 0 || pp.shadowx != 0 || pp.shadowy != 0 || pp.shadowz != 0)) // we can only do this in BGRA
+ {
+ // this is like mplayer subtitle rendering
+ // bbuffer, bitmap buffer: this is our font
+ // abuffer, alpha buffer: this is pp.buf
+ // tmp: this is pp.buf2
+
+ // create outline buffer
+ memset(pp.buf, 0, pp.bufwidth * pp.bufheight);
+ for(y = -*pad_t; y < h + *pad_b; ++y)
+ for(x = -*pad_l; x < w + *pad_r; ++x)
+ {
+ int x1 = max(-x, -pp.outlinepadding_r);
+ int y1 = max(-y, -pp.outlinepadding_b);
+ int x2 = min(pp.outlinepadding_l, w-1-x);
+ int y2 = min(pp.outlinepadding_t, h-1-y);
+ int mx, my;
+ int cur = 0;
+ int highest = 0;
+ for(my = y1; my <= y2; ++my)
+ for(mx = x1; mx <= x2; ++mx)
+ {
+ cur = pp.circlematrix[POSTPROCESS_MAXRADIUS+my][POSTPROCESS_MAXRADIUS+mx] * (int)imagedata[(x+mx) * bpp + pitch * (y+my) + (bpp - 1)];
+ if(cur > highest)
+ highest = cur;
+ }
+ pp.buf[((x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t))] = (highest + 128) / 255;
+ }
+
+ // blur the outline buffer
+ if(pp.blur > 0 || pp.shadowz != 0)
+ {
+ // horizontal blur
+ for(y = 0; y < pp.bufheight; ++y)
+ for(x = 0; x < pp.bufwidth; ++x)
+ {
+ int x1 = max(-x, -pp.blurpadding_rb);
+ int x2 = min(pp.blurpadding_lt, pp.bufwidth-1-x);
+ int mx;
+ int blurred = 0;
+ for(mx = x1; mx <= x2; ++mx)
+ blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+mx] * (int)pp.buf[(x+mx) + pp.bufpitch * y];
+ pp.buf2[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
+ }
+
+ // vertical blur
+ for(y = 0; y < pp.bufheight; ++y)
+ for(x = 0; x < pp.bufwidth; ++x)
+ {
+ int y1 = max(-y, -pp.blurpadding_rb);
+ int y2 = min(pp.blurpadding_lt, pp.bufheight-1-y);
+ int my;
+ int blurred = 0;
+ for(my = y1; my <= y2; ++my)
+ blurred += pp.gausstable[POSTPROCESS_MAXRADIUS+my] * (int)pp.buf2[x + pp.bufpitch * (y+my)];
+ pp.buf[x + pp.bufpitch * y] = bound(0, blurred, 65025) / 255;
+ }
+ }
+
+ // paste the outline below the font
+ for(y = -*pad_t; y < h + *pad_b; ++y)
+ for(x = -*pad_l; x < w + *pad_r; ++x)
+ {
+ unsigned char outlinealpha = pp.buf[(x + pp.padding_l) + pp.bufpitch * (y + pp.padding_t)];
+ if(outlinealpha > 0)
+ {
+ unsigned char oldalpha = imagedata[x * bpp + pitch * y + (bpp - 1)];
+ // a' = 1 - (1 - a1) (1 - a2)
+ unsigned char newalpha = 255 - ((255 - (int)outlinealpha) * (255 - (int)oldalpha)) / 255; // this is >= oldalpha
+ // c' = (a2 c2 - a1 a2 c1 + a1 c1) / a' = (a2 c2 + a1 (1 - a2) c1) / a'
+ unsigned char oldfactor = (255 * (int)oldalpha) / newalpha;
+ //unsigned char outlinefactor = ((255 - oldalpha) * (int)outlinealpha) / newalpha;
+ int i;
+ for(i = 0; i < bpp-1; ++i)
+ {
+ unsigned char c = imagedata[x * bpp + pitch * y + i];
+ c = (c * (int)oldfactor) / 255 /* + outlinecolor[i] * (int)outlinefactor */;
+ imagedata[x * bpp + pitch * y + i] = c;
+ }
+ imagedata[x * bpp + pitch * y + (bpp - 1)] = newalpha;
+ }
+ //imagedata[x * bpp + pitch * y + (bpp - 1)] |= 0x80;
+ }
+ }
+ }
+ else if(pitch)
+ {
+ // perform operation, not exceeding the passed padding values,
+ // but possibly reducing them
+ *pad_l = min(*pad_l, pp.padding_l);
+ *pad_r = min(*pad_r, pp.padding_r);
+ *pad_t = min(*pad_t, pp.padding_t);
+ *pad_b = min(*pad_b, pp.padding_b);