Skip to content

Commit

Permalink
Support PDF files with magic byte in the header (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-bashetty authored Oct 21, 2024
1 parent d925c83 commit f0e2192
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
Binary file added resources/PDF-2.0-with-offset-start.pdf
Binary file not shown.
14 changes: 8 additions & 6 deletions vips/foreign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"bytes"
"encoding/xml"
"fmt"
"golang.org/x/image/bmp"
"golang.org/x/net/html/charset"
"image/png"
"math"
"runtime"
"unsafe"

"golang.org/x/image/bmp"
"golang.org/x/net/html/charset"
)

// SubsampleMode correlates to a libvips subsample mode
Expand Down Expand Up @@ -154,14 +153,14 @@ func DetermineImageType(buf []byte) ImageType {
return ImageTypeHEIF
} else if isSVG(buf) {
return ImageTypeSVG
} else if isPDF(buf) {
return ImageTypePDF
} else if isBMP(buf) {
return ImageTypeBMP
} else if isJP2K(buf) {
return ImageTypeJP2K
} else if isJXL(buf) {
return ImageTypeJXL
} else if isPDF(buf) {
return ImageTypePDF
} else {
return ImageTypeUnknown
}
Expand Down Expand Up @@ -240,7 +239,10 @@ func isSVG(buf []byte) bool {
var pdf = []byte("\x25\x50\x44\x46")

func isPDF(buf []byte) bool {
return bytes.HasPrefix(buf, pdf)
if len(buf) <= 1024 {
return bytes.Contains(buf, pdf)
}
return bytes.Contains(buf[:1024], pdf)
}

var bmpHeader = []byte("BM")
Expand Down
11 changes: 11 additions & 0 deletions vips/foreign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ func Test_DetermineImageType__PDF(t *testing.T) {
assert.Equal(t, ImageTypePDF, imageType)
}

func Test_DetermineImageType__PDF_1(t *testing.T) {
Startup(&Config{})

buf, err := os.ReadFile(resources + "PDF-2.0-with-offset-start.pdf")
assert.NoError(t, err)
assert.NotNil(t, buf)

imageType := DetermineImageType(buf)
assert.Equal(t, ImageTypePDF, imageType)
}

func Test_DetermineImageType__BMP(t *testing.T) {
Startup(&Config{})

Expand Down
8 changes: 8 additions & 0 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,14 @@ func TestImage_Pixelate(t *testing.T) {
nil, nil)
}

func TestPDF_WithOffsetStart(t *testing.T) {
goldenTest(t, resources+"PDF-2.0-with-offset-start.pdf",
nil, func(img *ImageRef) {
assert.Equal(t, 612, img.Width())
assert.Equal(t, 396, img.Height())
}, nil)
}

func testWebpOptimizeIccProfile(t *testing.T, exportParams *WebpExportParams) []byte {
return goldenTest(t, resources+"has-icc-profile.png",
func(img *ImageRef) error {
Expand Down
11 changes: 11 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,17 @@ func TestImageRef_SetGamma(t *testing.T) {
require.NoError(t, err)
}

func Test_NewImageFromFile(t *testing.T) {
Startup(nil)

image, err := NewImageFromFile(resources + "PDF-2.0-with-offset-start.pdf")
require.NoError(t, err)

assert.Equal(t, ImageTypePDF, image.originalFormat)
assert.Equal(t, ImageTypePDF, image.format)
assert.Equal(t, 1, image.Pages())
}

// TODO unit tests to cover:
// NewImageFromReader failing test
// NewImageFromFile failing test
Expand Down

0 comments on commit f0e2192

Please sign in to comment.