A common use case when you want to use a formula that is more accurate than 257/256 is when you need to combine a lot of alpha values ββfor each pixel. As one example, when reducing the size of the image, you need to combine 4 alpha for each source pixel that contributes to the assignment, and then combine all the original pixels that contribute to the assignment.
I published an infinitely accurate version with a / 255 turntable, but it was rejected for no reason. So I will add that I implement alpha mixing equipment for life, I write real-time graphic code and game engines for life, and I published articles on this subject at conferences such as MICRO, so I really know what I'm saying about. And it can be helpful, or at least entertain people, to understand a more accurate formula that is EXACTLY 1/255:
Version 1: x = (x + (x β 8)) β 8 - the constant is not added, does not satisfy (x * 255) / 255 = x, but in most cases it will look normal. Version 2: x = (x + (x β 8) + 1) β 8 - WILL satisfy (x * 255) / 255 = x for integers, but will not beat the correct integer values ββfor all alpha
Version 3: (simple integer rounding): (x + (x β 8) + 128) β 8 - Will not hit the correct integer values ββfor all alpha, but on average it will be closer than version 2 at the same cost.
Version 4: An infinitely accurate version with any desired level of accuracy for any number of composite alpha: (useful for resizing images, rotation, etc.):
[(x + (x β 8)) β 8] + [((x and 255) + (x β 8)) β 8]
Why is version 4 infinitely accurate? Because 1/255 = 1/256 + 1/65536 + 1/256 ^ 3 + 1/256 ^ 4 + ...
The simplest expression above (version 1) does not handle rounding, but also does not handle hyphens that come from this infinite number of identical columns of the sum. The new term added above defines the execution (0 or 1) of this infinite number of base 256 digits. By adding it, you will get the same result as adding all the endless additions. At this point, you can round up by adding half a bit to any precision point you want.
It is not necessary for the OP, perhaps, but people should know that you do not need to approach at all. The above formula is actually more accurate than a double-precision floating point.
As for speed: in hardware, this method is faster than even one (full). In software, you must consider bandwidth and latency. In latency, it can be faster than narrow multiplication (definitely faster than full-width multiplication), but in the OP context, you can expand many pixels at the same time, and since modern multi-line units are pipelined, you're still fine. In Java translation, you probably don't have narrow breeding, so it can still be faster, but you need to check.
WRT is one person who said, βwhy not use the OSβs built-in capabilities for alpha blinting?β: If that OS already has a significant graphic code base, this might be a great option. If not, you are looking at hundreds and thousands of lines of code to use the OS version - code that is much more difficult to write and debug than this code. And, in the end, the OS code that you have is not portable at all, and this code can be used anywhere.