Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take decode arrays into account when processing grayscale images #159

Merged
merged 4 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions core/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2138,9 +2138,7 @@ func (enc *JBIG2Encoder) DecodeBytes(encoded []byte) ([]byte, error) {
if err != nil {
return nil, err
}

// Inverse the data representation if the decoder is marked as 'isChocolateData'.
bm.InverseData(enc.IsChocolateData)
bm.GetVanillaData()

// By default the bitmap data contains the rowstride padding.
// In order to get rid of the rowstride padding use the bitmap.GetUnpaddedData method.
Expand Down
4 changes: 2 additions & 2 deletions internal/e2etest/extract_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ var knownExtrImgsHashes = map[string]string{
"6773e6aa5d8a2d26362cf3fca2874b3a81025bae.pdf": "f052e3e333839508a8bdd8d1a3ba1973",
"d11a3ca55664828b69d7c39d83d5c0a63fcea89d.pdf": "29287cd44f009dce5aa9c2a0dc9a3c83",
"483933bf73cc4fcc264eb69214ff763ccf299e49.pdf": "627dcf88805786d03b2e76d367b42642",
"da1c5c4c4fe36f676dbca6ea01673c9fdf77c7a9.pdf": "aa7980a7d50a4f20ff368f035f9f1c5a",
"da1c5c4c4fe36f676dbca6ea01673c9fdf77c7a9.pdf": "7ffe076d1c7c88e07dfba7545b622ec6",
"f856baf7ffcd96003b6bda800171cb0e5680f78e.pdf": "a9505d8c22f1fd063fbe0b05aa33a5fc",
"201c20676fe8da14a8130852c91ed58b48cba8fb.pdf": "ffcb78d126c04be9ca2497bb43b6e964",
"f0152456494aa09e5cf82c4afe9ecd2fdc2e8d72.pdf": "d0e68157aaa7f9f4406807512db3e676",
"d95643acea1ec3f6215bda35e4cd89dbd8898c44.pdf": "1739aed3e1cbfa5e98f8d7fef17a614b",
"110d793aeaa7accbe40b5ab9db249d5a103d3b50.pdf": "a57e347edddfd3f6032b85553b3537cd",
"d15a0aa289524619a971188372dd05fb712f1b2c.pdf": "477a95136f40c9103a807c5023ab8459",
"d15a0aa289524619a971188372dd05fb712f1b2c.pdf": "380907273bb8ea64943d986491d827ec",
"932e0dfa52c20ffe83b8178fb98296a0dab177d1.pdf": "b44d8b073f99ac3db28d7951e3c7d970",
"60a8c28da5c23081834adac4170755904d8c4166.pdf": "9167f381d5eed7a2e5fd10eca567c519",
"e51296be2615b9389482c9c16505286619b6cf36.pdf": "ec6e1f6297dd1cbda6ccba39e0c7d3d2",
Expand Down
4 changes: 2 additions & 2 deletions model/colorspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ func (cs *PdfColorspaceDeviceGray) ColorToRGB(color PdfColor) (PdfColor, error)

// ImageToRGB convert 1-component grayscale data to 3-component RGB.
func (cs *PdfColorspaceDeviceGray) ImageToRGB(img Image) (Image, error) {
rgbImage := img

data := make([]byte, 3*img.Width*img.Height)
for y := 0; y < int(img.Height); y++ {
for x := 0; x < int(img.Width); x++ {
Expand All @@ -321,9 +319,11 @@ func (cs *PdfColorspaceDeviceGray) ImageToRGB(img Image) (Image, error) {
}
}

rgbImage := img
rgbImage.BitsPerComponent = 8
rgbImage.ColorComponents = 3
rgbImage.Data = data
rgbImage.decode = nil

common.Log.Trace("DeviceGray -> RGB")
common.Log.Trace("samples: %v", img.Data)
Expand Down
23 changes: 16 additions & 7 deletions model/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
gocolor "image/color"
"image/draw"
"io"
"math"

// Imported for initialization side effects.
_ "image/gif"
Expand Down Expand Up @@ -89,6 +88,7 @@ func (img *Image) SetSamples(samples []uint32) {
func (img *Image) ColorAt(x, y int) (gocolor.Color, error) {
data := img.Data
lenData := len(img.Data)
maxVal := uint32(1<<uint32(img.BitsPerComponent)) - 1

switch img.ColorComponents {
case 1:
Expand All @@ -110,10 +110,14 @@ func (img *Image) ColorAt(x, y int) (gocolor.Color, error) {
pos := 8 - uint(((y*int(img.Width)+x)%divider)*bpc+bpc)

// Extract gray color value starting at the calculated position.
val := ((1 << uint(img.BitsPerComponent)) - 1) & (data[idx] >> pos)
val := float64(((1 << uint(img.BitsPerComponent)) - 1) & (data[idx] >> pos))
if len(img.decode) == 2 {
dMin, dMax := img.decode[0], img.decode[1]
val = interpolate(float64(val), 0, float64(maxVal), dMin, dMax)
}

return gocolor.Gray{
Y: uint8(uint32(val) * 255 / uint32(math.Pow(2, float64(img.BitsPerComponent))-1) & 0xff),
Y: uint8(uint32(val) * 255 / maxVal & 0xff),
}, nil
case 16:
// 16 bit grayscale image.
Expand All @@ -131,9 +135,14 @@ func (img *Image) ColorAt(x, y int) (gocolor.Color, error) {
if idx >= lenData {
return nil, fmt.Errorf("image coordinates out of range (%d, %d)", x, y)
}
val := float64(data[idx])
if len(img.decode) == 2 {
dMin, dMax := img.decode[0], img.decode[1]
val = interpolate(float64(val), 0, float64(maxVal), dMin, dMax)
}

return gocolor.Gray{
Y: uint8(uint32(data[idx]) * 255 / uint32(math.Pow(2, float64(img.BitsPerComponent))-1) & 0xff),
Y: uint8(uint32(val) * 255 / maxVal & 0xff),
}, nil
}
case 3:
Expand Down Expand Up @@ -165,9 +174,9 @@ func (img *Image) ColorAt(x, y int) (gocolor.Color, error) {
}

return gocolor.RGBA{
R: uint8(uint32(r) * 255 / uint32(math.Pow(2, float64(img.BitsPerComponent))-1) & 0xff),
G: uint8(uint32(g) * 255 / uint32(math.Pow(2, float64(img.BitsPerComponent))-1) & 0xff),
B: uint8(uint32(b) * 255 / uint32(math.Pow(2, float64(img.BitsPerComponent))-1) & 0xff),
R: uint8(uint32(r) * 255 / maxVal & 0xff),
G: uint8(uint32(g) * 255 / maxVal & 0xff),
B: uint8(uint32(b) * 255 / maxVal & 0xff),
A: uint8(0xff),
}, nil
case 16:
Expand Down