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

Gravity function throwing exceptions on specific images #169

Closed
Transigent opened this issue Jun 2, 2022 · 6 comments
Closed

Gravity function throwing exceptions on specific images #169

Transigent opened this issue Jun 2, 2022 · 6 comments
Labels
question Further information is requested

Comments

@Transigent
Copy link

Transigent commented Jun 2, 2022

Hi, first of all thanks for the superb NetVips library.

I have been doing some format tests for various images. For some images I am having an odd problem when I call WriteToFile after the Gravity function. Most JPG and various other formats (eg. WEBP, GIF, BMP, SVG, PDF, AVIF, TGA, JP2) all seem to work fine. In the code below, for each image:

  • I load a thumbnail fit to a 640x640 bounding box.
  • I write the thumb to disk.
  • Then I use Gravity to fill the non-image areas with grey.
  • Then I write the 'gravitized' image to disk.

I have supplied 4 sample images that are giving me trouble. For the first 3 (0016524.tiff, abcd.jfif, dddJFIF.jfif) I successfully load and WriteToFile the thumbnail but I get an exception on the line where I WriteToFile the Gravity processed image :

GravitizedImage.WriteToFile("C:\TestImages\" & FileNameWithoutExtension & "_GRAVITIZED.jpg", options)

0016524.tiff

unable to call VipsForeignSaveJpegFile
tiff2vips: out of order read -- at line 923, but line 0 requested

abcd.jfif

unable to call VipsForeignSaveJpegFile
VipsJpeg: out of order read at line 602

dddJFIF.jfif

unable to call VipsForeignSaveJpegFile
VipsJpeg: out of order read at line 480

For the 4th file zz12AVIF.avif it does not get past the thumbnail stage with the error:

unable to call VipsForeignSaveJpegFile
C:\TestImagesAll\zz12AVIF.avif: bad seek to 269202
heif: Invalid input: Unspecified: Bitstream not supported by this decoder (2.0)
which seems to imply that the format is not yet supported.

I am using the latest vips-dev ALL 8.12.2 version along with NetVips 2.1.0

I have tried running the command line tool vipsthumbnail.exe to create thumbs and they work except for the 4th file. I successfully use vips.exe to generate a file with plain Gravity applied, for the first 3 files, using

vips gravity abcd.jfif abcd.jpg centre 640 640 --background "128,128,128"

which works for all except the 4th file. Its like I am doing something wrong in the vb.net code.

Can you make any suggestions as to what I am doing wrong? Thanks for any advice.


My code is as follows:

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim DI As New System.IO.DirectoryInfo("C:\TestImages\")
        For Each Fil In DI.GetFiles
            PreProcessImage(Fil.FullName)
        Next
    End Sub
Public Sub PreProcessImage(FileName As String)

        Dim options As New VOption From {{"strip", True}, {"Q", 80}}
        Dim bacgnd As Double() = {128.0}     '(mid grey)

        Try
            Dim FileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FileName)

            Dim OriginalImage = NetVips.Image.Thumbnail(FileName, 640)
            OriginalImage.WriteToFile("C:\TestImages\" & FileNameWithoutExtension & "_THUMBNAILED.jpg", options)

            Dim GravitizedImage = OriginalImage.Gravity(Enums.CompassDirection.Centre, 640, 640, Enums.Extend.Background, bacgnd)
            GravitizedImage.WriteToFile("C:\TestImages\" & FileNameWithoutExtension & "_GRAVITIZED.jpg", options)
            Dim ResizedToBitmap2 As System.Drawing.Bitmap = GravitizedImage.ToBitmap

        Catch ex As Exception
            Dim v = 888
        End Try

    End Sub

System - Windows 10, Version 10.0.19043 Build 19043
Project - VB.NET Framework 4.6.1, x64
Nuget Packages - NetVips 2.1.0, NetVips.Extensions 2.1.0, System.Buffers 4.5.1, System. Memory 4.5.5, System.Numerics.Vectors 4.5.0, System.Runtime.CompilerServices.Unsafe 6.0.0
libvips - vips-dev-w64-all-8.12.2.zip

TestFiles.zip

@CanadianHusky
Copy link

Hi,

when I get "out of order read" error message with Netvips, it usually means that the image was opened with sequential read method. Netvips does streaming and caching for most operations to preserve memory and sequential read can do a single pass only. It is possible that your gravity functions require more than one pass over the image data or require reads at random positions.

Try opening your image with random access. Snippet below is how I open images.
Also I use the ThumbnailImage function and never had a problem with it.

Using img As Image = Image.NewFromFile(filename, access:=Enums.Access.Random)
            Using img2 As Image = img.ThumbnailImage(640, 640, Enums.Size.Force)
                img2.Jpegsave("c:\temp\thumbnail.jpg")
           End Using
End Using

@Transigent
Copy link
Author

Transigent commented Jun 2, 2022

@CanadianHusky Thanks a lot for the response!

I have tried what you suggested and I get no faults now (except with image 4, zz12AVIF.avif above). But my concern is that I have read comments from @kleisauke like this one:
#97 (comment)

I'd avoid ThumbnailImage, if possible. It can't do any of the shrink-on-load tricks, so it's much, much slower. The "force" parameter (VIPS_SIZE_FORCE) breaks aspect ratio, so I can't recommend that either.

and speed is fairly important in my case, I have large source images to deal with, and the shrink on load functionality is a lifesaver.

I guess I could try Thumbnail, then if it fails, delete the zero bytes image it generates, then try ThumbnailImage. Slow is better than none at all.

@kleisauke kleisauke added the question Further information is requested label Jun 6, 2022
@kleisauke
Copy link
Owner

Hi @Transigent,

Thumbnail opens the image in sequential mode. These images can only be streamed once, so a (possible) solution is to remove the first WriteToFile() call, for example:

using var thumb = Image.Thumbnail("x.jpg", 640);
using var gravity = thumb.Gravity(Enums.CompassDirection.Centre, 640, 640,
    extend: Enums.Extend.Background, background: new[] { 128.0 });
gravity.Jpegsave("gravity_x.jpg", strip: true, q: 80);

You could also use CopyMemory() to thumbnail into a memory image (it shouldn't use too much space, since thumbnails are only small), for example:

using var thumb = Image.Thumbnail("x.jpg", 640);
using var memory = thumb.CopyMemory();
memory.Jpegsave("thumb_x.jpg", strip: true, q: 80);

using var gravity = memory.Gravity(Enums.CompassDirection.Centre, 640, 640,
    extend: Enums.Extend.Background, background: new[] { 128.0 });
gravity.Jpegsave("gravity_x.jpg", strip: true, q: 80);

Finally, you could also open the image in random access mode and use ThumbnailImage (as mentioned by @CanadianHusky), but that will indeed be much slower.

There's a handy chapter in the docs explaining how libvips opens files, which gives some more background.
https://www.libvips.org/API/current/How-it-opens-files.md.html

unable to call VipsForeignSaveJpegFile
C:\TestImagesAll\zz12AVIF.avif: bad seek to 269202
heif: Invalid input: Unspecified: Bitstream not supported by this decoder (2.0)

This AVIF image looks like 10-bit, which is unsupported by the pre-built libvips binaries provided by NetVips.

$ heif-info -d zz12AVIF.avif | grep bit
| | | bits_per_channel: 10
| | | high_bitdepth: 1
| | | twelve_bit: 0

See the CHANGELOG.native.md for 8.10.6.

@Transigent
Copy link
Author

Thank you so much @kleisauke, I really appreciate the great suggestions and the support in general. I will give these things a try ASAP.

@kleisauke
Copy link
Owner

@Transigent Were you able to make any progress with this?

@Transigent
Copy link
Author

@Transigent Were you able to make any progress with this?

Thanks for the support, I used the CopyMemory option and it worked great. Once again thanks for the great library. I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants