psd

package module
v0.0.0-...-5db5eaf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2022 License: MIT Imports: 11 Imported by: 23

README

PSD/PSB(Photoshop) file reader for Go programming language

It works almost well but it is still in development.

How to use

Example1

Simple psd -> png conversion.

package main

import (
	"image"
	"image/png"
	"os"

	_ "github.com/oov/psd"
)

func main() {
	file, err := os.Open("image.psd")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	img, _, err := image.Decode(file)
	if err != nil {
		panic(err)
	}

	out, err := os.Create("image.png")
	if err != nil {
		panic(err)
	}
	err = png.Encode(out, img)
	if err != nil {
		panic(err)
	}
}
Example2

Extract all layer images.

package main

import (
	"fmt"
	"image/png"
	"os"

	"github.com/oov/psd"
)

func processLayer(filename string, layerName string, l *psd.Layer) error {
	if len(l.Layer) > 0 {
		for i, ll := range l.Layer {
			if err := processLayer(
				fmt.Sprintf("%s_%03d", filename, i),
				layerName+"/"+ll.Name, &ll); err != nil {
				return err
			}
		}
	}
	if !l.HasImage() {
		return nil
	}
	fmt.Printf("%s -> %s.png\n", layerName, filename)
	out, err := os.Create(fmt.Sprintf("%s.png", filename))
	if err != nil {
		return err
	}
	defer out.Close()
	return png.Encode(out, l.Picker)
}

func main() {
	file, err := os.Open("image.psd")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	img, _, err := psd.Decode(file, &psd.DecodeOptions{SkipMergedImage: true})
	if err != nil {
		panic(err)
	}
	for i, layer := range img.Layer {
		if err = processLayer(fmt.Sprintf("%03d", i), layer.Name, &layer); err != nil {
			panic(err)
		}
	}
}

Current status

It is not implemented any blending functions because layer composition isn't covered by this package at present.

Color Modes

Implemented
  • Bitmap 1bit
  • Grayscale 8bit
  • Grayscale 16bit
  • Grayscale 32bit
  • Indexed
  • RGB 8bit
  • RGB 16bit
  • RGB 32bit
  • CMYK 8bit
  • CMYK 16bit
Not implemented
  • CMYK 32bit
  • Multichannel
  • Duotone
  • Lab

Supported Compression Methods

  • Raw
  • RLE(PackBits)
  • ZIP without prediction (not tested)
  • ZIP with prediction

Documentation

Index

Constants

View Source
const (
	BlendModePassThrough  = BlendMode("pass")
	BlendModeNormal       = BlendMode("norm")
	BlendModeDissolve     = BlendMode("diss")
	BlendModeDarken       = BlendMode("dark")
	BlendModeMultiply     = BlendMode("mul ")
	BlendModeColorBurn    = BlendMode("idiv")
	BlendModeLinearBurn   = BlendMode("lbrn")
	BlendModeDarkerColor  = BlendMode("dkCl")
	BlendModeLighten      = BlendMode("lite")
	BlendModeScreen       = BlendMode("scrn")
	BlendModeColorDodge   = BlendMode("div ")
	BlendModeLinearDodge  = BlendMode("lddg")
	BlendModeLighterColor = BlendMode("lgCl")
	BlendModeOverlay      = BlendMode("over")
	BlendModeSoftLight    = BlendMode("sLit")
	BlendModeHardLight    = BlendMode("hLit")
	BlendModeVividLight   = BlendMode("vLit")
	BlendModeLinearLight  = BlendMode("lLit")
	BlendModePinLight     = BlendMode("pLit")
	BlendModeHardMix      = BlendMode("hMix")
	BlendModeDifference   = BlendMode("diff")
	BlendModeExclusion    = BlendMode("smud")
	BlendModeSubtract     = BlendMode("fsub")
	BlendModeDivide       = BlendMode("fdiv")
	BlendModeHue          = BlendMode("hue ")
	BlendModeSaturation   = BlendMode("sat ")
	BlendModeColor        = BlendMode("colr")
	BlendModeLuminosity   = BlendMode("lum ")
)

These blend modes are defined in this document.

http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_13084

View Source
const (
	ColorModeBitmap       = ColorMode(0)
	ColorModeGrayscale    = ColorMode(1)
	ColorModeIndexed      = ColorMode(2)
	ColorModeRGB          = ColorMode(3)
	ColorModeCMYK         = ColorMode(4)
	ColorModeMultichannel = ColorMode(7)
	ColorModeDuotone      = ColorMode(8)
	ColorModeLab          = ColorMode(9)
)

These color modes are defined in this document.

http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_19840

View Source
const (
	CompressionMethodRaw                  = CompressionMethod(0)
	CompressionMethodRLE                  = CompressionMethod(1)
	CompressionMethodZIPWithoutPrediction = CompressionMethod(2)
	CompressionMethodZIPWithPrediction    = CompressionMethod(3)
)

These compression methods are defined in this document.

http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1054855

View Source
const (
	AdditionalInfoKeyLayerInfo              = AdditionalInfoKey("Layr")
	AdditionalInfoKeyLayerInfo16            = AdditionalInfoKey("Lr16")
	AdditionalInfoKeyLayerInfo32            = AdditionalInfoKey("Lr32")
	AdditionalInfoKeyUnicodeLayerName       = AdditionalInfoKey("luni")
	AdditionalInfoKeyBlendClippingElements  = AdditionalInfoKey("clbl")
	AdditionalInfoKeySectionDividerSetting  = AdditionalInfoKey("lsct")
	AdditionalInfoKeySectionDividerSetting2 = AdditionalInfoKey("lsdk")
)

