- 修改raw图像的常用操作,包括降低分辨率、平滑数据、分割数据、添加边缘点等,主要用于制作地形。
using System;
using System.IO;
namespace _06
{
class Program
{
static void Main(string[] args)
{
}
}
class Test
{
/// <summary>
/// 获取原数据
/// </summary>
/// <param name="sourceImagePath"></param>
/// <returns></returns>
public static byte[] GetSourceData(string sourceImagePath)
{
return File.ReadAllBytes(sourceImagePath);
}
/// <summary>
/// 翻转高度
/// </summary>
/// <param name="sourceData"></param>
public static void ReverseAltitude(byte[] sourceData)
{
for (int i = 0; i < sourceData.Length; i++)
{
sourceData[i] = (byte)(byte.MaxValue - sourceData[i]);
}
}
/// <summary>
/// 重构数据
/// </summary>
/// <param name="sourceData"></param>
/// <param name="row"></param>
/// <param name="col"></param>
/// <returns></returns>
public static byte[][] ReconstructData(byte[] sourceData, int row, int col)
{
byte[][] newData = new byte[row][];
for (int i = 0; i < row; i++)
{
newData[i] = new byte[col];
for (int j = 0; j < col; j++)
{
newData[i][j] = sourceData[i * col + j];
}
}
return newData;
}
/// <summary>
/// 垂直翻转数据
/// </summary>
/// <param name="sourceData"></param>
public static void VerticalReverseData(byte[][] sourceData)
{
int up = 0;
int down = sourceData.Length - 1;
byte[] temp;
while (up < down)
{
temp = sourceData[up];
sourceData[up] = sourceData[down];
sourceData[down] = temp;
up++;
down--;
}
}
/// <summary>
/// 降低分辨率
/// </summary>
/// <param name="sourceData"></param>
/// <param name="times"></param>
/// <returns></returns>
public static byte[][] DecreaseResolution(byte[][] sourceData, int times)
{
int newRow = sourceData.Length / times;
int newCol = sourceData[0].Length / times;
byte[][] newData = new byte[newRow][];
for (int i = 0; i < newRow; i++)
{
newData[i] = new byte[newCol];
for (int j = 0; j < newCol; j++)
{
newData[i][j] = sourceData[i * times][j * times];
}
}
return newData;
}
/// <summary>
/// 平滑数据
/// </summary>
/// <param name="sourceData"></param>
/// <param name="range"></param>
/// <returns></returns>
public static byte[][] SmoothData(byte[][] sourceData, int range)
{
int row = sourceData.Length;
int col = sourceData[0].Length;
byte[][] newData = new byte[row][];
for (int i = 0; i < row; i++)
{
newData[i] = new byte[col];
for (int j = 0; j < col; j++)
{
newData[i][j] = sourceData[i][j];
}
}
double sum;
double count = Math.Pow((1 + 2 * range), 2);
for (int i = range; i < row - range; i++)
{
for (int j = range; j < col - range; j++)
{
sum = 0;
for (int m = i - range; m <= i + range; m++)
{
for (int n = j - range; n <= j + range; n++)
{
sum += sourceData[m][n];
}
}
newData[i][j] = (byte)(sum / count);
}
}
return newData;
}
/// <summary>
/// 分割数据
/// </summary>
/// <param name="sourceData"></param>
/// <param name="row"></param>
/// <param name="col"></param>
/// <returns></returns>
public static byte[][][][] DivideData(byte[][] sourceData, int row, int col)
{
int sourceRow = sourceData.Length;
int sourceCol = sourceData[0].Length;
int newRow = sourceRow / row;
int newCol = sourceCol / col;
byte[][][][] newData = new byte[row][][][];
for (int i = 0; i < row; i++)
{
newData[i] = new byte[col][][];
for (int j = 0; j < col; j++)
{
newData[i][j] = new byte[newRow][];
for (int m = 0; m < newRow; m++)
{
newData[i][j][m] = new byte[newCol];
for (int n = 0; n < newCol; n++)
{
newData[i][j][m][n] = sourceData[i * newRow + m][j * newCol + n];
}
}
}
}
return newData;
}
/// <summary>
/// 添加边缘点
/// </summary>
/// <param name="sourceData"></param>
/// <returns></returns>
public static byte[][][][] AddEdgePoint(byte[][][][] sourceData)
{
int bRow = sourceData.Length;
int bCol = sourceData[0].Length;
int sRow = sourceData[0][0].Length;
int sCol = sourceData[0][0][0].Length;
int newsRow = sRow + 1;
int newsCol = sCol + 1;
byte[][][][] newData = new byte[bRow][][][];
for (int i = 0; i < bRow; i++)
{
newData[i] = new byte[bCol][][];
for (int j = 0; j < bCol; j++)
{
newData[i][j] = new byte[newsRow][];
for (int m = 0; m < newsRow; m++)
{
newData[i][j][m] = new byte[newsCol];
}
}
}
for (int i = 0; i < bRow; i++)
{
for (int j = 0; j < bCol; j++)
{
for (int m = 0; m < sRow; m++)
{
for (int n = 0; n < sCol; n++)
{
newData[i][j][m][n] = sourceData[i][j][m][n];
}
}
}
}
for (int i = 0; i < bRow; i++)
{
for (int j = 0; j < bCol; j++)
{
if (j < bCol - 1)
{
for (int k = 0; k < sRow; k++)
{
newData[i][j][k][sCol] = newData[i][j + 1][k][0];
}
}
else
{
for (int k = 0; k < sRow; k++)
{
newData[i][j][k][sCol] = newData[i][0][k][0];
}
}
}
}
for (int i = 0; i < bRow; i++)
{
for (int j = 0; j < bCol; j++)
{
if (i < bRow - 1)
{
for (int k = 0; k < newsCol; k++)
{
newData[i][j][sRow][k] = newData[i + 1][j][0][k];
}
}
else
{
for (int k = 0; k < newsCol; k++)
{
newData[i][j][sRow][k] = newData[i][j][sRow - 1][k];
}
}
}
}
return newData;
}
/// <summary>
/// 输出数据
/// </summary>
/// <param name="sourceData"></param>
/// <param name="outputDirectory"></param>
/// <param name="fileName"></param>
public static void OutputData(byte[][] sourceData, string outputDirectory, string fileName)
{
int row = sourceData.Length;
int col = sourceData[0].Length;
byte[] outputData = new byte[row * col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
outputData[i * col + j] = sourceData[i][j];
}
}
File.WriteAllBytes($"{outputDirectory}\\{fileName}.raw", outputData);
}
}
}