Initial orientator
This commit is contained in:
51
internal/rasterize/pdf.go
Normal file
51
internal/rasterize/pdf.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Package rasterize converts PDF pages to in-memory images using MuPDF.
|
||||
package rasterize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
|
||||
"github.com/gen2brain/go-fitz"
|
||||
)
|
||||
|
||||
// DPI used when rasterizing pages for the orientation classifier.
|
||||
// 150 mirrors the Ghostscript -r150 flag used in the original Python pipeline.
|
||||
const DPI = 150
|
||||
|
||||
type Page struct {
|
||||
Index int
|
||||
Img image.Image
|
||||
}
|
||||
|
||||
// PDF wraps an open MuPDF document.
|
||||
type PDF struct {
|
||||
doc *fitz.Document
|
||||
}
|
||||
|
||||
func Open(data []byte) (*PDF, error) {
|
||||
doc, err := fitz.NewFromMemory(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fitz open: %w", err)
|
||||
}
|
||||
return &PDF{doc: doc}, nil
|
||||
}
|
||||
|
||||
func (p *PDF) Close() error {
|
||||
if p == nil || p.doc == nil {
|
||||
return nil
|
||||
}
|
||||
return p.doc.Close()
|
||||
}
|
||||
|
||||
func (p *PDF) NumPages() int {
|
||||
return p.doc.NumPage()
|
||||
}
|
||||
|
||||
// RenderPage rasterizes a single page at the configured DPI.
|
||||
func (p *PDF) RenderPage(idx int) (image.Image, error) {
|
||||
img, err := p.doc.ImageDPI(idx, DPI)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render page %d: %w", idx, err)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
Reference in New Issue
Block a user