/// <summary> /// Parse the colorspace information from the specified colorspace item. /// </summary> /// <param name="colorSpace">The external color space object to parse.</param> public static PdfColorSpace Parse(PdfItem colorSpace) { if (colorSpace == null) { throw new ArgumentNullException("colorSpace", "The provided color space was null."); } Func <PdfItem, PdfColorSpace> defaultAction = (a) => { throw new Exception(String.Format("An unsupported colorspace '{0}' was provided.", GetColorSpaceName(a))); }; IDictionary <string, Func <PdfItem, PdfColorSpace> > map = new Dictionary <string, Func <PdfItem, PdfColorSpace> > { // An Indexed color space is defined by a four-element array, as follows: // // /Indexed base hival lookup // // base - an array or name that identifies the base color space in which the values in the color table are to be interpreted // hival - an integer that specifies the maximum valid index value. I // lookup - The color table is defined by the lookup parameter, which can be either a stream or (in PDF 1.2) a string { "/Indexed", (a) => { if (a == null) { throw new ArgumentNullException("a", "The indexed colorspace cannot be null."); } var array = (a as PdfArray) ?? (a.Get() as PdfArray); if (array == null) { throw new ArgumentException("a", "The indexed colorspace not specified as an array."); } PdfItem colorBase = array.Elements[1].Get(); // base int colors = array.Elements.GetInteger(2); // hival PdfItem lookup = array.Elements[3]; // lookup // We currently only support DeviceRGB indexed color spaces. if ((colorBase.IsName() && ((PdfName)colorBase).Value == "/DeviceRGB")) { return(new PdfIndexedRGBColorSpace(lookup, colors)); } throw new NotImplementedException(String.Format("The indexed colorspace '{0}' is not supported.", GetColorSpaceName(colorBase))); } }, // Standard RGB Colorspace { "/DeviceRGB", (a) => new PdfRGBColorSpace() }, // Gray Colorspace { "/DeviceGray", (a) => new PdfGrayColorSpace() }, // Standard CMYK Colorspace { "/DeviceCMYK", (a) => { throw new NotImplementedException("CMYK encoded images are not supported."); return(new PdfCMYKColorSpace()); } }, }; string name = GetColorSpaceName(colorSpace); var action = (map.ContainsKey(name) ? map[name] : defaultAction); return(action.Invoke(colorSpace)); }
/// <summary> /// Get's the colorspace name for the specified item. /// </summary> /// <param name="colorSpace">The colorspace to inspect.</param> /// <returns>The PdfName of the specified item.</returns> private static string GetColorSpaceName(PdfItem colorSpace) { if (colorSpace == null) return(null); if (colorSpace.IsArray()) return (GetColorSpaceName(((PdfArray)colorSpace).Elements[0])); if (colorSpace.IsName()) return (((PdfName) colorSpace).Value); if (colorSpace.IsReference()) { PdfReference reference = (colorSpace as PdfReference); if (reference != null) { return (GetColorSpaceName(reference.Value)); } } return (null); }
/// <summary> /// Get's the colorspace name for the specified item. /// </summary> /// <param name="colorSpace">The colorspace to inspect.</param> /// <returns>The PdfName of the specified item.</returns> private static string GetColorSpaceName(PdfItem colorSpace) { if (colorSpace == null) return(null); if (colorSpace.IsArray()) return (GetColorSpaceName(((PdfArray)colorSpace).Elements[0])); if (colorSpace.IsName()) return (((PdfName) colorSpace).Value); if (colorSpace.IsReference()) { PdfReference reference = (colorSpace as PdfReference); if (reference != null) { return (GetColorSpaceName(reference.Value)); } } return (null); }