Note that there are some explanatory texts on larger screens.

plurals
  1. POFind CMYK counts in bitmap pixels
    text
    copied!<ol> <li>RGB bitmap to CMYK?</li> <li>Count the CMYK to read the colour depth?</li> </ol> <p>or</p> <ol> <li>Count RGB values(done), not convert RGB --> CMYK using ICC profile</li> </ol> <p>How to find the percentage of CMYK in bitmap. Similar to Ghostscript API. I can't use this in my program because it is not managed and doesn't support for parallel processing. I have done this using RGB <a href="https://stackoverflow.com/q/18757027/1834352">(as per this)</a> , but for printer friendliness need this in CMYK.</p> <pre><code> public static bool IsCMYK(System.Drawing.Image image) { var flags = (ImageFlags)image.Flags; if (flags.HasFlag(ImageFlags.ColorSpaceCmyk) || flags.HasFlag(ImageFlags.ColorSpaceYcck)) { return true; } const int PixelFormat32bppCMYK = (15 | (32 &lt;&lt; 8)); return (int)image.PixelFormat == PixelFormat32bppCMYK; } public void ImagemagicTool(Bitmap bi) { ImageMagick.MagickImage img = new MagickImage(bi); string str= img.ColorSpace.ToString(); } public ImageColorFormat GetColorFormat(Bitmap bitmap) { const int pixelFormatIndexed = 0x00010000; const int pixelFormat32bppCMYK = 0x200F; const int pixelFormat16bppGrayScale = (4 | (16 &lt;&lt; 8)); // Check image flags var flags = (ImageFlags)bitmap.Flags; if (flags.HasFlag(ImageFlags.ColorSpaceCmyk) || flags.HasFlag(ImageFlags.ColorSpaceYcck)) { return ImageColorFormat.Cmyk; } else if (flags.HasFlag(ImageFlags.ColorSpaceGray)) { return ImageColorFormat.Grayscale; } // Check pixel format var pixelFormat = (int)bitmap.PixelFormat; if (pixelFormat == pixelFormat32bppCMYK) { return ImageColorFormat.Cmyk; } else if ((pixelFormat &amp; pixelFormatIndexed) != 0) { return ImageColorFormat.Indexed; } else if (pixelFormat == pixelFormat16bppGrayScale) { return ImageColorFormat.Grayscale; } // Default to RGB return ImageColorFormat.Rgb; } </code></pre> <p>*Update: Looks I have to separate the layers CMYK and then find the percentage of the each C,M,Y &amp; K.How do I do that in bitmap stream? This might be complex, for simpler approach Finding RGB and then Convert RGB to CMYK based on ICC US webcotedSWOP v2 .ICC...</p> <p>Just trying with the above mentioned, appreciate to put me in right direction</p> <hr> <p>Now I have got the CMYK propotionate values for RGB in bitmap, but not sure about the desired result in percentage, is there any upper limit for CMYK? </p> <pre><code>public static CMYK RGBtoCMYK(int red, int green, int blue) { double c = (double)(255 - red) / 255; double m = (double)(255 - green) / 255; double y = (double)(255 - blue) / 255; double min = (double)Math.Min(c, Math.Min(m, y)); if (min == 1.0) { return new CMYK(0, 0, 0, 1); } else { return new CMYK((c - min) / (1 - min), (m - min) / (1 - min), (y - min) / (1 - min), min); } } public class CMYK { public double Cyan { get; set; } public double Magenta { get; set; } public double Yellow { get; set; } public double Black { get; set; } public CMYK() { } public CMYK(double cyan, double magenta, double yellow,double black) { Cyan = cyan; Magenta = magenta; Yellow = yellow; Black = black; } } public static void GetRGBlock(Bitmap bmp) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); IntPtr ptr = bmpData.Scan0; List&lt;System.Drawing.Color&gt; cols = new List&lt;System.Drawing.Color&gt;(); int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); int x = 0; int y = 0; int dx = 0; int l = 0; for (y = 0; y &lt;= rect.Height - 1; y++) { l = y * bmpData.Stride; //calulate line based on stride for (x = 0; x &lt;= rect.Width - 1; x++) { dx = l + x * 3; //3 for RGB, 4 for ARGB, notice l is used as offset cols.Add(System.Drawing.Color.FromArgb(rgbValues[dx + 2], rgbValues[dx + 1], rgbValues[dx])); } } List&lt;CMYK&gt; cmykcols = new List&lt;CMYK&gt;(); foreach (var item in cols) cmykcols.Add(RGBtoCMYK(item.R, item.G, item.B)); bmp.UnlockBits(bmpData); bmp.Dispose(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload