DotImage Knowledgebase

Home : Typical cause of an inverted black and white TIFF image
Q10069 - INFO: Typical cause of an inverted black and white TIFF image

Each pixel in a Black and White image is represented by a single bit. A 1-bit TIFF image has no palette, and can only be black and white. An "on" bit and an "off" bit can represent either black or white depending on the photometric tag (ID262) of the TIFF image. The typical photometric orientation of a black and white TIFF image is 0 = White and 1 = Black, which corresponds to a photometric tag value of 0. The opposite, 0 = Black, 1 = White corresponds to a photometric tag value of 1. Some image viewers ignore the photometric tag completely always assuming 0 = White, most notably the Windows XP Image Viewer. To make matters even worse, many TWAIN drivers create images with 0 = Black and 1 = White. The misconception is that there is a bug in our product, when in fact it's a bug in the Windows Image Viewer!

The following code will correct the image and palette so this will no longer be a problem. The code below requires a license of DotImage.

[C#]
if (image.PixelFormat == PixelFormat.Pixel1bppIndexed)
{
   
// See if the 0 palette entry is less than the 1 palette entry.
   
Color p0 = image.Palette.GetEntry(0);
    Color p1 = image.Palette.GetEntry(1);

    if ((p0.R << 16 + p0.G << 8 + p0.B) < (p1.R << 16 + p1.G << 8 + p1.B))
    {
       
// Swap the palette entries.
       
image.Palette.SetEntry(0, p1);
        image.Palette.SetEntry(1, p0);

       
// Invert the image data.
       
Atalasoft.Imaging.ImageProcessing.Channels.InvertCommand inv = new Atalasoft.Imaging.ImageProcessing.Channels.InvertCommand();
        inv.Apply(image);
    }
}

[VB.NET]
If image.PixelFormat = PixelFormat.Pixel1bppIndexed Then
   
' See if the 0 palette entry is less than the 1 palette entry.
   
Dim p0 As Color = image.Palette.GetEntry(0)
   
Dim p1 As Color = image.Palette.GetEntry(1)

   
If (p0.R << 16 + p0.G << 8 + p0.B) < (p1.R << 16 + p1.G << 8 + p1.B) Then
       
' Swap the palette entries.
       
image.Palette.SetEntry(0, p1)
        image.Palette.SetEntry(1, p0)

       
' Invert the image data.
       
Dim inv As Atalasoft.Imaging.ImageProcessing.Channels.InvertCommand = New Atalasoft.Imaging.ImageProcessing.Channels.InvertCommand()
        inv.Apply(image)
    
End If
End If

The same inversion can be accomplished using GDI+ Bitmap directly (using DotTwain without DotImage) but it requires a lot more work and unsafe image processing. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp11152001.asp for more information on that.
Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
This was EXACTLY what I was looking for! Approved: Wednesday, July 09, 2008
Good info, and very useful. Would be nice to have a VB.NET example, but I can translate well enough. Approved: Wednesday, January 04, 2006

Powered By InstantKB.NET v1.3
Copyright © 2002, 2014. InstantASP Ltd. All Rights Reserved

preload preload preload