Skip to content

Commit

Permalink
Added TIFF support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaxray committed Mar 25, 2018
1 parent 725ebb5 commit 3e750d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
32 changes: 24 additions & 8 deletions img.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,35 @@ import (
"strconv"
"strings"

"os"

"github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/creator"
"golang.org/x/image/tiff"
)

var pageMargin [4]float64
var pageSize creator.PageSize
var sizeHasSet, marginHasSet = false, false
var tiffExts = []string{".tiff", ".TIFF", ".tif", ".TIF"}

type ImgSource struct {
source
}

func (s ImgSource) MergeTo(c *creator.Creator) error {
return addImage(s.path, c, s.mime)
debugInfo(fmt.Sprintf("Adding Image: %+v", s.source))
return addImage(s.source, c)
}

func addImage(filePath string, c *creator.Creator, fileType string) error {
debugInfo(fmt.Sprintf("Adding image: %s", filePath))

img, err := creator.NewImageFromFile(filePath)
func addImage(s source, c *creator.Creator) error {
img, err := createImage(s, c)
if err != nil {
return err
}

// The following funcs must be called in sequence
setEncoding(img, fileType)
setEncoding(img, s)
setMargin(img, c)
setSize(img, c)

Expand All @@ -44,6 +47,19 @@ func addImage(filePath string, c *creator.Creator, fileType string) error {
return nil
}

func createImage(s source, c *creator.Creator) (*creator.Image, error) {
if in_array(s.ext, tiffExts) {
f, _ := os.Open(s.path)
i, err := tiff.Decode(f)
if err != nil {
log.Fatalln(err.Error())
}
return creator.NewImageFromGoImage(i)
}

return creator.NewImageFromFile(s.path)
}

func setMargin(img *creator.Image, c *creator.Creator) {

if !marginHasSet {
Expand Down Expand Up @@ -99,9 +115,9 @@ func setSize(img *creator.Image, c *creator.Creator) {

// Set appropriate encoding for JPEG and TIFF
// MUST be called before changing image size
func setEncoding(img *creator.Image, fileType string) {
func setEncoding(img *creator.Image, s source) {

switch fileType {
switch s.mime {
case "image/jpeg":
debugInfo(fmt.Sprintf("JPEG found. Setting encoding to DCTEncoder."))
encoder := core.NewDCTEncoder()
Expand Down
4 changes: 4 additions & 0 deletions pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type PDFSource struct {
}

func (s PDFSource) MergeTo(c *creator.Creator) error {
debugInfo(fmt.Sprintf("Adding PDF: %+v", s.source))

f, _ := os.Open(s.path)
defer f.Close()

return addPdfPages(f, s.pages, c)
}

Expand Down

0 comments on commit 3e750d6

Please sign in to comment.