Skip to content

Commit 6223dfb

Browse files
Added Reset, tweaked Gotham and fixed ToArray() bug.
1 parent 1d1ad09 commit 6223dfb

File tree

20 files changed

+106
-359
lines changed

20 files changed

+106
-359
lines changed

src/ImageProcessor.Web/Config/ImageProcessorConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private void LoadGraphicsProcessors()
257257
// Add the available settings.
258258
foreach (IGraphicsProcessor processor in this.GraphicsProcessors)
259259
{
260-
processor.Settings = this.GetPluginSettings(processor.Name);
260+
processor.Settings = this.GetPluginSettings(processor.GetType().Name);
261261
}
262262
}
263263
catch (ReflectionTypeLoadException ex)

src/ImageProcessor/Helpers/Extensions/StringExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ public static string ToSHA512Fingerprint(this string expression)
114114
/// </summary>
115115
/// <param name="expression">The <see cref="T:System.String">String</see> instance that this method extends.</param>
116116
/// <returns>An array of integers scraped from the String.</returns>
117-
public static int[] ToIntegerArray(this string expression)
117+
public static int[] ToPositiveIntegerArray(this string expression)
118118
{
119119
Contract.Requires(!string.IsNullOrWhiteSpace(expression));
120120

121-
Regex regex = new Regex(@"(-|)\d+", RegexOptions.Compiled);
121+
Regex regex = new Regex(@"\d+", RegexOptions.Compiled);
122122

123123
MatchCollection matchCollection = regex.Matches(expression);
124124

src/ImageProcessor/ImageFactory.cs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class ImageFactory : IDisposable
3030
/// </summary>
3131
private const int DefaultJpegQuality = 90;
3232

33+
/// <summary>
34+
/// The backup image format.
35+
/// </summary>
36+
private ImageFormat backupImageFormat;
37+
3338
/// <summary>
3439
/// A value indicating whether this instance of the given entity has been disposed.
3540
/// </summary>
@@ -117,6 +122,7 @@ public ImageFactory Load(MemoryStream memoryStream)
117122

118123
// Set the other properties.
119124
this.JpegQuality = DefaultJpegQuality;
125+
this.backupImageFormat = ImageFormat.Jpeg;
120126
this.ImageFormat = ImageFormat.Jpeg;
121127
this.ShouldProcess = true;
122128

@@ -167,14 +173,47 @@ public ImageFactory Load(string imagePath)
167173

168174
// Set the other properties.
169175
this.JpegQuality = DefaultJpegQuality;
170-
this.ImageFormat = ImageUtils.GetImageFormat(imageName);
176+
ImageFormat imageFormat = ImageUtils.GetImageFormat(imageName);
177+
this.backupImageFormat = imageFormat;
178+
this.ImageFormat = imageFormat;
171179
this.ShouldProcess = true;
172180
}
173181
}
174182

175183
return this;
176184
}
177185

186+
/// <summary>
187+
/// Resets the ImageFactory to its original loaded state.
188+
/// </summary>
189+
/// <returns>
190+
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
191+
/// </returns>
192+
public ImageFactory Reset()
193+
{
194+
if (this.ShouldProcess)
195+
{
196+
MemoryStream memoryStream = (MemoryStream)this.Image.Tag;
197+
198+
// Set our new image as the memorystream value.
199+
Image newImage = Image.FromStream(memoryStream);
200+
201+
// Store the stream in the image Tag property so we can dispose of it later.
202+
newImage.Tag = memoryStream;
203+
204+
// Dispose and reassign the image.
205+
this.Image.Dispose();
206+
this.Image = newImage;
207+
208+
// Set the other properties.
209+
this.JpegQuality = DefaultJpegQuality;
210+
this.ImageFormat = this.backupImageFormat;
211+
}
212+
213+
return this;
214+
}
215+
216+
178217
#region Manipulation
179218
/// <summary>
180219
/// Adds a query-string to the image factory to allow auto-processing of remote files.
@@ -441,7 +480,7 @@ public ImageFactory Rotate(RotateLayer rotateLayer)
441480
/// <returns>
442481
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
443482
/// </returns>
444-
public ImageFactory Saturate(int percentage)
483+
public ImageFactory Saturation(int percentage)
445484
{
446485
if (this.ShouldProcess)
447486
{
@@ -451,7 +490,7 @@ public ImageFactory Saturate(int percentage)
451490
percentage = 0;
452491
}
453492

454-
Saturate saturate = new Saturate { DynamicParameter = percentage };
493+
Saturation saturate = new Saturation { DynamicParameter = percentage };
455494

456495
this.Image = saturate.ProcessImage(this);
457496
}
@@ -504,7 +543,10 @@ public ImageFactory Watermark(TextLayer textLayer)
504543
/// Saves the current image to the specified file path.
505544
/// </summary>
506545
/// <param name="filePath">The path to save the image to.</param>
507-
public void Save(string filePath)
546+
/// <returns>
547+
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
548+
/// </returns>
549+
public ImageFactory Save(string filePath)
508550
{
509551
if (this.ShouldProcess)
510552
{
@@ -527,16 +569,18 @@ public void Save(string filePath)
527569
ImageCodecInfo.GetImageEncoders().FirstOrDefault(
528570
ici => ici.MimeType.Equals("image/jpeg", StringComparison.OrdinalIgnoreCase));
529571

530-
// ReSharper disable AssignNullToNotNullAttribute
572+
// ReSharper disable AssignNullToNotNullAttribute
531573
this.Image.Save(filePath, imageCodecInfo, encoderParameters);
532-
// ReSharper restore AssignNullToNotNullAttribute
574+
// ReSharper restore AssignNullToNotNullAttribute
533575
}
534576
}
535577
else
536578
{
537579
this.Image.Save(filePath, this.ImageFormat);
538580
}
539581
}
582+
583+
return this;
540584
}
541585

542586
/// <summary>
@@ -545,7 +589,10 @@ public void Save(string filePath)
545589
/// <param name="memoryStream">
546590
/// The <see cref="T:System.IO.MemoryStream"/> to save the image information to.
547591
/// </param>
548-
public void Save(MemoryStream memoryStream)
592+
/// <returns>
593+
/// The current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
594+
/// </returns>
595+
public ImageFactory Save(MemoryStream memoryStream)
549596
{
550597
if (this.ShouldProcess)
551598
{
@@ -573,6 +620,8 @@ public void Save(MemoryStream memoryStream)
573620
this.Image.Save(memoryStream, this.ImageFormat);
574621
}
575622
}
623+
624+
return this;
576625
}
577626

578627
#region IDisposable Members

src/ImageProcessor/Imaging/Filters/ColorMatrixes.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace ImageProcessor.Imaging.Filters
1010
#region Using
1111
using System;
1212
using System.Collections.Generic;
13+
using System.Diagnostics.CodeAnalysis;
1314
using System.Linq;
1415
using System.Text;
1516
using System.Drawing.Imaging;
@@ -80,6 +81,7 @@ internal static ColorMatrix Polaroid
8081
/// <summary>
8182
/// Gets Lomograph.
8283
/// </summary>
84+
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
8385
internal static ColorMatrix Lomograph
8486
{
8587
get
@@ -115,25 +117,6 @@ internal static ColorMatrix GreyScale
115117
}
116118
}
117119

118-
/// <summary>
119-
/// Gets Gotham.
120-
/// </summary>
121-
internal static ColorMatrix Gotham
122-
{
123-
get
124-
{
125-
return new ColorMatrix(
126-
new float[][]
127-
{
128-
new float[] { .9f, .9f, .9f, 0, 0 },
129-
new float[] { .9f, .9f, .9f, 0, 0 },
130-
new float[] { .9f, .9f, .9f, 0, 0 },
131-
new float[] { 0, 0, 0, 1, 0 },
132-
new float[] { -.5f, -.5f, -.45f, 0, 1 }
133-
});
134-
}
135-
}
136-
137120
/// <summary>
138121
/// Gets Invert.
139122
/// </summary>
@@ -156,6 +139,7 @@ internal static ColorMatrix Invert
156139
/// <summary>
157140
/// Gets HiSatch.
158141
/// </summary>
142+
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
159143
internal static ColorMatrix HiSatch
160144
{
161145
get
@@ -175,6 +159,7 @@ internal static ColorMatrix HiSatch
175159
/// <summary>
176160
/// Gets LoSatch.
177161
/// </summary>
162+
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
178163
internal static ColorMatrix LoSatch
179164
{
180165
get

src/ImageProcessor/Imaging/Filters/ComicMatrixFilter.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace ImageProcessor.Imaging.Filters
99
{
1010
#region Using
1111
using System;
12+
using System.Diagnostics.CodeAnalysis;
1213
using System.Drawing;
1314
using System.Drawing.Drawing2D;
1415
using System.Drawing.Imaging;
16+
using System.Runtime.InteropServices;
1517
#endregion
1618

1719
/// <summary>
@@ -20,8 +22,9 @@ namespace ImageProcessor.Imaging.Filters
2022
internal class ComicMatrixFilter : IMatrixFilter
2123
{
2224
/// <summary>
23-
/// Enumurates Argb colour channels.
25+
/// Enumerates Argb color channels.
2426
/// </summary>
27+
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
2528
private enum ChannelArgb
2629
{
2730
/// <summary>
@@ -148,6 +151,7 @@ public Image TransformImage(ImageFactory factory, Image image, Image newImage)
148151
patternBitmap.Dispose();
149152
}
150153
}
154+
151155
return image;
152156
}
153157

@@ -185,7 +189,7 @@ private static void TransferOneArgbChannelFromOneBitmapToAnother(Bitmap source,
185189
byte[] sourceRgbValues = new byte[bytes];
186190

187191
// Copy the RGB values into the array.
188-
System.Runtime.InteropServices.Marshal.Copy(bitmapDataSource.Scan0, sourceRgbValues, 0, bytes);
192+
Marshal.Copy(bitmapDataSource.Scan0, sourceRgbValues, 0, bytes);
189193

190194
// Unlockbits the source.
191195
source.UnlockBits(bitmapDataSource);
@@ -197,7 +201,7 @@ private static void TransferOneArgbChannelFromOneBitmapToAnother(Bitmap source,
197201
byte[] destinationRgbValues = new byte[bytes];
198202

199203
// Copy the RGB values into the array.
200-
System.Runtime.InteropServices.Marshal.Copy(bitmapDataDestination.Scan0, destinationRgbValues, 0, bytes);
204+
Marshal.Copy(bitmapDataDestination.Scan0, destinationRgbValues, 0, bytes);
201205

202206
int s = (int)sourceChannel;
203207
int d = (int)destinationChannel;
@@ -210,11 +214,10 @@ private static void TransferOneArgbChannelFromOneBitmapToAnother(Bitmap source,
210214
}
211215

212216
// Copy the RGB values back to the bitmap
213-
System.Runtime.InteropServices.Marshal.Copy(destinationRgbValues, 0, bitmapDataDestination.Scan0, bytes);
217+
Marshal.Copy(destinationRgbValues, 0, bitmapDataDestination.Scan0, bytes);
214218

215219
// Unlock bits the destination.
216220
destination.UnlockBits(bitmapDataDestination);
217221
}
218-
219222
}
220223
}

src/ImageProcessor/Imaging/Filters/GothamMatrixFilter.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
namespace ImageProcessor.Imaging.Filters
99
{
1010
#region Using
11-
using System;
12-
using System.Collections.Generic;
13-
using System.Linq;
14-
using System.Text;
1511
using System.Drawing;
16-
using System.Drawing.Imaging;
1712
using System.Drawing.Drawing2D;
13+
using System.Drawing.Imaging;
14+
15+
using ImageProcessor.Processors;
16+
1817
#endregion
1918

2019
/// <summary>
@@ -27,7 +26,7 @@ internal class GothamMatrixFilter : IMatrixFilter
2726
/// </summary>
2827
public ColorMatrix Matrix
2928
{
30-
get { return ColorMatrixes.Gotham; }
29+
get { return ColorMatrixes.GreyScale; }
3130
}
3231

3332
/// <summary>
@@ -61,21 +60,30 @@ public Image TransformImage(ImageFactory factory, Image image, Image newImage)
6160

6261
// Paint a burgundy rectangle with a transparency of ~30% over the image.
6362
// Paint a blue rectangle with a transparency of 20% over the image.
64-
using (SolidBrush brush = new SolidBrush(Color.FromArgb(77, 43, 4, 18)))
63+
using (SolidBrush brush = new SolidBrush(Color.FromArgb(77, 38, 14, 28)))
6564
{
6665
Region oldClip = graphics.Clip;
6766
graphics.Clip = new Region(rectangle);
6867
graphics.FillRectangle(brush, rectangle);
6968

7069
// Fill the blue.
71-
brush.Color = Color.FromArgb(51, 12, 22, 88);
70+
brush.Color = Color.FromArgb(51, 29, 32, 59);
7271
graphics.FillRectangle(brush, rectangle);
7372
graphics.Clip = oldClip;
7473
}
7574
}
7675
}
7776
}
7877

78+
// Add brightness and contrast to finish the effect.
79+
factory.Image = newImage;
80+
Brightness brightness = new Brightness { DynamicParameter = 5 };
81+
newImage = (Bitmap)brightness.ProcessImage(factory);
82+
83+
factory.Image = newImage;
84+
Contrast contrast = new Contrast { DynamicParameter = 85 };
85+
newImage = (Bitmap)contrast.ProcessImage(factory);
86+
7987
// Reassign the image.
8088
image.Dispose();
8189
image = newImage;

src/ImageProcessor/Processors/Alpha.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,6 @@ public class Alpha : IGraphicsProcessor
2727
private static readonly Regex QueryRegex = new Regex(@"alpha=(?:100|[1-9]?[0-9])", RegexOptions.Compiled);
2828

2929
#region IGraphicsProcessor Members
30-
/// <summary>
31-
/// Gets the name.
32-
/// </summary>
33-
public string Name
34-
{
35-
get
36-
{
37-
return "Alpha";
38-
}
39-
}
40-
41-
/// <summary>
42-
/// Gets the description.
43-
/// </summary>
44-
public string Description
45-
{
46-
get
47-
{
48-
return "Changes the alpha component of the image to effect its transparency.";
49-
}
50-
}
51-
5230
/// <summary>
5331
/// Gets the regular expression to search strings for.
5432
/// </summary>
@@ -111,7 +89,7 @@ public int MatchRegexIndex(string queryString)
11189
{
11290
// Set the index on the first instance only.
11391
this.SortOrder = match.Index;
114-
int percentage = match.Value.ToIntegerArray()[0];
92+
int percentage = int.Parse(match.Value.Split('=')[1]);
11593

11694
this.DynamicParameter = percentage;
11795
}

0 commit comments

Comments
 (0)