ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Types | Public Member Functions | Protected Member Functions | List of all members
ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin Class Reference
Inheritance diagram for ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin:
Collaboration graph
[legend]

Public Types

enum  FilterType {
  None = 10, Prewitt = 0, V1 = 1, VSquare = 2,
  Sobel = 3, Laplacian = 4
}
 
enum  OutputType {
  CAIR = 0, Grayscale = 1, Edge = 2, VerticalEnergy = 3,
  HorizontalEnergy = 4, Removal = 5, CAIR_HD = 6
}
 
enum  EnergyType { Backward = 0, Forward = 1 }
 

Public Member Functions

IEnumerable< string > GetSupportedQuerystringKeys ()
 If the plugin reads any values from the querystring, the names of the keys should be specified here. This information is required so that the HttpModule knows when to handle an image request. More...
 
IPlugin Install (Configuration.Config c)
 
bool Uninstall (Configuration.Config c)
 
- Public Member Functions inherited from ImageResizer.Resizing.AbstractImageProcessor
 AbstractImageProcessor ()
 Creates a new AbstractImageProcessor with no extensions More...
 
 AbstractImageProcessor (IEnumerable< BuilderExtension > extensions)
 Creates a new AbstractImageProcessor which will run the specified extensions with each method call. More...
 
virtual Bitmap DecodeStreamFailed (Stream s, ResizeSettings settings, string optionalPath)
 Extensions are executed until one extension returns a non-null value. This is taken to mean that the error has been resolved. Extensions should not throw an exception unless they wish to cause subsequent extensions to not execute. If extensions throw an ArgumentException or ExternalException, it will be wrapped in an ImageCorruptedException instance. If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream); More...
 
virtual Bitmap DecodeStream (Stream s, ResizeSettings settings, string optionalPath)
 Extend this to support alternate image source formats. If the Bitmap class is used for decoding, read gdi-bugs.txt and make sure you set b.Tag to new BitmapTag(optionalPath,stream); More...
 
- Public Member Functions inherited from ImageResizer.Plugins.IPlugin
IPlugin Install (Config c)
 Installs the plugin in the specified Config instance. The plugin must handle all the work of loading settings, registering the plugin etc. More...
 
bool Uninstall (Config c)
 Uninstalls the plugin. Should reverse all changes made during Install More...
 

Protected Member Functions

override RequestedAction LayoutImage (ImageState s)
 Process.3(Layout).2: Rings 'image' and 'imageArea' are added to the layout. More...
 
override RequestedAction PostLayoutImage (ImageState s)
 Process.3(Layout).3: Add rings here to insert them between the image area and the padding More...
 
override RequestedAction PreRenderImage (ImageState s)
 Process.5(Render).9: Plugins have a chance to pre-process the source image before it gets rendered, and save it to s.preRenderBitmap More...
 
- Protected Member Functions inherited from ImageResizer.Resizing.AbstractImageProcessor
virtual void PreLoadImage (ref object source, ref string path, ref bool disposeSource, ref ResizeSettings settings)
 Extend this to allow additional types of source objects to be accepted by transforming them into Bitmap instances. More...
 
virtual Stream GetStream (object source, ResizeSettings settings, ref bool disposeStream, out string path, out bool restoreStreamPosition)
 Extend this to allow additional types of source objects to be accepted by transforming them into Stream instances. First plugin to return a Stream wins. More...
 
virtual RequestedAction PostDecodeStream (ref Bitmap img, ResizeSettings settings)
 Extend this to modify the Bitmap instance after it has been decoded by DecodeStream or DecodeStreamFailed More...
 
virtual void PreAcquireStream (ref object dest, ResizeSettings settings)
 Extend this to allow additional types of destination objects to be accepted by transforming them into a stream. More...
 
virtual RequestedAction BuildJob (ImageResizer.ImageJob job)
 The method to override if you want to replace the entire pipeline. All Build() calls call this method first. Does nothing in ImageBuilder More...
 
