mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-14 01:46:18 -07:00
Add support for colourspace (fix #45)
This commit is contained in:
parent
5874efef3e
commit
8a2b991ce8
7 changed files with 41 additions and 3 deletions
BIN
fixtures/test_image_colourspace_b_w.jpg
Normal file
BIN
fixtures/test_image_colourspace_b_w.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 58 KiB |
6
image.go
6
image.go
|
|
@ -134,6 +134,12 @@ func (i *Image) Convert(t ImageType) ([]byte, error) {
|
||||||
return i.Process(options)
|
return i.Process(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Colour space conversion
|
||||||
|
func (i *Image) Colourspace(c Interpretation) ([]byte, error) {
|
||||||
|
options := Options{Interpretation: c}
|
||||||
|
return i.Process(options)
|
||||||
|
}
|
||||||
|
|
||||||
// Transform the image by custom options
|
// Transform the image by custom options
|
||||||
func (i *Image) Process(o Options) ([]byte, error) {
|
func (i *Image) Process(o Options) ([]byte, error) {
|
||||||
image, err := Resize(i.buffer, o)
|
image, err := Resize(i.buffer, o)
|
||||||
|
|
|
||||||
|
|
@ -262,6 +262,14 @@ func TestImageMetadata(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImageColourspaceBW(t *testing.T) {
|
||||||
|
buf, err := initImage("test.jpg").Colourspace(B_W)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
Write("fixtures/test_image_colourspace_b_w.jpg", buf)
|
||||||
|
}
|
||||||
|
|
||||||
func TestFluentInterface(t *testing.T) {
|
func TestFluentInterface(t *testing.T) {
|
||||||
image := initImage("test.jpg")
|
image := initImage("test.jpg")
|
||||||
_, err := image.CropByWidth(300)
|
_, err := image.CropByWidth(300)
|
||||||
|
|
|
||||||
15
options.go
15
options.go
|
|
@ -55,6 +55,20 @@ const (
|
||||||
VERTICAL Direction = C.VIPS_DIRECTION_VERTICAL
|
VERTICAL Direction = C.VIPS_DIRECTION_VERTICAL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Interpretation int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ERROR Interpretation = C.VIPS_INTERPRETATION_ERROR
|
||||||
|
MULTIBAND Interpretation = C.VIPS_INTERPRETATION_MULTIBAND
|
||||||
|
B_W Interpretation = C.VIPS_INTERPRETATION_B_W
|
||||||
|
CMYK Interpretation = C.VIPS_INTERPRETATION_CMYK
|
||||||
|
RGB Interpretation = C.VIPS_INTERPRETATION_RGB
|
||||||
|
sRGB Interpretation = C.VIPS_INTERPRETATION_sRGB
|
||||||
|
RGB16 Interpretation = C.VIPS_INTERPRETATION_RGB16
|
||||||
|
GREY16 Interpretation = C.VIPS_INTERPRETATION_GREY16
|
||||||
|
scRGB Interpretation = C.VIPS_INTERPRETATION_scRGB
|
||||||
|
)
|
||||||
|
|
||||||
const WATERMARK_FONT = "sans 10"
|
const WATERMARK_FONT = "sans 10"
|
||||||
|
|
||||||
// Color represents a traditional RGB color scheme
|
// Color represents a traditional RGB color scheme
|
||||||
|
|
@ -97,4 +111,5 @@ type Options struct {
|
||||||
Watermark Watermark
|
Watermark Watermark
|
||||||
Type ImageType
|
Type ImageType
|
||||||
Interpolator Interpolator
|
Interpolator Interpolator
|
||||||
|
Interpretation Interpretation
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,7 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
Compression: o.Compression,
|
Compression: o.Compression,
|
||||||
Interlace: o.Interlace,
|
Interlace: o.Interlace,
|
||||||
NoProfile: o.NoProfile,
|
NoProfile: o.NoProfile,
|
||||||
|
Interpretation: o.Interpretation,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally save as buffer
|
// Finally save as buffer
|
||||||
|
|
@ -143,6 +144,9 @@ func applyDefaults(o *Options, imageType ImageType) {
|
||||||
if o.Type == 0 {
|
if o.Type == 0 {
|
||||||
o.Type = imageType
|
o.Type = imageType
|
||||||
}
|
}
|
||||||
|
if o.Interpretation == 0 {
|
||||||
|
o.Interpretation = sRGB
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeOperation(o *Options, inWidth, inHeight int) {
|
func normalizeOperation(o *Options, inWidth, inHeight int) {
|
||||||
|
|
|
||||||
7
vips.go
7
vips.go
|
|
@ -37,6 +37,7 @@ type vipsSaveOptions struct {
|
||||||
Type ImageType
|
Type ImageType
|
||||||
Interlace bool
|
Interlace bool
|
||||||
NoProfile bool
|
NoProfile bool
|
||||||
|
Interpretation Interpretation
|
||||||
}
|
}
|
||||||
|
|
||||||
type vipsWatermarkOptions struct {
|
type vipsWatermarkOptions struct {
|
||||||
|
|
@ -227,6 +228,10 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
length := C.size_t(0)
|
length := C.size_t(0)
|
||||||
err := C.int(0)
|
err := C.int(0)
|
||||||
interlace := C.int(boolToInt(o.Interlace))
|
interlace := C.int(boolToInt(o.Interlace))
|
||||||
|
if o.Interpretation == 0 {
|
||||||
|
o.Interpretation = sRGB
|
||||||
|
}
|
||||||
|
interpretation := C.VipsInterpretation(o.Interpretation)
|
||||||
|
|
||||||
// Remove ICC profile metadata
|
// Remove ICC profile metadata
|
||||||
if o.NoProfile {
|
if o.NoProfile {
|
||||||
|
|
@ -235,7 +240,7 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
|
|
||||||
// Force RGB color space
|
// Force RGB color space
|
||||||
var outImage *C.struct__VipsImage
|
var outImage *C.struct__VipsImage
|
||||||
C.vips_colourspace_bridge(image, &outImage)
|
C.vips_colourspace_bridge(image, &outImage, interpretation)
|
||||||
|
|
||||||
defer C.g_object_unref(C.gpointer(image))
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
defer C.g_object_unref(C.gpointer(outImage))
|
defer C.g_object_unref(C.gpointer(outImage))
|
||||||
|
|
|
||||||
4
vips.h
4
vips.h
|
|
@ -135,9 +135,9 @@ vips_extract_area_bridge(VipsImage *in, VipsImage **out, int left, int top, int
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_colourspace_bridge(VipsImage *in, VipsImage **out)
|
vips_colourspace_bridge(VipsImage *in, VipsImage **out, VipsInterpretation space)
|
||||||
{
|
{
|
||||||
return vips_colourspace(in, out, VIPS_INTERPRETATION_sRGB, NULL);
|
return vips_colourspace(in, out, space, NULL);
|
||||||
};
|
};
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue