Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: ChatGPT's solution (was: Re: Create email tracking image)

by Anonymous Monk
on Mar 23, 2023 at 16:19 UTC ( [id://11151162]=note: print w/replies, xml ) Need Help??


in reply to Re: ChatGPT's solution (was: Re: Create email tracking image)
in thread Create email tracking image

Thanks for noticing that the confident robot was wrong. I have some questions.

Your first example works and makes sense to me because the transparent method is called with an allocated color and this causes it to return the index of the transparent color: 0 instead of -1.

You second example works but since the transparent method is not called transparent continues to return -1 which according to the doc means transparency is disabled, so how is it transparent?

The answer from gpt looks like it should work like your first example works: the allocated alpha "color" of 127 (transparent) is passed to the transparent method, but instead of returning 0 or -1 or something else, the transparent method now returns 2130706432 which is... two gigabytes?

#!/usr/bin/perl use strict; use warnings; use GD; # First working example my $image = GD::Image->new(1, 1); my $transparent = $image->colorAllocate(0, 0, 0); warn $image->transparent; $image->transparent($transparent); warn $image->transparent; # Second working example $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); warn $image->transparent; $image->setPixel(0, 0, $transparent); warn $image->transparent; # Broken ChatGPT example $image = GD::Image->newTrueColor(1, 1); $image->alphaBlending(0); $image->saveAlpha(1); $transparent = $image->colorAllocateAlpha(0, 0, 0, 127); warn $image->transparent; $image->transparent($transparent); warn $image->transparent;
Output:
-1 at line 9.
0 at line 11.
-1 at line 18.
-1 at line 20.
-1 at line 27.
2130706432 at line 29.

Replies are listed 'Best First'.
Re^3: ChatGPT's solution (was: Re: Create email tracking image)
by Anonymous Monk on Mar 23, 2023 at 23:30 UTC

    To add to confusion -- even shorter code to generate transparent PNG (but not GIF) would be

    my $image = GD::Image->new(1, 1); $image->colorAllocateAlpha(0, 0, 0, 127); # output follows

    (return value ignored; no call to bot's beloved transparent)

    I am not an expert. For palette-based images, the colorAllocate (colorAllocateAlpha) would return palette entry index just added; -1 for failure (palette full?). For "TrueColor" images, these methods don't add anything anywhere (perhaps not quite so... I'll elaborate later if time's left). They are convenient shortcuts for pack/unpack as I see it:

    unpack 'V', pack 'C4', 0, 0, 0, 127

    which is... two gigabytes :) ? Because painting methods (such as setPixel) expect just an integer for color, be it palette index or 32-bit integer, where appropriate.

    GD creates images with zeroes-filled canvas. For palette-based images, it means that 1st palette entry (non-existent at the time i.e. yet to be added) would define "background". PNG palette can be thought of as "RGBA", though it's indirectly so (irrelevant detail): axillary tRNS chunk controls entries' partial transparency; it doesn't have to cover all palette; degenerate case would be binary GIF transparency; hence no real necessity to call transparent even if palette-based.

    0 for alpha usually designates full transparency. GD encodes as opposite, internally (not in PNG output, of course). 0 of newly created "TrueColor" canvas means "opaque". The '127' is fully transparent. And by the way, it follows that opening "TrueColor" semitransparent PNG and immediately saving leads to loss of information. I only vaguely remember the reason why 7-bit depth alpha-channel limit exists in GD. The "TrueColor" support is later addition on top of existing data-structures; backward compatibility compromise or something similar.

    To directly address another your question, 0-filled (i.e opaque) "TrueColor" canvas just has to be painted (e.g. setPixel) with transparent color (i.e. 32-bit integer with non-zero byte at big(? confused myself at this late time)-end byte) to introduce transparency. The transparent call would not change anything here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151162]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found