Skip to content

Commit

Permalink
Remove implicit bitmap to png conversions
Browse files Browse the repository at this point in the history
The bitmap library used for this does not support the various bitmap
formats in the wild. This is also very confusing behaviour for a user of
the pre-v2 version of govips, which was able to handle all the various
bmp files

Closes davidbyttow#300
  • Loading branch information
jhford committed Aug 4, 2022
1 parent 810eeb0 commit c889ff8
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 60 deletions.
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

0 comments on commit c889ff8

Please sign in to comment.