Here's how to get human readable bit depth or color depth for an image in C#.
Note: This is measure in bits not bytes, therefore the denominator is 1000 not 1024.
You can get bit depth using Windows API Code Pack, but understand JPEG do not use a color map, so it consistently returns 24 / 3 = 8bpp.
http://metadataconsulting.blogspot.com/2020/05/How-to-iterate-over-image-music-and-video-properties-using-Microsoft-Windows-API-Code-Pack.html
Update - Remove LINQ dependency use getting fast integer extraction
https://metadataconsulting.blogspot.com/2020/06/CSharp-Get-a-number-or-integer-from-a-string-fast-a-speed-comparison.html
Note: This is measure in bits not bytes, therefore the denominator is 1000 not 1024.
You can get bit depth using Windows API Code Pack, but understand JPEG do not use a color map, so it consistently returns 24 / 3 = 8bpp.
http://metadataconsulting.blogspot.com/2020/05/How-to-iterate-over-image-music-and-video-properties-using-Microsoft-Windows-API-Code-Pack.html
Update - Remove LINQ dependency use getting fast integer extraction
https://metadataconsulting.blogspot.com/2020/06/CSharp-Get-a-number-or-integer-from-a-string-fast-a-speed-comparison.html
//Bits Per Pixel Number of Colors Available Common Name(s) //1 2 Monochrome //2 4 CGA //4 16 EGA //8 256 VGA //16 65536 XGA, High Color //24 16777216 SVGA, True Color //32 16777216 + Transparency //48 281 Trillion //https://victoriaprice135050.wordpress.com/2015/04/30/investigate-bit-depth-sampling-bits-per-pixel-bpp-monochrome-256-high-colour-true-colour-2/
//https://en.wikipedia.org/wiki/Tera-
public static string ToBitsPerPixelDecimalReadableFormat(string numint) { string unit; double d; string firstnumber = new string(numint .SkipWhile(x => !char.IsDigit(x)) .TakeWhile(x => char.IsDigit(x)) .ToArray()); int pow = 0; //reqr'd -> cannot do a bitwise shift on a double int.TryParse(firstnumber , out pow); long abs = (pow < 0 ? -pow : pow); //absolute value, we could use ulong if you know you will not get negative values if (pow == 0) return "1-bit"; int bit = 1 << pow; abs = bit; if (abs >= 0x1000000000000000) // Exa - quintillion { unit = "Quintn"; d = (bit >> 50); d = (d / 1000); // Divide by 1000 to get fractional value } else if (abs >= 0x4000000000000) // Peta - quadrillion { unit = "Quadn"; d = (bit >> 40); d = (d / 1000); } else if (abs >= 0x10000000000) // Tera - trillion { unit = "Tn"; d = (bit >> 30); d = (d / 1000); } else if (abs >= 0x40000000) // Giga - billion { unit = "B"; d = (bit >> 20); d = (d / 1000); } else if (abs >= 0x100000) // Mega - million { unit = "M"; d = (bit >> 10); d = (d / 1000); } else if (abs >= 0x400) // Kilo - thousand { unit = "K"; d = bit; d = (d / 1000); } else { unit = ""; d = bit; } return string.Concat(pow, "-bits(", d.ToString("0.##"), unit, "bpp)"); }
No comments:
Post a Comment