Skip to content

Commit

Permalink
Fixing lab colorspace component input ranges. Fix Indexed cs Image to…
Browse files Browse the repository at this point in the history
… rgb conversion.
  • Loading branch information
Gunnsteinn Hall authored and Gunnsteinn Hall committed Aug 7, 2017
1 parent d3d0312 commit e2bfa90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
68 changes: 20 additions & 48 deletions pdf/contentstream/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func (this *ContentStreamProcessor) Process(resources *PdfPageResources) error {
this.graphicsState.ColorNonStroking = NewPdfColorDeviceGray(0)

for _, op := range this.operations {
var err error

// Internal handling.
switch op.Operand {
case "q":
Expand All @@ -211,65 +213,34 @@ func (this *ContentStreamProcessor) Process(resources *PdfPageResources) error {

// Color operations (Table 74 p. 179)
case "CS":
err := this.handleCommand_CS(op, resources)
if err != nil {
return err
}
err = this.handleCommand_CS(op, resources)
case "cs":
err := this.handleCommand_cs(op, resources)
if err != nil {
return err
}
err = this.handleCommand_cs(op, resources)
case "SC":
err := this.handleCommand_SC(op, resources)
if err != nil {
return err
}
err = this.handleCommand_SC(op, resources)
case "SCN":
err := this.handleCommand_SCN(op, resources)
if err != nil {
return err
}
err = this.handleCommand_SCN(op, resources)
case "sc":
err := this.handleCommand_sc(op, resources)
if err != nil {
return err
}
err = this.handleCommand_sc(op, resources)
case "scn":
err := this.handleCommand_scn(op, resources)
if err != nil {
return err
}
err = this.handleCommand_scn(op, resources)
case "G":
err := this.handleCommand_G(op, resources)
if err != nil {
return err
}
err = this.handleCommand_G(op, resources)
case "g":
err := this.handleCommand_g(op, resources)
if err != nil {
return err
}
err = this.handleCommand_g(op, resources)
case "RG":
err := this.handleCommand_RG(op, resources)
if err != nil {
return err
}
err = this.handleCommand_RG(op, resources)
case "rg":
err := this.handleCommand_rg(op, resources)
if err != nil {
return err
}
err = this.handleCommand_rg(op, resources)
case "K":
err := this.handleCommand_K(op, resources)
if err != nil {
return err
}
err = this.handleCommand_K(op, resources)
case "k":
err := this.handleCommand_k(op, resources)
if err != nil {
return err
}
err = this.handleCommand_k(op, resources)
}
if err != nil {
common.Log.Debug("Processor handling error (%s): %v", op.Operand, err)
common.Log.Debug("Operand: %#v", op.Operand)
return err
}

// Check if have external handler also, and process if so.
Expand Down Expand Up @@ -440,6 +411,7 @@ func (this *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation

color, err := cs.ColorFromPdfObjects(op.Params)
if err != nil {
common.Log.Debug("ERROR: Fail to get color from params: %+v (CS is %+v)", op.Params, cs)
return err
}

Expand Down
38 changes: 34 additions & 4 deletions pdf/model/colorspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -1413,19 +1413,34 @@ func (this *PdfColorspaceLab) ColorFromFloats(vals []float64) (PdfColor, error)

// L
l := vals[0]
if l < 0.0 || l > 1.0 {
if l < 0.0 || l > 100.0 {
common.Log.Debug("L out of range (got %v should be 0-100)", l)
return nil, errors.New("Range check")
}

// A
a := vals[1]
if a < 0.0 || a > 1.0 {
aMin := float64(-100)
aMax := float64(100)
if len(this.Range) > 1 {
aMin = this.Range[0]
aMax = this.Range[1]
}
if a < aMin || a > aMax {
common.Log.Debug("A out of range (got %v; range %v to %v)", a, aMin, aMax)
return nil, errors.New("Range check")
}

// B.
b := vals[2]
if b < 0.0 || b > 1.0 {
bMin := float64(-100)
bMax := float64(100)
if len(this.Range) > 3 {
bMin = this.Range[2]
bMax = this.Range[3]
}
if b < bMin || b > bMax {
common.Log.Debug("b out of range (got %v; range %v to %v)", b, bMin, bMax)
return nil, errors.New("Range check")
}

Expand Down Expand Up @@ -1462,6 +1477,8 @@ func (this *PdfColorspaceLab) ColorToRGB(color PdfColor) (PdfColor, error) {
}

// Get normalized L*, a*, b* values. [0-1]
// XXX/FIXME: According to the pdf standard these values are going to be in range 0-100, -100 - 100, -100 - 100
// by default.
LNorm := lab.L()
ANorm := lab.A()
BNorm := lab.B()
Expand Down Expand Up @@ -2207,7 +2224,15 @@ func (this *PdfColorspaceSpecialIndexed) ColorToRGB(color PdfColor) (PdfColor, e

// Convert an indexed image to RGB.
func (this *PdfColorspaceSpecialIndexed) ImageToRGB(img Image) (Image, error) {
baseImage := img
//baseImage := img
// Make a new representation of the image to be converted with the base colorspace.
baseImage := Image{}
baseImage.Height = img.Height
baseImage.Width = img.Width
baseImage.alphaData = img.alphaData
baseImage.BitsPerComponent = img.BitsPerComponent
baseImage.hasAlpha = img.hasAlpha
baseImage.ColorComponents = img.ColorComponents

samples := img.GetSamples()
N := this.Base.GetNumComponents()
Expand Down Expand Up @@ -2371,11 +2396,15 @@ func (this *PdfColorspaceSpecialSeparation) ColorFromFloats(vals []float64) (Pdf
input := []float64{tint}
output, err := this.TintTransform.Evaluate(input)
if err != nil {
common.Log.Debug("Error, failed to evaluate: %v", err)
common.Log.Trace("Tint transform: %+v", this.TintTransform)
return nil, err
}

common.Log.Trace("Processing ColorFromFloats(%+v) on AlternateSpace: %#v", output, this.AlternateSpace)
color, err := this.AlternateSpace.ColorFromFloats(output)
if err != nil {
common.Log.Debug("Error, failed to evaluate in alternate space: %v", err)
return nil, err
}

Expand Down Expand Up @@ -2469,6 +2498,7 @@ func (this *PdfColorspaceDeviceN) String() string {
return "DeviceN"
}

// GetNumComponents returns the number of input color components, i.e. that are input to the tint transform.
func (this *PdfColorspaceDeviceN) GetNumComponents() int {
return len(*this.ColorantNames)
}
Expand Down

0 comments on commit e2bfa90

Please sign in to comment.