virtual RequestedAction buildToStream (Bitmap source, Stream dest, ResizeSettings settings)
 Called for Build() calls that want the result encoded. (Not for Bitmap Build(source,settings) calls. Only override this method if you need to replace the behavior of image encoding and image processing together, such as adding support for resizing multi-page TIFF files or animated GIFs. More...
 
virtual Bitmap buildToBitmap (Bitmap source, ResizeSettings settings, bool transparencySupported)
 Most calls funnel through here. Default behavior configures an ImageState instance and calls Process(imageState); Shouldn't be overriden for any reason I can think of - use the appropriate virtual method under Process(). If an extension returns a Bitmap instance, it will be used instead of the default behavior. Does NOT dispose of 'source' or 'source's underlying stream. More...
 
virtual RequestedAction OnProcess (ImageState s)
 Process.0 First step of the Process() method. Can replace the entire Process method if RequestAction.Cancel is returned. Can be used to add points to translate (for image maps), and also to modify the settings More...
 
virtual RequestedAction PrepareSourceBitmap (ImageState s)
 Process.1 Switches the bitmap to the correct frame or page, and applies source flipping commands. More...
 
virtual RequestedAction PostPrepareSourceBitmap (ImageState s)
 Process.2 Extend this to apply any pre-processing to the source bitmap that needs to occur before Layout begins More...
 
virtual RequestedAction Layout (ImageState s)
 Process.3(Layout).0: This is the last point at which points to translate should be added. Only return RequestedAction.Cancel if you wish to replace the entire Layout sequence logic. More...
 
virtual RequestedAction FlipExistingPoints (ImageState s)
 Process.3(Layout).1: This is where the points in the layout are flipped the same way the source bitmap was flipped (unless their flags specify otherwise) More...
 
virtual RequestedAction LayoutPadding (ImageState s)
 Process.3(Layout).4: Ring "padding" is added to the layout More...
 
virtual RequestedAction PostLayoutPadding (ImageState s)
 Process.3(Layout).5: Add rings here to insert them between the padding and the border More...
 
virtual RequestedAction LayoutBorder (ImageState s)
 Process.3(Layout).6: Ring "border" is added to the layout More...
 
virtual RequestedAction PostLayoutBorder (ImageState s)
 Process.3(Layout).7: Add rings here to insert them between the border and the effect rings More...
 
virtual RequestedAction LayoutEffects (ImageState s)
 Process.3(Layout).8: Effects such as 'shadow' are added here. More...
 
virtual RequestedAction PostLayoutEffects (ImageState s)
 Process.3(Layout).9: Add rings here to insert them between the effects and the margin More...
 
virtual RequestedAction LayoutMargin (ImageState s)
 Process.3(Layout).10: Margins are added to the layout More...
 
virtual RequestedAction PostLayoutMargin (ImageState s)
 Process.3(Layout).11: Add rings here to insert them around the margin. Rings will be outermost More...
 
virtual RequestedAction LayoutRotate (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout is rotated. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutRotate (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout is rotated. May be called anytime during Layout() More...
 
virtual RequestedAction LayoutNormalize (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout is normalized to 0,0. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutNormalize (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout is normalized. May be called anytime during Layout() More...
 
virtual RequestedAction LayoutRound (ImageState s)
 Process.3(Layout).anytime: Occurs when the layout point values are rounded to integers. May be called anytime during Layout() More...
 
virtual RequestedAction PostLayoutRound (ImageState s)
 Process.3(Layout).anytime: Occurs after the layout point values are rounded to integers. May be called anytime during Layout() More...
 
virtual RequestedAction EndLayout (ImageState s)
 Process.3(Layout).12: Occurs once layout has finished. No more changes should occur to points or rings in the layout after this method. destSize is calculated here. More...
 
virtual RequestedAction PrepareDestinationBitmap (ImageState s)
 Process.4: The destination bitmap is created and sized based destSize. A graphics object is initialized for rendering. More...
 
virtual RequestedAction Render (ImageState s)
 Process.5(Render) Rendering. Do not return RequestedAction.Cancel unless you want to replace the entire rendering system. More...
 
virtual RequestedAction RenderBackground (ImageState s)
 Process.5(Render).1 The background color is rendered More...
 
virtual RequestedAction PostRenderBackground (ImageState s)
 Process.5(Render).2 After the background color is rendered More...
 
virtual RequestedAction RenderEffects (ImageState s)
 Process.5(Render).3 Effects (such as a drop shadow or outer glow) are rendered More...
 
virtual RequestedAction PostRenderEffects (ImageState s)
 Process.5(Render).4 After outer effects are rendered More...
 
virtual RequestedAction RenderPadding (ImageState s)
 Process.5(Render).5 Image padding is drawn More...
 
virtual RequestedAction PostRenderPadding (ImageState s)
 Process.5(Render).6 After image padding is drawn More...
 
virtual RequestedAction CreateImageAttribues (ImageState s)
 Process.5(Render).7: An ImageAttributes instance is created if it doesn't already exist. More...
 
virtual RequestedAction PostCreateImageAttributes (ImageState s)
 Process.5(Render).8: The ImageAttributes instance exists and can be modified or replaced. More...
 
virtual RequestedAction RenderImage (ImageState s)
 Process.5(Render).10: The image is copied to the destination parallelogram specified by ring 'image'. More...
 
virtual RequestedAction PostRenderImage (ImageState s)
 Process.5(Render).11: After the image is drawn More...
 
virtual RequestedAction RenderBorder (ImageState s)
 Process.5(Render).12: The border is rendered More...
 
virtual RequestedAction PostRenderBorder (ImageState s)
 Process.5(Render).13: After the border is drawn More...
 
virtual RequestedAction PreRenderOverlays (ImageState s)
 Process.5(Render).14: Any last-minute changes before watermarking or overlays are applied More...
 
virtual RequestedAction RenderOverlays (ImageState s)
 Process.5(Render).15: Watermarks can be rendered here. All image processing should be done More...
 
virtual RequestedAction PreFlushChanges (ImageState s)
 Process.5(Render).16: Called before changes are flushed and the graphics object is destroyed. More...
 
virtual RequestedAction FlushChanges (ImageState s)
 Process.5(Render).17: Changes are flushed to the bitmap here and the graphics object is destroyed. More...
 
virtual RequestedAction PostFlushChanges (ImageState s)
 Process.5(Render).18: Changes have been flushed to the bitmap, but the final bitmap has not been flipped yet. More...
 
virtual RequestedAction ProcessFinalBitmap (ImageState s)
 Process.6: Non-rendering changes to the bitmap object occur here, such as flipping. The graphics object is unavailable. More...
 
virtual RequestedAction EndProcess (ImageState s)
 Process.7: Layout and rendering are both complete. More...
 

Additional Inherited Members

- Protected Attributes inherited from ImageResizer.Resizing.AbstractImageProcessor
volatile IEnumerable
< BuilderExtension
exts
 Contains the set of extensions that are called for every method. More...
 

Detailed Description

Definition at line 13 of file SeamCarvingPlugin.cs.

Member Function Documentation

IEnumerable<string> ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin.GetSupportedQuerystringKeys ( )
inline

If the plugin reads any values from the querystring, the names of the keys should be specified here. This information is required so that the HttpModule knows when to handle an image request.

Returns

Implements ImageResizer.Plugins.IQuerystringPlugin.

Definition at line 201 of file SeamCarvingPlugin.cs.

201  {
202  return new string[] { "carve" };
203  }
override RequestedAction ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin.LayoutImage ( ImageState  s)
inlineprotectedvirtual

Process.3(Layout).2: Rings 'image' and 'imageArea' are added to the layout.

Parameters
s

Reimplemented from ImageResizer.Resizing.AbstractImageProcessor.

Definition at line 45 of file SeamCarvingPlugin.cs.

45  {
46  if (s.sourceBitmap == null) return RequestedAction.None;
47 
48 
49  //Parse carve data bitmap
50  if (!string.IsNullOrEmpty(s.settings["carve.data"])) {
51  string[] parts = s.settings["carve.data"].Split('|');
52  //Parse block count and string
53  var block_count = int.Parse(parts[0]);
54  var carveString = new LzwDecoder("012").Decode(PathUtils.FromBase64UToBytes(parts[1]));
55 
56  float block_size = (int)Math.Floor(Math.Sqrt(s.originalSize.Width * s.originalSize.Height / (double)block_count));
57 
58  var carveData = new CarveDataPlotter() {
59  BlockCount=block_count,
60  Stride = (int)Math.Ceiling((float)s.originalSize.Width / block_size),
61  Rows = (int)Math.Ceiling((float)s.originalSize.Height / block_size)
62  };
63 
64  carveData.Init(carveString);
65 
66  Size remove = carveData.GetRemovalSpace(s.originalSize.Width,s.originalSize.Height,(int)block_size);
67 
68  if (remove.Width / s.originalSize.Width > remove.Height / s.originalSize.Height) {
69  s.originalSize = new Size(s.originalSize.Width - remove.Width, s.originalSize.Height);
70  } else {
71  s.originalSize = new Size(s.originalSize.Width, s.originalSize.Height - remove.Height);
72  }
73 
74  //Save later
75  s.Data[CarveData] = carveData;
76  }
77 
78 
79 
80  return RequestedAction.None;
81 
82  }
ResizeSettings settings
The commands to apply to the bitmap
Definition: ImageState.cs:25
Bitmap sourceBitmap
The source bitmap. If null, skip drawing commands, but continue layout logic.
Definition: ImageState.cs:66
Size originalSize
The original size of the source bitmap. Use this instead of accessing the bitmap directly for this in...
Definition: ImageState.cs:30
override RequestedAction ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin.PostLayoutImage ( ImageState  s)
inlineprotectedvirtual

Process.3(Layout).3: Add rings here to insert them between the image area and the padding

Parameters
s

Reimplemented from ImageResizer.Resizing.AbstractImageProcessor.

Definition at line 85 of file SeamCarvingPlugin.cs.

85  {
86 
87  return RequestedAction.None;
88  }
override RequestedAction ImageResizer.Plugins.SeamCarving.SeamCarvingPlugin.PreRenderImage ( ImageState  s)
inlineprotectedvirtual

Process.5(Render).9: Plugins have a chance to pre-process the source image before it gets rendered, and save it to s.preRenderBitmap

Parameters
s

Reimplemented from ImageResizer.Resizing.AbstractImageProcessor.

Definition at line 92 of file SeamCarvingPlugin.cs.

92  {
93  //Skip this when we are doing simulations
94  if (s.destGraphics == null) return RequestedAction.None;
95 
96  s.ApplyCropping();
97  s.EnsurePreRenderBitmap();
98 
99  //Parse carve algorithm kind
100  FilterType ftype = s.settings.Get<FilterType>("carve", FilterType.None);
101  if ("true".Equals(s.settings["carve"], StringComparison.OrdinalIgnoreCase)) ftype = FilterType.Prewitt;
102  if (string.IsNullOrEmpty(s.settings["carve"]) && s.settings.Mode == FitMode.Carve) ftype = FilterType.Prewitt;
103 
104  //If we have carve data
105  CarveDataPlotter carveData = s.Data.ContainsKey(CarveData) ? (s.Data[CarveData] as CarveDataPlotter) : null;
106  if (carveData != null && ftype == FilterType.None) ftype = FilterType.Prewitt;
107 
108  RectangleF copyRect = s.copyRect;
109 
110  if (carveData != null) copyRect = new RectangleF(new PointF(0, 0), s.sourceBitmap.Size);
111 
112  if (ftype == FilterType.None) return RequestedAction.None; //Only override rendering when carving is requested.
113 
114  //The minimum dimensions of the temporary bitmap.
115  SizeF targetSize = PolygonMath.getParallelogramSize(s.layout["image"]);
116  targetSize = new SizeF((float)Math.Ceiling(targetSize.Width), (float)Math.Ceiling(targetSize.Height));
117 
118 
119  //The size of the temporary bitmap.
120  //We want it larger than the size we'll use on the final copy, so we never upscale it
121  //- but we also want it as small as possible so processing is fast.
122  SizeF tempSize = PolygonMath.ScaleOutside(targetSize, copyRect.Size);
123  int tempWidth = (int)Math.Ceiling(tempSize.Width);
124  int tempHeight = (int)Math.Ceiling(tempSize.Height);
125 
126  //The intermediate and seam carved files
127  string tempFile = Path.GetTempFileName();
128  string outputTempFile = Path.GetTempFileName();
129 
130  try {
131  try {
132 
133  //Create a temporary bitmap that is 'halfway resized', so we can efficiently perfom seam carving.
134 
135  //Unless it's already been done for us by FreeImageResize or something
136  if (s.preRenderBitmap != null && (tempWidth - s.preRenderBitmap.Width < 50 && tempHeight - s.preRenderBitmap.Height < 50)) {
137  s.preRenderBitmap.Save(tempFile, ImageFormat.Bmp);
138  tempWidth = s.preRenderBitmap.Width;
139  tempHeight = s.preRenderBitmap.Height;
140  } else {
141  //Create the temporary bitmap and graphics.
142  using (Bitmap temp = new Bitmap(tempWidth, tempHeight, PixelFormat.Format32bppArgb))
143  using (Graphics g = Graphics.FromImage(temp))
144  using (ImageAttributes ia = new ImageAttributes()) {
145  //High quality everthing
146  g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
147  g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
148  g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
149  g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
150  g.CompositingMode = CompositingMode.SourceOver;
151  ia.SetWrapMode(WrapMode.TileFlipXY);
152  if (s.preRenderBitmap != null) {
153  g.DrawImage(s.preRenderBitmap, new Rectangle(0, 0, tempWidth, tempHeight), 0, 0, s.preRenderBitmap.Width, s.preRenderBitmap.Height, GraphicsUnit.Pixel, ia);
154  } else {
155  g.DrawImage(s.sourceBitmap, new Rectangle(0, 0, tempWidth, tempHeight), copyRect.X, copyRect.Y, copyRect.Width, copyRect.Height, GraphicsUnit.Pixel, ia);
156  }
157  g.Flush(FlushIntention.Flush);
158  //Save
159  temp.Save(tempFile, ImageFormat.Bmp);
160  }
161  }
162 
163  string maskFile = carveData != null ? Path.GetTempFileName() : null;
164  try {
165  if (carveData != null)
166  carveData.SaveBitmapAs(maskFile, tempWidth, tempHeight);
167 
168  Size intTargetSize = new Size((int)targetSize.Width, (int)targetSize.Height);
169  CairJob job = new CairJob();
170  if (maskFile != null) job.WeightPath = maskFile;
171  job.SourcePath = tempFile;
172  job.DestPath = outputTempFile;
173  job.Size = intTargetSize;
174  job.Filter = ftype;
175  job.Timeout = 5000;
176  cair.CairyIt(job);
177  } finally {
178  if (maskFile != null) File.Delete(maskFile);
179  }
180 
181  } finally {
182  File.Delete(tempFile);
183  }
184 
185  //Dispose old intermediate bitmap first
186  if (s.preRenderBitmap != null) s.preRenderBitmap.Dispose();
187 
188  //Load the new intermediate file from disk
189  s.preRenderBitmap = new Bitmap(outputTempFile);
190  s.preRenderBitmap.MakeTransparent();
191 
192  } finally {
193  File.Delete(outputTempFile);
194  }
195 
196  return RequestedAction.Cancel;
197  }
Graphics destGraphics
A graphics object to write to the destination bitmap. If null, skip drawing commands, but continue layout logic.
Definition: ImageState.cs:182
Dictionary< string, object > Data
Allows extensions to store data along with the image state
Definition: ImageState.cs:192
ResizeSettings settings
The commands to apply to the bitmap
Definition: ImageState.cs:25
Bitmap sourceBitmap
The source bitmap. If null, skip drawing commands, but continue layout logic.
Definition: ImageState.cs:66
Bitmap preRenderBitmap
An optional intermediate bitmap, created by plugins who need to process the source bitmap it gets ren...
Definition: ImageState.cs:71

The documentation for this class was generated from the following file: