Search code examples
rubypngjpegrmagickcomposite

Compositing PNG onto JPEG fails (colors are all rendered as black) on only one particular JPEG file


I'm building an application which uses Ruby+RMagick to composite PNG images onto various JPEG backgrounds. Everything is working, but we have found one particular JPEG background for which the PNG is composited as a black spot. PNG transparency is respected; the shape of the "spot" is correct, but the colors are being lost and becoming black.

I have tried many JPEGs to try to find another which yields the same result, but (so far) have failed.

I suspect that it may have something to do with the bit depth or some other parameter of the JPEG file in question. I have been searching the Internet, looking for a tool which can analyze this JPEG and tell me all the parameters which might be relevant, but haven't found anything yet.

  1. Has anything like this ever happened to you? What was the cause?
  2. Based on your knowledge of the JPEG format, are there any other parameters which might be relevant?
  3. Do you know of any tool which can analyze JPEG files, and tell me the bit depth and other parameters? Or if I open the JPEG in a hex editor, can you tell me how to find this information?

Solution

  • I still haven't found what is special about that one JPG which the composite operation doesn't work correctly on, but I worked around it using this code:

    back  = Magick::Image.from_blob(jpg_data).first
    png   = Magick::Image.from_blob(png_data).first
    page1 = Magick::Image.new(back.columns, back.rows)
    page1.composite!(back, 0, 0, Magick::OverCompositeOp)
    page1.composite!(png, png_x, png_y, Magick::OverCompositeOp)
    

    Rather than:

    back  = Magick::Image.from_blob(jpg_data).first
    png   = Magick::Image.from_blob(png_data).first
    page1 = back.composite(png, png_x, png_y, Magick::OverCompositeOp)