Framwork/Assets/Scripts/Runtime/Test/TiffFileConverterWithLibrar...

115 lines
4.3 KiB
C#
Raw Normal View History

2025-07-02 10:24:01 +08:00
using UnityEngine;
using System.IO;
using BitMiracle.LibTiff.Classic;
public class TiffFileConverterWithLibrary : MonoBehaviour
{
public string pngPath;
public string tiffPath;
[ContextMenu("ConvertPngFileToTiff")]
void PNG()
{
ConvertPngFileToTiff(pngPath, tiffPath);
}
/// <summary>
/// 从文件路径加载 PNG并使用 LibTiff.Net 将其转换为 TIFF 文件。
/// </summary>
/// <param name="pngFilePath">源 PNG 文件的完整路径。</param>
/// <param name="outputTiffPath">目标 TIFF 文件的输出路径。</param>
public static void ConvertPngFileToTiff(string pngFilePath, string outputTiffPath)
{
if (!File.Exists(pngFilePath))
{
Debug.LogError($"Source PNG file not found at: {pngFilePath}");
return;
}
// 步骤 1: 读取 PNG 文件数据并加载到 Texture2D
byte[] fileData = File.ReadAllBytes(pngFilePath);
// 创建一个临时的 Texture2D。尺寸无关紧要LoadImage 会自动调整。
Texture2D pngTexture = new Texture2D(2, 2);
// LoadImage 会自动解码 PNG 数据并填充 Texture2D
if (!pngTexture.LoadImage(fileData))
{
Debug.LogError("Failed to load PNG data into Texture2D.");
return;
}
// 步骤 2: 从 Texture2D 提取像素数据并使用 LibTiff.Net 写入 TIFF 文件
int width = pngTexture.width;
int height = pngTexture.height;
try
{
using (Tiff tiff = Tiff.Open(outputTiffPath, "w"))
{
if (tiff == null)
{
Debug.LogError("Could not open TIFF file for writing.");
return;
}
tiff.SetField(TiffTag.IMAGEWIDTH, width);
tiff.SetField(TiffTag.IMAGELENGTH, height);
tiff.SetField(TiffTag.SAMPLESPERPIXEL, 4); // RGBA
tiff.SetField(TiffTag.BITSPERSAMPLE, 8);
tiff.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
tiff.SetField(TiffTag.ROWSPERSTRIP, height);
tiff.SetField(TiffTag.XRESOLUTION, 88.0);
tiff.SetField(TiffTag.YRESOLUTION, 88.0);
tiff.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
tiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
tiff.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
tiff.SetField(TiffTag.COMPRESSION, Compression.LZW);
Color32[] pixels = pngTexture.GetPixels32();
byte[] raster = new byte[width * height * 4];
// LibTiff 需要的像素顺序可能与 Unity 不同,需要垂直翻转
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color32 pixel = pixels[(height - 1 - y) * width + x];
int index = (y * width + x) * 4;
raster[index] = pixel.r;
raster[index + 1] = pixel.g;
raster[index + 2] = pixel.b;
raster[index + 3] = pixel.a;
}
}
tiff.WriteEncodedStrip(0, raster, raster.Length);
tiff.Close();
Debug.Log($"Successfully converted file '{pngFilePath}' to '{outputTiffPath}' using LibTiff.Net");
}
}
catch (System.Exception ex)
{
Debug.LogError($"Failed to convert PNG file to TIFF using LibTiff.Net: {ex.Message}");
}
finally
{
// 销毁临时的 Texture2D 对象以释放内存
Object.Destroy(pngTexture);
}
}
// 示例用法
void Start()
{
return;
string sourcePngPath = Path.Combine(Application.streamingAssetsPath, "YourImage.png");
string destinationTiffPath = Path.Combine(Application.persistentDataPath, "output_from_file_libtiff.tif");
#if UNITY_EDITOR || UNITY_STANDALONE
ConvertPngFileToTiff(sourcePngPath, destinationTiffPath);
#else
Debug.LogWarning("Direct file access to StreamingAssets might not work on this platform.");
#endif
}
}