Skip to content

feat: Add support for predictor - #87

Open
kvii wants to merge 3 commits into
ledongthuc:masterfrom
kvii:pr_predictor15
Open

kvii wants to merge 3 commits into
ledongthuc:masterfrom
kvii:pr_predictor15

Conversation

@kvii

@kvii kvii commented Sep 14, 2026

Copy link
Copy Markdown

Fix #26

This code is inspired from pdfcpu, gopdf and image/png

@kvii kvii changed the title test: Add support for predictor 10-15 test: Add support for predictor Sep 15, 2026
@kvii

kvii commented Sep 15, 2026

Copy link
Copy Markdown
Author

Test data for TIFF pdfminer/pdfminer.six#1058 (review)
You can use this file to test the logic of predictor 2 manually.

@kvii kvii changed the title test: Add support for predictor feat: Add support for predictor Sep 23, 2026
@ledongthuc

Copy link
Copy Markdown
Owner

@kvii, Thank you for this PR. It adds TIFF predictor 2 and PNG predictors 10 to 15. This change is useful.

Issue 1 (must fix): The Average filter gives wrong values.
In pngPredictorReader.Read, case 3 adds two byte values before it divides by 2. The sum can go over 255. The result is then wrong.
Example: previous row = [200, 200]. Current row raw = [0, 0].
Got: [100, 22]. Expected: [100, 150].
Change the sum to int. Add a test with values above 127.

Issue 2 (should fix): The new panics can stop valid PDFs.
decodeParams panics when Colors is not 1 or 3. It also panics when BitsPerComponent is not 8.
Before this change, these streams decoded without a panic. Now a CMYK image (Colors 4) can crash the caller.
Return an error, or pass the stream through unchanged.

Minor:

  • Add a comment in tiffPredictorReader. Explain what happens to a truncated last row.
  • Add a PR description. State which PDFs failed and what behavior changes.
  • The PR comment links TIFF test data. That file is not in this PR. Add a test PDF if you can.

Thanks.

@kvii

kvii commented Oct 29, 2026

Copy link
Copy Markdown
Author

@ledongthuc Issue 1 is fixed. But I don't think this PR changes any behavior. YMCK(Color 4) is valid if and only if Predictor is not 1 or null. The previous code also caused a panic when Predictor was not 12 or null. Perhaps I can stop parsing other params when predictor is 1 or null, like this:

func decodeParams(param Value) (predictor, rawLen, bpp int64) {
	if v := param.Key("Predictor"); v.Kind() == Null {
		predictor = 1
	} else {
		predictor = v.Int64()
	}
	switch predictor {
	case 1:
		return // <---
	case 2, 10, 11, 12, 13, 14, 15:
	default:
		panic(fmt.Errorf("Predictor %d is currently unsupported", predictor))
	}

	...
	switch colors {
	case 1, 3:
	default:
		panic(fmt.Errorf("Colors %d is currently unsupported", colors))
	}
	...
}

@kvii

kvii commented Oct 29, 2026

Copy link
Copy Markdown
Author

predictor 15
predictor15.pdf
tiff
tiff.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is this content stream reachable with the API?

2 participants