public static void SaveImage(Stream stream, FreeImageBitmap bitmap) { string comment = bitmap.Comment; var vspHeader = new VspHeader(); if (!String.IsNullOrEmpty(comment)) { vspHeader.ParseComment(comment); } if (bitmap.ColorDepth != 4 && bitmap.ColorDepth != 8) { if (bitmap.ColorDepth > 8) { if (bitmap.ColorDepth == 32) { bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP); } if (vspHeader.is8Bit == 0) { bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 16); } else { bitmap.Quantize(FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, 256); } } //throw new ArgumentException("image must be 4-bit or 8-bit"); } if ((bitmap.Width & 7) != 0) { int slackPixels = (bitmap.Width & 7); int pixelsToAdd = 8 - slackPixels; bitmap.EnlargeCanvas <RGBQUAD>(0, 0, pixelsToAdd, 0, new RGBQUAD(Color.Black)); } if (vspHeader.is8Bit == 0) { vspHeader.height = bitmap.Height; vspHeader.width = bitmap.Width / 8; SaveHeader(vspHeader, stream); SavePalette(bitmap.Palette, stream); SaveImageData(stream, bitmap); } else { vspHeader.height = bitmap.Height; if (vspHeader.width != bitmap.Width - 1) { } vspHeader.width = bitmap.Width - 1; SaveHeader(vspHeader, stream); var pmsHeader = vspHeader.ToPmsHeader(); Pms.SavePalette(stream, bitmap.Palette); Pms.SaveImageData8Bit(stream, bitmap); } }
public static unsafe void RemapPalette(FreeImageBitmap bitmap, FreeImageBitmap referenceImage, int numberOfColors) { using (FreeImageBitmap bitmap32 = bitmap.GetColorConvertedInstance(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP)) { if (referenceImage.Height < bitmap.Height || referenceImage.Width < bitmap.Width) { int h2 = referenceImage.Height; if (h2 < bitmap.Height) { h2 = bitmap.Height; } int w2 = referenceImage.Width; if (w2 < bitmap.Width) { w2 = bitmap.Width; } referenceImage.EnlargeCanvas <byte>(0, 0, w2 - referenceImage.Width, h2 - referenceImage.Height, 0); } //FreeImageBitmap reference32 = bitmap.GetColorConvertedInstance(FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP); if (bitmap.ColorDepth != 8) { bitmap.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP); } int *newPalette = (int *)(referenceImage.Palette.BaseAddress); int h = bitmap.Height; int w = bitmap.Width; //Ties are broken in ColorToIndex by popularity in the original image Dictionary <int, int> ColorToIndex = new Dictionary <int, int>(); int[] ColorHistogram = new int[256]; for (int y = 0; y < h; y++) { byte *srcScanline = (byte *)(referenceImage.GetScanlineFromTop8Bit(y).BaseAddress); for (int x = 0; x < w; x++) { int c = srcScanline[x]; ColorHistogram[c]++; } } //add each palette color to the dictionary for (int i = 0; i < numberOfColors; i++) { int c = newPalette[i] & 0xFFFFFF; if (ColorToIndex.ContainsKey(c)) { int otherIndex = ColorToIndex[c]; if (ColorHistogram[i] > ColorHistogram[otherIndex]) { ColorToIndex[c] = i; } } else { ColorToIndex[c] = i; } } for (int y = 0; y < h; y++) { int * trueColorScanline = (int *)(bitmap32.GetScanlineFromTop32Bit(y).BaseAddress); byte *srcScanline = (byte *)(bitmap.GetScanlineFromTop8Bit(y).BaseAddress); byte *referenceScanline = (byte *)(referenceImage.GetScanlineFromTop8Bit(y).BaseAddress); for (int x = 0; x < w; x++) { int srcColor = trueColorScanline[x] & 0xFFFFFF; int refIndex = referenceScanline[x]; int refColor = newPalette[refIndex] & 0xFFFFFF; if (srcColor == refColor) { srcScanline[x] = (byte)refIndex; } else { if (ColorToIndex.ContainsKey(srcColor)) { int newColorIndex = ColorToIndex[srcColor]; if (refColor == (newPalette[newColorIndex] & 0xFFFFFF)) { srcScanline[x] = (byte)refIndex; } else { srcScanline[x] = (byte)newColorIndex; } } else { float minDistance = float.MaxValue; int minIndex = 0; for (int i = 0; i < numberOfColors; i++) { int c = newPalette[i] & 0xFFFFFF; float distance = GetDistanceF(srcColor, c); if (distance < minDistance) { minIndex = i; minDistance = distance; } else if (distance == minDistance && ColorHistogram[i] > ColorHistogram[minIndex]) { minIndex = i; } } if (refColor == (newPalette[minIndex] & 0xFFFFFF)) { srcScanline[x] = (byte)refIndex; } else { srcScanline[x] = (byte)minIndex; } ColorToIndex.Add(srcColor, minIndex); } } } } bitmap.Palette.AsArray = referenceImage.Palette.AsArray; } }