These keys are defined in this document.

http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_71546

Variables

View Source
var Debug logger

Debug is useful for debugging.

You can use by performing the following steps.

psd.Debug = log.New(os.Stdout, "psd: ", log.Lshortfile)

Functions

This section is empty.

Types

type AdditionalInfoKey

type AdditionalInfoKey string

AdditionalInfoKey represents the key of the additional layer information.

func (AdditionalInfoKey) LenSize

func (a AdditionalInfoKey) LenSize(largeDocument bool) int

LenSize returns bytes of the length for this key.

type BlendMode

type BlendMode string

BlendMode represents the blend mode.

func (BlendMode) String

func (bm BlendMode) String() string

String implements fmt.Stringer interface.

The return value respects blend name that is described in "Compositing and Blending Level 1"(https://www.w3.org/TR/compositing-1/#blending).

type Channel

type Channel struct {
	// Data is uncompressed channel image data.
	Data   []byte
	Picker image.Image
}

Channel represents a channel of the color.

type ColorMode

type ColorMode int16

ColorMode represents color mode that is used in psd file.

func (ColorMode) Channels

func (c ColorMode) Channels() int

Channels returns the number of channels for the color mode. The return value is not including alpha channel.

type CompressionMethod

type CompressionMethod int16

CompressionMethod represents compression method that is used in psd file.

func (CompressionMethod) Decode

func (cm CompressionMethod) Decode(dest []byte, r io.Reader, sizeHint int64, rect image.Rectangle, depth int, channels int, large bool) (read int, err error)

Decode decodes the compressed image data from r.

You can pass 0 to sizeHint if unknown, but in this case may read more data than necessary from r.

type Config

type Config struct {
	Version       int
	Rect          image.Rectangle
	Channels      int
	Depth         int // 1 or 8 or 16 or 32
	ColorMode     ColorMode
	ColorModeData []byte
	Res           map[int]ImageResource
}

Config represents Photoshop image file configuration.

func DecodeConfig

func DecodeConfig(r io.Reader) (cfg Config, read int, err error)

DecodeConfig returns the color model and dimensions of a image without decoding the entire image.

func (*Config) PSB

func (cfg *Config) PSB() bool

PSB returns whether image is large document format.

func (*Config) Palette

func (cfg *Config) Palette() color.Palette

Palette returns Palette if any.

type DecodeOptions

type DecodeOptions struct {
	SkipLayerImage   bool
	SkipMergedImage  bool
	ConfigLoaded     func(cfg Config) error
	LayerImageLoaded func(layer *Layer, index int, total int)
}

DecodeOptions are the decoding options.

type ImageResource

type ImageResource struct {
	Name string
	Data []byte
}

ImageResource represents the image resource that is used in psd file.

type Layer

type Layer struct {
	SeqID int

	Name        string
	UnicodeName string
	MBCSName    string

	Rect image.Rectangle
	// Channel key is the channel ID.
	// 	0 = red, 1 = green, etc.
	// 	-1 = transparency mask
	// 	-2 = user supplied layer mask
	// 	-3 = real user supplied layer mask
	//         (when both a user mask and a vector mask are present)
	Channel               map[int]Channel
	BlendMode             BlendMode
	Opacity               uint8
	Clipping              bool
	BlendClippedElements  bool
	Flags                 uint8
	Mask                  Mask
	Picker                image.Image
	AdditionalLayerInfo   map[AdditionalInfoKey][]byte
	SectionDividerSetting struct {
		Type      int
		BlendMode BlendMode
		SubType   int
	}

	Layer []Layer
}

Layer represents layer.

func (*Layer) Folder

func (l *Layer) Folder() bool

Folder returns whether the layer is folder(group layer).

func (*Layer) FolderIsOpen

func (l *Layer) FolderIsOpen() bool

FolderIsOpen returns whether the folder is opened when layer is folder.

func (*Layer) HasImage

func (l *Layer) HasImage() bool

HasImage returns whether the layer has an image data.

func (*Layer) String

func (l *Layer) String() string

func (*Layer) TransparencyProtected

func (l *Layer) TransparencyProtected() bool

TransparencyProtected returns whether the layer's transparency being protected.

func (*Layer) Visible

func (l *Layer) Visible() bool

Visible returns whether the layer is visible.

type Mask

type Mask struct {
	Rect         image.Rectangle
	DefaultColor int
	Flags        int

	RealRect            image.Rectangle
	RealBackgroundColor int
	RealFlags           int

	UserMaskDensity   int
	UserMaskFeather   float64
	VectorMaskDensity int
	VectorMaskFeather float64
}

Mask represents layer mask and vector mask.

func (*Mask) Enabled

func (m *Mask) Enabled() bool

Enabled returns whether the mask is enabled.

func (*Mask) RealEnabled

func (m *Mask) RealEnabled() bool

RealEnabled returns whether the user real mask is enabled.

type PSD

type PSD struct {
	Config             Config
	Channel            map[int]Channel
	Layer              []Layer
	AdditinalLayerInfo map[AdditionalInfoKey][]byte
	// Data is uncompressed merged image data.
	Data   []byte
	Picker image.Image
}

PSD represents Photoshop image file.

func Decode

func Decode(r io.Reader, o *DecodeOptions) (psd *PSD, read int, err error)

Decode reads a image from r and returns it. Default parameters are used if a nil *DecodeOptions is passed.

Directories

Path Synopsis
Code generated by genrgba2nrgba.go.
Code generated by genrgba2nrgba.go.
Package composite implements PSD image compositor.
Package composite implements PSD image compositor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL