/// <summary> /// Parses the JPEG2000 file by traversing the boxes from the /// underlying IO stream. /// </summary> /// <returns> A reference to itself for convenience</returns> protected Jp2File Open() { if (_isRaw) { Codestream = new JP2Codestream(_stream, 0, _stream.Length); return(this); } _boxes = Jp2Box.TraverseBoxes(_stream, _stream.Length).ToList(); long boxPosition = GetCodestreamOffset(); if (boxPosition < 0) { throw new ArgumentException( "File does not contain JPEG2000 codestream box"); } var ccBox = _boxes.First(box => box.Type == BoxTypes.CodestreamBox); Codestream = new JP2Codestream( _stream, boxPosition, (long)ccBox.ContentLength); return(this); }
/// <summary> /// Creates from scratch a JPEG2000 conforming file /// with a JPEG2000 codestream skeleton. /// /// </summary> /// <param name="siz"></param> /// <param name="cod"></param> /// <param name="qcd"></param> /// <param name="reservedTileparts"></param> /// <param name="stream"></param> /// <returns></returns> public static Jp2File Create( IEnumerable <MarkerSegment> markers, int reservedTileparts, bool hasPropertyRights, Stream stream) { var sizs = markers.Where(mrk => mrk.Type == MarkerType.SIZ); var cods = markers.Where(mrk => mrk.Type == MarkerType.SIZ); var qcds = markers.Where(mrk => mrk.Type == MarkerType.QCD); if (!sizs.Any() || !cods.Any() || !qcds.Any()) { throw new ArgumentException( "Must supply SIZ, COD and QCD markers"); } SizMarker siz = sizs.First() as SizMarker; CodMarker cod = cods.First() as CodMarker; QcdMarker qcd = qcds.First() as QcdMarker; ColorSpace colorspace = GetColorspace(siz); Jp2Box signBox = new Jp2SignatureBox(); Jp2Box ftypBox = new FileTypeBox(); Jp2Box jp2hBox = new Jp2Box((uint)BoxTypes.JP2HeaderBox); ImageHeaderBox ihdrBox = new ImageHeaderBox(siz, hasPropertyRights); Jp2Box colrBox = new ColorspaceSpecificationBox(colorspace); var codestream = new JP2Codestream(markers, reservedTileparts); var csBox = new ContiguousCodestreamBox(codestream); if (ihdrBox.BitsPerComponent == ImageHeaderBox.USE_BPC_BOX) { throw new NotSupportedException( "Create image with bit per component specification box"); } jp2hBox.Add(new List <Jp2Box> { ihdrBox, colrBox }); Jp2File jp2File = new Jp2File(stream, new List <Jp2Box> { signBox, ftypBox, jp2hBox, csBox }); return(jp2File); }