jpeg corruption

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
miha
Posts: 6
Joined: 2016-10-22T03:56:05-07:00
Authentication code: 1151

jpeg corruption

Post by miha »

I sometimes get corrupted jpegs when converting from a lossless format and the image is large and has lots of fine detail. With quality 100% the result is corrupted. With 99% or 98% it usually is correct although some cases needed going even lower. What could be the problem for this? Internal buffer overflow? How can it be avoided while staying at 100% and not reducing detail in the image before the conversion?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: jpeg corruption

Post by Bonzo »

Corrupted in what way?
What code are you using?
Can you try it on another server or localhost?
What is your lossless format?
What is you output format?
What Imagemagick version are you using?
What platform and code language are you using?
miha
Posts: 6
Joined: 2016-10-22T03:56:05-07:00
Authentication code: 1151

Re: jpeg corruption

Post by miha »

- Image is black or one colour or totally desaturated after some initial block of correct lines, one or multiple corrupt areas possible
- for example gm convert -size 32768x24576 -quality 100% -depth 8 rgb:$1 $1_32K.jpg (can happen with smaller sizes too)
- yes
- raw bytes
- jpg
- Version: ImageMagick 6.8.7-5 Q16 x86_64 2013-12-12
- Centos 6.8, bash script
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: jpeg corruption

Post by Bonzo »

I see gm so assume you are using Graphicsmagick; what happens if you use Imagemagick directly?

I do not know about gm but with imagemagick the input image usually comes right after convert and the quality goes just before the save. For jpg the quality setting would be 100 not 100%
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: jpeg corruption

Post by glennrp »

With either IM or GM the input image (rgb:$1) would have to come after the -size option. With such large images you might need to use -limit to avoid running out of resources.
miha
Posts: 6
Joined: 2016-10-22T03:56:05-07:00
Authentication code: 1151

Re: jpeg corruption

Post by miha »

It happens with GM or IM. And the file comes after the size option.
Current limits
File Area Memory Map Disk Thread Throttle Time
--------------------------------------------------------------------------------
768 270.59GB 126GiB 252GiB unlimited 48 0 unlimited
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: jpeg corruption

Post by snibgo »

miha wrote:32768x24576
This is nearly a billion pixels. With IM v6, this would take 8 GB. The jpeg compression code will also need memory, and I don't know if it fails gracefully if it can't allocate the memory. I suspect it doesn't, and that's the problem you are hitting.

I suggest you set the various "-limit" options so the pixels are written to disk instead of memory, freeing memory for the jpeg code.

An alternative might be to do this in two stages, with mpc as the intermediate:

Code: Select all

convert -size 32768x24576 -depth 8 rgb:$1 temp.mpc
convert temp.mpc -quality 100% $1_32K.jpg 
(My comments apply only to ImageMagick. I know nothing about GraphicsMagick.)
snibgo's IM pages: im.snibgo.com
miha
Posts: 6
Joined: 2016-10-22T03:56:05-07:00
Authentication code: 1151

Re: jpeg corruption

Post by miha »

I have 128 GB RAM. Not enough?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: jpeg corruption

Post by snibgo »

I wouldn't know. Your operating system probably has tools that will tell you how much is used.
snibgo's IM pages: im.snibgo.com
miha
Posts: 6
Joined: 2016-10-22T03:56:05-07:00
Authentication code: 1151

Re: jpeg corruption

Post by miha »

I just had another case. There was enough RAM for sure as reported by htop. 100% quality and 99% failed. 98% worked. Must be an internal bug.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: jpeg corruption

Post by snibgo »

Does it fail consistently for a given input? If so, I suggest you put it online somewhere for the developers to examine. And give the exact command you used.
snibgo's IM pages: im.snibgo.com
Post Reply