Skip to content
Browse files

Rewrote Silverlight resize to use WritableBitmap for performance.

  • Loading branch information...
1 parent d717c7b commit e718e70baa184bd5fc2a1078b34c64f83b6cad8b @spocke spocke committed Dec 22, 2009
View
BIN js/plupload.silverlight.xap
Binary file not shown.
View
18 src/csharp/Plupload.sln
@@ -1,18 +0,0 @@
-
-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008
-# SharpDevelop 3.0.0.3437
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plupload", "Plupload\Plupload.csproj", "{95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Any CPU = Debug|Any CPU
- Release|Any CPU = Release|Any CPU
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.Build.0 = Release|Any CPU
- EndGlobalSection
-EndGlobal
View
BIN src/csharp/Plupload.suo
Binary file not shown.
View
36 src/csharp/Plupload/FJCore/Properties/AssemblyInfo.cs
@@ -1,36 +0,0 @@
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-// General Information about an assembly is controlled through the following
-// set of attributes. Change these attribute values to modify the information
-// associated with an assembly.
-[assembly: AssemblyTitle("FluxJpeg Core")]
-[assembly: AssemblyDescription("JPEG codec and image manipulation.")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("fluxcapacity.net")]
-[assembly: AssemblyProduct("FJCore")]
-[assembly: AssemblyCopyright("Copyright 2008")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// Setting ComVisible to false makes the types in this assembly not visible
-// to COM components. If you need to access a type in this assembly from
-// COM, set the ComVisible attribute to true on that type.
-[assembly: ComVisible(false)]
-
-// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("b177112a-db56-4edf-b0fd-e7b9b504924e")]
-
-// Version information for an assembly consists of the following four values:
-//
-// Major Version
-// Minor Version
-// Build Number
-// Revision
-//
-// You can specify all the values or you can default the Build and Revision Numbers
-// by using the '*' as shown below:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
View
91 src/csharp/Plupload/FileReference.cs
@@ -15,6 +15,7 @@
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Browser;
+using System.Windows.Media.Imaging;
using FluxJpeg.Core.Decoder;
using FluxJpeg.Core.Encoder;
using FluxJpeg.Core;
@@ -109,29 +110,12 @@ public class FileReference {
try {
// Is jpeg and image size is defined
- if (Regex.IsMatch(this.name, @"\.(jpeg|jpg)$", RegexOptions.IgnoreCase) && (image_width != 0 || image_height != 0)) {
- JpegDecoder decoder = new JpegDecoder(this.info.OpenRead());
- this.ResizeProgress(this, new ResizeProgressEventArgs(0));
- DecodedJpeg jpeg = decoder.Decode();
- ImageResizer resizer;
- Image resizedImage;
-
- if (ImageResizer.ResizeNeeded(jpeg.Image, Math.Max(image_width, image_height))) {
- resizer = new ImageResizer(jpeg.Image);
- this.ResizeProgress(this, new ResizeProgressEventArgs(33));
- resizedImage = resizer.Resize(image_width, image_height, FluxJpeg.Core.Filtering.ResamplingFilters.NearestNeighbor);
-
- this.ResizeProgress(this, new ResizeProgressEventArgs(66));
- this.fileStream = new MemoryStream();
-
- JpegEncoder encoder = new JpegEncoder(resizedImage, image_quality, this.fileStream);
- encoder.Encode();
- this.fileStream.Seek(0, SeekOrigin.Begin);
- this.size = this.fileStream.Length;
- this.ResizeProgress(this, new ResizeProgressEventArgs(100));
- }
- } else
- this.fileStream = this.info.OpenRead();
+ if (Regex.IsMatch(this.name, @"\.(jpeg|jpg)$", RegexOptions.IgnoreCase) && (image_width != 0 || image_height != 0)) {
+ this.fileStream = this.ResizeImage(this.info.OpenRead(), image_width, image_height, image_quality);
+ this.fileStream.Seek(0, SeekOrigin.Begin);
+ this.size = this.fileStream.Length;
+ } else
+ this.fileStream = this.info.OpenRead();
} catch (Exception ex) {
syncContext.Send(delegate {
this.OnIOError(new ErrorEventArgs(ex.Message, 0, this.chunks));
@@ -324,6 +308,67 @@ public class FileReference {
((ScriptObject) HtmlPage.Window.Eval("console")).Invoke("log", new string[] {msg});
}
+ private Stream ResizeImage(Stream image_stream, int width, int height, int quality) {
+ try {
+ // Load the image as a writeablebitmap
+ WriteableBitmap writableBitmap;
+ BitmapImage bitmapImage = new BitmapImage();
+ bitmapImage.SetSource(image_stream);
+ writableBitmap = new WriteableBitmap(bitmapImage);
+ image_stream.Close();
+ double scale = Math.Min((double) width / writableBitmap.PixelWidth, (double) height / writableBitmap.PixelHeight);
+
+ // No resize needed
+ if (scale >= 1.0)
+ return image_stream;
+
+ // Setup shorter names and pixelbuffers
+ int w = writableBitmap.PixelWidth;
+ int h = writableBitmap.PixelHeight;
+ int[] p = writableBitmap.Pixels;
+ byte[][,] pixelsForJpeg = new byte[3][,]; // RGB colors
+ pixelsForJpeg[0] = new byte[w, h];
+ pixelsForJpeg[1] = new byte[w, h];
+ pixelsForJpeg[2] = new byte[w, h];
+
+ // Copy WriteableBitmap data into buffer for FluxJpeg
+ int i = 0;
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ int color = p[i++];
+
+ pixelsForJpeg[0][x, y] = (byte) (color >> 16); // R
+ pixelsForJpeg[1][x, y] = (byte) (color >> 8); // G
+ pixelsForJpeg[2][x, y] = (byte) (color); // B
+ }
+ }
+
+ // Create new FluxJpeg image based on pixel data
+ FluxJpeg.Core.Image jpegImage = new FluxJpeg.Core.Image(new ColorModel {
+ colorspace = ColorSpace.RGB
+ }, pixelsForJpeg);
+
+ // Calc new proportional size
+ width = (int) Math.Round(writableBitmap.PixelWidth * scale);
+ height = (int) Math.Round(writableBitmap.PixelHeight * scale);
+
+ // Resize the image
+ ImageResizer resizer = new ImageResizer(jpegImage);
+ Image resizedImage = resizer.Resize(width, height, FluxJpeg.Core.Filtering.ResamplingFilters.NearestNeighbor);
+
+ // Encode the resized image as Jpeg
+ Stream imageStream = new MemoryStream();
+ JpegEncoder encoder = new JpegEncoder(resizedImage, quality, imageStream);
+ encoder.Encode();
+
+ return imageStream;
+ } catch {
+ // Ignore the error and let the server resize the image
+ }
+
+ return image_stream;
+ }
+
#endregion
}
View
75 src/csharp/Plupload/Plupload.csproj
@@ -37,6 +37,7 @@
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
+ <IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
@@ -49,7 +50,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
- <IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
@@ -100,30 +100,64 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
- <Compile Include="FJCore\DCT.cs" />
- <Compile Include="FJCore\DecodedJpeg.cs" />
+ <Compile Include="FJCore\DCT.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\DecodedJpeg.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="FJCore\Decoder\HuffmanTable.cs" />
<Compile Include="FJCore\Decoder\JpegComponent.cs" />
<Compile Include="FJCore\Decoder\JpegDecoder.cs" />
<Compile Include="FJCore\Decoder\JpegFrame.cs" />
<Compile Include="FJCore\Decoder\JpegHuffmanTable.cs" />
<Compile Include="FJCore\Decoder\JpegQuantizationTable.cs" />
<Compile Include="FJCore\Decoder\JpegScan.cs" />
- <Compile Include="FJCore\Encoder\JpegEncoder.cs" />
- <Compile Include="FJCore\FDCT.cs" />
- <Compile Include="FJCore\Filter\Convolution.cs" />
- <Compile Include="FJCore\Filter\FilterBase.cs" />
- <Compile Include="FJCore\Filter\FilterLowpassResize.cs" />
- <Compile Include="FJCore\Filter\FilterNNResize.cs" />
- <Compile Include="FJCore\Filter\GrayImage.cs" />
- <Compile Include="FJCore\Image.cs" />
- <Compile Include="FJCore\IO\BinaryReader.cs" />
- <Compile Include="FJCore\IO\BinaryWriter.cs" />
- <Compile Include="FJCore\IO\JpegBinaryReader.cs" />
- <Compile Include="FJCore\JpegMarker.cs" />
- <Compile Include="FJCore\Resize\ImageResizer.cs" />
- <Compile Include="FJCore\YCbCr.cs" />
- <Compile Include="FJCore\ZigZag.cs" />
+ <Compile Include="FJCore\Encoder\JpegEncoder.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\FDCT.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Filter\Convolution.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Filter\FilterBase.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Filter\FilterLowpassResize.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Filter\FilterNNResize.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Filter\GrayImage.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Image.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\IO\BinaryReader.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\IO\BinaryWriter.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\IO\JpegBinaryReader.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\JpegMarker.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\Resize\ImageResizer.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\YCbCr.cs">
+ <SubType>Code</SubType>
+ </Compile>
+ <Compile Include="FJCore\ZigZag.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Page.xaml.cs">
<DependentUpon>Page.xaml</DependentUpon>
</Compile>
@@ -161,15 +195,12 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
+ <ItemGroup />
<ItemGroup>
<Content Include="FJCore\IJG.txt" />
<Content Include="FJCore\JAI.txt" />
<Content Include="FJCore\License.txt" />
<Content Include="FJCore\README.txt" />
- <Content Include="test.html" />
- </ItemGroup>
- <ItemGroup>
- <Folder Include="FJCore\Properties\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\$(SilverlightVersion)\Microsoft.Silverlight.CSharp.targets" />
<PropertyGroup>
View
12 src/csharp/Plupload/Plupload.sln
@@ -1,7 +1,6 @@

-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio 2008
-# SharpDevelop 3.0.0.3800
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plupload", "Plupload.csproj", "{95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}"
EndProject
Global
@@ -10,9 +9,12 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.Build.0 = Release|Any CPU
+ {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
View
BIN src/csharp/Plupload/Plupload.suo
Binary file not shown.
View
18 src/csharp/Plupload/Upload.sln
@@ -1,18 +0,0 @@
-
-Microsoft Visual Studio Solution File, Format Version 9.00
-# Visual Studio 2005
-# SharpDevelop 2.2.1.2648
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Upload", "Upload.csproj", "{95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Any CPU = Debug|Any CPU
- Release|Any CPU = Release|Any CPU
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.Build.0 = Release|Any CPU
- {95F0DEE8-DE7A-46C5-9DCC-0570B0FC4643}.Release|Any CPU.ActiveCfg = Release|Any CPU
- EndGlobalSection
-EndGlobal

0 comments on commit e718e70

Please sign in to comment.
Something went wrong with that request. Please try again.