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

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
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

I do not see any %[bit-depth] listed at https://www.imagemagick.org/script/escape.php. Where is that documented?
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post 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.
Last edited by 246246 on 2018-03-22T20:03:59-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Is it not the other way around, as

16/1-bit
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post 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
Last edited by 246246 on 2018-03-22T17:20:19-07:00, edited 1 time in total.
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post by 246246 »

Thank you for your clear explaiation, snibgo! I neverimagined such simple calculation exists --- well, 255 * 257 = 65535.
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo »

So it is, and Fred asked for it!

@Fred: can this be documented in https://www.imagemagick.org/script/escape.php , please?
snibgo's IM pages: im.snibgo.com
Post Reply