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

NewImageFromBuffer Not Understand Image Format #124

Closed
phlqp opened this issue Nov 10, 2020 · 8 comments
Closed

NewImageFromBuffer Not Understand Image Format #124

phlqp opened this issue Nov 10, 2020 · 8 comments

Comments

@phlqp
Copy link

phlqp commented Nov 10, 2020

Hi there,

import "bytes"
import "image"
import "image/color"
import "image/png"
import "image/draw"

im := image.NewRGBA(image.Rect(0, 0, 100, 100))
draw.Draw(im, im.Bounds(), image.NewUniform(color.NRGBA{255, 0, 0, 255}), image.ZP, draw.Src)
var buff bytes.Buffer
err := png.Encode(&buff, im)

vipsImage, err := vips.NewImageFromBuffer(buff.Bytes())

[govips.info] failed to understand image format size=26149

Any suggestions how to solve this?

@tonimelisma
Copy link
Collaborator

What if you save the contents of buff into a .png image on your hard drive. Is it a valid png file, can you open it?

@phlqp
Copy link
Author

phlqp commented Nov 10, 2020

I want to create many images in memory, resize and composite them before save to disk

@tonimelisma
Copy link
Collaborator

Sure. I'm just wondering, to debug your code, if it made sense to take it a step at a time. Have you checked that your code is capable of producing valid .png files which you can open?

@phlqp
Copy link
Author

phlqp commented Nov 10, 2020

import "bufio"
import "os"
import "bytes"
import "image"
import "image/color"
import "image/png"
import "image/draw"

im := image.NewRGBA(image.Rect(0, 0, 100, 100))
draw.Draw(im, im.Bounds(), image.NewUniform(color.NRGBA{255, 0, 0, 255}), image.ZP, draw.Src)
var buff bytes.Buffer
err := png.Encode(&buff, im)

fo, err := os.Create("img.png")
defer fo.Close()
fw := bufio.NewWriter(fo)
fw.Write(buff.Bytes())
fw.Flush()

vipsImage, err := vips.NewImageFromBuffer(buff.Bytes())

Yes, It is valid png file
img

@Elad-Laufer
Copy link
Contributor

The error '[govips.info] failed to understand image format size=26149' stats that the data size is 26149 bytes, while the file is 292 bytes.

Maybe the buffer was changed before buff.Bytes() was called?

@phlqp
Copy link
Author

phlqp commented Nov 12, 2020

imageType is 3, but IsTypeSupported is false
IMG_2442

@phlqp
Copy link
Author

phlqp commented Nov 15, 2020

Change govips.go file:
initTypes {
...
if supportedImageTypes[k] {
govipsLog("govips", LogLevelInfo, fmt.Sprintf("registered image type loader type=%s", v))
}
...
}

@phlqp phlqp closed this as completed Nov 15, 2020
tonimelisma added a commit that referenced this issue Nov 16, 2020
@tonimelisma
Copy link
Collaborator

Thank you @dadaocongsanvietnam for the bug report. I've fixed this in the latest.

alon-ne added a commit to wix-playground/govips that referenced this issue Feb 9, 2021
* append \d after usage print for better user experience

* Add HasAlpha method for ImageRef

* Image#AutorotAngle delivers EXIF rotation angle

* Control over debug-level logs

* Make this repository a git module

* Avoid nil dereference on test failure

* Add `ImageRef.Clone()` method

* Fix copy-paste typo

* - add WebP orientation test

* - add WebP orientation test

* - add WebP orientation test

* Add comment

* Update comment

* Change Hue parameter to be float

* Control over debug-level logs

* Image#AutorotAngle delivers EXIF rotation angle

* Make this repository a git module

* Avoid nil dereference on test failure

* Cleaning extra files

* Fix URL and dependencies

* Fix URL

* Badges for README

* Add sharpen mode parameter

