Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: GD colorAllocate not changing colour

by afoken (Canon)
on May 16, 2021 at 16:53 UTC ( #11132666=note: print w/replies, xml ) Need Help??


in reply to Re^2: GD colorAllocate not changing colour
in thread GD colorAllocate not changing colour

As if by magic, the nice grey wall has turned into a black wall.

What image formats do you use? I see you are writing a PNG, that can be both a paletted image and a non-paletted image. What formats do you read?

(On palettes: A paletted image has a small array of colors, usually 24 bit RGB colors, and usually not more than 256 color entries. Each pixel is stored as an array index, typically one byte. A non-paletted image stores the color information directly in the pixel data, using 3 or 4 bytes per pixel.)

"Magic" color changes usually happen with paletted images, especially if the tool is too simple-minded, or when you run out of palette entries. What happens is that the palette is silently modified or the color allocation fails because the palette array is full. Try to use non-paletted images, i.e. make sure GD is in "true color" mode. Call isTrueColor() on your GD objects, should return true. Pass a true value for the optional truecolor parameter to the GD constructors. That way, paletted images are automatically upgraded to non-paletted mode (as far as I understand GD.)

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^4: GD colorAllocate not changing colour
by Discipulus (Abbot) on May 16, 2021 at 17:10 UTC
    > Pass a true value for the optional truecolor parameter to the GD constructors..

    ..or simply force truecolor with the class method

    use GD 2.0.33; GD::Image->trueColor(1);

    Infact:

    > For backwards compatibility with scripts previous versions of GD, new images created from scratch (width, height) are palette based by default.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^4: GD colorAllocate not changing colour
by Bod (Curate) on May 16, 2021 at 17:06 UTC
    What image formats do you use? I see you are writing a PNG, that can be both a paletted image and a non-paletted image. What formats do you read?

    I don't know why the output is PNG. As I said in the original question, this is old code that is being refactored. As the images are all real pictures of properties and the like, it would be much better if they were JPGs but that means changing lots of other code where they are read. I will be changing all the PNG files to JPG files, probably manually, and then updating the code that reads the files then revisiting this code that writes them to ensure in future everythings is JPG. But that's for another time.

    The input file is a stream from a website upload. The format could be any image format although practically it is likely to be a JPG as most of them will have originated from a digital camera which mostly produce JPG files.

    Call isTrueColor() on your GD objects, should return true

    A call of $image->isTrueColor() immediately before writing to the file does indeed return true.

      Please note that once a file is loaded inside a GD object, is no more a jpg nor a png: it is loaded into an internal GD format and it is up to you to write it to disk in the desired format.

      Also note that saving to png allows you to specify a compression in the range:

      > 0-9, where 0 means no compression (largest files, highest quality) and 9 means maximum compression (smallest files, worst quality). A compression level of -1 uses the default compression level selected when zlib was compiled on your system, and is the same as calling png() with no argument.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        A compression level of -1 uses the default compression level selected when zlib was compiled on your system, and is the same as calling png() with no argument.

        Oh! That's useful...
        So it would always be sensible to include a compression level unless you happened to be the one who did the installation.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2021-10-20 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (81 votes). Check out past polls.

    Notices?