From 61b6a81cad8e9d2674ef265b7814041b5ce5cdac Mon Sep 17 00:00:00 2001
From: 张洋洋 <10611411+yang-yang-z@user.noreply.gitee.com>
Date: 星期一, 20 一月 2025 09:34:04 +0800
Subject: [PATCH] [add]管线json

---
 src/main/java/com/se/simu/utils/TiffToRGBUtil.java       |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/main/java/com/se/simu/controller/SimuController.java |    2 +-
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/SimuController.java b/src/main/java/com/se/simu/controller/SimuController.java
index d4cae22..dc355c6 100644
--- a/src/main/java/com/se/simu/controller/SimuController.java
+++ b/src/main/java/com/se/simu/controller/SimuController.java
@@ -500,7 +500,7 @@
             dirFile.mkdirs();
         }
         String pngPath=path+"appearance\\terrain.png";
-        TiffToRGBUtil.tiffToPng(tifPath,pngPath);
+        TiffToRGBUtil.tifToPng(tifPath,pngPath);
         JSONObject jsonObject = getModule("terrainmodule.json");
         jsonObject.put("vertices",TiffCoordinateExtractorUtil.getCoordinate(tifPath));
         FileWriter fileWriter = new FileWriter(path + "terrain.json");
diff --git a/src/main/java/com/se/simu/utils/TiffToRGBUtil.java b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java
index 1519534..958b1d4 100644
--- a/src/main/java/com/se/simu/utils/TiffToRGBUtil.java
+++ b/src/main/java/com/se/simu/utils/TiffToRGBUtil.java
@@ -1,6 +1,10 @@
 package com.se.simu.utils;
 
 import org.apache.commons.imaging.*;
+import org.gdal.gdal.Band;
+import org.gdal.gdal.Dataset;
+import org.gdal.gdal.gdal;
+import org.gdal.gdalconst.gdalconstConstants;
 
 import javax.imageio.ImageIO;
 import java.awt.*;
@@ -9,6 +13,52 @@
 import java.io.IOException;
 
 public class TiffToRGBUtil {
+    public static void tifToPng(String tifPath, String pngPath) throws Exception {
+        // 娉ㄥ唽鎵�鏈夌殑 GDAL 椹卞姩
+        gdal.AllRegister();
+        // 杈撳叆鐨� TIFF 鏂囦欢璺緞
+        // 鎵撳紑杈撳叆鐨� TIFF 鏁版嵁闆�
+        Dataset dataset = gdal.Open(tifPath, gdalconstConstants.GA_ReadOnly);
+        if (dataset == null) {
+            System.err.println("鏃犳硶鎵撳紑杈撳叆鐨� TIFF 鏂囦欢");
+            return;
+        }
+        // 鑾峰彇鍦扮悊鍙樻崲淇℃伅
+        double[] geoTransform = dataset.GetGeoTransform();
+        // 鑾峰彇绗竴涓尝娈�
+        Band band = dataset.GetRasterBand(1);
+        // 鑾峰彇鍥惧儚瀹藉害鍜岄珮搴�
+        int width = dataset.getRasterXSize();
+        int height = dataset.getRasterYSize();
+        // 璇诲彇鍥惧儚鏁版嵁鍒板瓧鑺傛暟缁�
+        byte[] imageData = new byte[width * height];
+        band.ReadRaster(0, 0, width, height, imageData);
+        BufferedImage pngImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+        // 鍋囪楂樺害淇℃伅瀛樺偍鍦ㄧ伆搴﹀�间腑锛屾牴鎹湴鐞嗗彉鎹俊鎭拰鍍忕礌鍊艰绠楀疄闄呴珮搴�
+        for (int y = 0; y < height; y++) {
+            for (int x = 0; x < width; x++) {
+                int pixelValue = imageData[y * width + x] & 0xFF;
+                // 鍋囪楂樺害淇℃伅瀛樺偍鍦ㄥ儚绱犲�间腑
+                double pixelHeight = pixelValue * geoTransform[5];
+                //height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
+                int value = (int) (pixelHeight + 10000) * 10;
+                //value=(R * 256 * 256 + G * 256 + B);
+                int r = value / (256 * 256);
+                value = value % (256 * 256);
+                int g = value / 256;
+                int b = value % 256;
+                Color color = new Color(r, g, b);
+                System.out.printf("Pixel (%d, %d): R=%d, G=%d, B=%d%n", x, y, r, g, b);
+                int newRgb = color.getRGB();
+                // 灏嗗鐞嗗悗鐨勫儚绱犻鑹插�艰缃埌鏂扮殑 PNG 鍥惧儚涓�
+                pngImage.setRGB(x, y, newRgb);
+            }
+        }
+        // 淇濆瓨涓� PNG 鍥惧儚
+        ImageIO.write(pngImage, "png", new File(pngPath));
+        // 閲婃斁璧勬簮
+        dataset.delete();
+    }
 
     public static void tiffToPng(String tifPath, String pngPath) throws Exception {
         // 杈撳嚭鐨� PNG 鏂囦欢璺緞
@@ -37,7 +87,9 @@
                     System.out.println(heightValue);
                     // 绠�鍗曞湴灏嗙孩鑹插垎閲忔牴鎹珮搴﹀�艰繘琛岃皟鏁达紝渚嬪锛岃秺楂樿秺绾�
                     // 纭繚绾㈣壊鍒嗛噺涓嶈秴杩� 255
-                    //red = Math.min((int) (red + (heightValue * 0.5)), 255);
+                    red = Math.min(red, 255);
+                    green = Math.min(green, 255);
+                    blue = Math.min(blue, 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);

--
Gitblit v1.9.3