summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorNick White <git@njw.name>2022-11-17 12:49:51 +0000
committerNick White <git@njw.name>2022-11-17 13:29:35 +0000
commita9bcda93f3a19bee07aa04b79dc4fc515c74de17 (patch)
tree79d991d50421518711d116b1f5a15184736d7726 /cmd
parentbe3a836725a22d8bc41f576502f9db721283021c (diff)
rescribe: support CCITTFaxDecode (tiff) encoded images in PDF reading
Diffstat (limited to 'cmd')
-rw-r--r--cmd/rescribe/main.go34
1 files changed, 32 insertions, 2 deletions
diff --git a/cmd/rescribe/main.go b/cmd/rescribe/main.go
index 74f6440..2c37dc5 100644
--- a/cmd/rescribe/main.go
+++ b/cmd/rescribe/main.go
@@ -28,6 +28,7 @@ import (
"strings"
"time"
+ "golang.org/x/image/tiff"
"rescribe.xyz/bookpipeline"
"rescribe.xyz/bookpipeline/internal/pipeline"
"rescribe.xyz/pdf"
@@ -424,7 +425,9 @@ func extractPdfImgs(ctx context.Context, path string) (string, error) {
// rmIfNotImage attempts to decode a given file as an image. If it is
// decode-able as PNG, then rename file extension from .jpg to .png,
-// if it fails to be read as PNG or JPEG it will be deleted.
+// if it is decode-able as TIFF then convert to PNG and rename file
+// extension appropriately, if it fails to be read as PNG, TIFF or
+// JPEG it will just be deleted.
func rmIfNotImage(f string) error {
r, err := os.Open(f)
defer r.Close()
@@ -448,12 +451,39 @@ func rmIfNotImage(f string) error {
return fmt.Errorf("Failed to open image %s: %v\n", f, err)
}
_, err = jpeg.Decode(r)
+ if err == nil {
+ return nil
+ }
+
+ r, err = os.Open(f)
+ defer r.Close()
if err != nil {
+ return fmt.Errorf("Failed to open image %s: %v\n", f, err)
+ }
+ t, err := tiff.Decode(r)
+ if err == nil {
+ b := strings.TrimSuffix(f, ".jpg")
+ n, err := os.Create(b+".png")
+ defer n.Close()
+ if err != nil {
+ return fmt.Errorf("Failed to create file to store new png %s from tiff %s: %v\n", b+".png", f, err)
+ }
+ err = png.Encode(n, t)
+ if err != nil {
+ return fmt.Errorf("Failed to encode tiff as png for %s: %v\n", f, err)
+ }
r.Close()
err = os.Remove(f)
if err != nil {
- return fmt.Errorf("Failed to remove invalid image %s: %v", f, err)
+ return fmt.Errorf("Failed to remove original tiff %s: %v\n", f, err)
}
+ return nil
+ }
+
+ r.Close()
+ err = os.Remove(f)
+ if err != nil {
+ return fmt.Errorf("Failed to remove invalid image %s: %v", f, err)
}
return nil