Skip to content

Commit

Permalink
Fix tiff compression type
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Aug 12, 2023
1 parent eb11c3a commit 14610a2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions converter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/sunshineplan/imgconv v0.0.0-00010101000000-000000000000
github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906
github.com/sunshineplan/utils v0.1.46
)

Expand Down
2 changes: 2 additions & 0 deletions converter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sunshineplan/pdf v1.0.5 h1:hPdpaqMZEHY99kDO5fBfmHrojBpmxbVsNreVALcFWEU=
github.com/sunshineplan/pdf v1.0.5/go.mod h1:Ljdz22+kL80CUXCIGdRUwA7PsfB7gj7ni4U71k5ZHp4=
github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906 h1:+yYRCj+PGQNnnen4+/Q7eKD2J87RJs+O39bjtHhPauk=
github.com/sunshineplan/tiff v0.0.0-20220128141034-29b9d69bd906/go.mod h1:O+Ar7ouRbdfxLgoZLFz447/dvdM1NVKk1VpOQaijvAU=
github.com/sunshineplan/utils v0.1.46 h1:m43TSqYY0K+n5rY93+7Hr1UCWvtWQYve31nB7G5Us9g=
github.com/sunshineplan/utils v0.1.46/go.mod h1:7zhDUGgKo2FMFzs7j6IL7B/lh3BRuE7rb7R7IgGOAfc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
34 changes: 26 additions & 8 deletions converter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import (
"time"

"github.com/sunshineplan/imgconv"
"github.com/sunshineplan/tiff"
"github.com/sunshineplan/utils/flags"
"github.com/sunshineplan/utils/progressbar"
"github.com/sunshineplan/utils/workers"
)

var (
supported = regexp.MustCompile(`(?i)\.(jpe?g|png|gif|tiff?|bmp|pdf|webp)$`)
tiffImage = regexp.MustCompile(`(?i)\.tiff?$`)
)

var (
src = flag.String("src", "", "")
dst = flag.String("dst", "output", "")
Expand All @@ -29,7 +35,7 @@ var (
whiteBackground = flag.Bool("white-background", false, "")
gray = flag.Bool("gray", false, "")
quality = flag.Int("quality", 75, "")
compression = flag.String("compression", "lzw", "")
compression = flag.String("compression", "deflate", "")
autoOrientation = flag.Bool("auto-orientation", false, "")
watermark = flag.String("watermark", "", "")
opacity = flag.Uint("opacity", 128, "")
Expand Down Expand Up @@ -61,7 +67,7 @@ func usage() {
--quality
set jpeg or pdf quality (range 1-100, default: 75)
--compression
set tiff compression type (none, lzw, deflate, default: lzw)
set tiff compression type (none, deflate, default: deflate)
--auto-orientation
auto orientation (default: false)
--watermark
Expand Down Expand Up @@ -120,8 +126,6 @@ func main() {
switch strings.ToLower(*compression) {
case "none":
ct = imgconv.TIFFUncompressed
case "lzw":
ct = imgconv.TIFFLZW
case "deflate":
ct = imgconv.TIFFDeflate
default:
Expand Down Expand Up @@ -215,8 +219,7 @@ func main() {
}()

filepath.WalkDir(*src, func(path string, d fs.DirEntry, _ error) error {
if ok, _ := regexp.MatchString(`^\.(jpe?g|png|gif|tiff?|bmp|pdf|webp)$`,
strings.ToLower(filepath.Ext(d.Name()))); ok {
if supported.MatchString(d.Name()) {
images = append(images, path)
}

Expand Down Expand Up @@ -261,7 +264,7 @@ func main() {
return
}

img, err := imgconv.Open(image, imgconv.AutoOrientation(*autoOrientation))
img, err := open(image)
if err != nil {
log.Println(image, err)
return
Expand Down Expand Up @@ -302,7 +305,7 @@ func main() {
return
}

base, err := imgconv.Open(*src, imgconv.AutoOrientation(*autoOrientation))
base, err := open(*src)
if err != nil {
log.Print(err)
code = 1
Expand Down Expand Up @@ -331,3 +334,18 @@ func main() {
}
log.Print("Done.")
}

func open(file string) (image.Image, error) {
img, err := imgconv.Open(file, imgconv.AutoOrientation(*autoOrientation))
if err != nil {
if tiffImage.MatchString(file) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
return tiff.Decode(f)
}
}
return img, nil
}
11 changes: 1 addition & 10 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,12 @@ type TIFFCompression int
const (
TIFFUncompressed TIFFCompression = iota
TIFFDeflate
TIFFLZW
TIFFCCITTGroup3
TIFFCCITTGroup4
)

func (c TIFFCompression) value() tiff.CompressionType {
switch c {
case TIFFLZW:
return tiff.LZW
case TIFFDeflate:
return tiff.Deflate
case TIFFCCITTGroup3:
return tiff.CCITTGroup3
case TIFFCCITTGroup4:
return tiff.CCITTGroup4
}
return tiff.Uncompressed
}
Expand All @@ -106,7 +97,7 @@ var defaultEncodeConfig = encodeConfig{
gifQuantizer: nil,
gifDrawer: nil,
pngCompressionLevel: png.DefaultCompression,
tiffCompressionType: TIFFLZW,
tiffCompressionType: TIFFDeflate,
}

// EncodeOption sets an optional parameter for the Encode and Save functions.
Expand Down
2 changes: 1 addition & 1 deletion format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestEncode(t *testing.T) {
{Format: JPEG, EncodeOption: []EncodeOption{Quality(75)}},
{Format: PNG, EncodeOption: []EncodeOption{PNGCompressionLevel(png.DefaultCompression)}},
{Format: GIF, EncodeOption: []EncodeOption{GIFNumColors(256), GIFDrawer(draw.FloydSteinberg), GIFQuantizer(nil)}},
{Format: TIFF, EncodeOption: []EncodeOption{TIFFCompressionType(TIFFLZW)}},
{Format: TIFF, EncodeOption: []EncodeOption{TIFFCompressionType(TIFFDeflate)}},
{Format: BMP},
{Format: PDF, EncodeOption: []EncodeOption{Quality(75)}},
}
Expand Down

0 comments on commit 14610a2

Please sign in to comment.