Skip to content

Commit

Permalink
Merge pull request #57 from pixiv/feature/support_encoding_rgb_jpeg
Browse files Browse the repository at this point in the history
Support encoding rgb.Image.
  • Loading branch information
Michii Shunsuke authored Aug 22, 2019
2 parents 83566ac + a8de141 commit 3da21a7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
37 changes: 37 additions & 0 deletions jpeg/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ import (
"image"
"io"
"unsafe"

"github.com/pixiv/go-libjpeg/rgb"
)

// EncoderOptions specifies which settings to use during Compression.
Expand Down Expand Up @@ -235,6 +237,8 @@ func Encode(w io.Writer, src image.Image, opt *EncoderOptions) (err error) {
err = encodeGray(cinfo, s, opt)
case *image.RGBA:
err = encodeRGBA(cinfo, s, opt)
case *rgb.Image:
err = encodeRGB(cinfo, s, opt)
default:
return errors.New("unsupported image type")
}
Expand Down Expand Up @@ -350,6 +354,39 @@ func encodeRGBA(cinfo *C.struct_jpeg_compress_struct, src *image.RGBA, p *Encode
return
}

// encode rgb.Image.
func encodeRGB(cinfo *C.struct_jpeg_compress_struct, src *rgb.Image, p *EncoderOptions) (err error) {
// Set up compression parameters
w, h := src.Bounds().Dx(), src.Bounds().Dy()
cinfo.image_width = C.JDIMENSION(w)
cinfo.image_height = C.JDIMENSION(h)
cinfo.input_components = 3
cinfo.in_color_space = C.JCS_RGB

setupEncoderOptions(cinfo, p)

// Start compression
err = startCompress(cinfo)
if err != nil {
return
}
defer func() {
ferr := finishCompress(cinfo)
if ferr != nil && err == nil {
err = ferr
}
}()

for v := 0; v < h; {
line, err := writeScanline(cinfo, C.JSAMPROW(unsafe.Pointer(&src.Pix[v*src.Stride])), C.JDIMENSION(1))
if err != nil {
return err
}
v += line
}
return
}

// encode image.Gray
func encodeGray(cinfo *C.struct_jpeg_compress_struct, src *image.Gray, p *EncoderOptions) (err error) {
// Set up compression parameters
Expand Down
12 changes: 6 additions & 6 deletions jpeg/issue51_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/pixiv/go-libjpeg/jpeg"
)

var data = []byte("\xff\xd8\xff\xdb\x00C\x000000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"00000000000\xff\xc9\x00\v\b00\x000" +
"\x01\x01\x14\x00\xff\xda\x00\b\x01\x010\x00?\x0000")

// https://github.com/pixiv/go-libjpeg/issues/51
func TestIssue51(t *testing.T) {
data := []byte("\xff\xd8\xff\xdb\x00C\x000000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"00000000000\xff\xc9\x00\v\b00\x000" +
"\x01\x01\x14\x00\xff\xda\x00\b\x01\x010\x00?\x0000")

jpeg.Decode(bytes.NewReader(data), &jpeg.DecoderOptions{})
}
30 changes: 30 additions & 0 deletions jpeg/issue55_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package jpeg_test

import (
"bytes"
"testing"

"github.com/pixiv/go-libjpeg/jpeg"
)

// https://github.com/pixiv/go-libjpeg/issues/55
func TestDecodeAndEncodeRGBJPEG(t *testing.T) {
data := []byte("\xff\xd8\xff\xdb\x00C\x000000000000000" +
"00000000000000000000" +
"00000000000000000000" +
"00000000000\xff\xc0\x00\x11\b\x00000" +
"\x03R\"\x00G\x11\x00B\x11\x00\xff\xda\x00\f\x03R\x00G\x00B" +
"\x00")

img, err := jpeg.Decode(bytes.NewReader(data), &jpeg.DecoderOptions{})
if err != nil {
t.Log(err)
return
}

var w bytes.Buffer
err = jpeg.Encode(&w, img, &jpeg.EncoderOptions{})
if err != nil {
t.Errorf("encoding after decoding failed: %v", err)
}
}

0 comments on commit 3da21a7

Please sign in to comment.