# CoreGraphicsImage imageData size calculation mistake

**URL:** <https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156>\
**Category:** General JUCE discussion\
**Created:** [November 16, 2017, 11:08am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156 "2017-11-16T11:08:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 16, 2017, 11:08am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/1 "2017-11-16T11:08:37Z")

</div>

We had a user loading a 32000 × 19202 JPEG Image. Although we encouraged the user to scale his image down, his image resulted in a crash in our application.  
The problem originated from the CoreGraphicsImage, it was trying to allocate it’s HeapBlock with an incorrect size.  
This is why:  
CoreGraphicsImage constructor:  
…  
imageData.allocate ((size\_t) (lineStride \* jmax (1, height)), clearImage);

But it should actually be this:  
imageData.allocate ((size\_t)lineStride \* jmax (1, height), clearImage);

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 16, 2017, 12:06pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/2 "2017-11-16T12:06:55Z")

</div>

Oops, well also in CoreGraphicsImage::createImage the calculation is incorrect.  
But after fixing all cases i still experience a crash when loading and drawing an image with these dimensions.

So in short, guys can you help me out?  
If i load this huge image, JUCE will crash.  
It will only crash if you fix the size calculation, if not the size becomes so large (overflow) that it will fail allocating the memory for the bitmapData, so it won’t try to do anything. So although the code looks safe it’s actually wrong.

When calculation is fixed, bitmapData will be valid and Image is loaded.  
But when i try to draw it, it fails in CoreGraphicsContext::drawImage.  
As you see in the snippet i’m actually scaling the image. If i just call drawImageAt(image, 0,0); it actually works fine.

Here is the code snippet.  
HUGE.jpg is an Image with size 32000 × 19202

```
Image image = ImageFileFormat::loadFrom(File("HUGE.jpg"));
if (image.isValid())
{
    Image thumb(Image::ARGB, 160, 120, true);

    Graphics g(thumb);
    g.setColour(Colours::white);

    g.drawImageWithin( image, 0, 0, thumb.getWidth(), thumb.getHeight(), RectanglePlacement( RectanglePlacement::centred ), false );
    g.drawRect(0, 0, thumb.getWidth(), thumb.getHeight(), 10.0);
}

```

Here is the file:  
[HUGE JPEG](https://www.dropbox.com/s/3ba4pr0nw2cut7g/HUGE.jpg?dl=0)

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 20, 2017, 2:08pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/3 "2017-11-20T14:08:39Z")

</div>

Fixed:

> <https://github.com/WeAreROLI/JUCE/commit/1dafeafef783f3987ea86c1263dfde01f465349f>

There were a couple of other places where we were overflowing integers.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 20, 2017, 2:39pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/4 "2017-11-20T14:39:58Z")

</div>

Huge is the right word for it! Amazed that it works at all with images that big!

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 20, 2017, 4:20pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/5 "2017-11-20T16:20:27Z")

</div>

Thanx t0m for looking at it. Don’t forget the SoftwarePixelData class, it has the same issues.  
@jules: Yes huge it is. I will need to put some hardcoded resolution restrains on image loading. Because with the fixes you guys made the Image class handles the data just fine. But because of that the image is actually being drawn and that in turn causes a crash.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 20, 2017, 4:40pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/6 "2017-11-20T16:40:58Z")

</div>

I can draw the image, and a resized version of it, without a crash…

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 21, 2017, 9:03am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/7 "2017-11-21T09:03:17Z")

</div>

@t0m: i just checked out the develop branch. I took the AnimationAppExample and changed the MainComponent to this. It crashes in void CoreGraphicsContext::drawImage at line 528. CGContextDrawImage (context, imageRect, image);  
It’s quite possible this issue is related to the user’s hardware. I would appreciate it if you could test it again on another machine than your own. For me it’s a certain crash each time this code is run.

