package com.se.simu.utils;
|
|
import org.apache.commons.imaging.*;
|
|
import javax.imageio.ImageIO;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.File;
|
import java.io.IOException;
|
|
public class TiffToRGBUtil {
|
|
public static void tiffToPng(String tifPath, String pngPath) throws Exception {
|
// 输出的 PNG 文件路径
|
String outputPngFilePath = pngPath;
|
try {
|
// 读取 TIF 图像
|
File tiffFile = new File(tifPath);
|
BufferedImage tifImage = Imaging.getBufferedImage(tiffFile);
|
int width = tifImage.getWidth();
|
int height = tifImage.getHeight();
|
// 创建一个新的 BufferedImage 对象,类型为 TYPE_INT_ARGB
|
BufferedImage pngImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
// 遍历 TIF 图像的每个像素
|
for (int y = 0; y < height; y++) {
|
for (int x = 0; x < width; x++) {
|
// 获取 TIF 图像中当前像素的颜色值
|
int pixel = tifImage.getRGB(x, y);
|
// 在这里可以对像素的颜色值进行一些处理,例如根据高度信息调整颜色
|
// 假设我们简单地将像素的红色分量根据高度进行调整
|
int red = (pixel >> 16) & 0xff;
|
int green = (pixel >> 8) & 0xff;
|
int blue = (pixel) & 0xff;
|
// 假设高度信息存储在绿色分量中,你可以根据实际情况调整
|
//height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
|
int heightValue = (int) (-10000 + ((red * 256 * 256 + green * 256 + blue) * 0.1));
|
System.out.println(heightValue);
|
// 简单地将红色分量根据高度值进行调整,例如,越高越红
|
// 确保红色分量不超过 255
|
//red = Math.min((int) (red + (heightValue * 0.5)), 255);
|
// 重新组合 ARGB 值
|
Color color = new Color(red, green, blue);
|
System.out.printf("Pixel (%d, %d): R=%d, G=%d, B=%d%n", x, y, red, green, blue);
|
int newRgb = color.getRGB();
|
// 将处理后的像素颜色值设置到新的 PNG 图像中
|
pngImage.setRGB(x, y, newRgb);
|
}
|
}
|
// 保存为 PNG 图像
|
ImageIO.write(pngImage, "png", new File(outputPngFilePath));
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|