DcmStreamHandler.cs :  » Network-Clients » DICOM » org » dicomcs » data » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » DICOM 
DICOM » org » dicomcs » data » DcmStreamHandler.cs
#region Copyright
// 
// This library is based on dcm4che see http://www.sourceforge.net/projects/dcm4che
// Copyright (c) 2002 by TIANI MEDGRAPH AG. All rights reserved.
//
// Modifications Copyright (C) 2002,2008 Fang Yang. All rights reserved.
// 
// This file is part of dicomcs, see http://www.sourceforge.net/projects/dicom-cs
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.                                 
// 
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Fang Yang (yangfang@email.com)
//
#endregion

namespace org.dicomcs.data{
  using System;
  using System.IO;
  using org.dicomcs.data;
  using org.dicomcs.dict;
  using org.dicomcs.util;
  
  /// <summary>
  /// Stream format of DICOM data
  /// </summary>
  class DcmStreamHandler : DcmHandlerI
  {    
    private const uint ITEM_TAG = 0xFFFEE000;
    private const uint ITEM_DELIMITATION_ITEM_TAG = 0xFFFEE00D;
    private const uint SEQ_DELIMITATION_ITEM_TAG = 0xFFFEE0DD;
    
    private byte[] b12 = new byte[12];
    private ByteBuffer bb12;
    private bool explicitVR = false;
    private uint tag = 0;
    private int vr = 0;
    
    private BinaryWriter os;
  
    public virtual DcmDecodeParam DcmDecodeParam
    {
      set
      {
        bb12.SetOrder(value.byteOrder);
        this.explicitVR = value.explicitVR;
      }      
    }

    /// <summary>
    /// Creates a new instance of DcmStreamHandlerImpl 
    /// </summary>
    public DcmStreamHandler(Stream os)
    {
      bb12 = ByteBuffer.Wrap(b12, ByteOrder.LITTLE_ENDIAN);
      this.os = new BinaryWriter( os );
    }
    
    public virtual void  StartCommand()
    {
      // noop
    }
    
    public virtual void  EndCommand()
    {
      // noop
    }
    
    public virtual void  StartDcmFile()
    {
      // noop
    }
    
    public virtual void  EndDcmFile()
    {
      // noop
    }
    
    public virtual void  StartFileMetaInfo(byte[] preamble)
    {
      if (preamble != null)
      {
        os.Write( preamble, 0, 128);
        os.Write( FileMetaInfo.DICM_PREFIX, 0, 4);
      }
    }
    
    public virtual void  EndFileMetaInfo()
    {
      // noop
    }
    
    public virtual void  StartDataset()
    {
      // noop
    }
    
    public virtual void  EndDataset()
    {
      // noop
    }
    
    /*    
    public final void setByteOrder(ByteOrder byteOrder) {
    bb12.order(byteOrder);
    }
    
    public final void setExplicitVR(boolean explicitVR) {
    this.explicitVR = explicitVR;
    }*/
    public virtual int WriteHeader(uint tag, int vr, int len)
    {
      bb12.Clear();
      bb12.Write((short)(tag >>16));
      bb12.Write((short)tag);
      if (!explicitVR || vr == VRs.NONE)
      {
        bb12.Write(len);
        os.Write(b12, 0, 8);
        return 8;
      }
      bb12.Write((byte)((vr)>>8));
      bb12.Write((byte)vr);
      if (VRs.IsLengthField16Bit(vr))
      {
        bb12.Write((short) len);
        os.Write(b12, 0, 8);
        return 8;
      }
      bb12.Write((byte) 0);
      bb12.Write((byte) 0);
      bb12.Write(len);
      os.Write(b12, 0, 12);
      return 12;
    }
    
    public virtual void  StartElement(uint tag, int vr, long pos)
    {
      this.tag = tag;
      this.vr = vr;
    }
    
    public virtual void  EndElement()
    {
    }
    
    public virtual void  StartSequence(int len)
    {
      WriteHeader(tag, vr, len);
    }
    
    public virtual void  EndSequence(int len)
    {
      if (len == - 1)
        WriteHeader(SEQ_DELIMITATION_ITEM_TAG, VRs.NONE, 0);
    }
    
    public virtual void  StartItem(int id, long pos, int len)
    {
      WriteHeader(ITEM_TAG, VRs.NONE, len);
    }
    
    public virtual void  EndItem(int len)
    {
      if (len == - 1)
        WriteHeader(ITEM_DELIMITATION_ITEM_TAG, VRs.NONE, 0);
    }

    public void  Value(dicomcs.util.ByteBuffer bb)
    {
      int length = bb.length();

      WriteHeader(tag, vr, (length + 1) & (~1));

      long tempPos = bb.Position;
      bb.WriteTo(os.BaseStream);
      bb.Position = tempPos;

      if ((length & 1) != 0)
        os.Write((byte)VRs.GetPadding(vr));
    }
    
    public virtual void  Value(byte[] data, int Start, int length)
    {
      WriteHeader(tag, vr, (length + 1) & (~ 1));
      os.Write(data, Start, length);
      if ((length & 1) != 0)
        os.Write((byte)VRs.GetPadding(vr));
    }
    
    public virtual void  Fragment(int id, long pos, byte[] data, int Start, int length)
    {
      WriteHeader(ITEM_TAG, VRs.NONE, (length + 1) & (~ 1));
      os.Write(data, Start, length);
      if ((length & 1) != 0)
        os.Write((byte)0);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.