将 TIFF 转换为 1 位 [英] Convert TIFF to 1bit

查看:47
本文介绍了将 TIFF 转换为 1 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个桌面应用程序,可以将 8 位 TIFF 转换为 1 位,但无法在 Photoshop(或其他图形软件)中打开输出文件.应用程序的作用是

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

  • 它迭代原始图像的每 8 个字节(每个像素 1 个字节)
  • 然后将每个值转换为 bool(即 0 或 1)
  • 每 8 个像素保存一个字节 - 字节中的位与原始图像中的像素顺序相同

我设置的 TIFF 标签:MINISBLACK,压缩为 NONE,填充顺序为 MSB2LSB,平面配置是连续的.我正在使用 BitMiracle 的 LibTiff.NET 来读取和写入文件.

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?

输入图片:http://www.filedropper.com/input
输出图像:http://www.filedropper.com/output

推荐答案

从您对字节操作部分的描述来看,您似乎正确地将图像数据从 8 位转换为 1 位.如果是这种情况,并且您没有特定理由使用自己的代码从头开始创建,则可以使用 System.Drawing.Bitmap 和 System.Drawing.Imaging.ImageCodecInfo 简化创建有效 TIFF 文件的任务.这允许您保存未压缩的 1 位 TIFF 或具有不同压缩类型的压缩文件.代码如下:

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

这篇关于将 TIFF 转换为 1 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