Print Page | Close Window

Colour Detection on Page

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2809
Printed Date: 13 Mar 26 at 5:27PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Colour Detection on Page
Posted By: making
Subject: Colour Detection on Page
Date Posted: 09 Jan 14 at 11:20PM
Hi All,
 
This is my first post so please be gentle Embarrassed. Is there any way to tell if a PDF page is black and white or colour (e.g. 256 colour, 65536 colour, 24bit etc). Reason why I'm asking is I want to decide which format to save an image in before I render it (Black and White G4 TIFF, everything else JPG etc.)
 



Replies:
Posted By: Ingo
Date Posted: 12 Jan 14 at 1:41PM
Hi Making,

there's a lot you can "see" without a pdf-library.
Please read my blog here:
http://pdfcomments.blogspot.de/2009/09/under-surface.html
What you're looking for is code like the following:
// ...
// textcomplete contains the filecontent ...
devrgb  := CountSubStr('/DEVICERGB', UpperCase(textcomplete));
devcmyk := CountSubStr('/DEVICECMYK', UpperCase(textcomplete));
devgray := CountSubStr('/DEVICEGRAY', UpperCase(textcomplete));
// ...

Cheers and welcome here,
Ingo



-------------
Cheers,
Ingo



Posted By: AndrewC
Date Posted: 14 Jan 14 at 11:13AM
A complex question.

Ingo's answer is a good solution.

Another idea is to render the PDF to a low DPI BMP file and then manually counting the colours.

There is no easy solution to this.

Another option is to call QP.GetPageColorSpaces functions to see which colorspaces are directly referenced.  

You will need to do testing with each solution to make sure it is working correctly with your PDF's. The PDF spec is very complex and there are many tricks that can be used including functions and Indexed colorspaces.

It will be interesting to hear about your testing.

Andrew.




Posted By: making
Date Posted: 14 Jan 14 at 1:26PM
Thanks Andrew and Ingo, After a lot of playing about it looks like the rendering could be the safest approach. There's lot of colourspaces but we also need to take into account the actual colour of any images being rendered. I came up with the following C# which seems to work most of the time (I've got a class derived from the Quick PDF class, hence the base. calls):
public bool IsBiColour(int FileHandle, int PageRef)
{
    double renderScale = base.GetRenderScale();
    SetRenderScale(0.1);
    base.SetGDIPlusOptions(1, 0);       // turn text / vector smoothing off so we get a true bicolour image
    base.SetGDIPlusOptions(3, 0);       // turn image smoothing off (off as default anyway)
    bool biColour = false;
    using (MemoryStream ms = new MemoryStream(base.DARenderPageToString(FileHandle, PageRef, 0, 300)))
    {
        using (Bitmap bitmap = new Bitmap(ms))
        {
            List<int> colours = new List<int>();
            for (int y = 0; y < bitmap.Height && colours.Count <= 2; y++)
            {
                for (int x = 0; x < bitmap.Width && colours.Count <= 2; x++)
                {
                    int colour = bitmap.GetPixel(x, y).ToArgb();
                    if (!colours.Contains(colour)) colours.Add(colour);
                }
            }
            biColour = (colours.Count <= 2);
        }
    }
    SetRenderScale(renderScale);
    return biColour;
}
A (very) minor annoyance is there is no GetGDIPlusOptions. I can set the options but I cannot restore them back to their previous values (like I do with renderScale).
 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk