张洋洋
2025-01-16 94d808f30053f8d03185cca31df3c8673d815699
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
        }
    }
}