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

Remove implicit bitmap to png conversions #301

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
31 changes: 0 additions & 31 deletions vips/foreign.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"bytes"
"encoding/xml"
"fmt"
"image/png"
"math"
"runtime"
"unsafe"

"golang.org/x/image/bmp"
"golang.org/x/net/html/charset"
)

Expand Down Expand Up @@ -257,19 +255,8 @@ func vipsLoadFromBuffer(buf []byte, params *ImportParams) (*C.VipsImage, ImageTy
// Reference src here so it's not garbage collected during image initialization.
defer runtime.KeepAlive(src)

var err error

imageType := DetermineImageType(src)

if imageType == ImageTypeBMP {
src, err = bmpToPNG(src)
if err != nil {
return nil, ImageTypeUnknown, err
}

imageType = ImageTypePNG
}

if !IsTypeSupported(imageType) {
govipsLog("govips", LogLevelInfo, fmt.Sprintf("failed to understand image format size=%d", len(src)))
return nil, ImageTypeUnknown, ErrUnsupportedImageFormat
Expand All @@ -284,24 +271,6 @@ func vipsLoadFromBuffer(buf []byte, params *ImportParams) (*C.VipsImage, ImageTy
return importParams.outputImage, imageType, nil
}

func bmpToPNG(src []byte) ([]byte, error) {
i, err := bmp.Decode(bytes.NewReader(src))
if err != nil {
return nil, err
}

var w bytes.Buffer
pngEnc := png.Encoder{
CompressionLevel: png.NoCompression,
}
err = pngEnc.Encode(&w, i)
if err != nil {
return nil, err
}

return w.Bytes(), nil
}

func maybeSetBoolParam(p BoolParameter, cp *C.Param) {
if p.IsSet() {
C.set_bool_param(cp, toGboolean(p.Get()))
Expand Down
16 changes: 0 additions & 16 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@ func TestImageRef_HEIF_ftypmsf1(t *testing.T) {
assert.Equal(t, ImageTypeHEIF, metadata.Format)
}

func TestImageRef_BMP__ImplicitConversionToPNG(t *testing.T) {
Startup(nil)

raw, err := ioutil.ReadFile(resources + "bmp.bmp")
require.NoError(t, err)

img, err := NewImageFromBuffer(raw)
require.NoError(t, err)
require.NotNil(t, img)

exported, metadata, err := img.ExportNative()
assert.NoError(t, err)
assert.Equal(t, ImageTypePNG, metadata.Format)
assert.NotNil(t, exported)
}

func TestImageRef_SVG(t *testing.T) {
Startup(nil)

Expand Down
14 changes: 1 addition & 13 deletions vips/resample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vips
// #include "resample.h"
import "C"
import (
"io/ioutil"
"runtime"
"unsafe"
)
Expand Down Expand Up @@ -75,13 +74,7 @@ func vipsThumbnailFromFile(filename string, width, height int, crop Interesting,

if err := C.thumbnail(cFileName, &out, C.int(width), C.int(height), C.int(crop), C.int(size)); err != 0 {
err := handleImageError(out)
if src, err2 := ioutil.ReadFile(filename); err2 == nil {
if isBMP(src) {
if src2, err3 := bmpToPNG(src); err3 == nil {
return vipsThumbnailFromBuffer(src2, width, height, crop, size, params)
}
}
}

return nil, ImageTypeUnknown, err
}

Expand Down Expand Up @@ -109,11 +102,6 @@ func vipsThumbnailFromBuffer(buf []byte, width, height int, crop Interesting, si
}
if err != 0 {
err := handleImageError(out)
if isBMP(src) {
if src2, err2 := bmpToPNG(src); err2 == nil {
return vipsThumbnailFromBuffer(src2, width, height, crop, size, params)
}
}
return nil, ImageTypeUnknown, err
}

Expand Down