/// <summary> /// Creates a new pix instance using an existing handle to a pix structure. /// </summary> /// <remarks> /// Note that the resulting instance takes ownership of the data structure. /// </remarks> /// <param name="handle"></param> private Pix(IntPtr handle) { if (handle == IntPtr.Zero) { throw new ArgumentNullException("handle"); } this.handle = new HandleRef(this, handle); this.width = LeptonicaApi.Native.pixGetWidth(this.handle); this.height = LeptonicaApi.Native.pixGetHeight(this.handle); this.depth = LeptonicaApi.Native.pixGetDepth(this.handle); var colorMapHandle = LeptonicaApi.Native.pixGetColormap(this.handle); if (colorMapHandle != IntPtr.Zero) { this.colormap = new PixColormap(colorMapHandle); } }
private void CopyColormap(Bitmap img, Pix pix) { var imgPalette = img.Palette; var imgPaletteEntries = imgPalette.Entries; var pixColormap = PixColormap.Create(pix.Depth); try { for (int i = 0; i < imgPaletteEntries.Length; i++) { if (!pixColormap.AddColor((PixColor)imgPaletteEntries[i])) { throw new InvalidOperationException(String.Format("Failed to add colormap entry {0}.", i)); } } pix.Colormap = pixColormap; } catch (Exception) { pixColormap.Dispose(); throw; } }