JPEG: issue with sampling-factor

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

JPEG: issue with sampling-factor

Post by Lars-Daniel »

Hi there.
Version: ImageMagick 7.0.3-1 Q16 x64 2016-09-20
What I'm trying to do:

Code: Select all

magick rose: -interlace Plane -define jpeg:dct-method=float -quality 95 -sampling-factor 4:2:2 422.jpg
magick rose: -interlace Plane -define jpeg:dct-method=float -quality 95 -sampling-factor 4:4:4 444.jpg
1. identify -verbose 422.jpg | grep sampling-factor
2. identify -verbose 444.jpg | grep sampling-factor
What's the output?
1. jpeg:sampling-factor: 2x1,1x1,1x1
2. jpeg:sampling-factor: 1x1,1x1,1x1
Hmmm... Does this look right?

But there's another problem when creating JPEG in TIFF:

Code: Select all

magick rose: -interlace Plane -define jpeg:dct-method=float -quality 95 -sampling-factor 4:2:2 -compress jpeg 422.tif
magick rose: -interlace Plane -define jpeg:dct-method=float -quality 95 -sampling-factor 4:4:4 -compress jpeg 444.tif
1. identify -verbose 422.tif | grep sampling-factor
2. identify -verbose 444.tif | grep sampling-factor
Results:
1. jpeg:sampling-factor: 2x2
2. jpeg:sampling-factor: 2x2
Oops... seems like we've got a bug?!

Best regards,
Lars-Daniel
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: JPEG: issue with sampling-factor

Post by fmw42 »

I am not an expert on -sampling-factor.

But the information at http://www.imagemagick.org/Usage/formats/#jpg_write, seems to imply that your command should not have subsampled, since your quality is 95. I do not know if this information is old and has been superseded.

-sampling-factor {horizontal}x{vertical}
Adjust the sampling factor used by JPEG library for chroma down sampling. This can be set to '2x1' for creating MPEG-2 animation files. "2x2, 1x1, 1x1" is IM's standard sub-sampling method and corresponds to 4:2:0, see Wikipedia, Chroma Sub-Sampling. However when "quality" is 90 or higher, the channels are not sub-sampled. Basically it will define whether the processing 'block' or 'cell' size is 8 pixels or 16 pixels.

This seems to contradict the information at http://www.imagemagick.org/script/comma ... ing-factor

sampling factors used by JPEG or MPEG-2 encoder and YUV decoder/encoder.
This option specifies the sampling factors to be used by the JPEG encoder for chroma downsampling. If this option is omitted, the JPEG library will use its own default values. When reading or writing the YUV format and when writing the M2V (MPEG-2) format, use -sampling-factor 2x1 or -sampling-factor 4:2:2 to specify the 4:2:2 downsampling method.

I think the documentation needs some clarification about the two different notations and what options IM permits for JPG images and for TIFF images with JPG compression.

It seems that there is are only two choices, if I understand it. The first is to supply no -sampling-factor and then the jpg library chooses what it wants. The second is to use -sampling-factor 2x2 (or 4:2:2). I do not know what one gets if one supplies 4:4:4 or anything but 4:2:2. Though your results imply that 4:4:4 is either allowed or ignored and is the nominal value for IM
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

Re: JPEG: issue with sampling-factor

Post by Lars-Daniel »

fmw42, thanks for your info about 2x1 instead of 4:2:2 ... both are still working, but I'll switch to 2x1.
I tried 1x1 with TIFF, but it's still 4:2:2 (2x1) :(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: JPEG: issue with sampling-factor

Post by fmw42 »

IM may not allow downsampling for TIFF with JPG compression. I do not know. There does not appear to be any information either way about it at http://www.imagemagick.org/script/formats.php nor at http://www.imagemagick.org/script/comma ... p#compress
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

Re: JPEG: issue with sampling-factor

Post by Lars-Daniel »

fmw42 wrote:IM may not allow downsampling for TIFF with JPG compression. I do not know. There does not appear to be any information either way about it at http://www.imagemagick.org/script/formats.php
I bet it's a problem of using libtiff again... Another problem is that IM can't embedd JPEG to TIFF in a lossless way. Photoshop (of course) also can't.
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

Re: JPEG: issue with sampling-factor

Post by Lars-Daniel »

Is there a way to contact the developers?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: JPEG: issue with sampling-factor

Post by fmw42 »

User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: JPEG: issue with sampling-factor

Post by dlemstra »

The issue is that the sampling factor will only be stored when the colorspace is YCbCr. The sampling factor is stored in the following tiff tag: http://www.awaresystems.be/imaging/tiff ... pling.html. I also found the following in the code of libtiff:

Code: Select all

	switch (sp->photometric) {
	case PHOTOMETRIC_YCBCR:
	        -- snip --
	default:
		/* TIFF 6.0 forbids subsampling of all other color spaces */
		sp->h_sampling = 1;
		sp->v_sampling = 1;
		break;
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

Re: JPEG: issue with sampling-factor

Post by Lars-Daniel »

dlemstra wrote:The issue is that the sampling factor will only be stored when the colorspace is YCbCr. The sampling factor is stored in the following tiff tag: http://www.awaresystems.be/imaging/tiff ... pling.html. I also found the following in the code of libtiff:
But doesn't this default to 1x1 if it's not YCbCr? In my initial post, it was forced to 2x2.

I've just tested this in Photoshop:
  • RGB image, saved as JPEG in TIFF: jpeg:sampling-factor: 1x1
    CMYK image, saved as JPEG in TIFF: jpeg:sampling-factor: 2x2
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: JPEG: issue with sampling-factor

Post by dlemstra »

You are getting 2x2 because that is the default value for YCbCrSubSampling. The next version of ImageMagick will only show the jpeg:sampling-factor when it was stored in your file. This means that you will no longer see it with the command from your initial post in the next version.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Lars-Daniel
Posts: 37
Joined: 2015-09-20T05:55:17-07:00
Authentication code: 1151

Re: JPEG: issue with sampling-factor

Post by Lars-Daniel »

dlemstra wrote:You are getting 2x2 because that is the default value for YCbCrSubSampling. The next version of ImageMagick will only show the jpeg:sampling-factor when it was stored in your file. This means that you will no longer see it with the command from your initial post in the next version.
Thanks for your explaination and sorry for this question: So I can't get 1x1 subsampling when using JPEG compression in TIFF? Hell, since JPEG can't handle ppi correctly (fractals are always a problem here), I have to switch to PDF. Or is there another wide spread format, which can handle CMYK, RGB with lossless and JPEG compression? Don't think so :(

Actually, I never liked JPEG in TIFF. Most users think, TIFF is lossless because of ZIP and LZW. It should better be namjed JTIF or TIFJ :o
Post Reply