/// <summary> /// Utility conversion method allowing to convert object implementing <see cref="DjvuNet.Graphics.IMap"/> /// interface to <see cref="Bitmap"/> object. /// </summary> /// <param name="map"></param> /// <param name="rect"></param> /// <param name="format"></param> /// <returns>Returns <see cref="Bitmap"/> object which should be disposed after use by caller. </returns> public static Bitmap ImageFromMap(GMap map, Rectangle rect, PixelFormat format) { Bitmap retVal = new Bitmap(rect.Width, rect.Height, format); BitmapData bmpData = retVal.LockBits(rect, ImageLockMode.WriteOnly, format); int pixelSize = GetPixelSize(format); int bytesPerRow = pixelSize * rect.Width; GCHandle hMapData = GCHandle.Alloc(map.Data, GCHandleType.Pinned); IntPtr pMapData = hMapData.AddrOfPinnedObject(); for (int i = 0; i < rect.Height; i++) { IntPtr destPtr = bmpData.Scan0 + (bmpData.Stride * i); IntPtr srcPtr = (IntPtr)((long)pMapData + (i * bytesPerRow)); MemoryUtilities.MoveMemory(destPtr, srcPtr, bytesPerRow); } if (hMapData.IsAllocated) { hMapData.Free(); } retVal.UnlockBits(bmpData); return(retVal); }
public GMap GetMap(GRect segment, int subsample, GMap retval) { Verify.SubsampleRange(subsample); if (IsColor) retval = GetPixelMap(segment, subsample, 0.0D, (retval is GPixmap) ? (GPixmap)retval : null); else retval = GetBitmap(segment, subsample, 1, (retval is GBitmap) ? (GBitmap)retval : null); return retval; }
public static Bitmap BuildPageImage(this DjvuPageVisuals image, bool rebuild = false) { const int subsample = 1; int width = image.Page.Width / subsample; int height = image.Page.Height / subsample; GMap map = null; Rectangle rect = new Rectangle(0, 0, width, height); Bitmap retVal = null; if (rebuild || image.Image == null) { map = image.Page.GetMap(new GRect(0, 0, width, height), subsample, null); if (map == null) { return(new Bitmap(width, height)); } if (map.BytesPerPixel == 3) { const PixelFormat format = PixelFormat.Format24bppRgb; retVal = ImageFromMap(map, rect, format); } else if (map.BytesPerPixel == 1) { const PixelFormat format = PixelFormat.Format8bppIndexed; retVal = ImageFromMap(map, rect, format); } } else { retVal = image.Image; } if (map.BytesPerPixel == 3 && image.IsInverted) { retVal = DjvuImage.InvertColor(retVal); } else if (map.BytesPerPixel == 1) { System.Drawing.Imaging.ColorPalette palette = retVal.Palette; if (!image.IsInverted) { for (int i = 0; i < 256; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } retVal.Palette = palette; } else { int j = 0; for (int i = 0; i < 256; i++) { j = 255 - i; palette.Entries[i] = Color.FromArgb(j, j, j); } retVal.Palette = palette; } } return(retVal); //int[] pixels = new int[width * height]; //map.FillRgbPixels(0, 0, width, height, pixels, 0, width); //var image = ConvertDataToImage(pixels); //if (IsInverted == true) // image = InvertImage(image); //return image; }