Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CMYK+alpha bitmap, mapping black to specific CMYK color

Given this arched text image, black pixels on transparent background, with antialiasing

arched text, RGB black on transparent

I would ultimately like to have a CMYK rendering of this text, where every black pixel becomes a specific CMYK color, say magenta, or {72,36,9,28}, or whatever my customer specifies. The ultimate output will be PDF. Every transparent pixel needs to remain transparent, as there may be other PDF objects under the text.

I would be content with a CMYK+Alpha TIFF equivalent written to disk. Or, possibly, a System.Windows.Media.Imaging.Bitmapimage. Or something else.

My PDF library is ABCpdf.NET 10. I realize that System.Drawing does not support CMYK color format. I'm ok with other third party libraries; I'm already using AForge in another context in the library.

I am not looking to do RGB to CMYK conversion, not even with a profile. I am not looking to do generic color replacement in a PDF. My customer will specify a definite CMYK target color, and I need to match it exactly; using color conversion on an RGB approximation is not okay.

I've flailed around with various things. The closest I got was to swap the colors while staying in RGB -- black becomes transparent, transparent becomes white -- and draw a filled magenta box under the image. That then draws white pixels where the text is not, and the magenta underneath shows through. Considered as a group, this ends up looking like warped magenta text on white. But anything sitting under the group does not show through. link

Any ideas?

like image 871
Ross Presser Avatar asked Nov 26 '25 06:11

Ross Presser


1 Answers

Jos Vernon at websupergoo came up with a splendidly simple answer that uses ABCpdf capabilities.

using WebSupergoo.ABCpdf10;
using WebSupergoo.ABCpdf10.Objects;

namespace RecolorRenderedImage
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Doc theDoc = new Doc()) {
                // this background image is there to prove that the text is not obscuring other pdf objects
                theDoc.AddImage("c:\\temp\\background-pic.jpg");

                // this is the black warped text
                string fullFilePath = "c:\\temp\\foobar.png";

                // read it into an XImage. Add to the doc, and retrieve the pixmap
                XReadOptions readOptions = new XReadOptions();
                XImage image = XImage.FromFile(fullFilePath, readOptions);
                int theID = theDoc.AddImageObject(image, true);
                int imageID = theDoc.GetInfoInt(theID, "XObject");
                PixMap thePM = (PixMap)theDoc.ObjectSoup[imageID];

                // recolor the pixmap temporary spot color space with the desired color (gold in this case)
                int spotColorID = theDoc.AddColorSpaceSpot("tempSpace", "24 44 100 2");
                ColorSpace theSP = (ColorSpace)theDoc.ObjectSoup[spotColorID];
                thePM.Recolor(theSP);

                // immediately recolor the pixmap back to CMYK
                ColorSpace theSP2 = new ColorSpace(theDoc.ObjectSoup, ColorSpaceType.DeviceCMYK);
                thePM.Recolor(theSP2);

                theDoc.Save("c:\\temp\\test.pdf");
            }
        }

    }
}

LATE EDIT: removed the link to the output file since I deleted it from GDrive at some point.

like image 67
Ross Presser Avatar answered Nov 28 '25 22:11

Ross Presser