Search

Atalasoft Knowledge Base

HOWTO: Convert a MinIsBlack TIFF into a MinIsWhite TIFF

Administrator
DotImage

Most black and white TIFF images are saved such that 0 = White and 1 = Black. This equates to a Photometric value of 0 for MinIsWhite and 1 for MinIsBlack. Some image viewers ignore this tag. this is code code it takes to convert from one to another using DotImage.

InvertCommand invert = new InvertCommand();
AtalaImage invertImage = invert.Apply(image).Image;
invertImage.Palette.SetEntry(0, Color.Black);
invertImage.Palette.SetEntry(1, Color.White);
invertImage.Save("MinIsBlack.tif", new TiffEncoder(), null; 

If you simply wish to alter the tiff PhotometricInterpretation tag, you can use the following code:

string inFile = @"C:\badFile.tif";
string outFile = @"C:\fixedFile.tif";


TiffFile file = new TiffFile();
using (FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    file.Read(fs);    for (int i = 0; i < file.Images.Count; i++)    {
        TiffTag tag = file.Images[i].Tags.LookupTag(TiffTagID.PhotometricInterpretation);
        TiffTag newTag;

        string data = tag.Data.ToString();

        if (data == "0")
        {
            // Current PhotometricInterpretation: MinIsWhite
            //   Setting to MinIsBlack
            newTag = new TiffTag(TiffTagID.PhotometricInterpretation, 1, TiffTagDataType.Short);            // NOTE: you may want to comment this out if your goal is to keep it MinIsWhite
        }
        else
        {
            // Current PhotometricInterpretation: MinIsBlack
            //   Setting to MinIsWhite
            newTag = new TiffTag(TiffTagID.PhotometricInterpretation, 0, TiffTagDataType.Short);        }

        file.Images[i].Tags.Remove(tag);
        file.Images[i].Tags.Add(newTag)0;        // NOTE: this KB example is just switching the photometric tag.        // if you want to actually ensure that the palate entries match,        // you may want to implement something more akin to         // Q10069 - INFO: Typical cause of an inverted black and white TIFF image        // http://www.atalasoft.com/KB/article.aspx?id=10069        // if your issue is that you want to ensure the palate really is MinIsWhite
    }

    file.Save(outFile);
}

Original Article:
Q10154 - HOWTO: Convert a MinIsBlack TIFF into a MinIsWhite TIFF

Details
Last Modified: 4 Years Ago
Last Modified By: Tananda
Type: HOWTO
Article not rated yet.
Article has been viewed 3.4K times.
Options
Also In This Category