```
class MainContentComponent : public AnimatedAppComponent
{
public:
	Image image;
	
//==============================================================================
MainContentComponent()
{
    setSize (800, 600);
    setFramesPerSecond (60);
		image = ImageFileFormat::loadFrom(File("/Users/edwindekoning/Downloads/HUGE.jpg"));
		
}

void update() override
{
    // This function is called at the frequency specified by the setFramesPerSecond() call
    // in the constructor. You can use it to update counters, animate values, etc.
}

void paint (Graphics& g) override
{
		if (image.isValid())
		{
			g.drawImageWithin( image, 0, 0, getWidth(), getHeight(), RectanglePlacement( RectanglePlacement::centred ), false );
		}
}

void resized() override
{
    // This is called when the MainContentComponent is resized.
    // If you add any child components, this is where you should
    // update their positions.
}

private:
//==============================================================================

// Your private member variables go here...

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};

```

![machinespecs](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/0f28cf6e0bad95c35b82813868496ff591a4b322.png)

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 21, 2017, 4:59pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/8 "2017-11-21T16:59:37Z")

</div>

You’re trying to animate that at 60 frames per second?! The call to `.drawImageWithin` takes about 5 seconds on my machine…

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 21, 2017, 5:10pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/9 "2017-11-21T17:10:56Z")

</div>

It’s working on a mid 2010 Macbook Pro running 10.12.6 and mid 2015 Macbook Pro running 10.13.1.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 21, 2017, 5:12pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/10 "2017-11-21T17:12:14Z")

</div>

Could you post the stacktrace of the crash?

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 22, 2017, 12:10pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/11 "2017-11-22T12:10:15Z")

</div>

Here is the stack trace:

 ![StackTrace](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/f/fc4a0577f1dbc677df57db39d8d723ef6fa208cc.png)

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 22, 2017, 4:22pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/12 "2017-11-22T16:22:16Z")

</div>

I’ve managed to generate a crash on a 10.11.6 MacMini with Xcode 8.1.

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [November 23, 2017, 10:46am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/13 "2017-11-23T10:46:57Z")

</div>

It appears to be an issue on 10.11, rather than hardware - I updated the MacMini to 10.13 to get better debugging tools and can no longer reproduce the problem.

Unfortunately everything looked correct when calling `CGContextDrawImage` and I didn’t find anything more informative, so I can’t suggest anything useful.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 23, 2017, 10:48am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/14 "2017-11-23T10:48:13Z")

</div>



---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [November 23, 2017, 11:35am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/15 "2017-11-23T11:35:43Z")

</div>

I had the feeling it wasn’t really something we could fix easily. Thanx for looking into it though.

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [August 23, 2019, 10:55am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/16 "2019-08-23T10:55:36Z")

</div>

I ran into this issue today again because someone send us a JPEG with a resolution of 30000x24000 😉  
So i tried to load this file using the JUCE Image class but the JPEGImageFormat crashes because it uses the BitmapData getLinePointer and that performs explicit casts and thus cause an integer overflow.

So i would suggest the following changes:

```
    inline uint8* getLinePointer (int y) const noexcept { return data + (size_t)y * (size_t)lineStride; }

    inline uint8* getPixelPointer (int x, int y) const noexcept { return data + (size_t)y * (size_t)lineStride + (size_t)x * (size_t)pixelStride; }
```

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [August 25, 2019, 12:13pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/17 "2019-08-25T12:13:41Z")

</div>

Bump, the BitmapData::getLinePointer and BitmapData::getPixelPointer are faulty. They have integer overflow issues. You fixed similar issues in the Image class a while ago but without these fixes it’s still broken.

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [August 28, 2019, 2:56pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/18 "2019-08-28T14:56:15Z")

</div>

Thanks! I’ve just got around to adding this in [383d69c](https://github.com/WeAreROLI/JUCE/commit/383d69c421b289d247ec60966f87476a1988cc68).

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [September 2, 2019, 6:12am UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/19 "2019-09-02T06:12:45Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![rebbur](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rebbur](https://forum.juce.com/u/rebbur)\
**Post date:** [March 13, 2020, 12:50pm UTC](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156/20 "2020-03-13T12:50:36Z")

</div>

Another offender is in Image::moveImageSection

```auto
const size_t lineSize = (size_t)(destData.pixelStride * w);

```

Should become

```auto
const size_t lineSize = (size_t)destData.pixelStride * (size_t)w;

```

[Next page](https://forum.juce.com/t/coregraphicsimage-imagedata-size-calculation-mistake/25156.md?page=2)
