Pygame and blitting: white on white = gray?

I use pygame (1.9.0rc3, although this also happens in 1.8.1) to create a plugin. To build a heatmap, I use a small 24-bit PNG image with a resolution of 11x11 pixels with a white background and a gray dot with very low opacity, which stops exactly at the edges:

Bitmap http://img442.imageshack.us/img442/465/dot.png

The area around the dot is perfect white, #ffffff, as it should be. However, when I use pygame to split the image several times onto a new surface using BLEND_MULT, a gray square appears, as if the dotted background was not perfect white, which makes no sense.

The following code, plus the included images, can reproduce this:

import os
import numpy
import pygame

os.environ['SDL_VIDEODRIVER'] = 'dummy'
pygame.display.init()
pygame.display.set_mode((1,1), 0, 32)

dot_image = pygame.image.load('dot.png').convert_alpha()

surf = pygame.Surface((100, 100), 0, 32)
surf.fill((255, 255, 255))
surf = surf.convert_alpha()

for i in range(50):
    surf.blit(dot_image, (20, 40), None, pygame.BLEND_MULT)    

for i in range(100):
    surf.blit(dot_image, (60, 40), None, pygame.BLEND_MULT)      

pygame.image.save(surf, 'result.png')

When you run the code, you will get the following image:

http://img263.imageshack.us/img263/4568/result.png

, ? ?

+5
2

, , , , 100% . 255 1 - . pygame, , surface.h:

#define BLEND_MULT(sR, sG, sB, sA, dR, dG, dB, dA) \
    dR = (dR && sR) ? (dR * sR) >> 8 : 0;          \
    dG = (dG && sG) ? (dG * sG) >> 8 : 0;          \
    dB = (dB && sB) ? (dB * sB) >> 8 : 0;

Pygame

new_val = old_dest * old_source / 256

, ,

new_val = old_dest * old_source / 255

, , - - , . 255 / 256 , , , - " ": , , - - , , .

, :

  • , - .
  • 1 . , , .
  • , 255 * 255 == 255 ( , ), ORing 1 .

, 1, , , C Python.

+6

, , balpha, "" ( ) .

(s * d) >> 8

(s * d) / 255 

alphablit.c ( surface.h). , , (heatmap) .

+1

All Articles