Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factored out ToImage from ToJpegData #6

Merged
merged 1 commit into from
Dec 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions raw_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,8 @@ func (r *RawData) ConvertToDataMap() data.Map {
}
}

// ToJpegData convert JPGE format image bytes.
func (r *RawData) ToJpegData(quality int) ([]byte, error) {
if r.Format == TypeJPEG {
return r.Data, nil
}
// ToImage converts to image.Image
func (r *RawData) ToImage() (image.Image, error) {
// BGR to RGB
rgba := image.NewRGBA(image.Rect(0, 0, r.Width, r.Height))
if r.Format == TypeCVMAT {
Expand All @@ -169,9 +166,21 @@ func (r *RawData) ToJpegData(quality int) ([]byte, error) {
rgba.Pix[i+3] = r.Data[j+3]
}
} else {
return []byte{}, fmt.Errorf("'%v' cannot convert to JPEG", r.Format)
return nil, fmt.Errorf("'%v' cannot convert to image", r.Format)
}
return rgba, nil
}

// ToJpegData convert JPGE format image bytes.
func (r *RawData) ToJpegData(quality int) ([]byte, error) {
if r.Format == TypeJPEG {
return r.Data, nil
}
if rgba, err := r.ToImage(); err != nil {
return []byte{}, err
} else {
w := bytes.NewBuffer([]byte{})
err := jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality})
return w.Bytes(), err
}
w := bytes.NewBuffer([]byte{})
err := jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality})
return w.Bytes(), err
}