0

I wrote a desktop app which converts an 8bit TIFF to a 1bit but the output file cannot be opened in Photoshop (or other graphics software). What the application does is

  • it iterates every 8 bytes (1 byte per pixel) of the original image
  • then converts each value to bool (so either 0 or 1)
  • saves every 8 pixels in a byte - bits in the byte are in the same order as the pixels in the original image

The TIFF tags I set: MINISBLACK, compression is NONE, fill order is MSB2LSB, planar config is contiguous. I'm using BitMiracle's LibTiff.NET for reading and writing the files.

What am I doing wrong that the output cannot be opened by popular software?

Input image: http://www.filedropper.com/input
Output image: http://www.filedropper.com/output

Zoe
  • 27,060
  • 21
  • 118
  • 148
Val
  • 1,548
  • 1
  • 20
  • 36
  • 1
    For a start you could try looking at what e.g. Photoshop generates when you do the same steps there. – Joey Nov 14 '14 at 13:12
  • Spec: https://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf – Scott Solmer Nov 14 '14 at 13:14
  • You probably specify wrong BITSPERSAMPLE and/or SAMPLESPERPIXEL values. Try and open your image using AsTiffTagViewer utility and see what it will display. – Bobrovsky Nov 14 '14 at 19:02
  • 1
    Post a sample file and I'll tell you what's wrong with it. – BitBank Nov 14 '14 at 20:20
  • Input image: http://www.filedropper.com/input Output image: http://www.filedropper.com/output My conversion code: http://paste.ofcode.org/jqQ4zQp5SYaJwR2rUybBa Thx! – Val Nov 18 '14 at 09:15
  • Sorry about the long absence, I didn't see your comment until now. I downloaded the file from the link you provided and it's a 360MB multi-color PDF file. Weren't you creating a bitonal TIFF? – BitBank Dec 04 '14 at 09:13

1 Answers1

2

From your description of the byte manipulation part, it appears you are converting the image data from 8-bit to 1-bit correctly. If that's the case, and you don't have specific reasons to do it from scratch using your own code, you can simplify the task of creating valid TIFF files by using System.Drawing.Bitmap and System.Drawing.Imaging.ImageCodecInfo. This allows you to save either uncompressed 1-bit TIFF or compressed files with different types of compression. The code is as follows:

// first convert from byte[] to pointer
IntPtr pData = Marshal.AllocHGlobal(imgData.Length);
Marshal.Copy(imgData, 0, pData, imgData.Length);
int bytesPerLine = (imgWidth + 31) / 32 * 4; //stride must be a multiple of 4. Make sure the byte array already has enough padding for each scan line if needed
System.Drawing.Bitmap img = new Bitmap(imgWidth, imgHeight, bytesPerLine, PixelFormat.Format1bppIndexed, pData);

ImageCodecInfo TiffCodec = null;
foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
   if (codec.MimeType == "image/tiff")
   {
      TiffCodec = codec;
      break;
   }
EncoderParameters parameters = new EncoderParameters(2);
parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
parameters.Param[1] = new EncoderParameter(Encoder.ColorDepth, (long)1);
img.Save("OnebitLzw.tif", TiffCodec, parameters);

parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
img.Save("OnebitFaxGroup4.tif", TiffCodec, parameters);

parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
img.Save("OnebitUncompressed.tif", TiffCodec, parameters);

img.Dispose();
Marshal.FreeHGlobal(pData); //important to not get memory leaks
LEADTOOLS Support
  • 2,755
  • 1
  • 12
  • 12
  • I'm working on a different project now but I'll definitely try your solution when I get back to this – Val Feb 09 '15 at 09:37
  • thanks for your great tips. It fixes my this issue https://stackoverflow.com/questions/52299045/image-created-using-gdi-is-not-showing-in-vb6-leadtools :) – Imran Qadir Baksh - Baloch Sep 12 '18 at 18:29