Charles,

Thank you.  After I got objects from my code in a compatible datatype it 
worked on the first go.

I will study your code later in an effort to learn what I did not 
understand.

Jonathon

On Friday, March 20, 2020 at 3:40:56 PM UTC-4, Charles M wrote:
>
> Hey Jonathon, I tried to explain it to you earlier (math), but I think you 
> still have a fundamental misunderstanding of how blending and blit_into 
> works. Blending is combining pixels using the blending formula.  That 
> formula determines the output of the resulting pixel. Normally this is done 
> automatically in the framebuffer of drawn images, but you aren't doing; 
> that you have two separate sets of data. With blitting you are writing your 
> data to the texture. Writing data twice doesn't mean they combine 
> automatically, even if they are different data sets.
>
> Example: If you take two text files A and B, and output a file [not in 
> append] and write file A to the same output, then write the file B to the 
> same output. It doesn't become an intertwined mix of A and B, it just 
> becomes B, since you are REPLACING the data. They don't just combine on 
> their own in the data. This is the same as what you are doing, you are just 
> replacing the data in those spots. It doesn't matter about alpha or colors; 
> it's all data and you are replacing it. You need to COMBINE the data using 
> the formula I linked you.
>
> It's a slow day for me, but I made you an example using two images of the 
> same size:
>
>
> import pyglet
> import ctypes
>
>
> image_one = pyglet.image.load("one.png")
> image_two = pyglet.image.load("two.png")
>
>
> window = pyglet.window.Window()
>
>
> def blend_color(color_src, color_dst, src_factor, dst_factor):
>     # Blending Formula = source * src_factor + dst * dst_factor. Make 
> sure they are int on return
>     return int((color_src * src_factor + color_dst * dst_factor) * 255)
>     
> def blend_alpha(alpha_src, alpha_dst, src_factor, dst_factor):
>     # Blend alpha, no source factor, can change if you want.
>     return int((alpha_src + alpha_dst * dst_factor) * 255)
>
>
> def combine_pixels(source, dst):
>     """ source and dst are pixels of RGBA
>     This demonstarates formula for glBlendFunc(GL_SRC_ALPHA, 
> GL_ONE_MINUS_SRC_ALPHA)
>     """
>     converted_src = [px / 255 for px in source] # From bytes to values in 
> relation to 1.
>     converted_dst = [px / 255 for px in dst]
>
>
>     # blend mode
>     src_factor = converted_src[3]  # Source Factor: Source alpha. AKA 
> GL_SRC_ALPHA
>     dst_factor = (1 - converted_src[3])  # Dst Factor: 1 minus source 
> alpha AKA GL_ONE_MINUS_SRC_ALPHA
>     
>     r = blend_color(converted_src[0], converted_dst[0], src_factor, 
> dst_factor)
>     g = blend_color(converted_src[1], converted_dst[1], src_factor, 
> dst_factor)
>     b = blend_color(converted_src[2], converted_dst[2], src_factor, 
> dst_factor)
>     a = blend_alpha(converted_src[3], converted_dst[3], src_factor, 
> dst_factor)
>          
>     return r, g, b, a
>
>
>     
> # Turn both images in a list of RGBA int values.
> pixel_data_one = list(image_one.get_image_data().get_data('RGBA', 
> image_one.width * 4))
> pixel_data_two = list(image_two.get_image_data().get_data('RGBA', 
> image_two.width * 4))
>
>
> combined_image = []
> for i in range(0, len(pixel_data_one), 4):
>     pixel_one = pixel_data_one[i:i + 4]
>     pixel_two = pixel_data_two[i:i + 4]
>     
>     # Pixel_two (source) on top of pixel_one (destination)   
>     combined_image.extend(combine_pixels(pixel_one, pixel_two))
>
> data = (ctypes.c_ubyte * len(pixel_data_one))(*combined_image)
> new_image = pyglet.image.ImageData(image_one.width, image_two.height, 
> "RGBA", data)
>
>
> new_image.save("combined_image.png")
>
>
> # draw new image.
> sprite = pyglet.sprite.Sprite(new_image, x=0, y=0)
>
>
> @window.event
> def on_draw():
>     window.clear()
>     
>     sprite.draw()
>
> pyglet.app.run()
>     
>
>
>
>
> On Thursday, March 19, 2020 at 11:18:32 AM UTC-5, Jonathon Parker wrote:
>>
>> So there is no solution for this?  All I want to do is:
>>
>> # imgA, imgB are identically sized instances of pyglet.image.ImageData in 
>> RGBA format
>>
>> imgC = imgA + imgB
>>
>> # imgC is now imgA with imgB superimposed on top of it. Transparent 
>> pixels in imgB allow *opaque* pixels in imgA to be visible
>>
>> I never thought something so simple would be so hard!
>>
>> On Wednesday, March 11, 2020 at 3:34:15 PM UTC-4, Jonathon Parker wrote:
>>>
>>> A minimal example
>>>
>>> # rawdata1 and rawdata2 are a list of c_ubytes
>>>
>>> my_texture_region = pyglet.image.ImageData(x, y, 'RGB', 
>>> rawdata1).get_texture()
>>> my_image = pyglet.image.ImageData(x, y, 'RGB', rawdata2)
>>> my_texture_region.blit_into(my_image, 0, 0, 0)
>>>
>>> When I create a sprite using my_texture region, I only see my_image.  
>>> What I want is my_image on top of my_texture_region.
>>>
>>> What am I doing wrong?
>>>
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pyglet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyglet-users/31e5e28f-64bb-4b77-b045-1914db2ec3c5%40googlegroups.com.

Reply via email to