コード例 #1
0
ファイル: WicBuilder.cs プロジェクト: timgaunt/resizer
        protected virtual RequestedAction Encode(IWICComponentFactory factory, IWICBitmapSource data, Size imageSize, ImageJob job)
        {
            WicEncoderPlugin encoder = new WicEncoderPlugin(job.Settings, job.SourcePathData);

            //Create the IStream/MemoryStream
            var outputStream = new MemoryIStream();

            job.ResultInfo["final.width"]  = imageSize.Width;
            job.ResultInfo["final.height"] = imageSize.Height;

            encoder.EncodeToStream(factory, data, imageSize, outputStream);

            object dest = job.Dest;

            // Try to save the bitmap
            if (dest is string)
            {
                //Make physical and resolve variable references all at the same time.
                job.FinalPath = job.ResolveTemplatedPath(job.Dest as string,
                                                         delegate(string var) {
                    if ("ext".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(encoder.Extension);
                    }
                    if ("width".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(imageSize.Width.ToString());
                    }
                    if ("height".Equals(var, StringComparison.OrdinalIgnoreCase))
                    {
                        return(imageSize.Height.ToString());
                    }
                    return(null);
                });
                //If requested, auto-create the parent directory(ies)
                if (job.CreateParentDirectory)
                {
                    string dirName = Path.GetDirectoryName(job.FinalPath);
                    if (!Directory.Exists(dirName))
                    {
                        Directory.CreateDirectory(dirName);
                    }
                }

                using (FileStream fs = new FileStream(job.FinalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) {
                    outputStream.WriteTo(fs);
                }
            }
            else if (dest is Stream)
            {
                outputStream.WriteTo((Stream)dest);
            }
            else
            {
                return(RequestedAction.None);
            }

            return(RequestedAction.Cancel);
        }
コード例 #2
0
        /// <summary>
        /// Adds alternate pipeline based on FreeImage. Invoked by &builder=freeimage. 
        /// This method doesn't handle job.DisposeSource or job.DesposeDest or settings filtering, that's handled by ImageBuilder.
        /// All the bitmap processing is handled by buildFiBitmap, this method handles all the I/O
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        protected override RequestedAction BuildJob(ImageJob job)
        {
            if (!"freeimage".Equals(job.Settings["builder"])) return RequestedAction.None;
            if (!FreeImageAPI.FreeImage.IsAvailable()) return RequestedAction.None;

            //StringBuilder log = new StringBuilder();

            //FreeImageAPI.FreeImageEngine.Message += (delegate(FREE_IMAGE_FORMAT fmt, string msg) {
            //    log.AppendLine(msg);
            //});

            // Variables
            Stream s = null;
            bool disposeStream = !(job.Source is Stream);
            long originalPosition = 0;
            bool restoreStreamPosition = false;

            //Get a Stream instance for the job
            string path;
            s = c.CurrentImageBuilder.GetStreamFromSource(job.Source, job.Settings, ref disposeStream, out path, out restoreStreamPosition);
            if (s == null) return RequestedAction.None;
            if (job.ResetSourceStream) restoreStreamPosition = true;
            job.SourcePathData = path;

            //Save the original stream positione
            originalPosition = (restoreStreamPosition) ? s.Position : -1;
            try {
                //What is our destination format
                IEncoder managedEncoder = c.Plugins.GetEncoder(job.Settings, job.SourcePathData); //Use the existing pipeline to parse the querystring
                //FREE_IMAGE_FORMAT destFormat = FreeImage.GetFIFFromMime(managedEncoder.MimeType); //Use the resulting mime-type to determine the output format.
                //This prevents us from supporting output formats that don't already have registered encoders. Good, right?

                bool supportsTransparency = managedEncoder.SupportsTransparency;

                return (RequestedAction)FreeImageDecoder.FreeImageDecoderPlugin.DecodeAndCall(s, job.Settings, delegate(ref FIBITMAP original, bool mayUnloadOriginal) {
                    FIBITMAP b = FIBITMAP.Zero;
                    try {
                        //Do all the bitmap stuff in another method
                        b = buildFiBitmap(ref original, job, supportsTransparency, mayUnloadOriginal);
                        if (b.IsNull) return RequestedAction.None;

                        // Try to save the bitmap
                        if (job.Dest is string || job.Dest is Stream) {
                            FreeImageEncoderPlugin e = new FreeImageEncoderPlugin(job.Settings, path);
                            if (job.Dest is string) {
                                //Make physical and resolve variable references all at the same time.
                                job.FinalPath = job.ResolveTemplatedPath(job.Dest as string,
                                    delegate(string var) {
                                        if ("width".Equals(var, StringComparison.OrdinalIgnoreCase)) return FreeImage.GetWidth(b).ToString();
                                        if ("height".Equals(var, StringComparison.OrdinalIgnoreCase)) return FreeImage.GetHeight(b).ToString();
                                        if ("ext".Equals(var, StringComparison.OrdinalIgnoreCase)) return e.Extension;
                                        return null;
                                    });
                                //If requested, auto-create the parent directory(ies)
                                if (job.CreateParentDirectory) {
                                    string dirName = Path.GetDirectoryName(job.FinalPath);
                                    if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName);
                                }
                                if (!FreeImage.Save(e.Format, b, job.FinalPath, e.EncodingOptions)) return RequestedAction.None;
                            } else if (job.Dest is Stream) {
                                if (!FreeImage.SaveToStream(b, (Stream)job.Dest, e.Format, e.EncodingOptions)) return RequestedAction.None;
                            }
                        } else if (job.Dest == typeof(Bitmap)) {
                            job.Result = FreeImage.GetBitmap(b);
                        } else return RequestedAction.None;
                        return RequestedAction.Cancel;
                    } finally {
                        if (!b.IsNull && b != original) FreeImage.UnloadEx(ref b);
                    }

                });
            } finally {
                if (s != null && restoreStreamPosition && s.CanSeek) s.Seek(originalPosition, SeekOrigin.Begin);
                if (disposeStream) s.Dispose();
            }
        }
コード例 #3
0
ファイル: WicBuilder.cs プロジェクト: hahmed/resizer
        protected virtual RequestedAction Encode(IWICComponentFactory factory, IWICBitmapSource data, Size imageSize, ImageJob job)
        {
            WicEncoderPlugin encoder = new WicEncoderPlugin(job.Settings, job.SourcePathData);

            //Create the IStream/MemoryStream
            var outputStream = new MemoryIStream();

            encoder.EncodeToStream(factory, data, imageSize, outputStream);

            object dest = job.Dest;
            // Try to save the bitmap
            if (dest is string) {
                //Make physical and resolve variable references all at the same time.
                job.FinalPath = job.ResolveTemplatedPath(job.Dest as string,
                    delegate(string var) {
                        if ("ext".Equals(var, StringComparison.OrdinalIgnoreCase)) return encoder.Extension;
                        if ("width".Equals(var, StringComparison.OrdinalIgnoreCase)) return imageSize.Width.ToString();
                        if ("height".Equals(var, StringComparison.OrdinalIgnoreCase)) return imageSize.Height.ToString();
                        return null;
                    });
                //If requested, auto-create the parent directory(ies)
                if (job.CreateParentDirectory) {
                    string dirName = Path.GetDirectoryName(job.FinalPath);
                    if (!Directory.Exists(dirName)) Directory.CreateDirectory(dirName);
                }

                using (FileStream fs = new FileStream(job.FinalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) {
                    outputStream.WriteTo(fs);
                }
            } else if (dest is Stream) {
                outputStream.WriteTo((Stream)dest);
            } else return RequestedAction.None;

            return RequestedAction.Cancel;
        }
コード例 #4
0
ファイル: FreeImageBuilder.cs プロジェクト: timgaunt/resizer
        /// <summary>
        /// Adds alternate pipeline based on FreeImage. Invoked by &amp;builder=freeimage.
        /// This method doesn't handle job.DisposeSource or job.DesposeDest or settings filtering, that's handled by ImageBuilder.
        /// All the bitmap processing is handled by buildFiBitmap, this method handles all the I/O
        /// </summary>
        /// <param name="job"></param>
        /// <returns></returns>
        protected override RequestedAction BuildJob(ImageJob job)
        {
            if (!"freeimage".Equals(job.Settings["builder"]))
            {
                return(RequestedAction.None);
            }
            if (!FreeImageAPI.FreeImage.IsAvailable())
            {
                return(RequestedAction.None);
            }

            //StringBuilder log = new StringBuilder();

            //FreeImageAPI.FreeImageEngine.Message += (delegate(FREE_IMAGE_FORMAT fmt, string msg) {
            //    log.AppendLine(msg);
            //});

            // Variables
            Stream s                     = null;
            bool   disposeStream         = !(job.Source is Stream);
            long   originalPosition      = 0;
            bool   restoreStreamPosition = false;

            //Get a Stream instance for the job
            string path;

            s = c.CurrentImageBuilder.GetStreamFromSource(job.Source, job.Settings, ref disposeStream, out path, out restoreStreamPosition);
            if (s == null)
            {
                return(RequestedAction.None);
            }
            if (job.ResetSourceStream)
            {
                restoreStreamPosition = true;
            }
            job.SourcePathData = path;

            //Save the original stream position
            originalPosition = (restoreStreamPosition) ? s.Position : -1;
            try {
                //What is our destination format
                IEncoder managedEncoder = c.Plugins.GetEncoder(job.Settings, job.SourcePathData); //Use the existing pipeline to parse the querystring
                //FREE_IMAGE_FORMAT destFormat = FreeImage.GetFIFFromMime(managedEncoder.MimeType); //Use the resulting mime-type to determine the output format.
                //This prevents us from supporting output formats that don't already have registered encoders. Good, right?

                bool supportsTransparency = managedEncoder.SupportsTransparency;

                var decodeTime = Stopwatch.StartNew();
                return((RequestedAction)FreeImageDecoder.FreeImageDecoderPlugin.DecodeAndCall(s, job.Settings, delegate(ref FIBITMAP original, bool mayUnloadOriginal) {
                    decodeTime.Stop();
                    job.DecodeTicks = decodeTime.ElapsedTicks;

                    if (!original.IsNull)
                    {
                        job.ResultInfo["source.width"] = (int)FreeImage.GetWidth(original);
                        job.ResultInfo["source.height"] = (int)FreeImage.GetHeight(original);
                    }

                    FIBITMAP b = FIBITMAP.Zero;
                    try {
                        //Do all the bitmap stuff in another method
                        b = buildFiBitmap(ref original, job, supportsTransparency, mayUnloadOriginal);
                        if (b.IsNull)
                        {
                            return RequestedAction.None;
                        }

                        // Try to save the bitmap
                        if (job.Dest is string || job.Dest is Stream)
                        {
                            FreeImageEncoderPlugin e = new FreeImageEncoderPlugin(job.Settings, path);
                            if (job.Dest is string)
                            {
                                //Make physical and resolve variable references all at the same time.
                                job.FinalPath = job.ResolveTemplatedPath(job.Dest as string,
                                                                         delegate(string var) {
                                    if ("width".Equals(var, StringComparison.OrdinalIgnoreCase))
                                    {
                                        return FreeImage.GetWidth(b).ToString();
                                    }
                                    if ("height".Equals(var, StringComparison.OrdinalIgnoreCase))
                                    {
                                        return FreeImage.GetHeight(b).ToString();
                                    }
                                    if ("ext".Equals(var, StringComparison.OrdinalIgnoreCase))
                                    {
                                        return e.Extension;
                                    }
                                    return null;
                                });
                                //If requested, auto-create the parent directory(ies)
                                if (job.CreateParentDirectory)
                                {
                                    string dirName = Path.GetDirectoryName(job.FinalPath);
                                    if (!Directory.Exists(dirName))
                                    {
                                        Directory.CreateDirectory(dirName);
                                    }
                                }
                                var encodeTime = Stopwatch.StartNew();
                                if (!FreeImage.Save(e.Format, b, job.FinalPath, e.EncodingOptions))
                                {
                                    return RequestedAction.None;
                                }
                                encodeTime.Stop();
                                job.EncodeTicks = encodeTime.ElapsedTicks;
                            }
                            else if (job.Dest is Stream)
                            {
                                var encodeTime = Stopwatch.StartNew();
                                if (!FreeImage.SaveToStream(b, (Stream)job.Dest, e.Format, e.EncodingOptions))
                                {
                                    return RequestedAction.None;
                                }
                                encodeTime.Stop();
                                job.EncodeTicks = encodeTime.ElapsedTicks;
                            }
                        }
                        else if (job.Dest == typeof(Bitmap))
                        {
                            job.Result = FreeImage.GetBitmap(b);
                        }
                        else
                        {
                            return RequestedAction.None;
                        }
                        return RequestedAction.Cancel;
                    } finally {
                        if (!b.IsNull && b != original)
                        {
                            FreeImage.UnloadEx(ref b);
                        }
                    }
                }));
            } finally {
                if (s != null && restoreStreamPosition && s.CanSeek)
                {
                    s.Seek(originalPosition, SeekOrigin.Begin);
                }
                if (disposeStream)
                {
                    s.Dispose();
                }
            }
        }