Skip to content

Commit

Permalink
Add GUI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
daramkun committed Apr 10, 2018
1 parent ed8a085 commit c085c67
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Daramee.IconGenerator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>
9 changes: 9 additions & 0 deletions Daramee.IconGenerator/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Daramee.IconGenerator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Daramee.IconGenerator"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions Daramee.IconGenerator/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Daramee.IconGenerator
{
/// <summary>
/// App.xaml에 대한 상호 작용 논리
/// </summary>
public partial class App : Application
{
}
}
114 changes: 114 additions & 0 deletions Daramee.IconGenerator/Daramee.IconGenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{34109481-1F43-4408-BFB5-2965C344DB6B}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>Daramee.IconGenerator</RootNamespace>
<AssemblyName>IconGenerator</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>Resources\MainIcon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="IconEncoder.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.6">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.6%28x86 및 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\MainIcon.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
150 changes: 150 additions & 0 deletions Daramee.IconGenerator/IconEncoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Daramee.IconGenerator
{
public class IconEncoder : IDisposable
{
[StructLayout ( LayoutKind.Sequential, Pack = 1 )]
struct IconHeader
{
public ushort reserved, type, count;
}

[StructLayout ( LayoutKind.Sequential, Pack = 1 )]
struct EntryHeader
{
public byte width;
public byte height;
public byte colorCount;
public byte reserved;
public ushort planes;
public ushort bitCount;
public uint sizeInBytes;
public uint fileOffset;
}

Stream outputStream;

List<KeyValuePair<Stream, EntryHeader>> entries;

public IconEncoder ( string filename )
: this ( new FileStream ( filename, FileMode.Create ) )
{ }
public IconEncoder ( Stream stream )
{
outputStream = stream;
entries = new List<KeyValuePair<Stream, EntryHeader>> ();
}

private Stream EncodeImageToPNG ( BitmapSource imageSource )
{
PngBitmapEncoder encoder = new PngBitmapEncoder ();
encoder.Frames.Add ( BitmapFrame.Create ( imageSource ) );

MemoryStream stream = new MemoryStream ();
encoder.Save ( stream );
stream.Position = 0;

return stream;
}

private void WriteToStream<T> ( Stream stream, T s ) where T : struct
{
IntPtr alloc = Marshal.AllocHGlobal ( Marshal.SizeOf<T> () );
Marshal.StructureToPtr<T> ( s, alloc, false );
byte [] array = new byte [ Marshal.SizeOf<T> () ];
Marshal.Copy ( alloc, array, 0, array.Length );
stream.Write ( array, 0, array.Length );
Marshal.FreeHGlobal ( alloc );
}

private BitmapSource ConvertIndexedColorImage ( BitmapSource source )
{
BitmapPalette myPalette = new BitmapPalette ( source, 256 );
PixelFormat pixelFormat = PixelFormats.Indexed8;
if ( myPalette.Colors.Count < 2 )
pixelFormat = PixelFormats.Indexed1;
else if ( myPalette.Colors.Count < 4 )
pixelFormat = PixelFormats.Indexed2;
else if ( myPalette.Colors.Count < 16 )
pixelFormat = PixelFormats.Indexed4;
else if ( myPalette.Colors.Count == 256 )
return source;

FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap ();
newFormatedBitmapSource.BeginInit ();
newFormatedBitmapSource.Source = source;
newFormatedBitmapSource.DestinationPalette = myPalette;
newFormatedBitmapSource.DestinationFormat = pixelFormat;
newFormatedBitmapSource.EndInit ();

return newFormatedBitmapSource;
}

public void InsertImage ( BitmapSource imageSource )
{
//imageSource = ConvertIndexedColorImage ( imageSource );
Stream stream = EncodeImageToPNG ( imageSource );

EntryHeader header = new EntryHeader
{
width = ( byte ) ( uint ) imageSource.Width,
height = ( byte ) ( uint ) imageSource.Height,
colorCount = ( byte ) ( imageSource.Palette != null ? imageSource.Palette.Colors.Count : 0 ),
planes = 1,
bitCount = 0,
sizeInBytes = ( uint ) stream.Length
};

entries.Add ( new KeyValuePair<Stream, EntryHeader> ( stream, header ) );
}

public void Encode ()
{
IconHeader header = new IconHeader ()
{
reserved = 0,
type = 1,
count = ( ushort ) entries.Count,
};
WriteToStream<IconHeader> ( outputStream, header );

uint totalWritten = ( uint ) Marshal.SizeOf<IconHeader> ()
+ ( uint ) entries.Count * ( uint ) Marshal.SizeOf<EntryHeader> ();
foreach ( var pair in entries )
{
var entryHeader = pair.Value;
entryHeader.fileOffset = totalWritten;
WriteToStream<EntryHeader> ( outputStream, entryHeader );
totalWritten += ( uint ) pair.Key.Length;
}
byte [] buffer = new byte [ 4194304 ];
foreach ( var pair in entries )
{
var stream = pair.Key;
int totalCopy = 0;
while ( totalCopy < stream.Length )
{
int read = stream.Read ( buffer, 0, buffer.Length );
outputStream.Write ( buffer, 0, read );
totalCopy += read;
}
}

outputStream.Flush ();
}

public void Dispose ()
{
outputStream.Dispose ();
}
}
}
Loading

0 comments on commit c085c67

Please sign in to comment.