/// <summary> /// Query media informations. /// </summary> /// <param name="path"> /// A <see cref="String"/> that specify the media path. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// A <see cref="ImageInfo"/> containing information about the specified media. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public ImageInfo QueryInfo(Stream stream, ImageCodecCriteria criteria) { if (stream == null) { throw new ArgumentNullException("stream"); } if (criteria == null) { throw new ArgumentNullException("criteria"); } ImageInfo info = new ImageInfo(); using (Bitmap iBitmap = new Bitmap(stream)) { PixelLayout iBitmapPixelType; string containerFormat; ConvertImageFormat(iBitmap.RawFormat, out containerFormat); ConvertPixelFormat(iBitmap, out iBitmapPixelType); info.ContainerFormat = containerFormat; info.PixelType = iBitmapPixelType; info.Width = (uint)iBitmap.Width; info.Height = (uint)iBitmap.Height; } return(info); }
/// <summary> /// Load media from stream. /// </summary> /// <param name="stream"> /// A <see cref="Stream"/> where the media data is stored. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// An <see cref="Image"/> holding the media data. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public Image Load(Stream stream, ImageCodecCriteria criteria) { if (stream == null) { throw new ArgumentNullException("stream"); } if (criteria == null) { throw new ArgumentNullException("criteria"); } const int BufferBlockSize = 4096; byte[] buffer = new byte[stream.Length]; int bufferOffset = 0, bytesRed; // Read the stream content while ((bytesRed = stream.Read(buffer, bufferOffset, BufferBlockSize)) > 0) { bufferOffset += bytesRed; } GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { Gdal.FileFromMemBuffer("/vsimem/GdalLoad", buffer.Length, bufferHandle.AddrOfPinnedObject()); using (Dataset dataset = Gdal.Open("/vsimem/GdalLoad", Access.GA_ReadOnly)) { return(Load(dataset, criteria)); } } finally { // Release virtual path Gdal.Unlink("/vsimem/GdalLoad"); bufferHandle.Free(); } }
/// <summary> /// Query media informations. /// </summary> /// <param name="path"> /// A <see cref="String"/> that specify the media path. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// A <see cref="ImageInfo"/> containing information about the specified media. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public ImageInfo QueryInfo(string path, ImageCodecCriteria criteria) { ImageInfo info = new ImageInfo(); return(info); }
/// <summary> /// Load media from stream. /// </summary> /// <param name="stream"> /// A <see cref="Stream"/> where the media data is stored. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// An <see cref="Image"/> holding the media data. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public Image Load(Stream stream, ImageCodecCriteria criteria) { if (stream == null) throw new ArgumentNullException("stream"); if (criteria == null) throw new ArgumentNullException("criteria"); using (System.Drawing.Bitmap iBitmap = new System.Drawing.Bitmap(stream)) { Image image; PixelLayout pType, pConvType; // Allocate image raster ConvertPixelFormat(iBitmap, out pType); // Check for hardware/software support if (Pixel.IsSupportedInternalFormat(pType) == false) { if (criteria.IsSet(ImageCodecCriteria.SoftwareSupport) && (bool)criteria[ImageCodecCriteria.SoftwareSupport]) { // Pixel type not directly supported by hardware... try to guess suitable software conversion pConvType = Pixel.GuessBestSupportedConvertion(pType); if (pConvType == PixelLayout.None) throw new InvalidOperationException("pixel type " + pType.ToString() + " is not supported by hardware neither software"); } else throw new InvalidOperationException("pixel type " + pType.ToString() + " is not supported by hardware"); } else pConvType = pType; image = new Image(); image.Create(pType, (uint)iBitmap.Width, (uint)iBitmap.Height); switch (iBitmap.PixelFormat) { case System.Drawing.Imaging.PixelFormat.Format1bppIndexed: case System.Drawing.Imaging.PixelFormat.Format4bppIndexed: case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: if (Runtime.RunningMono) { // Bug 676362 - Bitmap Clone does not format return image to requested PixelFormat // https://bugzilla.novell.com/show_bug.cgi?id=676362 // // ATM no mono version has resolved the bug; current workaround is performing image // sampling pixel by pixel, using internal conversion routines, even if it is very slow LoadBitmapByPixel(iBitmap, image); } else LoadBitmapByClone(iBitmap, image); break; default: LoadBitmapByLockBits(iBitmap, image); break; } // ConvertItemType image to supported format, if necessary if ((pConvType != PixelLayout.None) && (pConvType != pType)) image = image.Convert(pConvType); return (image); } }
/// <summary> /// Query media informations. /// </summary> /// <param name="stream"> /// A <see cref="Stream"/> where the media data is stored. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// A <see cref="ImageInfo"/> containing information about the specified media. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public ImageInfo QueryInfo(Stream stream, ImageCodecCriteria criteria) { if (stream == null) { throw new ArgumentNullException("stream"); } if (criteria == null) { throw new ArgumentNullException("criteria"); } ImageInfo info = new ImageInfo(); return(info); }
private static Image Load(Dataset dataset, ImageCodecCriteria criteria) { if (dataset == null) { throw new ArgumentNullException("dataset"); } ColorInterp[] bandSemantic = new ColorInterp[dataset.RasterCount]; for (int i = 1; i <= dataset.RasterCount; i++) { using (Band band = dataset.GetRasterBand(i)) { bandSemantic[i - 1] = band.GetColorInterpretation(); } } switch (bandSemantic[0]) { case ColorInterp.GCI_GrayIndex: return(Load_GrayIndex(dataset, 1, criteria)); default: throw new NotSupportedException(); // Driver dependent definitions case ColorInterp.GCI_Undefined: switch (dataset.RasterCount) { case 1: switch (dataset.GetDriver().ShortName) { case "SRTMHGT": case "VRT": return(Load_GrayIndex(dataset, 1, criteria)); default: throw new NotSupportedException(); } default: throw new NotSupportedException(); } } }
/// <summary> /// Save media to stream. /// </summary> /// <param name="stream"> /// A <see cref="IO.Stream"/> which stores the media data. /// </param> /// <param name="image"> /// A <see cref="Image"/> holding the data to be stored. /// </param> /// <param name="format"> /// A <see cref="String"/> that specify the media format to used for saving <paramref name="image"/>. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an image stream. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/>, <paramref name="image"/> or <paramref name="criteria"/> is null. /// </exception> public void Save(Stream stream, Image image, string format, ImageCodecCriteria criteria) { }
/// <summary> /// Save media to stream. /// </summary> /// <param name="path"> /// A <see cref="String"/> that specify the media path. /// </param> /// <param name="image"> /// A <see cref="Image"/> holding the data to be stored. /// </param> /// <param name="format"> /// A <see cref="String"/> that specify the media format to used for saving <paramref name="image"/>. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an image stream. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/>, <paramref name="image"/> or <paramref name="criteria"/> is null. /// </exception> public void Save(string path, Image image, string format, ImageCodecCriteria criteria) { }
internal static Image Load_GrayIndex(Dataset dataset, int bandIndex, ImageCodecCriteria criteria, Image image) { using (Band band = dataset.GetRasterBand(bandIndex)) { Rectangle?rasterSection = criteria.ImageSection; if (rasterSection.HasValue == false) { rasterSection = new Rectangle(0, 0, band.XSize, band.YSize); } uint width = (uint)rasterSection.Value.Width, height = (uint)rasterSection.Value.Height; if (image == null) { switch (band.DataType) { case OSGeo.GDAL.DataType.GDT_Byte: image = new Image(PixelLayout.GRAY8, width, height); break; case OSGeo.GDAL.DataType.GDT_Int16: image = new Image(PixelLayout.GRAY16S, width, height); break; case OSGeo.GDAL.DataType.GDT_Float32: image = new Image(PixelLayout.GRAYF, width, height); break; default: throw new NotSupportedException(); } } try { if (rasterSection.Value.Left < 0 || rasterSection.Value.Right >= band.XSize) { return(null); // throw new ArgumentOutOfRangeException("criteria", "section out of bounds"); } if (rasterSection.Value.Top < 0 || rasterSection.Value.Bottom >= band.YSize) { return(null); // throw new ArgumentOutOfRangeException("criteria", "section out of bounds"); } } catch { return(null); } // Read raster CPLErr err = band.ReadRaster( rasterSection.Value.X, rasterSection.Value.Y, rasterSection.Value.Width, rasterSection.Value.Height, image.ImageBuffer, (int)image.Width, (int)image.Height, band.DataType, 0, 0 ); // Error handling switch (err) { case CPLErr.CE_None: break; } return(image); } }
internal static Image Load_GrayIndex(Dataset dataset, int bandIndex, ImageCodecCriteria criteria) { return(Load_GrayIndex(dataset, bandIndex, criteria, null)); }
/// <summary> /// Load media from stream. /// </summary> /// <param name="stream"> /// A <see cref="Stream"/> where the media data is stored. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// An <see cref="Image"/> holding the media data. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public Image Load(string path, ImageCodecCriteria criteria) { using (Dataset dataset = Gdal.Open(path, Access.GA_ReadOnly)) { return(Load(dataset, criteria)); } }
/// <summary> /// Save media to stream. /// </summary> /// <param name="stream"> /// A <see cref="IO.Stream"/> which stores the media data. /// </param> /// <param name="image"> /// A <see cref="Image"/> holding the data to be stored. /// </param> /// <param name="format"> /// A <see cref="String"/> that specify the media format to used for saving <paramref name="image"/>. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an image stream. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/>, <paramref name="image"/> or <paramref name="criteria"/> is null. /// </exception> public void Save(Stream stream, Image image, string format, ImageCodecCriteria criteria) { System.Drawing.Imaging.ImageFormat bitmapFormat; System.Drawing.Imaging.PixelFormat bitmapPixelFormat; int iBitmapFlags; ConvertImageFormat(format, out bitmapFormat); ConvertPixelFormat(image.PixelLayout, out bitmapPixelFormat, out iBitmapFlags); // Obtain source and destination data pointers using (Bitmap bitmap = new Bitmap((int)image.Width, (int)image.Height, bitmapPixelFormat)) { #region Copy Image To Bitmap BitmapData iBitmapData = null; IntPtr imageData = image.ImageBuffer; try { iBitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); // Copy Image data dst Bitmap unsafe { byte *hImageDataPtr = (byte *)imageData.ToPointer(); byte *iBitmapDataPtr = (byte *)iBitmapData.Scan0.ToPointer(); uint hImageDataStride = image.Stride; uint iBitmapDataStride = (uint)iBitmapData.Stride; // .NET Image Library stores bitmap scan line data in memory padded dst 4 bytes boundaries // .NET Image Library expect a bottom up image, so invert the scan line order iBitmapDataPtr = iBitmapDataPtr + ((image.Height - 1) * iBitmapDataStride); for (uint line = 0; line < image.Height; line++, hImageDataPtr += hImageDataStride, iBitmapDataPtr -= iBitmapDataStride) { Memory.MemoryCopy(iBitmapDataPtr, hImageDataPtr, hImageDataStride); } } } finally { if (iBitmapData != null) { bitmap.UnlockBits(iBitmapData); } } #endregion #region Support Indexed Pixel Formats if ((iBitmapFlags & (int)ImageFlags.ColorSpaceGray) != 0) { ColorPalette bitmapPalette = bitmap.Palette; switch (bitmapPixelFormat) { case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: // Create grayscale palette for (int i = 0; i <= 255; i++) { bitmapPalette.Entries[i] = Color.FromArgb(i, i, i); } break; } bitmap.Palette = bitmapPalette; } #endregion // Save image with the specified format ImageCodecInfo encoderInfo = Array.Find(ImageCodecInfo.GetImageEncoders(), delegate(ImageCodecInfo item) { return(item.FormatID == bitmapFormat.Guid); }); EncoderParameters encoderParams = null; try { EncoderParameters encoderInfoParamList = bitmap.GetEncoderParameterList(encoderInfo.Clsid); EncoderParameter[] encoderInfoParams = encoderInfoParamList != null ? encoderInfoParamList.Param : null; bool supportQuality = false; int paramsCount = 0; if (encoderInfoParams != null) { Array.ForEach(encoderInfoParams, delegate(EncoderParameter item) { if (item.Encoder.Guid == Encoder.Quality.Guid) { supportQuality = true; paramsCount++; } }); } encoderParams = new EncoderParameters(paramsCount); paramsCount = 0; if (supportQuality) { encoderParams.Param[paramsCount++] = new EncoderParameter(Encoder.Quality, 100); } } catch (Exception) { // Encoder does not support parameters } bitmap.Save(stream, encoderInfo, encoderParams); } }
/// <summary> /// Save media to stream. /// </summary> /// <param name="path"> /// A <see cref="String"/> that specify the media path. /// </param> /// <param name="image"> /// A <see cref="Image"/> holding the data to be stored. /// </param> /// <param name="format"> /// A <see cref="String"/> that specify the media format to used for saving <paramref name="image"/>. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an image stream. /// </param> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/>, <paramref name="image"/> or <paramref name="criteria"/> is null. /// </exception> public void Save(string path, Image image, string format, ImageCodecCriteria criteria) { using (FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write)) { Save(fs, image, format, criteria); } }
/// <summary> /// Load media from stream. /// </summary> /// <param name="stream"> /// A <see cref="Stream"/> where the media data is stored. /// </param> /// <param name="criteria"> /// A <see cref="MediaCodecCriteria"/> that specify parameters for loading an media stream. /// </param> /// <returns> /// An <see cref="Image"/> holding the media data. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if <paramref name="stream"/> or <paramref name="criteria"/> is null. /// </exception> public Image Load(string path, ImageCodecCriteria criteria) { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { return(Load(fs, criteria)); } }