Skip to content

Commit 4aa90c1

Browse files
committed
添加项目文件。
1 parent ee87293 commit 4aa90c1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

ConvertPPM.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<UseWPF>true</UseWPF>
7+
<ImplicitUsings>true</ImplicitUsings>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<PlatformTarget>x64</PlatformTarget>
10+
</PropertyGroup>
11+
12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<DebugType>none</DebugType>
14+
</PropertyGroup>
15+
16+
</Project>

ConvertPPM.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32811.315
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertPPM", "ConvertPPM.csproj", "{A394BD61-6A4A-43DE-9D4B-4B6526E2FBA8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A394BD61-6A4A-43DE-9D4B-4B6526E2FBA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A394BD61-6A4A-43DE-9D4B-4B6526E2FBA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A394BD61-6A4A-43DE-9D4B-4B6526E2FBA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A394BD61-6A4A-43DE-9D4B-4B6526E2FBA8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FB628B64-E27F-4D85-85C3-4554935441E0}
24+
EndGlobalSection
25+
EndGlobal

Program.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.IO;
2+
using System.Windows.Media;
3+
using System.Windows.Media.Imaging;
4+
5+
string fileName;
6+
start:
7+
if (args.Length == 1) fileName = args[0];
8+
else
9+
{
10+
Console.WriteLine("请输入要转换的ppm,留空认为\"binary.ppm\":");
11+
fileName = Console.ReadLine();
12+
if (string.IsNullOrWhiteSpace(fileName)) fileName = "binary.ppm";
13+
}
14+
int resX, resY;
15+
try
16+
{
17+
using StreamReader openText = File.OpenText(fileName);
18+
string MagicNumber = openText.ReadLine();
19+
if (MagicNumber != "P6")
20+
{
21+
Console.WriteLine("Magic number is not P6. It's " + MagicNumber);
22+
return;
23+
}
24+
string[] res = openText.ReadLine().Split(' ');
25+
resX = Convert.ToInt32(res[0]);
26+
resY = Convert.ToInt32(res[1]);
27+
}
28+
catch(FileNotFoundException)
29+
{
30+
Console.WriteLine(fileName + "文件未找到.");
31+
goto start;
32+
}
33+
using var inputFS = File.OpenRead(fileName);
34+
int enterCount = 3;
35+
while (enterCount > 0)
36+
{
37+
if (inputFS.ReadByte() == '\n') enterCount--;
38+
}
39+
WriteableBitmap bitmap = new(resX, resY, 96, 96, PixelFormats.Rgb24, null);
40+
bitmap.Lock();
41+
Span<byte> sp;
42+
unsafe
43+
{
44+
void* bufferPtr = (void*)bitmap.BackBuffer;
45+
sp = new(bufferPtr, resX * resY * 3);
46+
}
47+
inputFS.Read(sp);
48+
bitmap.Unlock();
49+
PngBitmapEncoder encoder = new();
50+
encoder.Frames.Add(BitmapFrame.Create(bitmap));
51+
string outputName = Path.ChangeExtension(fileName, "png");
52+
using FileStream outputFS = new(outputName, FileMode.Create);
53+
encoder.Save(outputFS);
54+
Console.WriteLine(outputName + "输出完毕.");

0 commit comments

Comments
 (0)