Imageprocessor: Index was outside the bounds of the array.

Created on 6 May 2016  路  28Comments  路  Source: JimBobSquarePants/ImageProcessor

When trying to do a resize of an image that's 1500x2800 to 400x746 I get this error.

An unhandled exception has occurred while executing the request
ImageProcessorCore.ImageProcessingException: An error occured when processing the image using Resize. See the inner exception for more detail. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at ImageProcessorCore.Samplers.Resampler.PrecomputeWeights(Int32 destinationSize, Int32 sourceSize)
at ImageProcessorCore.Samplers.Resize.OnApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle)
at ImageProcessorCore.ParallelImageProcessor.Apply(ImageBase target, ImageBase source, Int32 width, Int32 height, Rectangle targetRectangle, Rectangle sourceRectangle)
--- End of inner exception stack trace ---
at ImageProcessorCore.ParallelImageProcessor.Apply(ImageBase target, ImageBase source, Int32 width, Int32 height, Rectangle targetRectangle, Rectangle sourceRectangle)
at ImageProcessorCore.ImageExtensions.<>c__DisplayClass7_0.<Process>b__0(ImageBase sourceImage, ImageBase targetImage)
at ImageProcessorCore.ImageExtensions.PerformAction(Image source, Boolean clone, Action`2 action)
at ImageProcessorCore.ImageExtensions.Process(Image source, Int32 width, Int32 height, Rectangle sourceRectangle, Rectangle targetRectangle, IImageSampler[] processors)
at ImageProcessorCore.Samplers.ImageSamplerExtensions.Resize(Image source, Int32 width, Int32 height, IResampler sampler, Rectangle sourceRectangle, Boolean compand, ProgressEventHandler progressHandler)
at ImageProcessorCore.Samplers.ImageSamplerExtensions.Resize(Image source, Int32 width, Int32 height, ProgressEventHandler progressHandler)

Tried 100x100 and 50x50 : same error.

Surprisingly, doing 10x10 works.

Ideas?

bug core

All 28 comments

Tried 100x100 and 50x50 : same error.

Do you mean you tried resizing from 1500x2800 to those values?

That's odd, shouldn't happen. Will investigate.

exactly.

  • Source: 1500x2800

Target:

  • 400x746 = fail
  • 100x100 = fail
  • 50x50 = fail
  • 10x10 = resize ok.

I can send you the file, but must be private.

Nah that's cool. I should be able to replicate by making one the same size.

If not I'll get you to email me one. Was it a png?

It's a .JPG file.

here the details on file properties:

image

let me know if you need something else.

So I'm not able to replicate this on my machine.

Could you email me the image [email protected] and also post a code sample of how you are resizing the image?

Email sent with the image and the exact code i'm using.
Thanks!!

Here is a minimal repro of the code:

using (var sourceStream = await fileManager.GetStreamAsync(source))
using (var image = new Image(sourceStream))
using (var destinationStream = new MemoryStream())
{
    image.Resize(100,100).Save(destinationStream);
}

I've seen this also.

The image was 7200x10800 and I was trying to jpeg it to 320x180,

I found that if I used a multiple of its' lowest common ratio (108x72) and began multiple (*2, *3, *4...)

to a thumbnail of size 216 x 324, it ran fine... but was odd.

@Bartmax @razblack What build are you both using? I'm able to resize against the emailed image to the specified dimensions no problem.

I actually compiled it myself a couple days ago.. I can check the dates.

"dnxcore50": {
      "dependencies": {
        "ImageProcessorCore": "1.0.0-alpha-780"
      }
    }

is this bad?

just updated to 790, same outcome.

here's the project.json I used to do the build. I had to push it up to net46, otherwise I was runing into System.IO.Compression problems (not sure why though, https://github.com/JimBobSquarePants/ImageProcessor/issues/363#issuecomment-216007733 )


{
  "version": "1.0.0-*",
  "title": "ImageProcessorCore",
  "description": "A cross-platform library for processing of image files written in C#",
  "authors": [
    "James Jackson-South and contributors"
  ],
  "tags": [
    "Image Resize Crop Quality Gif Jpg Jpeg Bitmap Png Fluent Animated"
  ],
  "compilationOptions": {
    "allowUnsafe": true
  },
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "dotnet5.5": {
      "dependencies": {
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "Microsoft.CSharp": "4.0.1-beta-23516",
        "Microsoft.NETCore.Portable.Compatibility": "1.0.1-beta-23516",
        "System.Runtime.Extensions": "4.0.11-beta-23516",
        "System.Reflection": "4.1.0-beta-23516",
        "System.IO": "4.0.11-beta-23516",
        "StyleCop.Analyzers": "1.0.0",
        "Microsoft.NETCore": "5.0.1-beta-23516",
        "Microsoft.NETCore.Platforms": "1.0.1-beta-23516"
      }
    },
    "net46": {
      "dependencies": {
        "System.Numerics.Vectors": "4.0.0",
        "System.IO": "4.0.11-beta-23516",
        "System.Runtime": "4.0.21-beta-23516",
        "System.Text.Encoding": "4.0.11-beta-23516",
        "System.IO.Compression": "4.1.0-beta-23516",
        "System.Collections": "4.0.11-beta-23516",
        "System.Linq": "4.0.1-beta-23516",
        "System.Threading": "4.0.11-beta-23516",
        "System.Reflection": "4.1.0-beta-23516"
      },
      "frameworkAssemblies": {
        "System.Runtime": ""
      }
    }
  }
}

Super odd. That's the build version I'm testing against.

I'm afraid you're going to have to attach a debugger against the source to figure this one out. We know the error is happening in PrecomputeWeights but not why.

Can you both please pull the latest source and give it a go?

absolutely, it will have to be tomorrow though, and I'll try to do a sample project with ImageProcessorCore as an added project instead of a nuget dependency to see if I can help trace it down.

Brilliant. Thanks!

in the meantime, here's the sample image i was working with for tests:

https://onedrive.live.com/redir?resid=3DD1F9A38CC00B28!46237&authkey=!AHld1zYjSxZRfBI&ithint=file%2c7z

Pressing F10 here throws. I don't understand what's going on unfortunately. Feel free to give me some gidance or tell me if there's any data you need to know.

image

As you can see, is trying to get results[18].Values[15] but it only has 15 items (0 to 14).

image

Changed this line and worked:

Values = new Weight[(int)Math.Floor((2 * width) + 1)]

to:

Values = new Weight[(int)Math.Ceiling((2 * width) + 1)]

this was a guess shot so I'm not sure if this is really the issue here. but felt soooo goood :) hahhaa

i'd go with Values = new Weight[right - left] instead?

actually new Weight[right - left + 1] i guess. given the forloop is <= right.

Values = new Weight[(int)(right-left + 1)] also worked.

@JimBobSquarePants I guess you have everything you need... the only question I still have is why you weren't having the same issue.

Let me know if I can help any further. This was so far the most satisfying experience on OSS so far for me. Thanks!

This was a nasty little bug!

Protip: Never base array sizes on floating point calculations. :smiling_imp:

My two test machine must support extended floating point types which meant they gave more accurate answers for rounding. @Bartmax Your solution would work but the solution @davepermen gave will be quicker. I'll push some new code in a mo.

There should be an updated build available soon. You can track it's progress here

https://ci.appveyor.com/project/JamesSouth/imageprocessor/branch/Core

looking forward to give that a try soon..however, I believe I may be seeing something different:

Exception thrown: 'ImageProcessorCore.Formats.JpegDecoderCore.errMissingFF00' in ImageProcessorCore
Exception thrown: 'ImageProcessorCore.Formats.JpegDecoderCore.errMissingFF00' in ImageProcessorCore
Exception thrown: 'ImageProcessorCore.Formats.JpegDecoderCore.errMissingFF00' in ImageProcessorCore
Exception thrown: 'ImageProcessorCore.Formats.JpegDecoderCore.errMissingFF00' in ImageProcessorCore
Exception thrown: 'ImageProcessorCore.Formats.JpegDecoderCore.errMissingFF00' in ImageProcessorCore
Exception thrown: 'System.IndexOutOfRangeException' in ImageProcessorCore
Exception thrown: 'ImageProcessorCore.ImageProcessingException' in ImageProcessorCore

somewhere in JpegDecoderCore.cs @#468 (decodeHuffman)

this is the first i've dug this far into your code.. not sure what that means exactly.

ok with ImageProcessorCore v1.0.0.802 (DL'd the source and compiled here locally),

I still get this "errMissingFF00" exception while it generates the jpeg; however, it no longer "Breaks" on that throw (which is good) and generates the blob (which is my end result):


            string filePath = storagePath + fileName + fileExtension;

            try
            {
                var blob = "";
                var blobName = storagePath + fileName + ".blob";

                using (FileStream stream = File.OpenRead(filePath))
                using (Image image = new Image(stream))
                using (FileStream output = File.OpenWrite(blobName))
                {
                    image.Resize(320, 180).Save(output, new JpegFormat());
                    output.Close();
                    var img = File.ReadAllBytes(blobName);
                    blob = Convert.ToBase64String(img);
                }
                return blob;
            }
            catch (Exception e)
            {
                Console.WriteLine("ImageConverter [CreateThumbnail], Exception: " + e.Message);
                return string.Empty;
            }

this is happier :+1:

althought, i see it takes a 'slowpath' to process this.. is that bad?

@razblack It just means that the software that encoded your jpeg didn't do a great job. The decoder recognizes the error and works around it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rcharb1 picture rcharb1  路  8Comments

SergeBerwert picture SergeBerwert  路  4Comments

srdjo picture srdjo  路  6Comments

Bartmax picture Bartmax  路  8Comments

joshua-stewart picture joshua-stewart  路  10Comments