Skip to content
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
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/go-pdfkit/render
go 1.26.4

require (
github.com/go-gfx/gfx v0.12.0
github.com/go-gfx/gfx v0.14.0
github.com/go-opentype/fonts v0.9.0
github.com/go-opentype/opentype v0.10.0
github.com/go-pdfkit/reader v0.6.0
Expand All @@ -16,6 +16,9 @@ require (

require (
github.com/ajroetker/go-highway v0.0.4 // indirect
github.com/dkrisman/gobig2 v0.0.0-20260513123937-51e39052fde6
github.com/dkrisman/gobig2 v0.0.0-20260513123937-51e39052fde6 // indirect
github.com/sergeymakinen/go-bmp v1.0.0 // indirect
github.com/sergeymakinen/go-ico v1.0.0 // indirect
golang.org/x/image v0.45.0 // indirect
golang.org/x/sys v0.47.0 // indirect
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/ajroetker/go-jpeg2000 v0.0.2 h1:ni8brffZrci4Kacx3nM5d92ipmTDfak84KgHY
github.com/ajroetker/go-jpeg2000 v0.0.2/go.mod h1:7ld88W47lZy0x8gRQesRGAonDPOpr6ev8rckjCAfbzE=
github.com/dkrisman/gobig2 v0.0.0-20260513123937-51e39052fde6 h1:hHPgbpPdcaSXtpLb9NMEUJPxsZ0cxGtGHwmvKGI36NA=
github.com/dkrisman/gobig2 v0.0.0-20260513123937-51e39052fde6/go.mod h1:2Ij0rpAVy1tZ+PQ9FM1jCpnVo/OOCMCD6rLtSp4JkEI=
github.com/go-gfx/gfx v0.12.0 h1:zBaYHahRwM6yYr86ifRelX132ZjhN5hZNr/uW9dpIzQ=
github.com/go-gfx/gfx v0.12.0/go.mod h1:DpRUcQrlLZH02CSB23J0iEk1F7mQH8w9WMF60xQBxDU=
github.com/go-gfx/gfx v0.14.0 h1:G6A0ZIirv9XjlxVFOl3HLscLRJOdhe9e1HqiCK5CHz0=
github.com/go-gfx/gfx v0.14.0/go.mod h1:8KcjwJmHW2ImXcjeMEIQ1bY+7Q2DNcMYoQCl270Isr8=
github.com/go-opentype/fonts v0.9.0 h1:slB6OB3riLyUPrOxqXe0s6/AzdenF1TDvCN8N87hhQk=
github.com/go-opentype/fonts v0.9.0/go.mod h1:C6yQL2apHItfEZ5hztpsHF0S5mlX/hklLlq/Z5fRG/g=
github.com/go-opentype/opentype v0.10.0 h1:cZVMZ3RVkcijXmxlmpqyVkjlNc33aSB2ugKVWYvoLUg=
Expand All @@ -14,5 +14,11 @@ github.com/go-pdfkit/pdffont v0.3.0 h1:G5DKcAmsZJ0e17QhSrcUaL7PKEXKjUx4P/iGMl7bA
github.com/go-pdfkit/pdffont v0.3.0/go.mod h1:bfmNLna1l1CljNX/Utg55YzFylovfmI7sJnvgA3bzKI=
github.com/go-pdfkit/reader v0.6.0 h1:KAabNOYUcTlZlNBTbG9bEhWP1NiZhjuhzUdavdTdfes=
github.com/go-pdfkit/reader v0.6.0/go.mod h1:fQFOVfCMUui1AdvD4qhimdyvvNr9KvvJ1S7IuKZjyV8=
github.com/sergeymakinen/go-bmp v1.0.0 h1:SdGTzp9WvCV0A1V0mBeaS7kQAwNLdVJbmHlqNWq0R+M=
github.com/sergeymakinen/go-bmp v1.0.0/go.mod h1:/mxlAQZRLxSvJFNIEGGLBE/m40f3ZnUifpgVDlcUIEY=
github.com/sergeymakinen/go-ico v1.0.0 h1:uL3khgvKkY6WfAetA+RqsguClBuu7HpvBB/nq/Jvr80=
github.com/sergeymakinen/go-ico v1.0.0/go.mod h1:wQ47mTczswBO5F0NoDt7O0IXgnV4Xy3ojrroMQzyhUk=
golang.org/x/image v0.45.0 h1:FMb1nTbH5H9vF55SriQHgFw5GnNL9Jg6L25BwXKzhB0=
golang.org/x/image v0.45.0/go.mod h1:n62x/7RqlwXDvGsSU4u6IUTUf6KghUZ9Bt7cG/T9Fx4=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
30 changes: 11 additions & 19 deletions jbig2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
package render

import (
"bytes"
"image"

"github.com/dkrisman/gobig2"
"github.com/go-gfx/gfx/codec"
"github.com/go-pdfkit/reader"
)

Expand All @@ -31,8 +28,7 @@ func (r *renderer) decodeJBIG2(dict reader.Dict, data []byte, w, h int) []byte {
if err != nil || img == nil {
return nil
}
b := img.Bounds()
if b.Dx() != w || b.Dy() != h {
if img.W != w || img.H != h {
// The dictionary's size is the one the page's geometry was computed
// from. A stream that decodes to another size is not this image.
return nil
Expand All @@ -44,8 +40,7 @@ func (r *renderer) decodeJBIG2(dict reader.Dict, data []byte, w, h int) []byte {
}
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
gr, gg, gb, _ := img.At(b.Min.X+x, b.Min.Y+y).RGBA()
if (gr*299+gg*587+gb*114)/1000 < 0x8000 {
if img.Pix[(y*w+x)*4] < 128 {
out[y*rowBytes+x/8] &^= 0x80 >> uint(x%8)
}
}
Expand Down Expand Up @@ -90,14 +85,11 @@ func (r *renderer) globalsFrom(parms reader.Object) []byte {
// jbig2Decode is a variable so a test can watch what happens when a decoder
// refuses, which is the case that decides whether the image is drawn wrong or
// not drawn.
var jbig2Decode = func(data, globals []byte) (image.Image, error) {
d, err := gobig2.NewDecoderEmbedded(bytes.NewReader(data), globals)
if err != nil {
return nil, err
}
img, err := d.Decode()
if err != nil {
return nil, err
}
return img, nil
}
//
// The decoder is reached through go-gfx/gfx/codec, which is where the fleet
// keeps its image decoders, rather than named here. It matters more than usual
// for this one: its resource limits are process-global rather than per-decode,
// so a library cannot raise them without changing them for everything else in
// the binary, and it publishes no tagged version. The day it is swapped should
// be a change in one place.
var jbig2Decode = codec.DecodeEmbeddedJBIG2
8 changes: 4 additions & 4 deletions jbig2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package render

import (
"errors"
"image"
"testing"

"github.com/go-gfx/gfx/raster"
"github.com/go-pdfkit/reader"
)

Expand Down Expand Up @@ -160,7 +160,7 @@ func TestAJBIG2DecoderThatRefusesTakesTheImageWithIt(t *testing.T) {
// answer; drawing the rectangle anyway is not.
restore := jbig2Decode
defer func() { jbig2Decode = restore }()
jbig2Decode = func(data, globals []byte) (image.Image, error) {
jbig2Decode = func(data, globals []byte) (*raster.Image, error) {
return nil, errors.New("resource budget exceeded")
}
d := jbig2Page(t, jbig2Ink, 16, 8, nil)
Expand All @@ -176,7 +176,7 @@ func TestAJBIG2DecoderThatRefusesTakesTheImageWithIt(t *testing.T) {
func TestAJBIG2DecoderThatReturnsNothingTakesTheImageWithIt(t *testing.T) {
restore := jbig2Decode
defer func() { jbig2Decode = restore }()
jbig2Decode = func(data, globals []byte) (image.Image, error) { return nil, nil }
jbig2Decode = func(data, globals []byte) (*raster.Image, error) { return nil, nil }
d := jbig2Page(t, jbig2Ink, 16, 8, nil)
img, err := Page(d, 1, Options{Scale: 1})
if err != nil {
Expand All @@ -195,7 +195,7 @@ func seenGlobals(t *testing.T, parms reader.Object) []byte {
defer func() { jbig2Decode = restore }()
var got []byte
var called bool
jbig2Decode = func(data, globals []byte) (image.Image, error) {
jbig2Decode = func(data, globals []byte) (*raster.Image, error) {
got, called = globals, true
return restore(data, globals)
}
Expand Down
Loading