internal static InputDescriptor FromFile(string fileName) { var result = new InputDescriptor { FailOnError = true, File = fileName, }; return(result); }
internal static InputDescriptor FromBuffer(byte[] buffer) { var result = new InputDescriptor { Buffer = buffer, FailOnError = true }; return(result); }
internal static InputDescriptor FromCreate(int width, int height, int channels, int red, int green, int blue, int alpha) { if (width < 0) { throw new ArgumentOutOfRangeException(nameof(width), "Width must be greater than zero."); } if (height < 0) { throw new ArgumentOutOfRangeException(nameof(height), "Height must be greater than zero."); } if (channels != 3 || channels != 4) { throw new ArgumentOutOfRangeException(nameof(channels), "Channels must be 3 or 4."); } if (red < 0 || red > 255) { throw new ArgumentOutOfRangeException(nameof(red), "Red must be 0 to 255."); } if (green < 0 || green > 255) { throw new ArgumentOutOfRangeException(nameof(green), "Green must be 0 to 255."); } if (blue < 0 || blue > 255) { throw new ArgumentOutOfRangeException(nameof(blue), "Blue must be 0 to 255."); } if (alpha < 0 || alpha > 255) { throw new ArgumentOutOfRangeException(nameof(alpha), "Alpha must be 0 to 255."); } var result = new InputDescriptor { CreateWidth = width, CreateHeight = height, CreateChannels = channels, FailOnError = true, CreateBackground = new List <double> { red, green, blue, alpha } }; return(result); }
internal static InputDescriptor FromRaw(int width, int height, int channels) { if (width < 0) { throw new ArgumentOutOfRangeException(nameof(width), "Width must be greater than zero."); } if (height < 0) { throw new ArgumentOutOfRangeException(nameof(height), "Height must be greater than zero."); } if (channels < 1 || channels > 4) { throw new ArgumentOutOfRangeException(nameof(channels), "Channels must be 1 to 4."); } var result = new InputDescriptor { Buffer = Array.Empty <byte>(), FailOnError = true, RawWidth = width, RawHeight = height, RawChannels = channels, }; return(result); }
public static (Image Image, ImageType ImageType) OpenInput(InputDescriptor descriptor, string accessMethod) { descriptor = Guard.NotNull(descriptor, nameof(descriptor)); Image image; ImageType imageType; if (descriptor.Buffer != null) { if (descriptor.RawChannels > 0) { // raw, uncompressed pixel data image = Image.NewFromMemory(descriptor.Buffer, descriptor.RawWidth, descriptor.RawHeight, descriptor.RawChannels, Enums.BandFormat.Uchar); image.Set("interpretation", descriptor.RawChannels < 3 ? Enums.Interpretation.Bw : Enums.Interpretation.Srgb); imageType = ImageType.Raw; } else { // Compressed data imageType = ImageType.FromBuffer(descriptor.Buffer); if (imageType == ImageType.Unknown) { throw new VipsException("Input buffer contains unsupported image format."); } try { var option = new VOption { { "access", accessMethod }, { "fail", descriptor.FailOnError } }; if (imageType == ImageType.Svg || imageType == ImageType.Pdf) { option.Add("dpi", descriptor.Density); } if (imageType == ImageType.Magick) { option.Add("density", descriptor.Density); } if (imageType.SupportsPages) { option.Add("n", descriptor.Pages); option.Add("page", descriptor.Page); } image = Image.NewFromBuffer(descriptor.Buffer, null, null, null, option); if (imageType == ImageType.Svg || imageType == ImageType.Pdf || imageType == ImageType.Magick) { image.SetDensity(descriptor.Density); } } catch (Exception ex) { throw new VipsException($"Input buffer has corrupt header: {ex.Message}", ex); } } } else { if (descriptor.CreateChannels > 0) { // create new image var background = new List <double> { descriptor.CreateBackground[0], descriptor.CreateBackground[1], descriptor.CreateBackground[2] }; if (descriptor.CreateChannels == 4) { background.Add(descriptor.CreateBackground[3]); } image = Image.NewFromArray(background.ToArray()); // TODO: suspect image.Set("interpretation", Enums.Interpretation.Srgb); imageType = ImageType.Raw; } else { imageType = ImageType.FromFile(descriptor.File !); if (imageType == ImageType.Missing) { throw new VipsException($"Input file {descriptor.File} is missing."); } if (imageType == ImageType.Unknown) { throw new VipsException("Input file contains unsupported image format."); } try { var option = new VOption { { "access", accessMethod }, { "fail", descriptor.FailOnError } }; if (imageType == ImageType.Svg || imageType == ImageType.Pdf) { option.Add("dpi", descriptor.Density); } if (imageType == ImageType.Magick) { option.Add("density", descriptor.Density); } if (imageType.SupportsPages) { option.Add("n", descriptor.Pages); option.Add("page", descriptor.Page); } image = Image.NewFromFile(descriptor.File, null, null, null, option); if (imageType == ImageType.Svg || imageType == ImageType.Pdf || imageType == ImageType.Magick) { image.SetDensity(descriptor.Density); } } catch (Exception ex) { throw new VipsException($"Input file has corrupt header: {ex.Message}", ex); } } } return(image, imageType); }