-
Notifications
You must be signed in to change notification settings - Fork 335
Support DCTDecodeFilter, which is no-op, actually #15
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pdf_test | ||
|
||
import ( | ||
"image/jpeg" | ||
"testing" | ||
|
||
"rsc.io/pdf" | ||
) | ||
|
||
func TestReaderExtractXObjectDCTDecode(t *testing.T) { | ||
const ( | ||
testscan = "testdata/testscan.pdf" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 50K seems a bit large for such a basic test imho. It does not have to be a real scan coming from your scanner, as long as it is structured the same. Is it difficult to forge a pdf with a an image of less than 1K in it? |
||
) | ||
f, err := pdf.Open(testscan) | ||
if err != nil { | ||
t.Fatalf("could not open %v: %v", testscan, err) | ||
} | ||
x := f.Page(1).Resources().Key("XObject") | ||
if x.Kind() != pdf.Dict || len(x.Keys()) == 0 { | ||
t.Fatalf("no xobject dict on page 1") | ||
} | ||
k := x.Key(x.Keys()[0]) | ||
if k.IsNull() || k.Kind() != pdf.Stream || k.Key("Subtype").Name() != "Image" { | ||
t.Fatalf("first xobject child is not an image stream") | ||
} | ||
defer func() { | ||
if r := recover(); r != nil { | ||
s, ok := r.(string) | ||
if ok && s == "unknown filter DCTDecode" { | ||
t.Fatalf("DCTDecode filter handling is not implemented") | ||
} | ||
panic(r) // re-panic everything else | ||
} | ||
}() | ||
rc := k.Reader() | ||
defer rc.Close() | ||
_, err = jpeg.Decode(rc) | ||
if err != nil { | ||
t.Fatalf("could not decode embedded JPEG: %v", err) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably check that the decoded image is what we expect. we'd usually check it pixel by pixel but that might be overkill here, so it's probably ok to e.g. check the hashsum of its bytes against a hardcoded hashsum of what we know to be the initial image. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// DCTDecode indicates that the Image XObject data is a full JPEG encoded image, so we return the original reader as is, and leave it up to the caller to decode the image.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. thanks. Done.