* Remove mode parameter for sharpen (can't find this in APIs)

* Resetting travis config to get CI/CD working

* Fix future-proof bug in tests

* Add MacOS build for Travis

* Upgrade macOS CI/CD images

* Fix documentation

* Update README with working example

* Replace custom scripts with standard Makefile

* Update test image data

* Disable bit-based image comparison tests

* Package documentation

* Remove file size checks - not relevant for testing

* Test coverage reporting via coveralls.io

* Add latest version badge to README

* Fix URLs

* Add make test

* Add instructions on dependencies

* Update to version 2.0.0

* Bump dependencies

* Update badges

* Add `StripMetadata` parameter back

Without this flag some EXIF tags (for example: `PixelXDimension`, `PixelYDimension`)
always added for PNG files.

Even the original file didn't have any EXIF tags.

* Fix travis link

* Fix dependency instructions for macOS

* fixes davidbyttow#112

Include stdlib.h to fix `could not determine kind of name for C.free` error

* Fix old macOS build issues with Python

* Remove old macOS versions due to missing homebrew packages

* Update README.md

* Update README.md

* Update README.md to point to latest godoc directly

* README update

* Clean documentation

* Fix nonexistent automatic kernel for resize()

* Add GC support for `vips.ImageRef`

* Create codeql-analysis.yml

* Undeprecate Copy() function

* Fix test dependencies and add Label() unit test

* Fix github caching test coverage metrics

* Create codeql-analysis.yml

* Undeprecate Copy() function

* Fix test dependencies and add Label() unit test

* Fix github caching test coverage metrics

* Consolidate glib, libvips and govips logging into one channel

* Option to set logging verbosity and override handler

* Clarify logging documentation and provide example

* Best practice for export parameters

* Deprecate Close() as imageRefs are closed automatically via gc

* re-enable golden tests and add macos resources

* Add linux golden resources and rename examples

* Remove Close() from unit test

* Add CI docker images

* Update codeql-analysis.yml

* Enable github CI

* Fix bug reported in issue davidbyttow#124

* Add unit tests and groovy assets

* govips logo assets

* Add logo and wix.com attribution to README.md

* Add the vips thumbnail function for easier image thumbnailing

* Add unit tests for Thumbnail() with and without crop

* 1. add support of `CompositeMulti`
2. add composite test

* Fix ineffectual assignment in example

* add debugging to find memory leaks

* groovy assets for crop unit test

* macos crop assets

* Add benchmark for the exporting of an image

* Avoid allocating a new go byte slice buffer and instead reuse the C buffer

* Add unit tests and groovy assets

* macos unit test assets

* Add linux assets for unit tests

* Add unit tests

* Add colorspace unit test

* Add unit tests for ExtractBand and BandJoin

* mac assets for unit tests

* Final unit test to bring us above 80%

* Just a couple more unit tests

* Add macos assets

* Revert "Avoid allocating a new go byte slice buffer and instead reuse the C buffer"

This reverts commit bf2aa07.

* implement vips_xyz

* implement divide

* add cgo stuff

* implement mapim

* remove public function

* Add Multiply() and Add() methods and tests for linux

* Add macos testing assets

* Add unit test for HEIF subtype ftypmsf1

* Fix HEIF subtype ftypmsf1 recognition

* add public method and test

* add comment, fix naming

* fix test

* delete divide golden image

* Add Windows section to README

* add failing test

* add pub method and test

* add pub method and tests

* Add macOS divide test

* More golden test reference images

* Fix panic on shutdown

* Add null logging handler and fix log override chec

* Change CI logging to console logging without cb

* Replace CI test console logging with TestMain

* Proper temporary files, remove hardcoded paths

* Fix issue davidbyttow#63, if vips compiled without ICC

* Fixes TIFF compression #21

* Remove node_modules

* Remove golden files, add macos golden

* Ubuntu golden images

* Clean up logging override flag behavior

* Support vips_similarity

* - Add bandjoin_const
- Make similarity support alpha background pixels (if the input image has an alpha channel/band)

* Comment

* Add a function to convert vips images as golang image.Image objects for interoperability

* Tests

* Test ToImage

* Use validation function in tests

* - Also support vips_smartcrop
- Commit golden test images

* WIP - Adds format specific export params

* Add groovy golden resources

* Add linux golden resources

* Add draw_rect

* Change test

* Add black

* Adds additional methods and cleans up export

* Add docs and failing unit tests

* Adds more options and default parameters in C

* implement Identity

* adjust comment to look nice

* implement a public method for Cast

* add a test for cast

* implement Maplut

* Adds major version to darwin golden tests

* Adds darwin-10 files

* Update file names

* Update macos-11 files

* Remove seemingly dead/unused code for embed/extend

* Fixes header

* add ushort parameter

* fix ushort implementation

* implement vips_avg

* add failing linear test to bump up coverage

* Format C files according to google style

* Remove redundant pkg-config directives

* Reduces noise in golden image tests during failure

* Uses libvips operation API for saving

* fix defaults

* Put back sharpen mode, nicer golden tests output

* Fix some C compilation warnings because they fail go tests

* Use format-specific export from vips

* ImageRef.OptimizeICCProfile now uses vipsICCTransform that maps to vips_icc_transform, instead of wrapping this in C function optimize_icc_profile (not done, doesn't compile yet)

* Put back support for "near lossless" parameter in webp, fix icc profile paths

* Add missing parameters jpegOptimizeCoding and jpegSubsample

* Change govips jpeg defaults to match previous version

* Restore icc profile parameter passed to save webp

Co-authored-by: Leslie Qi Wang <leslie.qiwa@gmail.com>
Co-authored-by: Tomasz Jezierski <deco.jezierski@gmail.com>
Co-authored-by: Tilo Prütz <tilo@infopark.de>
Co-authored-by: Alessandro Segala <43508+ItalyPaleAle@users.noreply.github.com>
Co-authored-by: John Ford <john@johnford.org>
Co-authored-by: David Byttow <david.byttow@gmail.com>
Co-authored-by: Artem V. Navrotskiy <bozaro@yandex.ru>
Co-authored-by: elad laufer <elad@wix.com>
Co-authored-by: Toni Melisma <toni.melisma@iki.fi>
Co-authored-by: Toni Melisma <59022391+tonimelisma@users.noreply.github.com>
Co-authored-by: Phani Rithvij <phanirithvij2000@gmail.com>
Co-authored-by: Paul van Santen <pvansanten@transip.nl>
Co-authored-by: zzjin <tczzjin@gmail.com>
Co-authored-by: aleksandr.kirillov <aleksandr.kirillov@myposter.de>
Co-authored-by: Aleksandr Kirillov <32141102+axkirillov@users.noreply.github.com>
Co-authored-by: boosh <boosh@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants