Page 1 of 1

Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-21T23:57:12-07:00
by 246246
First I create a 2 color indexed tiff via png.

Code: Select all

$ magick logo: -colors 2 -define png:include-chunk=none -define png:bit-depth=1 -define png:color-type=3 logo1.png
$ magick logo1.png -compress none logo1.tif
$ identify -verbose logo1.tif | grep -A 2 -B 5 -i colormap:
  Colors: 2
  Histogram:
     42971: ( 83, 62, 82) #533E52 srgb(83,62,82)
    264229: (253,253,253) #FDFDFD srgb(253,253,253)
  Colormap entries: 2
  Colormap:
         0: (253,253,253,255) #FDFDFDFF srgba(253,253,253,1)
         1: ( 83, 62, 82,255) #533E52FF srgba(83,62,82,1)
Second, I created another one directly:

Code: Select all

$ magick logo: -type palette -depth 1 -colors 2 -compress none logo2.tif
$ identify -verbose logo2.tif | grep -A 2 -B 5 -i colormap:
  Colors: 2
  Histogram:
     38008: ( 63, 33, 84) #3F2154 srgb(63,33,84)
    269192: (255,255,255) #FFFFFF white
  Colormap entries: 2
  Colormap:
         0: (255,255,255,255) #FFFFFFFF white
         1: ( 63, 33, 84,255) #3F2154FF srgba(63,33,84,1)
Colors are different but I do not care it for now. The point is both are 1-bit indexed tiff. They have same size (as I specified no compression) and seems to have correct 16-bit colormap.

Code: Select all

$ identify *.tif
logo1.tif TIFF 640x480 640x480+0+0 1-bit sRGB 2c 38682B 0.000u 0:00.001
logo2.tif TIFF 640x480 640x480+0+0 1-bit sRGB 2c 38682B 0.000u 0:00.000

$ tiffdump.exe logo1.tif | grep -i colormap
Colormap (320) SHORT (3) 6<65021 21331 65021 15934 65021 21074>

$ tiffdump.exe logo2.tif | grep -i colormap
Colormap (320) SHORT (3) 6<65535 16251 65535 8547 65535 21646>
However identify reports different value for bit-depth:

Code: Select all

$ identify -format '%[depth]/%[bit-depth]\n' logo1.tif logo2.tif
1/8
1/16
Please let me suggest if I missed something, or let me know how bit-depth are determined by ImageMagick.
Best Wishes,

Here is the version of ImageMagick that comes with MSYS2 on Windows.

Code: Select all

$ magick -version
Version: ImageMagick 7.0.6-7 Q16 x86_64 2017-08-16 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf xml zlib

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T08:58:27-07:00
by fmw42
I do not see any %[bit-depth] listed at https://www.imagemagick.org/script/escape.php. Where is that documented?

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T15:57:20-07:00
by 246246
I read it about this forum some years ago, but I cannot find the post. (I'll add it later if I could find it.)
I'm sure that property is implemented. https://github.com/ImageMagick/ImageMag ... =%E2%9C%93

It seems same value are shown in the output of identify -verbose, after the slash of the Depth:

Code: Select all

  Depth: 1/16-bit
In other words, `identify -verbose xxx | grep Depth:` always shows the same result as `identify -format 'Depth: %[depth]/%[bit-depth]-bit\n' for any image xxx.

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T16:19:20-07:00
by fmw42
Is it not the other way around, as

16/1-bit

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T16:51:40-07:00
by snibgo
@246246: Did you follow the trail? property.c "bit-depth" calls attribute.c GetImageDepth() which reads the entire image to find the number of bits required.

Your command for logo2.tif finds the best two colours that fit the image. You use Q16, and those colours are likely to need more than 8 bits.

Your command for logo1.png rounds pixels to 8-bit/channel.
Your command for logo2.tiff rounds pixels to 1-bit/channel.

Obviously, the results will be different.

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T17:02:55-07:00
by 246246
fmw42 wrote:
2018-03-22T16:19:20-07:00
Is it not the other way around, as

16/1-bit
Sorry I don't understand what you mean:

$ magick identify -verbose logo1.tif | grep Depth
Depth: 1/8-bit
$ magick identify -verbose logo2.tif | grep Depth
Depth: 1/16-bit

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T17:15:28-07:00
by 246246
snibgo wrote:
2018-03-22T16:51:40-07:00
@246246: Did you follow the trail? property.c "bit-depth" calls attribute.c GetImageDepth() which reads the entire image to find the number of bits required.
I'm now trying to :)
snibgo wrote:
2018-03-22T16:51:40-07:00
Your command for logo2.tif finds the best two colours that fit the image. You use Q16, and those colours are likely to need more than 8 bits.
I understand that. If all the colors of colormap can be represented in 8 bit, the result of %[bit-depth] would be 8?
If so, I begin to understand. It depends on how to check the value can be represented in 8 bit .
The colormap value of logo1.tif is:
Colormap (320) SHORT (3) 6<65021 21331 65021 15934 65021 21074>
and these 2 colors are regarded as 'within 8bit' somehow, using tables or some algorithms.

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T17:41:12-07:00
by snibgo
246246 wrote:The colormap value of logo1.tif is:
Colormap (320) SHORT (3) 6<65021 21331 65021 15934 65021 21074>
and these 2 colors are regarded as 'within 8bit' somehow, using tables or some algorithms.
Yes. All those numbers are exactly divisible by 257. For example, 65021/257 = 253.0 exactly. So those values can be represented as 8-bit without losing precision.

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T19:02:12-07:00
by fmw42
I get in IM 6.9.9.39

Code: Select all

convert lena.png -depth 1 x.png

identify -verbose x.png
Depth: 8/1-bit

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-03-22T19:55:12-07:00
by 246246
Thank you for your clear explaiation, snibgo! I neverimagined such simple calculation exists --- well, 255 * 257 = 65535.

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-04-13T20:54:24-07:00
by 246246
fmw42 wrote:
2018-03-22T08:58:27-07:00
I do not see any %[bit-depth] listed at https://www.imagemagick.org/script/escape.php. Where is that documented?
I found it is mentioned here:
viewtopic.php?t=24291

Re: Why %[bit-depth] is sometimes 8 and sometimes 16 for indexed tiff?

Posted: 2018-04-13T21:05:43-07:00
by snibgo
So it is, and Fred asked for it!

@Fred: can this be documented in https://www.imagemagick.org/script/escape.php , please?