├── .gitattributes ├── .gitignore ├── ImageResizer.Plugins.PngOptimizer.sln ├── ImageResizer.Plugins.PngOptimizer ├── Analyzers │ └── PaletteAnalyzer.cs ├── ImageResizer.Plugins.PngOptimizer.csproj ├── ImageResizer.Plugins.PngOptimizer.nuspec ├── LICENCE.txt ├── Models │ ├── CubeCut.cs │ ├── DeltaPixel.cs │ └── MappedError.cs ├── PngOptimizerPlugin.cs ├── Properties │ └── AssemblyInfo.cs ├── Quantization │ └── DitheredLuminanceQuantizer.cs ├── content │ ├── demo.png │ └── web.config.transform └── packages.config ├── LICENCE ├── README.md ├── build-packages.ps1 └── packages.config /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageResizer.Plugins.PngOptimizer", "ImageResizer.Plugins.PngOptimizer\ImageResizer.Plugins.PngOptimizer.csproj", "{9686024B-9291-4AEB-9B03-BA8B5E8690FE}" 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 | {9686024B-9291-4AEB-9B03-BA8B5E8690FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {9686024B-9291-4AEB-9B03-BA8B5E8690FE}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {9686024B-9291-4AEB-9B03-BA8B5E8690FE}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {9686024B-9291-4AEB-9B03-BA8B5E8690FE}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Analyzers/PaletteAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Drawing; 3 | using System.Drawing.Imaging; 4 | 5 | namespace ImageResizer.Plugins.PngOptimizer.Analyzers 6 | { 7 | public class PaletteAnalyzer 8 | { 9 | public readonly long Colors; 10 | 11 | public PaletteAnalyzer(BitmapData bitmap) 12 | { 13 | Colors = GetColorCount(bitmap); 14 | } 15 | 16 | private unsafe long GetColorCount(BitmapData bitmap) 17 | { 18 | var colorRef = new HashSet(); 19 | long colors = 0; 20 | 21 | var bpp = Image.GetPixelFormatSize(bitmap.PixelFormat) / 8; 22 | 23 | var h = bitmap.Height; 24 | var w = bitmap.Width; 25 | var s = bitmap.Stride; 26 | var s0 = (byte*)bitmap.Scan0; 27 | 28 | for (var y = 0; y < h; y++) 29 | { 30 | var row = s0 + y * s; 31 | 32 | for (var x = 0; x < w; x++) 33 | { 34 | var p = x * bpp; 35 | 36 | int v = row[p]; 37 | for (var b = 1; b < bpp; b++) 38 | { 39 | v |= row[p + b] << (b * 8); 40 | } 41 | 42 | if (colorRef.Contains(v)) continue; 43 | 44 | colorRef.Add(v); 45 | colors++; 46 | } 47 | } 48 | 49 | return colors; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/ImageResizer.Plugins.PngOptimizer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {9686024B-9291-4AEB-9B03-BA8B5E8690FE} 8 | Library 9 | Properties 10 | ImageResizer.Plugins.PngOptimizer 11 | ImageResizer.Plugins.PngOptimizer 12 | v4.5.2 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | true 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | true 34 | 35 | 36 | 37 | ..\..\ImageResizer.Plugins.AutoCrop\packages\ImageResizer.4.0.5\lib\net45\ImageResizer.dll 38 | 39 | 40 | ..\..\ImageResizer.Plugins.AutoCrop\packages\nQuant.1.0.3\lib\net40\nQuant.Core.dll 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | Designer 57 | 58 | 59 | 60 | 61 | 62 | 63 | 70 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/ImageResizer.Plugins.PngOptimizer.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ImageResizer.Plugins.PngOptimizer 5 | 2.1.0 6 | ImageResizer.Plugins.PngOptimizer 7 | Geta 8 | Geta 9 | true 10 | Implementation of nQuant for png optimization. http://nquant.codeplex.com 11 | Copyright © Geta 2015 12 | https://github.com/svenrog/ImageResizer.Plugins.PngOptimizer/blob/master/LICENCE 13 | https://github.com/svenrog/ImageResizer.Plugins.PngOptimizer 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/LICENCE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Models/CubeCut.cs: -------------------------------------------------------------------------------- 1 | namespace ImageResizer.Plugins.PngOptimizer.Models 2 | { 3 | public struct CubeCut 4 | { 5 | public readonly byte? Position; 6 | public readonly float Value; 7 | 8 | public CubeCut(byte? cutPoint, float result) 9 | { 10 | Position = cutPoint; 11 | Value = result; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Models/DeltaPixel.cs: -------------------------------------------------------------------------------- 1 | namespace ImageResizer.Plugins.PngOptimizer.Models 2 | { 3 | public struct DeltaPixel 4 | { 5 | public int Alpha; 6 | public int Red; 7 | public int Green; 8 | public int Blue; 9 | 10 | public DeltaPixel(int alpha, int red, int green, int blue) 11 | { 12 | Alpha = alpha; 13 | Red = red; 14 | Green = green; 15 | Blue = blue; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Models/MappedError.cs: -------------------------------------------------------------------------------- 1 | namespace ImageResizer.Plugins.PngOptimizer.Models 2 | { 3 | public class MappedError 4 | { 5 | public int Index; 6 | public int AlphaError; 7 | } 8 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/PngOptimizerPlugin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Drawing.Imaging; 5 | using System.IO; 6 | using ImageResizer.Configuration; 7 | using ImageResizer.Plugins.PngOptimizer.Analyzers; 8 | using ImageResizer.Plugins.PngOptimizer.Quantization; 9 | using ImageResizer.Resizing; 10 | 11 | namespace ImageResizer.Plugins.PngOptimizer 12 | { 13 | public class PngOptimizerPlugin : BuilderExtension, IPlugin, IQuerystringPlugin 14 | { 15 | private const int _maxColors = 5120; 16 | private const int _maxDither = 24; 17 | private const int _defaultDither = 6; 18 | private const byte _ditherThreshold = 210; 19 | 20 | public IPlugin Install(Config c) 21 | { 22 | c.Plugins.add_plugin(this); 23 | return this; 24 | } 25 | 26 | public bool Uninstall(Config c) 27 | { 28 | c.Plugins.remove_plugin(this); 29 | return true; 30 | } 31 | 32 | public IEnumerable GetSupportedQuerystringKeys() 33 | { 34 | return new[] 35 | { 36 | "optimizePng", 37 | "optimizePngDebug", 38 | "dither" 39 | }; 40 | } 41 | 42 | protected override RequestedAction PostRenderImage(ImageState state) 43 | { 44 | var enabled = DetermineEnabled(state); 45 | if (!enabled) 46 | return RequestedAction.None; 47 | 48 | long colors = 0; 49 | byte dither = GetDitherSetting(state); 50 | bool debug = DetermineDebug(state); 51 | 52 | var bitmap = state.destBitmap; 53 | 54 | try 55 | { 56 | var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); 57 | var analyzer = new PaletteAnalyzer(data); 58 | 59 | bitmap.UnlockBits(data); 60 | colors = analyzer.Colors; 61 | } 62 | catch (Exception) 63 | { 64 | // ignored 65 | } 66 | 67 | if (colors > byte.MaxValue) 68 | { 69 | var ditherMax = (byte)(_maxDither * (Math.Min(_maxColors, colors) / _maxColors)); 70 | dither = Math.Min(ditherMax, dither); 71 | colors = byte.MaxValue; 72 | } 73 | else 74 | { 75 | dither = 0; 76 | } 77 | 78 | var quantizer = new DitheredLuminanceQuantizer(bitmap.Width, (byte)colors, _ditherThreshold, dither, debug); 79 | 80 | try 81 | { 82 | var processedBitmap = (Bitmap)quantizer.QuantizeImage(bitmap, 1, 1); 83 | state.destBitmap = processedBitmap; 84 | bitmap.Dispose(); 85 | } 86 | catch (Exception) 87 | { 88 | 89 | } 90 | 91 | return RequestedAction.None; 92 | } 93 | 94 | protected virtual byte GetDitherSetting(ImageState state) 95 | { 96 | if (state.settings == null) return _defaultDither; 97 | 98 | var setting = state.settings.Get("dither", _defaultDither); 99 | 100 | if (setting > _maxDither) return _maxDither; 101 | 102 | return setting; 103 | } 104 | 105 | protected virtual bool DetermineEnabled(ImageState state) 106 | { 107 | if (state == null) return false; 108 | if (state.settings == null) return false; 109 | if (state.destBitmap == null) return false; 110 | 111 | var setting = state.settings["optimizePng"] ?? string.Empty; 112 | 113 | if (setting == "0") return false; 114 | if (setting.Equals("false", StringComparison.InvariantCultureIgnoreCase)) return false; 115 | 116 | return IsPngFile(state); 117 | } 118 | 119 | protected virtual bool IsPngFile(ImageState state) 120 | { 121 | var extension = state.Job?.ResultFileExtension ?? 122 | GetExtension(state.sourceBitmap) ?? 123 | string.Empty; 124 | 125 | return extension.Equals("png", StringComparison.InvariantCultureIgnoreCase); 126 | } 127 | 128 | public string GetExtension(Bitmap source) 129 | { 130 | if (source == null) 131 | return null; 132 | 133 | var tag = source.Tag as BitmapTag; 134 | if (tag == null) 135 | return null; 136 | 137 | var path = tag.Path; 138 | if (string.IsNullOrEmpty(path)) 139 | return null; 140 | 141 | return Path.GetExtension(path) 142 | .TrimStart('.'); 143 | } 144 | 145 | protected virtual bool DetermineDebug(ImageState state) 146 | { 147 | var setting = state.settings["optimizePngDebug"]; 148 | 149 | if (string.IsNullOrEmpty(setting)) return false; 150 | if (setting == "0") return false; 151 | if (setting.Equals("false", StringComparison.InvariantCultureIgnoreCase)) return false; 152 | 153 | return true; 154 | } 155 | } 156 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("ImageResizer.Plugins.PngOptimizer")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("ImageResizer.Plugins.PngOptimizer")] 12 | [assembly: AssemblyCopyright("Copyright © 2016")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("9686024b-9291-4aeb-9b03-ba8b5e8690fe")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("2.1.0.0")] 35 | [assembly: AssemblyFileVersion("2.1.0.0")] 36 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/Quantization/DitheredLuminanceQuantizer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using ImageResizer.Plugins.PngOptimizer.Models; 5 | using nQuant; 6 | 7 | namespace ImageResizer.Plugins.PngOptimizer.Quantization 8 | { 9 | public class DitheredLuminanceQuantizer : WuQuantizerBase, IWuQuantizer 10 | { 11 | //Colorspace centric luminance 12 | private const double _luminance_a = 0.1; 13 | 14 | private const double _byteInverted = 1.0 / 256.0; 15 | 16 | //Percieved luminance 17 | private const double _luminance_r = 0.299; 18 | private const double _luminance_g = 0.587; 19 | private const double _luminance_b = 0.114; 20 | 21 | private readonly byte _ditherThreshold; 22 | private readonly byte _ditherAmount; 23 | private readonly byte _targetColorCount; 24 | 25 | private readonly int _width; 26 | private readonly bool _debug; 27 | 28 | // Bayer ordered dithering is used because it simply does not appear grainy if images are presented in sequence 29 | 30 | private readonly int[] _bayerPattern = { 31 | 0, 32, 8, 40, 2, 34, 10, 42, /* 8x8 Bayer ordered dithering */ 32 | 48, 16, 56, 24, 50, 18, 58, 26, /* pattern. Each input pixel */ 33 | 12, 44, 4, 36, 14, 46, 6, 38, /* is scaled to the 0..63 range */ 34 | 60, 28, 52, 20, 62, 30, 54, 22, /* before looking in this table */ 35 | 3, 35, 11, 43, 1, 33, 9, 41, /* to determine the action. */ 36 | 51, 19, 59, 27, 49, 17, 57, 25, 37 | 15, 47, 7, 39, 13, 45, 5, 37, 38 | 63, 31, 55, 23, 61, 29, 53, 21 }; 39 | 40 | private readonly double[] _adjustedPattern = new double[64]; 41 | 42 | public DitheredLuminanceQuantizer(int width, byte targetColorCount, byte ditherThreshold, byte ditherAmount = 12, bool debug = false) 43 | { 44 | _width = width; 45 | _ditherThreshold = ditherThreshold; 46 | _ditherAmount = ditherAmount; 47 | _targetColorCount = targetColorCount; 48 | 49 | _debug = debug; 50 | 51 | for (var i = 0; i < 64; i++) 52 | _adjustedPattern[i] = _bayerPattern[i] / 65.0 - 0.5; 53 | } 54 | 55 | protected override QuantizedPalette GetQuantizedPalette(int colorCount, ColorData data, IEnumerable cubes, int alphaThreshold) 56 | { 57 | if (_targetColorCount > 0) 58 | colorCount = _targetColorCount; 59 | 60 | int imageSize = data.PixelsCount; 61 | 62 | LookupData lookups = BuildLookups(cubes, data); 63 | 64 | IList quantizedPixels = data.QuantizedPixels; 65 | 66 | for (var index = 0; index < imageSize; ++index) 67 | { 68 | var indexParts = BitConverter.GetBytes(quantizedPixels[index]); 69 | 70 | quantizedPixels[index] = 71 | lookups.Tags[indexParts[Alpha], indexParts[Red], indexParts[Green], indexParts[Blue]]; 72 | } 73 | 74 | var alphas = new int[colorCount + 1]; 75 | var reds = new int[colorCount + 1]; 76 | var greens = new int[colorCount + 1]; 77 | var blues = new int[colorCount + 1]; 78 | var sums = new int[colorCount + 1]; 79 | 80 | var palette = new QuantizedPalette(imageSize); 81 | 82 | IList pixels = data.Pixels; 83 | int pixelsCount = data.PixelsCount; 84 | IList lookupsList = lookups.Lookups; 85 | int lookupsCount = lookupsList.Count; 86 | 87 | Dictionary cachedMatches = new Dictionary(); 88 | 89 | for (int pixelIndex = 0; pixelIndex < pixelsCount; pixelIndex++) 90 | { 91 | Pixel pixel = pixels[pixelIndex]; 92 | palette.PixelIndex[pixelIndex] = -1; 93 | 94 | if (pixel.Alpha <= alphaThreshold) 95 | continue; 96 | 97 | var x = pixelIndex % _width; 98 | var y = (pixelIndex - x) / _width; 99 | 100 | if (pixel.Alpha > 1 && _ditherAmount > 0) 101 | { 102 | // Get bayer dithering. 103 | var ov = _adjustedPattern[x % 8 + y % 8 * 8] * _ditherAmount; 104 | 105 | // Check for semi transparent areas 106 | if (pixel.Alpha < _ditherThreshold) 107 | { 108 | ov *= 1.5; 109 | 110 | // Apply dithering with a magnitude of 2 to the alpha 111 | pixel = new Pixel(Fit(pixel.Alpha + ov), pixel.Red, pixel.Green, pixel.Blue); 112 | } 113 | else 114 | { 115 | var l = (pixel.Red * _luminance_r + pixel.Blue * _luminance_b + pixel.Green * _luminance_g) * _byteInverted; 116 | 117 | if (l > 0.05 && l < 0.95) 118 | { 119 | if (_debug) 120 | { 121 | // Draw fuchsia on solid dither area 122 | pixel = new Pixel(255, 255, 0, 128); 123 | } 124 | else 125 | { 126 | // Apply dithering to the color layer 127 | pixel = new Pixel(pixel.Alpha, Fit(pixel.Red + ov), Fit(pixel.Green + ov), Fit(pixel.Blue + ov)); 128 | } 129 | } 130 | } 131 | } 132 | 133 | MappedError bestMatch; 134 | int argb = pixel.Argb; 135 | 136 | if (!cachedMatches.TryGetValue(argb, out bestMatch)) 137 | { 138 | int match = quantizedPixels[pixelIndex]; 139 | 140 | bestMatch = new MappedError 141 | { 142 | Index = match 143 | }; 144 | 145 | int bestDistance = int.MaxValue; 146 | 147 | for (int lookupIndex = 0; lookupIndex < lookupsCount; lookupIndex++) 148 | { 149 | Lookup lookup = lookupsList[lookupIndex]; 150 | 151 | var deltaAlpha = pixel.Alpha - lookup.Alpha; 152 | var deltaRed = pixel.Red - lookup.Red; 153 | var deltaGreen = pixel.Green - lookup.Green; 154 | var deltaBlue = pixel.Blue - lookup.Blue; 155 | 156 | // Take luminance into account when calculating color distance (green is always the most percievable, blue is the least). 157 | var distance = 158 | (int) 159 | ((double)deltaAlpha * deltaAlpha * _luminance_a + 160 | (double)deltaRed * deltaRed * _luminance_r + 161 | (double)deltaGreen * deltaGreen * _luminance_g + 162 | (double)deltaBlue * deltaBlue * _luminance_b); 163 | 164 | if (distance >= bestDistance) 165 | continue; 166 | 167 | bestDistance = distance; 168 | 169 | bestMatch.AlphaError = deltaAlpha; 170 | bestMatch.Index = lookupIndex; 171 | } 172 | 173 | cachedMatches[argb] = bestMatch; 174 | } 175 | 176 | alphas[bestMatch.Index] += pixel.Alpha; 177 | reds[bestMatch.Index] += pixel.Red; 178 | greens[bestMatch.Index] += pixel.Green; 179 | blues[bestMatch.Index] += pixel.Blue; 180 | sums[bestMatch.Index]++; 181 | 182 | palette.PixelIndex[pixelIndex] = bestMatch.Index; 183 | } 184 | 185 | for (var paletteIndex = 0; paletteIndex < colorCount; paletteIndex++) 186 | { 187 | if (sums[paletteIndex] > 0) 188 | { 189 | alphas[paletteIndex] /= sums[paletteIndex]; 190 | reds[paletteIndex] /= sums[paletteIndex]; 191 | greens[paletteIndex] /= sums[paletteIndex]; 192 | blues[paletteIndex] /= sums[paletteIndex]; 193 | } 194 | 195 | var color = Color.FromArgb(alphas[paletteIndex], reds[paletteIndex], greens[paletteIndex], blues[paletteIndex]); 196 | palette.Colors.Add(color); 197 | } 198 | 199 | palette.Colors.Add(Color.FromArgb(0, 0, 0, 0)); 200 | 201 | return palette; 202 | } 203 | 204 | protected virtual byte Fit(double value) 205 | { 206 | if (value < byte.MinValue) return byte.MinValue; 207 | if (value > byte.MaxValue) return byte.MaxValue; 208 | return (byte)value; 209 | } 210 | } 211 | } -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/content/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/svenrog/ImageResizer.Plugins.PngOptimizer/4de70d3e829ecd0d3a9b878711087469d9c304aa/ImageResizer.Plugins.PngOptimizer/content/demo.png -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/content/web.config.transform: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ImageResizer.Plugins.PngOptimizer/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ImageResizer.Plugins.PngOptimizer 2 | ==================================== 3 | PNG Optimizer for ImageResizer.NET 4 | 5 | ![bike example](https://raw.githubusercontent.com/svenrog/ImageResizer.Plugins.PngOptimizer/master/ImageResizer.Plugins.PngOptimizer/content/demo.png) 6 | 7 | nQuant based optimizer for reducing png file sizes by palette quantization. 8 | Uses a Bayer matrix ordered dithering on alpha channel for smoother shadows. 9 | 10 | Error diffusion dithering can sometimes give better results, but give noisy results during animation. 11 | 12 | ### Parameters 13 | 14 | * **optimizePng** - values '1' or 'true', specifies if plugin should be used. 15 | * **dither** - numeric value '0' to '24' (default 6), how much dithering to apply when reducing colors. -------------------------------------------------------------------------------- /build-packages.ps1: -------------------------------------------------------------------------------- 1 | cd .\.nuget 2 | 3 | .\nuget.exe pack ..\ImageResizer.Plugins.PngOptimizer\ImageResizer.Plugins.PngOptimizer.csproj -Properties Configuration=Release 4 | 5 | cd ..\ -------------------------------------------------------------------------------- /packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | --------------------------------------------------------------------------------