/// <summary> Get the ICCProfile information JP2 ColorSpace</summary> /// <param name="csm">provides all necessary info about the colorspace /// </param> /// <returns> ICCMatrixBasedInputProfile for 3 component input and /// ICCMonochromeInputProfile for a 1 component source. Returns /// null if exceptions were encountered. /// </returns> /// <exception cref="ColorSpaceException"> /// </exception> /// <exception cref="ICCProfileException"> /// </exception> /// <exception cref="IllegalArgumentException"> /// </exception> private RestrictedICCProfile getICCProfile(CSJ2K.Color.ColorSpace csm) { switch (ncomps) { case 1: icc = ICCMonochromeInputProfile.createInstance(csm); ricc = icc.parse(); if (ricc.Type != RestrictedICCProfile.kMonochromeInput) { throw new System.ArgumentException("wrong ICCProfile type" + " for image"); } break; case 3: icc = ICCMatrixBasedInputProfile.createInstance(csm); ricc = icc.parse(); if (ricc.Type != RestrictedICCProfile.kThreeCompInput) { throw new System.ArgumentException("wrong ICCProfile type" + " for image"); } break; default: throw new System.ArgumentException("illegal number of " + "components (" + ncomps + ") in image"); } return(ricc); }
/// <summary> Ctor resamples a BlkImgDataSrc so that all components /// have the same number of samples. /// /// Note the present implementation does only two to one /// respampling in either direction (row, column). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal Resampler(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap) { int c; // Calculate the minimum and maximum subsampling factor // across all channels. int minX = src.getCompSubsX(0); int minY = src.getCompSubsY(0); int maxX = minX; int maxY = minY; for (c = 1; c < ncomps; ++c) { minX = System.Math.Min(minX, src.getCompSubsX(c)); minY = System.Math.Min(minY, src.getCompSubsY(c)); maxX = System.Math.Max(maxX, src.getCompSubsX(c)); maxY = System.Math.Max(maxY, src.getCompSubsY(c)); } // Throw an exception for other than 2:1 sampling. if ((maxX != 1 && maxX != 2) || (maxY != 1 && maxY != 2)) { throw new ColorSpaceException("Upsampling by other than 2:1 not" + " supported"); } minCompSubsX = minX; minCompSubsY = minY; maxCompSubsX = maxX; maxCompSubsY = maxY; /* end Resampler ctor */ }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// /// </param> /// <exception cref="IOException"> /// </exception> /// <exception cref="ICCProfileException"> /// </exception> /// <exception cref="IllegalArgumentException"> /// </exception> protected internal ICCProfiler(BlkImgDataSrc src, CSJ2K.Color.ColorSpace csMap) : base(src, csMap) { initialize(); iccp = getICCProfile(csMap); if (ncomps == 1) { xform = new MonochromeTransformTosRGB(iccp, maxValueArray[0], shiftValueArray[0]); } else { xform = new MatrixBasedTransformTosRGB(iccp, maxValueArray, shiftValueArray); } /* end ICCProfiler ctor */ }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> ColorSpaceMapper instance /// </returns> /// <exception cref="IOException">profile access exception /// </exception> public static BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { // Check parameters csMap.pl.checkList(OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(pinfo)); // Perform ICCProfiling or ColorSpace tranfsormation. if (csMap.Method == ColorSpace.MethodEnum.ICC_PROFILED) { return(ICCProfiler.createInstance(src, csMap)); } else { ColorSpace.CSEnum colorspace = csMap.getColorSpace(); if (colorspace == ColorSpace.CSEnum.sRGB) { return(EnumeratedColorSpaceMapper.createInstance(src, csMap)); } else if (colorspace == ColorSpace.CSEnum.GreyScale) { return(EnumeratedColorSpaceMapper.createInstance(src, csMap)); } else if (colorspace == ColorSpace.CSEnum.sYCC) { return(SYccColorSpaceMapper.createInstance(src, csMap)); } if (colorspace == ColorSpace.CSEnum.esRGB) { return(EsRgbColorSpaceMapper.createInstance(src, csMap)); } else if (colorspace == ColorSpace.CSEnum.Unknown) { return(null); } else { throw new ColorSpaceException("Bad color space specification in image"); } } }
/// <summary>Return a suitable String representation of the class instance, e.g. /// <p> /// [PalettizedColorSpaceMapper /// ncomps= 3, scomp= 1, nentries= 1024 /// column=0, 7 bit signed entry /// column=1, 7 bit unsigned entry /// column=2, 7 bit signed entry] /// <p> /// /// </summary> public override System.String ToString() { int c; System.Text.StringBuilder rep = new System.Text.StringBuilder("[PalettizedColorSpaceMapper "); System.Text.StringBuilder body = new System.Text.StringBuilder(" " + eol); if (pbox != null) { body.Append("ncomps= ").Append(NumComps).Append(", scomp= ").Append(srcChannel); for (c = 0; c < NumComps; ++c) { body.Append(eol).Append("column= ").Append(c).Append(", ").Append(pbox.getBitDepth(c)).Append(" bit ").Append(pbox.isSigned(c)?"signed entry":"unsigned entry"); } } else { body.Append("image does not contain a palette box"); } rep.Append(ColorSpace.indent(" ", body)); return(rep.Append("]").ToString()); }
public static Image FromStream(Stream stream) { RandomAccessIO in_stream = new ISRandomAccessIO(stream); // Initialize default parameters ParameterList defpl = GetDefaultParameterList(decoder_pinfo); // Create parameter list using defaults ParameterList pl = new ParameterList(defpl); // **** File Format **** // If the codestream is wrapped in the jp2 fileformat, Read the // file format wrapper FileFormatReader ff = new FileFormatReader(in_stream); ff.readFileFormat(); if (ff.JP2FFUsed) { in_stream.seek(ff.FirstCodeStreamPos); } // +----------------------------+ // | Instantiate decoding chain | // +----------------------------+ // **** Header decoder **** // Instantiate header decoder and read main header HeaderInfo hi = new HeaderInfo(); HeaderDecoder hd; try { hd = new HeaderDecoder(in_stream, pl, hi); } catch (EndOfStreamException e) { throw new ApplicationException("Codestream too short or bad header, unable to decode.", e); } int nCompCod = hd.NumComps; int nTiles = hi.sizValue.NumTiles; DecoderSpecs decSpec = hd.DecoderSpecs; // Get demixed bitdepths int[] depth = new int[nCompCod]; for (int i = 0; i < nCompCod; i++) { depth[i] = hd.getOriginalBitDepth(i); } // **** Bit stream reader **** BitstreamReaderAgent breader; try { breader = BitstreamReaderAgent. createInstance(in_stream, hd, pl, decSpec, false, hi); } catch (IOException e) { throw new ApplicationException("Error while reading bit stream header or parsing packets.", e); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate bit stream reader.", e); } // **** Entropy decoder **** EntropyDecoder entdec; try { entdec = hd.createEntropyDecoder(breader, pl); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate entropy decoder.", e); } // **** ROI de-scaler **** ROIDeScaler roids; try { roids = hd.createROIDeScaler(entdec, pl, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate roi de-scaler.", e); } // **** Dequantizer **** Dequantizer deq; try { deq = hd.createDequantizer(roids, depth, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate dequantizer.", e); } // **** Inverse wavelet transform *** InverseWT invWT; try { // full page inverse wavelet transform invWT = InverseWT.createInstance(deq, decSpec); } catch (ArgumentException e) { throw new ApplicationException("Cannot instantiate inverse wavelet transform.", e); } int res = breader.ImgRes; invWT.ImgResLevel = res; // **** Data converter **** (after inverse transform module) ImgDataConverter converter = new ImgDataConverter(invWT, 0); // **** Inverse component transformation **** InvCompTransf ictransf = new InvCompTransf(converter, decSpec, depth, pl); // **** Color space mapping **** BlkImgDataSrc color; if (ff.JP2FFUsed && pl.getParameter("nocolorspace").Equals("off")) { try { ColorSpace csMap = new ColorSpace(in_stream, hd, pl); BlkImgDataSrc channels = hd.createChannelDefinitionMapper(ictransf, csMap); BlkImgDataSrc resampled = hd.createResampler(channels, csMap); BlkImgDataSrc palettized = hd.createPalettizedColorSpaceMapper(resampled, csMap); color = hd.createColorSpaceMapper(palettized, csMap); } catch (ArgumentException e) { throw new ApplicationException("Could not instantiate ICC profiler.", e); } catch (ColorSpaceException e) { throw new ApplicationException("Error processing ColorSpace information.", e); } } else { // Skip colorspace mapping color = ictransf; } // This is the last image in the decoding chain and should be // assigned by the last transformation: BlkImgDataSrc decodedImage = color; if (color == null) { decodedImage = ictransf; } int numComps = decodedImage.NumComps; int bytesPerPixel = (numComps == 4 ? 4 : 3); // **** Copy to Bitmap **** PixelFormat pixelFormat; switch (numComps) { case 1: pixelFormat = PixelFormat.Format24bppRgb; break; case 3: pixelFormat = PixelFormat.Format24bppRgb; break; case 4: pixelFormat = PixelFormat.Format32bppArgb; break; default: throw new ApplicationException("Unsupported PixelFormat. " + numComps + " components."); } Bitmap dst = new Bitmap(decodedImage.ImgWidth, decodedImage.ImgHeight, pixelFormat); Coord numTiles = decodedImage.getNumTiles(null); int tIdx = 0; for (int y = 0; y < numTiles.y; y++) { // Loop on horizontal tiles for (int x = 0; x < numTiles.x; x++, tIdx++) { decodedImage.setTile(x, y); int height = decodedImage.getTileCompHeight(tIdx, 0); int width = decodedImage.getTileCompWidth(tIdx, 0); int tOffx = decodedImage.getCompULX(0) - (int)Math.Ceiling(decodedImage.ImgULX / (double)decodedImage.getCompSubsX(0)); int tOffy = decodedImage.getCompULY(0) - (int)Math.Ceiling(decodedImage.ImgULY / (double)decodedImage.getCompSubsY(0)); DataBlkInt[] db = new DataBlkInt[numComps]; int[] ls = new int[numComps]; int[] mv = new int[numComps]; int[] fb = new int[numComps]; for (int i = 0; i < numComps; i++) { db[i] = new DataBlkInt(); ls[i] = 1 << (decodedImage.getNomRangeBits(0) - 1); mv[i] = (1 << decodedImage.getNomRangeBits(0)) - 1; fb[i] = decodedImage.getFixedPoint(0); } for (int l = 0; l < height; l++) { for (int i = numComps - 1; i >= 0; i--) { db[i].ulx = 0; db[i].uly = l; db[i].w = width; db[i].h = 1; decodedImage.getInternCompData(db[i], i); } int[] k = new int[numComps]; for (int i = numComps - 1; i >= 0; i--) k[i] = db[i].offset + width - 1; byte[] rowvalues = new byte[width * bytesPerPixel]; for (int i = width - 1; i >= 0; i--) { int[] tmp = new int[numComps]; for (int j = numComps - 1; j >= 0; j--) { tmp[j] = (db[j].data_array[k[j]--] >> fb[j]) + ls[j]; tmp[j] = (tmp[j] < 0) ? 0 : ((tmp[j] > mv[j]) ? mv[j] : tmp[j]); if (decodedImage.getNomRangeBits(j) != 8) tmp[j] = (int)Math.Round(((double)tmp[j] / Math.Pow(2D, (double)decodedImage.getNomRangeBits(j))) * 255D); } int offset = i * bytesPerPixel; switch (numComps) { case 1: rowvalues[offset + 0] = (byte)tmp[0]; rowvalues[offset + 1] = (byte)tmp[0]; rowvalues[offset + 2] = (byte)tmp[0]; break; case 3: rowvalues[offset + 0] = (byte)tmp[2]; rowvalues[offset + 1] = (byte)tmp[1]; rowvalues[offset + 2] = (byte)tmp[0]; break; case 4: rowvalues[offset + 0] = (byte)tmp[3]; rowvalues[offset + 1] = (byte)tmp[2]; rowvalues[offset + 2] = (byte)tmp[1]; rowvalues[offset + 3] = (byte)tmp[0]; break; } } BitmapData dstdata = dst.LockBits( new System.Drawing.Rectangle(tOffx, tOffy + l, width, 1), ImageLockMode.ReadWrite, pixelFormat); IntPtr ptr = dstdata.Scan0; System.Runtime.InteropServices.Marshal.Copy(rowvalues, 0, ptr, rowvalues.Length); dst.UnlockBits(dstdata); } } } return dst; }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal EsRgbColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src, csMap) { initialize(); }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal EnumeratedColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src, csMap) { /* end EnumeratedColorSpaceMapper ctor */ }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> ICCProfiler instance /// </returns> /// <exception cref="IOException">profile access exception /// </exception> /// <exception cref="ICCProfileException">profile content exception /// </exception> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, CSJ2K.Color.ColorSpace csMap) { return(new ICCProfiler(src, csMap)); }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal PalettizedColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap) { pbox = csMap.PaletteBox; initialize(); }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal PalettizedColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src, csMap) { pbox = csMap.PaletteBox; initialize(); }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal SYccColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src, csMap) { initialize(); /* end SYccColorSpaceMapper ctor */ }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> SYccColorSpaceMapper instance /// </returns> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return(new EsRgbColorSpaceMapper(src, csMap)); }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> SYccColorSpaceMapper instance /// </returns> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return new SYccColorSpaceMapper(src, csMap); }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> ChannelDefinitionMapper instance /// </returns> /// <exception cref="ColorSpaceException"> /// </exception> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return new ChannelDefinitionMapper(src, csMap); }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> Resampler instance /// </returns> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return new Resampler(src, csMap); }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> ColorSpaceMapper instance /// </returns> /// <exception cref="IOException">profile access exception /// </exception> public static BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { // Check parameters csMap.pl.checkList(OPT_PREFIX, CSJ2K.j2k.util.ParameterList.toNameArray(pinfo)); // Perform ICCProfiling or ColorSpace tranfsormation. if (csMap.Method == ColorSpace.MethodEnum.ICC_PROFILED) { return ICCProfiler.createInstance(src, csMap); } else { ColorSpace.CSEnum colorspace = csMap.getColorSpace(); if (colorspace == ColorSpace.CSEnum.sRGB) { return EnumeratedColorSpaceMapper.createInstance(src, csMap); } else if (colorspace == ColorSpace.CSEnum.GreyScale) { return EnumeratedColorSpaceMapper.createInstance(src, csMap); } else if (colorspace == ColorSpace.CSEnum.sYCC) { return SYccColorSpaceMapper.createInstance(src, csMap); } if (colorspace == ColorSpace.CSEnum.esRGB) { return EsRgbColorSpaceMapper.createInstance(src, csMap); } else if (colorspace == ColorSpace.CSEnum.Unknown) { return null; } else { throw new ColorSpaceException("Bad color space specification in image"); } } }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// /// </param> protected internal ColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src) { InitBlock(); this.src = src; this.csMap = csMap; initialize(); /* end ColorSpaceMapper ctor */ }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal ChannelDefinitionMapper(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap) { /* end ChannelDefinitionMapper ctor */ }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal EnumeratedColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap) { /* end EnumeratedColorSpaceMapper ctor */ }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> Resampler instance /// </returns> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return(new Resampler(src, csMap)); }
/// <summary> Factory method for creating instances of this class.</summary> /// <param name="src">-- source of image data /// </param> /// <param name="csMap">-- provides colorspace info /// </param> /// <returns> ChannelDefinitionMapper instance /// </returns> /// <exception cref="ColorSpaceException"> /// </exception> public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap) { return(new ChannelDefinitionMapper(src, csMap)); }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal SYccColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap) { initialize(); /* end SYccColorSpaceMapper ctor */ }
/// <summary> Ctor which creates an ICCProfile for the image and initializes /// all data objects (input, working, and output). /// /// </summary> /// <param name="src">-- Source of image data /// </param> /// <param name="csm">-- provides colorspace info /// </param> protected internal ChannelDefinitionMapper(BlkImgDataSrc src, ColorSpace csMap) : base(src, csMap) { /* end ChannelDefinitionMapper ctor */ }