Thursday, August 7, 2014

Masking - Take 2

Ok, I found a solution. My solution is to type-cast the masked layer's alpha array to contain values of type <type 'numpy.uint16'> instead of <type 'numpy.uint8'>, which allows me to multiply the values of the two alpha arrays without worrying about modulation since 255*255 (the largest value this could possibly produce) isn't bigger than nor equal to 2^16. I then divide the resulting product-array by the value 255, and then finally assign it to the alpha values in the masked layer.

        masked_alpha = pygame.surfarray.pixels_alpha(self.output_layer)
        mask_alpha = pygame.surfarray.pixels_alpha(self.mask_layer)

        new_masked_alpha = numpy.uint16(masked_alpha)
        new_masked_alpha *= mask_alpha
        new_masked_alpha /= 255
        masked_alpha *= 0
        masked_alpha += new_masked_alpha

        del new_masked_alpha
        del masked_alpha
        del mask_alpha


The result is beautiful.


The only complaint that I have about using this method is that since I have to type-cast the one array, the values have to be copied over instead of referenced, which can be SLOW. Even so, I'm very happy with this result since it performs perfectly and is relatively fast.

No comments:

Post a Comment