public static extern FIBITMAP Dither(FIBITMAP dib, FREE_IMAGE_DITHER algorithm);
public static extern uint Dither(uint dib, FREE_IMAGE_DITHER algorithm);
private static extern FIBITMAP DitherWindows(FIBITMAP dib, FREE_IMAGE_DITHER algorithm);
private static extern FIBITMAP DitherLinux(FIBITMAP dib, FREE_IMAGE_DITHER algorithm);
/// <summary> /// Converts a FreeImage bitmap from one color depth to another. /// If the conversion fails the original FreeImage bitmap is returned. /// </summary> /// <param name="dib">Handle to a FreeImage bitmap.</param> /// <param name="conversion">The desired output format.</param> /// <param name="ditherMethod">Dither algorithm when converting with /// <see cref="FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_DITHER"/>.</param> /// <param name="unloadSource">When true the structure will be unloaded on success.</param> /// <returns>Handle to a FreeImage bitmap.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="dib"/> is null.</exception> public static FIBITMAP ConvertColorDepth( FIBITMAP dib, FREE_IMAGE_COLOR_DEPTH conversion, FREE_IMAGE_DITHER ditherMethod, bool unloadSource) { return ConvertColorDepth( dib, conversion, 128, ditherMethod, FREE_IMAGE_QUANTIZE.FIQ_WUQUANT, unloadSource); }
/// <summary> /// Converts a FreeImage bitmap from one color depth to another. /// If the conversion fails the original FreeImage bitmap is returned. /// </summary> /// <param name="dib">Handle to a FreeImage bitmap.</param> /// <param name="conversion">The desired output format.</param> /// <param name="threshold">Threshold value when converting with /// <see cref="FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_THRESHOLD"/>.</param> /// <param name="ditherMethod">Dither algorithm when converting with /// <see cref="FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_DITHER"/>.</param> /// <param name="quantizationMethod">The quantization algorithm for conversion to 8-bit color depth.</param> /// <param name="unloadSource">When true the structure will be unloaded on success.</param> /// <returns>Handle to a FreeImage bitmap.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="dib"/> is null.</exception> internal static FIBITMAP ConvertColorDepth( FIBITMAP dib, FREE_IMAGE_COLOR_DEPTH conversion, byte threshold, FREE_IMAGE_DITHER ditherMethod, FREE_IMAGE_QUANTIZE quantizationMethod, bool unloadSource) { if (dib.IsNull) { throw new ArgumentNullException("dib"); } FIBITMAP result = 0; FIBITMAP dibTemp = 0; uint bpp = GetBPP(dib); bool reorderPalette = ((conversion & FREE_IMAGE_COLOR_DEPTH.FICD_REORDER_PALETTE) > 0); bool forceGreyscale = ((conversion & FREE_IMAGE_COLOR_DEPTH.FICD_FORCE_GREYSCALE) > 0); switch (conversion & (FREE_IMAGE_COLOR_DEPTH)0xFF) { case FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_THRESHOLD: if (bpp != 1) { result = Threshold(dib, threshold); } else { bool isGreyscale = IsGreyscaleImage(dib); if ((forceGreyscale && (!isGreyscale)) || (reorderPalette && isGreyscale)) { result = Threshold(dib, threshold); } } break; case FREE_IMAGE_COLOR_DEPTH.FICD_01_BPP_DITHER: if (bpp != 1) { result = Dither(dib, ditherMethod); } else { bool isGreyscale = IsGreyscaleImage(dib); if ((forceGreyscale && (!isGreyscale)) || (reorderPalette && isGreyscale)) { result = Dither(dib, ditherMethod); } } break; case FREE_IMAGE_COLOR_DEPTH.FICD_04_BPP: if (bpp != 4) { // Special case when 1bpp and FIC_PALETTE if (forceGreyscale && (bpp == 1) && (GetColorType(dib) == FREE_IMAGE_COLOR_TYPE.FIC_PALETTE)) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo4Bits(dibTemp); Unload(dibTemp); } // All other cases are converted directly else { result = ConvertTo4Bits(dib); } } else { bool isGreyscale = IsGreyscaleImage(dib); if ((forceGreyscale && (!isGreyscale)) || (reorderPalette && isGreyscale)) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo4Bits(dibTemp); Unload(dibTemp); } } break; case FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP: if (bpp != 8) { if (forceGreyscale) { result = ConvertToGreyscale(dib); } else { dibTemp = ConvertTo24Bits(dib); result = ColorQuantize(dibTemp, quantizationMethod); Unload(dibTemp); } } else { bool isGreyscale = IsGreyscaleImage(dib); if ((forceGreyscale && (!isGreyscale)) || (reorderPalette && isGreyscale)) { result = ConvertToGreyscale(dib); } } break; case FREE_IMAGE_COLOR_DEPTH.FICD_16_BPP_555: if (forceGreyscale) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo16Bits555(dibTemp); Unload(dibTemp); } else if (bpp != 16 || GetRedMask(dib) != FI16_555_RED_MASK || GetGreenMask(dib) != FI16_555_GREEN_MASK || GetBlueMask(dib) != FI16_555_BLUE_MASK) { result = ConvertTo16Bits555(dib); } break; case FREE_IMAGE_COLOR_DEPTH.FICD_16_BPP: if (forceGreyscale) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo16Bits565(dibTemp); Unload(dibTemp); } else if (bpp != 16 || GetRedMask(dib) != FI16_565_RED_MASK || GetGreenMask(dib) != FI16_565_GREEN_MASK || GetBlueMask(dib) != FI16_565_BLUE_MASK) { result = ConvertTo16Bits565(dib); } break; case FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP: if (forceGreyscale) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo24Bits(dibTemp); Unload(dibTemp); } else if (bpp != 24) { result = ConvertTo24Bits(dib); } break; case FREE_IMAGE_COLOR_DEPTH.FICD_32_BPP: if (forceGreyscale) { dibTemp = ConvertToGreyscale(dib); result = ConvertTo32Bits(dibTemp); Unload(dibTemp); } else if (bpp != 32) { result = ConvertTo32Bits(dib); } break; } if (result.IsNull) { return dib; } if (unloadSource) { Unload(dib); } return result; }