Skip to content

Commit

Permalink
Fix #222
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Sep 18, 2020
1 parent 4c18535 commit f5a85ae
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions pkg/pdfcpu/readImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package pdfcpu

import (
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
_ "image/png"

Expand Down Expand Up @@ -52,7 +54,6 @@ func createSMaskObject(xRefTable *XRefTable, buf []byte, w, h, bpc int) (*Indire
}

func createFlateImageObject(xRefTable *XRefTable, buf, sm []byte, w, h, bpc int, cs string) (*StreamDict, error) {

var softMaskIndRef *IndirectRef

if sm != nil {
Expand Down Expand Up @@ -122,7 +123,6 @@ func createDCTImageObject(xRefTable *XRefTable, buf []byte, w, h int, cs string)
}

func writeRGBAImageBuf(img image.Image) []byte {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand All @@ -142,7 +142,6 @@ func writeRGBAImageBuf(img image.Image) []byte {
}

func writeRGBA64ImageBuf(img image.Image) []byte {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand All @@ -165,7 +164,6 @@ func writeRGBA64ImageBuf(img image.Image) []byte {
}

func writeYCbCrToRGBAImageBuf(img image.Image) []byte {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand All @@ -185,16 +183,17 @@ func writeYCbCrToRGBAImageBuf(img image.Image) []byte {
return buf
}
func writeNRGBAImageBuf(xRefTable *XRefTable, img image.Image) ([]byte, []byte) {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
buf := make([]byte, w*h*3)
var sm []byte
var softMask bool

fmt.Printf("writeNRGBAImageBuf: w=%d h=%d\n", w, h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
fmt.Printf("(%d/%d)\n", x, y)
c := img.At(x, y).(color.NRGBA)
if !softMask {
if xRefTable != nil && c.A != 0xFF {
Expand All @@ -220,7 +219,6 @@ func writeNRGBAImageBuf(xRefTable *XRefTable, img image.Image) ([]byte, []byte)
}

func writeNRGBA64ImageBuf(xRefTable *XRefTable, img image.Image) ([]byte, []byte) {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand Down Expand Up @@ -261,7 +259,6 @@ func writeNRGBA64ImageBuf(xRefTable *XRefTable, img image.Image) ([]byte, []byte
}

func writeGrayImageBuf(img image.Image) []byte {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand All @@ -279,7 +276,6 @@ func writeGrayImageBuf(img image.Image) []byte {
}

func writeCMYKImageBuf(img image.Image) []byte {

w := img.Bounds().Dx()
h := img.Bounds().Dy()
i := 0
Expand All @@ -300,12 +296,16 @@ func writeCMYKImageBuf(img image.Image) []byte {
return buf
}

func imgToImageDict(xRefTable *XRefTable, img image.Image) (*StreamDict, error) {
func convertToRGBA(img image.Image) *image.RGBA {
b := img.Bounds()
m := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(m, m.Bounds(), img, b.Min, draw.Src)
return m
}

func imgToImageDict(xRefTable *XRefTable, img image.Image) (*StreamDict, error) {
bpc := 8

// TODO if dpi != 72 resample (applies to PNG,JPG,TIFF)

w := img.Bounds().Dx()
h := img.Bounds().Dy()

Expand Down Expand Up @@ -364,9 +364,9 @@ func imgToImageDict(xRefTable *XRefTable, img image.Image) (*StreamDict, error)
return nil, errors.New("unsupported image type: NYCbCrA")

case *image.Paletted:
// uint8 indices into a given color palette.
// In-memory image of uint8 indices into a given palette.
cs = DeviceRGBCS
buf = writeRGBAImageBuf(img)
buf = writeRGBAImageBuf(convertToRGBA(img))

default:
return nil, errors.Errorf("unsupported image type: %T", img)
Expand All @@ -380,7 +380,6 @@ func imgToImageDict(xRefTable *XRefTable, img image.Image) (*StreamDict, error)
// ReadJPEG generates a PDF image object for a JPEG stream
// and appends this object to the cross reference table.
func ReadJPEG(xRefTable *XRefTable, buf []byte, c image.Config) (*StreamDict, error) {

var cs string

switch c.ColorModel {
Expand Down

0 comments on commit f5a85ae

Please sign in to comment.