Beispiel #1
0
 private void bGreyscale_Click(object sender, EventArgs e)
 {
     if (Bitmap)
     {
         // Convert the bitmap to 8bpp and greyscale
         ReplaceBitmap(bitmap.GetColorConvertedInstance(
                           FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP |
                           FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE));
     }
 }
Beispiel #2
0
 /// <summary>
 /// 灰度
 /// </summary>
 public void GreyscaleImage()
 {
     if (isBitmap)
     {
         // Convert the bitmap to 8bpp and greyscale
         ReplaceBitmap(bitmap.GetColorConvertedInstance(
                           FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP |
                           FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE));
     }
 }
Beispiel #3
0
 public void GetColorConvertedInstance()
 {
     using (FreeImageBitmap fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb))
     {
         Assert.AreEqual(32, fib.ColorDepth);
         using (FreeImageBitmap conv = fib.GetColorConvertedInstance(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP))
         {
             Assert.IsNotNull(conv);
             Assert.AreEqual(24, conv.ColorDepth);
         }
     }
 }
Beispiel #4
0
        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;
            }
        }
Beispiel #5
0
 public void GetColorConvertedInstance()
 {
     using (FreeImageBitmap fib = new FreeImageBitmap(10, 10, PixelFormat.Format32bppArgb))
     {
         Assert.AreEqual(32, fib.ColorDepth);
         using (FreeImageBitmap conv = fib.GetColorConvertedInstance(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP))
         {
             Assert.IsNotNull(conv);
             Assert.AreEqual(24, conv.ColorDepth);
         }
     }
 }