Home  >  Article  >  Backend Development  >  Colors incorrect when using image.Decode to decode JPEG and write to PDF?

Colors incorrect when using image.Decode to decode JPEG and write to PDF?

PHPz
PHPzforward
2024-02-08 22:40:121058browse

使用 image.Decode 解码 JPEG 并写入 PDF 时颜色不正确?

php editor Zimo encountered a problem when using image.Decode to decode JPEG and write it to PDF, that is, the color was incorrect. This problem may be caused by a mismatch in color spaces. When decoding a JPEG image, you need to ensure that the color space of the image matches the color space of the PDF, otherwise it will cause incorrect colors. To solve this problem, you can try to use the image.DecodeConfig function to get the color mode of the JPEG image, and then match it with the color space of the PDF to ensure consistency. In addition, you can also try to use other image processing libraries or adjust the color space of the image to solve this problem.

Question content

I try to use the image.decode() method to decode image files (png, jpg, gif and bmp formats) to create pdf to get image.image. I then write the pixel data to a pdf stream and subsequently compress it. The problem I'm having is that when I decode the jpeg, the colors in the resulting pdf are incorrect. All other image formats work as expected. I've attached a screenshot of the issue.

screenshot: https://i.stack.imgur.com/e3hc8.png

Does anyone know what causes this problem? Is there a specific way I need to handle jpegs differently when using image.decode()? Any suggestions on how to resolve this issue would be greatly appreciated!

edit:

Code:

var idata image.image
ifile, err := os.open(path)
if err != nil {
  [...]
} else {
  idata, _, err = image.decode(ifile)
}
[...]
x.Dictionary.Set("ColorSpace", "/DeviceRGB")
x.Dictionary.Set("BitsPerComponent", 8)
for j := 0; j < iData.Bounds().Dy()/pixelMul; j++ {
    for k := 0; k < iData.Bounds().Dx()/pixelMul; k++ {
        r, g, b, _ := iData.At(k*pixelMul, j*pixelMul).RGBA()
        x.Write([]byte{byte(r), byte(g), byte(b)})
    }
}
[...]

When using jpeg.decode directly, the resulting image in the pdf looks the same.

I want the images in the generated pdf to look just like the original pngs, but maybe with some degradation.

Original png: https://i.stack.imgur.com/rnkgq.png

Converted jpg: https://i.stack.imgur.com/yj69y.jpg

Other jpegs have the same problem, such as the first test jpeg from w3c https://www.w3.org/markup/test/xhtml-print/20050519/tests/a_2_1-bf-01.htm

Solution

color.rgba() Returns the range of alpha premultiplied color components 0..0xffff.

Converting a value like byte(r) to byte will retain its lowest 8 bits, which will appear to be just random compared to the original value. You need an 8-bit color component, don't convert it to byte but use the upper 8 bits, which means right shift by 8 (or divide by 256):

x.write([]byte{byte(r>>8), byte(g>>8), byte(b>>8)})

Explanation why it still works for png and gif, but not jpeg:

Decoding png and gif images may use the color.rgba的图像模型> color model, using 8-bit values ​​to store components. But its rgba.rgba() method converts these values ​​to 16-bit values ​​by copying the original 8-bit value:

func (c RGBA) RGBA() (r, g, b, a uint32) {
    r = uint32(c.R)
    r |= r << 8
    g = uint32(c.G)
    g |= g << 8
    b = uint32(c.B)
    b |= b << 8
    a = uint32(c.A)
    a |= a << 8
    return
}

This means that if you take the lower 8 bits, you will get the same original value as if you take the upper 8 bits. Decoding jpeg images may use the color.ycbcr color type, which does not reproduce this "implementation behavior".

Don't rely on this. When you need an 8-bit component out of a 16-bit component, always use the higher 8-bit.

The above is the detailed content of Colors incorrect when using image.Decode to decode JPEG and write to PDF?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete