mirror of
https://github.com/gen2brain/cbconvert
synced 2026-06-30 01:01:54 +02:00
Use document native resolution, issue #55
This commit is contained in:
+7
-5
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
pngstructure "github.com/dsoprea/go-png-image-structure"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/gen2brain/go-fitz"
|
||||
)
|
||||
|
||||
// Options type.
|
||||
@@ -41,7 +42,7 @@ type Options struct {
|
||||
Fit bool
|
||||
// Do not upscale images already smaller than the requested width/height
|
||||
NoUpscale bool
|
||||
// Document rendering resolution in DPI (PDF, EPUB, etc.); 0 uses the default
|
||||
// Document rendering resolution in DPI (PDF, EPUB, etc.); 0 uses the page's native resolution
|
||||
DPI int
|
||||
// 0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 5=Gaussian, 6=Lanczos
|
||||
Filter int
|
||||
@@ -209,13 +210,14 @@ func (o Options) Args() []string {
|
||||
return args
|
||||
}
|
||||
|
||||
// renderDPI returns the document rendering resolution, falling back to 300 when unset.
|
||||
func (c *Converter) renderDPI() float64 {
|
||||
// renderPage renders document page n at the configured DPI, or at the page's
|
||||
// native resolution when DPI is unset.
|
||||
func (c *Converter) renderPage(doc *fitz.Document, n int) (*image.RGBA, error) {
|
||||
if c.Opts.DPI > 0 {
|
||||
return float64(c.Opts.DPI)
|
||||
return doc.ImageDPI(n, float64(c.Opts.DPI))
|
||||
}
|
||||
|
||||
return 300
|
||||
return doc.Image(n)
|
||||
}
|
||||
|
||||
// Cancel cancels the operation.
|
||||
|
||||
@@ -48,7 +48,7 @@ func (c *Converter) convertDocument(ctx context.Context, fileName string) error
|
||||
return fmt.Errorf("convertDocument: %w", ctx.Err())
|
||||
}
|
||||
|
||||
img, err := doc.ImageDPI(n, c.renderDPI())
|
||||
img, err := c.renderPage(doc, n)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convertDocument: %w", err)
|
||||
}
|
||||
|
||||
+2
-2
@@ -52,7 +52,7 @@ func (c *Converter) coverDocument(fileName string) (image.Image, error) {
|
||||
}
|
||||
defer doc.Close()
|
||||
|
||||
img, err := doc.ImageDPI(0, c.renderDPI())
|
||||
img, err := c.renderPage(doc, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("coverDocument: %w", err)
|
||||
}
|
||||
@@ -124,7 +124,7 @@ func (c *Converter) pageDocument(fileName string, page int) (image.Image, error)
|
||||
return nil, fmt.Errorf("pageDocument: page %d out of range (%d pages)", page+1, doc.NumPage())
|
||||
}
|
||||
|
||||
img, err := doc.ImageDPI(page, c.renderDPI())
|
||||
img, err := c.renderPage(doc, page)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pageDocument: %w", err)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Minimální velikost (MiB):",
|
||||
TipSize: "Zpracovat pouze soubory větší než minimální velikost",
|
||||
LblDPI: "DPI dokumentu:",
|
||||
TipDPI: "Rozlišení pro vykreslování dokumentů (PDF, EPUB atd.); výchozí je 300",
|
||||
TipDPI: "Rozlišení pro vykreslování dokumentů (PDF, EPUB atd.); výchozí používá původní rozlišení",
|
||||
|
||||
LblOutDir: "Výstupní adresář:",
|
||||
TipOutDir: "Adresář, do kterého se zapisují převedené soubory (povinné)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Mindestgröße (MiB):",
|
||||
TipSize: "Nur Dateien größer als die Mindestgröße verarbeiten",
|
||||
LblDPI: "Dokument-DPI:",
|
||||
TipDPI: "Auflösung zum Rendern von Dokumenten (PDF, EPUB usw.); Standard ist 300",
|
||||
TipDPI: "Auflösung zum Rendern von Dokumenten (PDF, EPUB usw.); Standard verwendet die Originalauflösung",
|
||||
|
||||
LblOutDir: "Ausgabeverzeichnis:",
|
||||
TipOutDir: "Verzeichnis, in das konvertierte Dateien geschrieben werden (erforderlich)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Minimum Size (MiB):",
|
||||
TipSize: "Process only files larger than minimum size",
|
||||
LblDPI: "Document DPI:",
|
||||
TipDPI: "Resolution for rendering documents (PDF, EPUB, etc.); Default is 300",
|
||||
TipDPI: "Resolution for rendering documents (PDF, EPUB, etc.); Default uses the original resolution",
|
||||
|
||||
LblOutDir: "Output Directory:",
|
||||
TipOutDir: "Directory where converted files are written (required)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Tamaño mínimo (MiB):",
|
||||
TipSize: "Procesar solo archivos mayores que el tamaño mínimo",
|
||||
LblDPI: "DPI del documento:",
|
||||
TipDPI: "Resolución para renderizar documentos (PDF, EPUB, etc.); el valor predeterminado es 300",
|
||||
TipDPI: "Resolución para renderizar documentos (PDF, EPUB, etc.); el valor predeterminado usa la resolución original",
|
||||
|
||||
LblOutDir: "Directorio de salida:",
|
||||
TipOutDir: "Directorio donde se escriben los archivos convertidos (obligatorio)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Taille minimale (Mio) :",
|
||||
TipSize: "Ne traiter que les fichiers plus grands que la taille minimale",
|
||||
LblDPI: "DPI du document :",
|
||||
TipDPI: "Résolution pour le rendu des documents (PDF, EPUB, etc.) ; la valeur par défaut est 300",
|
||||
TipDPI: "Résolution pour le rendu des documents (PDF, EPUB, etc.) ; la valeur par défaut utilise la résolution d'origine",
|
||||
|
||||
LblOutDir: "Dossier de sortie :",
|
||||
TipOutDir: "Dossier où les fichiers convertis sont écrits (obligatoire)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Dimensione minima (MiB):",
|
||||
TipSize: "Elabora solo i file più grandi della dimensione minima",
|
||||
LblDPI: "DPI del documento:",
|
||||
TipDPI: "Risoluzione per il rendering dei documenti (PDF, EPUB, ecc.); il valore predefinito è 300",
|
||||
TipDPI: "Risoluzione per il rendering dei documenti (PDF, EPUB, ecc.); il valore predefinito usa la risoluzione originale",
|
||||
|
||||
LblOutDir: "Cartella di uscita:",
|
||||
TipOutDir: "Cartella in cui vengono scritti i file convertiti (obbligatoria)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "最小サイズ (MiB):",
|
||||
TipSize: "最小サイズより大きいファイルのみ処理する",
|
||||
LblDPI: "ドキュメント DPI:",
|
||||
TipDPI: "ドキュメント(PDF、EPUB など)をレンダリングする解像度。既定値は 300",
|
||||
TipDPI: "ドキュメント(PDF、EPUB など)をレンダリングする解像度。既定値は元の解像度を使用します",
|
||||
|
||||
LblOutDir: "出力ディレクトリ:",
|
||||
TipOutDir: "変換されたファイルが書き込まれるディレクトリ(必須)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Tamanho mínimo (MiB):",
|
||||
TipSize: "Processar apenas arquivos maiores que o tamanho mínimo",
|
||||
LblDPI: "DPI do documento:",
|
||||
TipDPI: "Resolução para renderizar documentos (PDF, EPUB, etc.); o padrão é 300",
|
||||
TipDPI: "Resolução para renderizar documentos (PDF, EPUB, etc.); o padrão usa a resolução original",
|
||||
|
||||
LblOutDir: "Diretório de saída:",
|
||||
TipOutDir: "Diretório onde os arquivos convertidos são gravados (obrigatório)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "Минимальный размер (МиБ):",
|
||||
TipSize: "Обрабатывать только файлы больше минимального размера",
|
||||
LblDPI: "DPI документа:",
|
||||
TipDPI: "Разрешение для рендеринга документов (PDF, EPUB и т. д.); по умолчанию 300",
|
||||
TipDPI: "Разрешение для рендеринга документов (PDF, EPUB и т. д.); по умолчанию используется исходное разрешение",
|
||||
|
||||
LblOutDir: "Каталог вывода:",
|
||||
TipOutDir: "Каталог, в который записываются преобразованные файлы (обязательно)",
|
||||
|
||||
@@ -27,7 +27,7 @@ func init() {
|
||||
LblMinSize: "最小大小 (MiB):",
|
||||
TipSize: "仅处理大于最小大小的文件",
|
||||
LblDPI: "文档 DPI:",
|
||||
TipDPI: "渲染文档的分辨率(PDF、EPUB 等);默认值为 300",
|
||||
TipDPI: "渲染文档的分辨率(PDF、EPUB 等);默认使用原始分辨率",
|
||||
|
||||
LblOutDir: "输出目录:",
|
||||
TipOutDir: "写入转换后文件的目录(必填)",
|
||||
|
||||
@@ -216,7 +216,7 @@ func parseFlags() (cbconvert.Options, []string) {
|
||||
convert.IntVar(&opts.Height, "height", base.Height, "Image height")
|
||||
convert.BoolVar(&opts.Fit, "fit", base.Fit, "Best fit for required width and height")
|
||||
convert.BoolVar(&opts.NoUpscale, "no-upscale", base.NoUpscale, "Do not upscale images already smaller than the requested width/height")
|
||||
convert.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the default (300)")
|
||||
convert.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the original resolution")
|
||||
convert.StringVar(&opts.Format, "format", base.Format, "Image format, valid values are jpeg, png, tiff, bmp, webp, avif, jxl")
|
||||
convert.StringVar(&opts.Archive, "archive", base.Archive, "Archive format, valid values are zip, tar")
|
||||
convert.IntVar(&opts.ZipLevel, "zip-level", base.ZipLevel, "ZIP compression level, 0 disables compression, 1-9 sets deflate level (1 fastest, 9 smallest), -1 uses the default")
|
||||
@@ -246,7 +246,7 @@ func parseFlags() (cbconvert.Options, []string) {
|
||||
cover.IntVar(&opts.Height, "height", base.Height, "Image height")
|
||||
cover.BoolVar(&opts.Fit, "fit", base.Fit, "Best fit for required width and height")
|
||||
cover.BoolVar(&opts.NoUpscale, "no-upscale", base.NoUpscale, "Do not upscale images already smaller than the requested width/height")
|
||||
cover.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the default (300)")
|
||||
cover.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the original resolution")
|
||||
cover.StringVar(&opts.Format, "format", base.Format, "Image format, valid values are jpeg, png, tiff, bmp, webp, avif")
|
||||
cover.IntVar(&opts.Quality, "quality", base.Quality, "Image quality")
|
||||
cover.IntVar(&opts.Effort, "effort", base.Effort, "Encoder speed/effort, format-specific (webp method 0-6, avif speed 0-10, jxl effort 1-10), -1 uses the format default")
|
||||
@@ -263,7 +263,7 @@ func parseFlags() (cbconvert.Options, []string) {
|
||||
thumbnail.IntVar(&opts.Height, "height", base.Height, "Image height")
|
||||
thumbnail.BoolVar(&opts.Fit, "fit", base.Fit, "Best fit for required width and height")
|
||||
thumbnail.BoolVar(&opts.NoUpscale, "no-upscale", base.NoUpscale, "Do not upscale images already smaller than the requested width/height")
|
||||
thumbnail.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the default (300)")
|
||||
thumbnail.IntVar(&opts.DPI, "dpi", base.DPI, "Document rendering resolution in DPI (PDF, EPUB, etc.), 0 uses the original resolution")
|
||||
thumbnail.IntVar(&opts.Filter, "filter", base.Filter, "0=NearestNeighbor, 1=Box, 2=Linear, 3=MitchellNetravali, 4=CatmullRom, 5=Gaussian, 6=Lanczos")
|
||||
thumbnail.StringVar(&opts.OutDir, "outdir", base.OutDir, "Output directory")
|
||||
thumbnail.StringVar(&opts.OutFile, "outfile", base.OutFile, "Output file")
|
||||
|
||||
@@ -8,7 +8,7 @@ require (
|
||||
github.com/dustin/go-humanize v1.0.1
|
||||
github.com/fvbommel/sortorder v1.1.0
|
||||
github.com/gen2brain/avif v0.5.1
|
||||
github.com/gen2brain/go-fitz v1.24.15
|
||||
github.com/gen2brain/go-fitz v1.28.0
|
||||
github.com/gen2brain/jpegli v0.4.1
|
||||
github.com/gen2brain/jpegxl v0.5.1
|
||||
github.com/gen2brain/webp v0.6.1
|
||||
@@ -32,7 +32,6 @@ require (
|
||||
github.com/go-errors/errors v1.5.1 // indirect
|
||||
github.com/golang/geo v0.0.0-20260625163123-7c0e84413537 // indirect
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||
github.com/jupiterrider/ffi v0.7.0 // indirect
|
||||
github.com/klauspost/compress v1.18.6 // indirect
|
||||
github.com/klauspost/pgzip v1.2.6 // indirect
|
||||
github.com/mikelolasagasti/xz v1.0.1 // indirect
|
||||
|
||||
@@ -40,8 +40,8 @@ github.com/fvbommel/sortorder v1.1.0 h1:fUmoe+HLsBTctBDoaBwpQo5N+nrCp8g/BjKb/6ZQ
|
||||
github.com/fvbommel/sortorder v1.1.0/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
|
||||
github.com/gen2brain/avif v0.5.1 h1:LQzLsJpWyGlsa4wuZ3D57qEbCiICIK7Yidz5ZPEwzTk=
|
||||
github.com/gen2brain/avif v0.5.1/go.mod h1:QgrYqdVE9y40PCfArK9VakcMIpYeDYpZmCSLkW6C1n8=
|
||||
github.com/gen2brain/go-fitz v1.24.15 h1:sJNB1MOWkqnzzENPHggFpgxTwW0+S5WF/rM5wUBpJWo=
|
||||
github.com/gen2brain/go-fitz v1.24.15/go.mod h1:SftkiVbTHqF141DuiLwBBM65zP7ig6AVDQpf2WlHamo=
|
||||
github.com/gen2brain/go-fitz v1.28.0 h1:RovqgQPAcOuyv5HZrWsTWl8qwlwbAHSKcAZXZUw0Vlk=
|
||||
github.com/gen2brain/go-fitz v1.28.0/go.mod h1:pY2hqAjp9Zy7qfPI2gwbJMHBFAdZpVXOLrRxD82l3Bs=
|
||||
github.com/gen2brain/jpegli v0.4.1 h1:qc11IQU0jTYFltroulT4MXmbu9YRftqHV0YBZ0Bqz5o=
|
||||
github.com/gen2brain/jpegli v0.4.1/go.mod h1:zJ++s4symmKCN1CLkrY0dGXTY3s0NWbd94Rz9KLdCzk=
|
||||
github.com/gen2brain/jpegxl v0.5.1 h1:UuBUIkZ35DErImU3bTA6rltfV5zSgVNOA/K5a6JibfE=
|
||||
@@ -56,6 +56,7 @@ github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3Bop
|
||||
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
|
||||
github.com/golang/geo v0.0.0-20200319012246-673a6f80352d/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
|
||||
github.com/golang/geo v0.0.0-20260625163123-7c0e84413537 h1:KeIaDS/+VEy/bhDYjG3Z78dOyLAU4HXcVxmd0WYHJTE=
|
||||
github.com/golang/geo v0.0.0-20260625163123-7c0e84413537/go.mod h1:Mymr9kRGDc64JPr03TSZmuIBODZ3KyswLzm1xL0HFA8=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
@@ -64,8 +65,6 @@ github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25 h1:YLvr1eE6cdCqjOe972w/cYF+FjW34v27+9Vo5106B4M=
|
||||
github.com/jsummers/gobmp v0.0.0-20230614200233-a9de23ed2e25/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw=
|
||||
github.com/jupiterrider/ffi v0.7.0 h1:RKsl6Ascal+3kyAqR5Qcbp83LceQMLc1VZbPfHWoNzs=
|
||||
github.com/jupiterrider/ffi v0.7.0/go.mod h1:9dauhpOfNqrqk28fxuu0kkdeFtT9Qr4vbfigiuIXN7c=
|
||||
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao=
|
||||
github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
|
||||
Reference in New Issue
Block a user