diff --git a/pdf/contentstream/processor.go b/pdf/contentstream/processor.go index 50f0a25a..550702c7 100644 --- a/pdf/contentstream/processor.go +++ b/pdf/contentstream/processor.go @@ -127,7 +127,7 @@ func (csp *ContentStreamProcessor) getColorspace(name string, resources *PdfPage } // Otherwise unsupported. - common.Log.Debug("Unknown colorspace requested: %s", name) + common.Log.Error("Unknown colorspace requested: %s", name) return nil, errors.New("Unsupported colorspace") } @@ -140,6 +140,10 @@ func (csp *ContentStreamProcessor) getInitialColor(cs PdfColorspace) (PdfColor, return NewPdfColorDeviceRGB(0.0, 0.0, 0.0), nil case *PdfColorspaceDeviceCMYK: return NewPdfColorDeviceCMYK(0.0, 0.0, 0.0, 1.0), nil + case *PdfColorspaceCalGray: + return NewPdfColorCalGray(0.0), nil + case *PdfColorspaceCalRGB: + return NewPdfColorCalRGB(0.0, 0.0, 0.0), nil case *PdfColorspaceLab: l := 0.0 a := 0.0 @@ -190,7 +194,7 @@ func (csp *ContentStreamProcessor) getInitialColor(cs PdfColorspace) (PdfColor, return nil, nil } - common.Log.Debug("Unable to determine initial color for unknown colorspace: %T", cs) + common.Log.Error("Unable to determine initial color for unknown colorspace: %T", cs) return nil, errors.New("Unsupported colorspace") } @@ -364,8 +368,8 @@ func (this *ContentStreamProcessor) handleCommand_SC(op *ContentStreamOperation, cs := this.graphicsState.ColorspaceStroking if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -389,8 +393,8 @@ func (this *ContentStreamProcessor) handleCommand_SCN(op *ContentStreamOperation if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } } @@ -411,8 +415,8 @@ func (this *ContentStreamProcessor) handleCommand_sc(op *ContentStreamOperation, if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } } @@ -433,8 +437,8 @@ func (this *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation if !isPatternCS(cs) { if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } } @@ -454,8 +458,8 @@ func (this *ContentStreamProcessor) handleCommand_scn(op *ContentStreamOperation func (this *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceGray() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -475,8 +479,8 @@ func (this *ContentStreamProcessor) handleCommand_G(op *ContentStreamOperation, func (this *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceGray() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -496,8 +500,8 @@ func (this *ContentStreamProcessor) handleCommand_g(op *ContentStreamOperation, func (this *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceRGB() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -516,8 +520,8 @@ func (this *ContentStreamProcessor) handleCommand_RG(op *ContentStreamOperation, func (this *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceRGB() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -537,8 +541,8 @@ func (this *ContentStreamProcessor) handleCommand_rg(op *ContentStreamOperation, func (this *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceCMYK() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } @@ -557,8 +561,8 @@ func (this *ContentStreamProcessor) handleCommand_K(op *ContentStreamOperation, func (this *ContentStreamProcessor) handleCommand_k(op *ContentStreamOperation, resources *PdfPageResources) error { cs := NewPdfColorspaceDeviceCMYK() if len(op.Params) != cs.GetNumComponents() { - common.Log.Debug("Invalid number of parameters for SC") - common.Log.Debug("Number %d not matching colorspace %T", len(op.Params), cs) + common.Log.Error("Invalid number of parameters for SC") + common.Log.Error("Number %d not matching colorspace %T", len(op.Params), cs) return errors.New("Invalid number of parameters") } diff --git a/pdf/model/colorspace.go b/pdf/model/colorspace.go index ef1bbb05..f4a6572f 100644 --- a/pdf/model/colorspace.go +++ b/pdf/model/colorspace.go @@ -354,6 +354,7 @@ func (this *PdfColorspaceDeviceRGB) ImageToRGB(img Image) (Image, error) { } func (this *PdfColorspaceDeviceRGB) ImageToGray(img Image) (Image, error) { + grayImage := img samples := img.GetSamples() @@ -374,7 +375,9 @@ func (this *PdfColorspaceDeviceRGB) ImageToGray(img Image) (Image, error) { // Convert to uint32 val := uint32(grayValue * maxVal) + graySamples = append(graySamples, val) + } grayImage.SetSamples(graySamples) grayImage.ColorComponents = 1 @@ -893,7 +896,7 @@ func (this *PdfColorspaceCalRGB) String() string { } func (this *PdfColorspaceCalRGB) GetNumComponents() int { - return 1 + return 3 } func newPdfColorspaceCalRGBFromPdfObject(obj PdfObject) (*PdfColorspaceCalRGB, error) { @@ -1119,7 +1122,7 @@ func (this *PdfColorspaceCalRGB) ImageToRGB(img Image) (Image, error) { maxVal := math.Pow(2, float64(img.BitsPerComponent)) - 1 rgbSamples := []uint32{} - for i := 0; i < len(samples); i++ { + for i := 0; i < len(samples)-2; i++ { // A, B, C in range 0.0 to 1.0 aVal := float64(samples[i]) / maxVal bVal := float64(samples[i+1]) / maxVal @@ -1916,6 +1919,7 @@ func (this *PdfColorspaceSpecialPattern) ColorFromFloats(vals []float64) (PdfCol // the name of the pattern. func (this *PdfColorspaceSpecialPattern) ColorFromPdfObjects(objects []PdfObject) (PdfColor, error) { if len(objects) < 1 { + common.Log.Error("ColorFromPdfObjects: len(objects)=%d", len(objects)) return nil, errors.New("Invalid number of parameters") } patternColor := &PdfColorPattern{}