Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!

Debenu Quick PDF Library - PDF SDK Community Forum Homepage
Forum Home Forum Home > For Users of the Library > I need help - I can help
  New Posts New Posts RSS Feed - Colour Detection on Page
  FAQ FAQ  Forum Search   Register Register  Login Login

Colour Detection on Page

 Post Reply Post Reply
Author
Message
making View Drop Down
Beginner
Beginner
Avatar

Joined: 09 Jan 14
Location: UK
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote making Quote  Post ReplyReply Direct Link To This Post Topic: Colour Detection on Page
    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.)
 
Back to Top
Ingo View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 29 Oct 05
Status: Offline
Points: 3530
Post Options Post Options   Thanks (0) Thanks(0)   Quote Ingo Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
AndrewC View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 08 Dec 10
Location: Geelong, Aust
Status: Offline
Points: 841
Post Options Post Options   Thanks (0) Thanks(0)   Quote AndrewC Quote  Post ReplyReply Direct Link To This Post 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.


Back to Top
making View Drop Down
Beginner
Beginner
Avatar

Joined: 09 Jan 14
Location: UK
Status: Offline
Points: 5
Post Options Post Options   Thanks (0) Thanks(0)   Quote making Quote  Post ReplyReply Direct Link To This Post 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).
 
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.01
Copyright ©2001-2014 Web Wiz Ltd.

Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. AboutContactBlogSupportOnline Store