/// <summary> /// Writes the indexed PNG or GIF file /// </summary> /// <param name="i"></param> /// <param name="s"></param> public void Write(Image i, System.IO.Stream s) { bool useMax = (Colors < 0); byte colors = (byte)Math.Min(Math.Max(this.Colors, 1), 255); if (ImageFormat.Png.Equals(OutputFormat)) { if (useMax) { DefaultEncoder.SavePng(i, s); } else { SaveIndexed(ImageFormat.Png, i, s, colors, Dither || FourPassDither, FourPassDither, DitherPercent); } } else if (ImageFormat.Gif.Equals(OutputFormat)) { if (useMax) { SaveIndexed(ImageFormat.Gif, i, s, 255, Dither || FourPassDither, FourPassDither, DitherPercent); } else { SaveIndexed(ImageFormat.Gif, i, s, colors, Dither || FourPassDither, FourPassDither, DitherPercent); } } }
public void when_encoding_an_object_with_custom_naming_of_property_it_should_decode_taking_into_account_custom_property_name() { var CombinedResolverStrategy = new CombinedResolverStrategy( new JsonResolverStrategy(), new DataContractResolverStrategy(), new XmlResolverStrategy(), new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.PascalCase), new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.CamelCase), new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.Lowercase, "-"), new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.Uppercase, "_")); IEnumerable <IDataWriter> writers = new List <IDataWriter> { new JsonWriter(new DataWriterSettings(CombinedResolverStrategy), HttpContentTypes.ApplicationJson) }; var encoder = new DefaultEncoder(new RegExBasedDataWriterProvider(writers)); var customObject = new CustomNamedObject { UpperPropertyName = "someValue" }; var encoded = encoder.Encode(customObject, HttpContentTypes.ApplicationJson); var str = System.Text.Encoding.UTF8.GetString(encoded); StringAssert.Contains("upperPropertyName", str); }
public void TestMultiValuedSortByScore() { Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))); Document doc = new Document(); FieldType type = new FieldType(TextField.TYPE_STORED); type.StoreTermVectorOffsets = (true); type.StoreTermVectorPositions = (true); type.StoreTermVectors = (true); type.Freeze(); doc.Add(new Field("field", "zero if naught", type)); // The first two fields contain the best match doc.Add(new Field("field", "hero of legend", type)); // but total a lower score (3) than the bottom doc.Add(new Field("field", "naught of hero", type)); // two fields (4) doc.Add(new Field("field", "naught of hero", type)); writer.AddDocument(doc); FastVectorHighlighter highlighter = new FastVectorHighlighter(); ScoreOrderFragmentsBuilder fragmentsBuilder = new ScoreOrderFragmentsBuilder(); fragmentsBuilder.IsDiscreteMultiValueHighlighting = (true); IndexReader reader = DirectoryReader.Open(writer, true); String[] preTags = new String[] { "<b>" }; String[] postTags = new String[] { "</b>" }; IEncoder encoder = new DefaultEncoder(); int docId = 0; BooleanQuery query = new BooleanQuery(); query.Add(clause("field", "hero"), Occur.SHOULD); query.Add(clause("field", "of"), Occur.SHOULD); query.Add(clause("field", "legend"), Occur.SHOULD); FieldQuery fieldQuery = highlighter.GetFieldQuery(query, reader); foreach (IFragListBuilder fragListBuilder in new IFragListBuilder[] { new SimpleFragListBuilder(), new WeightedFragListBuilder() }) { String[] bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 20, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 28, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 30000, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); } reader.Dispose(); writer.Dispose(); dir.Dispose(); }
public IEncoder CreateIfSuitable(ResizeSettings settings, object original) { ImageFormat requestedFormat = DefaultEncoder.GetRequestedFormat(settings.Format, ImageFormat.Jpeg); if (requestedFormat == null || !IsValidOutputFormat(requestedFormat)) { return(null); //An unsupported format was explicitly specified. } if (!"wic".Equals(settings["encoder"], StringComparison.OrdinalIgnoreCase)) { return(null); } return(new WicEncoderPlugin(settings, original)); }
public void when_encoding_an_object_that_contains_an_enum() { IEnumerable <IDataWriter> writers = new List <IDataWriter> { new JsonWriter(new DataWriterSettings(), "application/.*json") }; var _encoder = new DefaultEncoder(new RegExBasedDataWriterProvider(writers)); var data = new Foo { Baz = Bar.First }; var result = _encoder.Encode(data, "application/vnd.fubar+json"); Assert.Greater(result.Length, 0); }
public ImageFormat GetFormatIfSuitable(ResizeSettings settings, object original) { //What format was the image originally (used as a fallback). ImageFormat originalFormat = DefaultEncoder.GetOriginalFormat(original); if (!IsValidOutputFormat(originalFormat)) { originalFormat = null; //No valid info available about the original format. } //What format was specified? ImageFormat requestedFormat = DefaultEncoder.GetRequestedFormat(settings.Format, originalFormat); //fallback to originalFormat if not specified. if (IsValidOutputFormat(requestedFormat)) { return(requestedFormat); } return(null); }
public void EstimateResponseInfo() { IEncoder guessedEncoder = null; //Only use an encoder to determine extension/mime-type when it's an image extension or when we've set process = always. if (ProcessingIndicated) { guessedEncoder = conf.GetImageBuilder().EncoderProvider.GetEncoder(new ResizeSettings(this.RewrittenInstructions), this.RewrittenVirtualPath); if (guessedEncoder == null) { throw new ImageProcessingException("Image Resizer: No image encoder was found for the request."); } } //Determine the file extension for the caching system to use if we aren't processing the image //Use the existing one if is an image extension. If not, use "unknown". // We don't want to suggest writing .exe or .aspx files to the cache! string fallbackExtension = PathUtils.GetFullExtension(RewrittenVirtualPath).TrimStart('.'); if (!conf.IsAcceptedImageType(RewrittenVirtualPath)) { fallbackExtension = "unknown"; } //Determine the mime-type if we aren't processing the image. string fallbackContentType = "application/octet-stream"; //Support JPEG, PNG, GIF, BMP, TIFF mime-types. Otherwise use "application/octet-stream". //We can't set it to null - it will default to text/html System.Drawing.Imaging.ImageFormat recognizedExtension = DefaultEncoder.GetImageFormatFromExtension(fallbackExtension); if (recognizedExtension != null) { fallbackContentType = DefaultEncoder.GetContentTypeFromImageFormat(recognizedExtension); } EstimatedContentType = ProcessingIndicated ? guessedEncoder.MimeType : fallbackContentType; EstimatedFileExtension = ProcessingIndicated ? guessedEncoder.Extension : fallbackExtension; Performance.GlobalPerf.Singleton.IncrementCounter("module_response_ext_" + EstimatedFileExtension); }
protected string Highlight(LuceneQuery query, string text, bool htmlEncodeOutput) { if (string.IsNullOrEmpty(text)) { return(string.Empty); } try { Encoder encoder; if (htmlEncodeOutput) { encoder = new SimpleHTMLEncoder(); } else { encoder = new DefaultEncoder(); } if (query == null) { return(encoder.encodeText(text)); // nothing to highlight } // Build the highlighter. var formatter = new SimpleHTMLFormatter(_configuration.StartTag, _configuration.EndTag); var highlighter = new LuceneHighlighter(formatter, encoder, new QueryScorer(query)); highlighter.setTextFragmenter(new NullFragmenter()); // Perform highlighting. var highlightedHtml = highlighter.getBestFragment(_contentAnalyzer, string.Empty, text); return(highlightedHtml ?? encoder.encodeText(text)); } catch (Exception) { // on error just return the original string return(text); } }
protected string Summarize(LuceneQuery query, string text, bool htmlEncodeOutput) { if (query == null || string.IsNullOrEmpty(text)) { return(null); } try { // Build the highlighter. var formatter = new SimpleHTMLFormatter(_configuration.StartTag, _configuration.EndTag); var scorer = new QueryScorer(query); Encoder encoder; if (htmlEncodeOutput) { encoder = new SimpleHTMLEncoder(); } else { encoder = new DefaultEncoder(); } var highlighter = new LuceneHighlighter(formatter, encoder, scorer); highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, _configuration.FragmentSize)); // Perform highlighting. var tokenStream = _contentAnalyzer.tokenStream(string.Empty, new java.io.StringReader(text)); return(highlighter.getBestFragments(tokenStream, text, _configuration.MaxFragments, _configuration.Separator)); } catch (Exception) { // on error just return the original string return(text); } }
private void matchedFieldsTestCase(bool useMatchedFields, bool fieldMatch, String fieldValue, String expected, params Query[] queryClauses) { Document doc = new Document(); FieldType stored = new FieldType(TextField.TYPE_STORED); stored.StoreTermVectorOffsets = (true); stored.StoreTermVectorPositions = (true); stored.StoreTermVectors = (true); stored.Freeze(); FieldType matched = new FieldType(TextField.TYPE_NOT_STORED); matched.StoreTermVectorOffsets = (true); matched.StoreTermVectorPositions = (true); matched.StoreTermVectors = (true); matched.Freeze(); doc.Add(new Field("field", fieldValue, stored)); // Whitespace tokenized with English stop words doc.Add(new Field("field_exact", fieldValue, matched)); // Whitespace tokenized without stop words doc.Add(new Field("field_super_exact", fieldValue, matched)); // Whitespace tokenized without toLower doc.Add(new Field("field_characters", fieldValue, matched)); // Each letter is a token doc.Add(new Field("field_tripples", fieldValue, matched)); // Every three letters is a token doc.Add(new Field("field_sliced", fieldValue.Substring(0, // Sliced at 10 chars then analyzed just like field Math.Min(fieldValue.Length - 1, 10) - 0), matched)); doc.Add(new Field("field_der_red", new CannedTokenStream( // Hacky field containing "der" and "red" at pos = 0 token("der", 1, 0, 3), token("red", 0, 0, 3) ), matched)); Analyzer analyzer = new AnalyzerWrapperAnonymousHelper(); Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)); writer.AddDocument(doc); FastVectorHighlighter highlighter = new FastVectorHighlighter(); IFragListBuilder fragListBuilder = new SimpleFragListBuilder(); IFragmentsBuilder fragmentsBuilder = new ScoreOrderFragmentsBuilder(); IndexReader reader = DirectoryReader.Open(writer, true); String[] preTags = new String[] { "<b>" }; String[] postTags = new String[] { "</b>" }; IEncoder encoder = new DefaultEncoder(); int docId = 0; BooleanQuery query = new BooleanQuery(); foreach (Query clause in queryClauses) { query.Add(clause, Occur.MUST); } FieldQuery fieldQuery = new FieldQuery(query, reader, true, fieldMatch); String[] bestFragments; if (useMatchedFields) { ISet <String> matchedFields = new HashSet <String>(); matchedFields.Add("field"); matchedFields.Add("field_exact"); matchedFields.Add("field_super_exact"); matchedFields.Add("field_characters"); matchedFields.Add("field_tripples"); matchedFields.Add("field_sliced"); matchedFields.Add("field_der_red"); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", matchedFields, 25, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); } else { bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 25, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); } assertEquals(expected, bestFragments[0]); reader.Dispose(); writer.Dispose(); dir.Dispose(); }
/// <summary> /// Generates the resized image to disk (if needed), then rewrites the request to that location. /// Perform 404 checking before calling this method. Assumes file exists. /// Called during PostAuthorizeRequest /// </summary> /// <param name="context"></param> /// <param name="virtualPath"></param> /// <param name="queryString"></param> /// <param name="vf"></param> protected virtual void HandleRequest(HttpContext context, string virtualPath, NameValueCollection queryString, IVirtualFile vf) { Stopwatch s = new Stopwatch(); s.Start(); ResizeSettings settings = new ResizeSettings(queryString); bool isCaching = settings.Cache == ServerCacheMode.Always; bool isProcessing = settings.Process == ProcessWhen.Always; //By default, we process it if is both (a) a recognized image extension, and (b) has a resizing directive (not just 'cache'). if (settings.Process == ProcessWhen.Default) { //Check for resize directive by removing ('non-resizing' items from the current querystring) NameValueCollection copy = new NameValueCollection(queryString); copy.Remove("cache"); copy.Remove("process"); copy.Remove("useresizingpipeline"); copy.Remove("404"); copy.Remove("404.filterMode"); copy.Remove("404.except"); //If the 'copy' still has directives, and it's an image request, then let's process it. isProcessing = conf.IsAcceptedImageType(virtualPath) && conf.HasPipelineDirective(copy); } //By default, we only cache it if we're processing it. if (settings.Cache == ServerCacheMode.Default && isProcessing) { isCaching = true; } //Resolve the 'cache' setting to 'no' unless we want it cache. if (!isCaching) { settings.Cache = ServerCacheMode.No; } //If we are neither processing nor caching, don't do anything more with the request if (!isProcessing && !isCaching) { return; } context.Items[conf.ResponseArgsKey] = ""; //We are handling the requests //Communicate to the MVC plugin this request should not be affected by the UrlRoutingModule. context.Items[conf.StopRoutingKey] = true; //Find out if we have a modified date that we can work with bool hasModifiedDate = (vf == null) || vf is IVirtualFileWithModifiedDate; DateTime modDate = DateTime.MinValue; if (hasModifiedDate && vf != null) { modDate = ((IVirtualFileWithModifiedDate)vf).ModifiedDateUTC; if (modDate == DateTime.MinValue || modDate == DateTime.MaxValue) { hasModifiedDate = false; //Skip modified date checking if the file has no modified date } } IEncoder guessedEncoder = null; //Only use an encoder to determine extension/mime-type when it's an image extension or when we've set process = always. if (isProcessing) { guessedEncoder = conf.GetImageBuilder().EncoderProvider.GetEncoder(settings, virtualPath); if (guessedEncoder == null) { throw new ImageProcessingException("Image Resizer: No image encoder was found for the request."); } } //Determine the file extenson for the caching system to use if we aren't processing the image //Use the exsiting one if is an image extension. If not, use "unknown". // We don't want to suggest writing .exe or .aspx files to the cache! string fallbackExtension = PathUtils.GetFullExtension(virtualPath).TrimStart('.'); if (!conf.IsAcceptedImageType(virtualPath)) { fallbackExtension = "unknown"; } //Determine the mime-type if we aren't processing the image. string fallbackContentType = "application/octet-stream"; //Support jpeg, png, gif, bmp, tiff mime-types. Otherwise use "application/octet-stream". //We can't set it to null - it will default to text/html System.Drawing.Imaging.ImageFormat recognizedExtension = DefaultEncoder.GetImageFormatFromExtension(fallbackExtension); if (recognizedExtension != null) { fallbackContentType = DefaultEncoder.GetContentTypeFromImageFormat(recognizedExtension); } //Build CacheEventArgs ResponseArgs e = new ResponseArgs(); e.RequestKey = virtualPath + PathUtils.BuildQueryString(queryString); e.RewrittenQuerystring = settings; e.ResponseHeaders.ContentType = isProcessing ? guessedEncoder.MimeType : fallbackContentType; e.SuggestedExtension = isProcessing ? guessedEncoder.Extension : fallbackExtension; e.HasModifiedDate = hasModifiedDate; //Add delegate for retrieving the modified date of the source file. e.GetModifiedDateUTC = new ModifiedDateDelegate(delegate() { if (vf == null) { return(System.IO.File.GetLastWriteTimeUtc(HostingEnvironment.MapPath(virtualPath))); } else if (hasModifiedDate) { return(modDate); } else { return(DateTime.MinValue); //Won't be called, no modified date available. } }); //A delegate for accessing the source file e.GetSourceImage = new GetSourceImageDelegate(delegate() { return((vf != null) ? vf.Open() : File.Open(HostingEnvironment.MapPath(virtualPath), FileMode.Open, FileAccess.Read, FileShare.Read)); }); //Add delegate for writing the data stream e.ResizeImageToStream = new ResizeImageDelegate(delegate(System.IO.Stream stream) { //This runs on a cache miss or cache invalid. This delegate is preventing from running in more //than one thread at a time for the specified cache key try { if (!isProcessing) { //Just duplicate the data using (Stream source = e.GetSourceImage()) StreamExtensions.CopyToStream(source, stream); //4KiB buffer } else { //Process the image if (vf != null) { conf.GetImageBuilder().Build(vf, stream, settings); } else { conf.GetImageBuilder().Build(HostingEnvironment.MapPath(virtualPath), stream, settings); //Use a physical path to bypass virtual file system } } //Catch not found exceptions } catch (System.IO.FileNotFoundException notFound) { //This will be called later, if at all. FileMissing(context, virtualPath, queryString); throw new ImageMissingException("The specified resource could not be located", "File not found", notFound); } catch (System.IO.DirectoryNotFoundException notFound) { FileMissing(context, virtualPath, queryString); throw new ImageMissingException("The specified resource could not be located", "File not found", notFound); } }); context.Items[conf.ResponseArgsKey] = e; //store in context items //Fire events (for client-side caching plugins) conf.FirePreHandleImage(this, context, e); //Pass the rest of the work off to the caching module. It will handle rewriting/redirecting and everything. //We handle request headers based on what is found in context.Items ICache cache = conf.GetCacheProvider().GetCachingSystem(context, e); //Verify we have a caching system if (cache == null) { throw new ImageProcessingException("Image Resizer: No caching plugin was found for the request"); } cache.Process(context, e); s.Stop(); context.Items["ResizingTime"] = s.ElapsedMilliseconds; }
public FreeImageEncoderPlugin(ResizeSettings settings, object original) { ImageFormat originalFormat = DefaultEncoder.GetOriginalFormat(original); if (!IsValidOutputFormat(originalFormat)) { originalFormat = ImageFormat.Jpeg; //No valid info available about the original format. Use Jpeg. } //What format was specified? ImageFormat requestedFormat = DefaultEncoder.GetRequestedFormat(settings.Format, originalFormat); //fallback to originalFormat if not specified. if (!IsValidOutputFormat(requestedFormat)) { throw new ArgumentException("An unrecognized or unsupported output format (" + (settings.Format != null ? settings.Format : "(null)") + ") was specified in 'settings'."); } this.format = FreeImage.GetFormat(requestedFormat); //Parse JPEG settings. int quality = 90; if (string.IsNullOrEmpty(settings["quality"]) || !int.TryParse(settings["quality"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out quality)) { quality = 90; } if (format == FREE_IMAGE_FORMAT.FIF_JPEG) { if (quality >= 100) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB; } else if (quality >= 75) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYGOOD; } else if (quality >= 50) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL; } else if (quality >= 25) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYAVERAGE; } else { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYBAD; } if ("true".Equals(settings["progressive"])) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_PROGRESSIVE; } if ("411".Equals(settings["subsampling"])) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_411; } if ("420".Equals(settings["subsampling"])) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_420; } if ("422".Equals(settings["subsampling"])) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_422; } if ("444".Equals(settings["subsampling"])) { encodingOptions |= FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444; } } if (string.IsNullOrEmpty(settings["colors"]) || !int.TryParse(settings["colors"], NumberStyles.Number, NumberFormatInfo.InvariantInfo, out colors)) { colors = -1; } if (format == FREE_IMAGE_FORMAT.FIF_GIF) { //encodingOptions = FREE_IMAGE_SAVE_FLAGS. } }
/// <summary> /// Returns an encoder for a given file name. /// </summary> static ILineEncoder GetEncoderForFile(string fileName) { ILineEncoder encoder = null; int extIndex = fileName.LastIndexOf('.'); if (extIndex != -1) { string ext = fileName.Substring(extIndex + 1).ToLowerCultureInvariant(); string highlighterPath = System.Configuration.ConfigurationSettings.AppSettings["encoder_" + ext]; if (highlighterPath != null) { Assembly encoderAssem = Assembly.LoadFrom(highlighterPath); Type encoderType = encoderAssem.GetType("LineEncoderFactory"); ILineEncoderFactory factory = (ILineEncoderFactory)Activator.CreateInstance(encoderType); encoder = factory.GetLineEncoder(ext); } } if (encoder == null) encoder = new DefaultEncoder(); return encoder; }