[AD] improvements on alpha blending...

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


hi, i've been thinking
why not speed-up allegro some alpha blending modes?

this would be as follows

supposing the image has more than 20000 pixels (200 * 100 pixels for
example)
we could speed up things making on each draw call a table of 65kb (65536
bytes) having the average of each two numbers ranging from 0 to 255 given
the asked opacity
since for an image (bigger than 20000 pixels, say 320x200 = 64000 * 3
components (RGB) = calculations, that is 192000 calculations, where as with
this method we would only do 65536 calculations (no matter how big the image
is)

that is a table of

unsinged char average[256][256];

that way we shouldn't have to calculate for each pixel

we should only have to do
(where opacity is the given opacity from 0 to 255)
for (int src = 0; src < 256; src++) {
  for (int dst = 0; dst < 256; dst++) {
    average[src][dst] = (src * (255 / opacity)) + (dst * (255 / (255 -
opacity)));
  }
}

where that could be optimized (and getting ride of the divide by 0 errors)
to

if (opacity == 0) {
  for (int src = 0; src < 256; src++) {
    for (int dst = 0; dst < 256; dst++) {
      average[src][dst] = dst;
    }
  }
}
else if (opacity == 255) {
  for (int src = 0; src < 256; src++) {
    for (int dst = 0; dst < 256; dst++) {
      average[src][dst] = src;
    }
  }
}
else {
  float srcmul, dstmul;
  srcmul = 255 /opacity;
  dstmul = 255 / (255 - opacity);
  for (int src = 0; src < 256; src++) {
    for (int dst = 0; dst < 256; dst++) {
      average[src][dst] = src * srcmul + dst * dstmul;
    }
  }
}

and then with that table, say we wanna calculate the blending of
pixels 255, 30, 10 and 20, 30, 150
we sould only do
r = average[255][20];
g= average[30][30];
b= average[10][150];

giving an awesome speedup to big images
(even a 250% on 320x200 images and even more on bigger images)

then when the drawing is over, deallocate this buffer
what do you think



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/