unioffice/common/image.go

108 lines
3.0 KiB
Go
Raw Normal View History

2017-08-28 20:56:18 -05:00
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
2017-09-10 20:05:47 -05:00
package common
2017-08-28 20:56:18 -05:00
import (
"fmt"
"image"
"os"
"baliance.com/gooxml/measurement"
2017-08-28 20:56:18 -05:00
// Add image format support
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"baliance.com/gooxml"
2017-08-28 20:56:18 -05:00
)
2017-09-10 20:05:47 -05:00
// Image is a container for image information. It's used as we need format and
// and size information to use images.
2017-08-28 20:56:18 -05:00
type Image struct {
Size image.Point
Format string
Path string
}
2017-09-10 20:05:47 -05:00
// ImageRef is a reference to an image within a document.
type ImageRef struct {
d *DocBase
rels Relationships
img Image
2017-08-28 20:56:18 -05:00
}
2017-09-10 20:05:47 -05:00
// MakeImageRef constructs an image reference which is a reference to a
// particular image file inside a document. The same image can be used multiple
// times in a document by re-use the ImageRef.
2017-09-10 20:05:47 -05:00
func MakeImageRef(img Image, d *DocBase, rels Relationships) ImageRef {
return ImageRef{img: img, d: d, rels: rels}
2017-08-28 20:56:18 -05:00
}
// RelID returns the relationship ID.
func (i ImageRef) RelID() string {
2017-09-10 20:05:47 -05:00
for imgIdx, ir := range i.d.Images {
if ir.img == i.img {
imgID := i.rels.FindRIDForN(imgIdx, gooxml.ImageType)
return imgID
}
}
return ""
}
// Format returns the format of the underlying image
func (i ImageRef) Format() string {
return i.img.Format
}
// Path returns the path to an image file
func (i ImageRef) Path() string {
return i.img.Path
}
2017-10-10 20:02:33 -04:00
// Size returns the size of an image
2017-09-10 20:05:47 -05:00
func (i ImageRef) Size() image.Point {
return i.img.Size
}
// RelativeHeight returns the relative height of an image given a fixed width.
// This is used when setting image to a fixed width to calculate the height
// required to keep the same image aspect ratio.
func (i ImageRef) RelativeHeight(w measurement.Distance) measurement.Distance {
hScale := float64(i.Size().Y) / float64(i.Size().X)
return w * measurement.Distance(hScale)
}
// RelativeWidth returns the relative width of an image given a fixed height.
// This is used when setting image to a fixed height to calculate the width
// required to keep the same image aspect ratio.
func (i ImageRef) RelativeWidth(h measurement.Distance) measurement.Distance {
wScale := float64(i.Size().X) / float64(i.Size().Y)
return h * measurement.Distance(wScale)
}
2017-08-28 20:56:18 -05:00
// ImageFromFile reads an image from a file on disk. It doesn't keep the image
// in memory and only reads it to determine the format and size. You can also
// construct an Image directly if the file and size are known.
func ImageFromFile(path string) (Image, error) {
f, err := os.Open(path)
r := Image{}
if err != nil {
return r, fmt.Errorf("error reading image: %s", err)
}
defer f.Close()
imgDec, ifmt, err := image.Decode(f)
if err != nil {
return r, fmt.Errorf("unable to parse image: %s", err)
}
r.Path = path
r.Format = ifmt
r.Size = imgDec.Bounds().Size()
return r, nil
}