Re: [GD-DEVEL] Blending images with alpha (patches inside)
[email protected] (Pierre)
| Newsgroups | php.gd.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi Patrick! First all thanks for your work here, it raises a bug in the image copy function when the blending mode is set. I wrote a simple script to show how it should work using gdImageCopyResized (I wrote in PHP, no perl or compiler at hand :): http://pierre.libgd.org/blend.txt and the result image is visible here: http://pierre.libgd.org/b.png As you can see, the blending works as expected (in this example, the red over blue area). However, as part of the 2.1 development, we like to add a more general blending function. It should support different modes (add, sub, or, etc.). It should cover all blending needs. The performance of all image copy and transformation functions can be improved by not using the image getter or setter (it is not the only problem but it already helps a lot). gdImageGet/SetPixel are nice when used by an application but should be avoided in the internal GD function. Cheers, --Pierre On 8/19/07, patrick keshishian <[email protected]> wrote: > Greetings, > > This is my first time posting to this list. Please excuse me if > I miss any standard protocol for contributing patches, etc. > > I had a need to blend two true-colour images. A large set of > photos that need to be watermarked, and one watermark image. > Using GD was an obvious choice for a simple Perl script that > would run through directories of photos and applied the watermark > to each photo. > > gdImageCopy() does a great job for most use-cases. However, > it seems there lacks an API by which I could adjust the > transparency of the watermark ('source') image during "blending" > time. > > Initially, it seemed to me that gdImageCopyMerge() would be the > function I'm interested in, but it doesn't consider the alpha- > channel of the source image. > > I tried writing a wrapper function in my perl program to adjust > the alpha value of each pixel in the source image before calling > gdImageCopy() but there was a great performance hit. > > I created these patches which add a new function to libgd named > gdImageBlend(). It accepts almost identical parameters as > gdImageCopyMerge(), with the last parameter indicating the > opacity of the source image. > > gdImageBlend() only works on true-colour images. If it detects > that either image isn't true-colour or the destination image does > not have alphaBlendingFlag set, it will call gdImageCopyMerge() > and return. > > Note that I don't have any background in digital imaging nor > colour theory. My method may be flawed/not exact, but the results > seem correct, or at least "close enough". > > Speed improvement is considerable. A test case where doing this > alpha channel adjustment in the perl wrapper takes 2.15 seconds > takes only 0.18 seconds with the new GD::Image->blend() method. > > The patches are made against gd-2.0.34 and GD-2.30. If there is a > good chance they would be considered for adoption, I am willing > to make them against the latest sources. Though, the patches are > pretty trivial. > > I'm not subscribed to your list, feel free to Cc me on replies. > > Best regards, > --patrick > > -- > GD Devel Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >