internal static extern CodecError EncodeFile( IntPtr inData, int width, int height, int stride, int channelCount, EncodeParams parameters, IOCallbacks callbacks);
public static extern WebPEncodingError WebPSave( WebPWriteImage writeImageCallback, IntPtr scan0, int width, int height, int stride, EncodeParams parameters, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MetadataCustomMarshaler))] MetadataParams metadata, WebPReportProgress callback);
private void Encode(EncodeParams encode, IProgressReport progress) { var process = new Process { StartInfo = { FileName = At9ToolPath, Arguments = encode.BuildCommand(InFile, OutDir), UseShellExecute = false, RedirectStandardOutput = true } }; process.Start(); process.WaitForExit(); progress.ReportAdd(1); }
/// <summary> /// The WebP save function. /// </summary> /// <param name="writeImageCallback">The callback used to write the WebP image.</param> /// <param name="input">The input surface.</param> /// <param name="parameters">The encode parameters.</param> /// <param name="metadata">The image metadata.</param> /// <param name="callback">The progress callback.</param> /// <exception cref="ArgumentNullException"><paramref name="writeImageCallback"/> is null. /// or /// <paramref name="input"/> is null.</exception> /// <exception cref="OutOfMemoryException">Insufficient memory to save the image.</exception> /// <exception cref="WebPException">The encoder returned a non-memory related error.</exception> internal static void WebPSave( Surface input, Stream output, EncodeParams parameters, MetadataParams metadata, WebPReportProgress callback) { if (input == null) { throw new ArgumentNullException(nameof(input)); } StreamIOHandler handler = new StreamIOHandler(output); WebPWriteImage writeImageCallback = handler.WriteImageCallback; WebPEncodingError retVal = WebPEncodingError.Ok; if (IntPtr.Size == 8) { retVal = WebP_64.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback); } else { retVal = WebP_32.WebPSave(writeImageCallback, input.Scan0.Pointer, input.Width, input.Height, input.Stride, parameters, metadata, callback); } GC.KeepAlive(writeImageCallback); if (retVal != WebPEncodingError.Ok) { switch (retVal) { case WebPEncodingError.OutOfMemory: case WebPEncodingError.BitStreamOutOfMemory: throw new OutOfMemoryException(Resources.InsufficientMemoryOnSave); case WebPEncodingError.FileTooBig: throw new WebPException(Resources.EncoderFileTooBig); case WebPEncodingError.ApiVersionMismatch: throw new WebPException(Resources.ApiVersionMismatch); case WebPEncodingError.MetadataEncoding: throw new WebPException(Resources.EncoderMetadataError); case WebPEncodingError.UserAbort: throw new OperationCanceledException(); case WebPEncodingError.BadDimension: throw new WebPException(Resources.InvalidImageDimensions); case WebPEncodingError.NullParameter: throw new WebPException(Resources.EncoderNullParameter); case WebPEncodingError.InvalidConfiguration: throw new WebPException(Resources.EncoderInvalidConfiguration); case WebPEncodingError.PartitionZeroOverflow: throw new WebPException(Resources.EncoderPartitionZeroOverflow); case WebPEncodingError.PartitionOverflow: throw new WebPException(Resources.EncoderPartitionOverflow); case WebPEncodingError.BadWrite: if (handler.WriteException != null) { throw new IOException(Resources.EncoderBadWrite, handler.WriteException); } else { throw new IOException(Resources.EncoderBadWrite); } default: throw new WebPException(Resources.EncoderGenericError); } } }
public static void EncodeFile(IntPtr inData, int width, int height, int stride, int channelCount, EncodeParams parameters, Stream output) { StreamIOCallbacks streamCallbacks = new StreamIOCallbacks(output); IOCallbacks callbacks = new IOCallbacks() { Read = new ReadDelegate(streamCallbacks.Read), Write = new WriteDelegate(streamCallbacks.Write), Seek = new SeekDelegate(streamCallbacks.Seek) }; CodecError result; if (IntPtr.Size == 8) { result = IO_x64.EncodeFile(inData, width, height, stride, channelCount, parameters, callbacks); } else { result = IO_x86.EncodeFile(inData, width, height, stride, channelCount, parameters, callbacks); } GC.KeepAlive(callbacks); GC.KeepAlive(streamCallbacks); if (result != CodecError.Ok) { string message = string.Empty; switch (result) { case CodecError.InitFailed: message = Resources.InitFailed; break; case CodecError.OutOfMemory: throw new OutOfMemoryException(); case CodecError.EncodeFailure: message = Resources.EncodeFailure; break; case CodecError.ImageBufferWrite: message = Resources.ImageBufferWrite; break; } throw new FormatException(message); } }
/// <summary> Do "smart" encodging on a string. This means that valid HTML entities and tags, /// Helma macros and HTML comments are passed through unescaped. /// </summary> public static void Encode(string str, StringBuilder ret, ArrayList allowedTags) { if (string.IsNullOrEmpty(str)) { return; } var strLength = str.Length; var openTags = new Stack(); EncodeParams.Init(); for (var charIndex = 0; charIndex < strLength; charIndex++) { var character = str[charIndex]; if (character == LesserThan) { if (ProcessLesserThan(str, ret, allowedTags, openTags, ref charIndex)) { continue; } } if ((EncodeParams.Linebreaks > 0 || EncodeParams.SwallowLinebreaks > 0) && !char.IsWhiteSpace(character)) { if (!EncodeParams.InsidePreTag && EncodeParams.Linebreaks > EncodeParams.SwallowLinebreaks) { EncodeParams.Linebreaks -= EncodeParams.SwallowLinebreaks; for (var lineBreakIndex = 0; lineBreakIndex < EncodeParams.Linebreaks; lineBreakIndex++) { ret.Append(BreakTagNewLine); } } if (!EncodeParams.InsideTag) { EncodeParams.SwallowLinebreaks = 0; } EncodeParams.Linebreaks = 0; } ProcessTags(str, ret, character, charIndex, openTags); } // if tags were opened but not closed, close them. for (var tagIndex = 0; tagIndex < openTags.Count; tagIndex++) { var tag = openTags.Pop(); if (!emptyTags.Contains(tag)) { ret.AppendFormat("{0}tag{1}", PrefixCloseTag, SuffixTagEnd); } } // add remaining newlines we may have collected if (EncodeParams.Linebreaks > 0 && EncodeParams.Linebreaks > EncodeParams.SwallowLinebreaks) { EncodeParams.Linebreaks -= EncodeParams.SwallowLinebreaks; for (var lineBreakIndex = 0; lineBreakIndex < EncodeParams.Linebreaks; lineBreakIndex++) { ret.Append(BreakTagNewLine); } } }