FragmentElement.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 » FragmentElement.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)
//
// 7/23/08: Solved bug by Marcel de Wijs. virtual changed into override (changed lines 128, 206).
#endregion

namespace org.dicomcs.data{
  using System;
  using System.Reflection;
  using System.Runtime.InteropServices;
  using System.Collections;
  using System.Text;
  using org.dicomcs.dict;
  using org.dicomcs.util;
    
  /// <summary>
  /// </summary>
  public abstract class FragmentElement : DcmElement
  {    
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    private ArrayList m_list = new ArrayList();
    
    /// <summary>
    /// Creates a new instance of ElementImpl 
    /// </summary>
    public FragmentElement(uint tag):base(tag)
    {
    }
    
    public override int vm()
    {
      return m_list.Count;
    }
    
    public override bool HasDataFragments()
    {
      return true;
    }
    
    public override ByteBuffer GetDataFragment(int index)
    {
      if (index >= vm())
      {
        return null;
      }

      int offsetSize = Marshal.SizeOf(typeof(uint)),
        end = m_list.Count-1;

      ByteBuffer data = (ByteBuffer) m_list[index];

      if ((0 == index)
      &&  (this.tag() == org.dicomcs.dict.Tags.PixelData)
      &&  (data.length() == (end * offsetSize)))
      {
        uint nOffsetCorrection = 0;
        ByteBuffer mybuffy = new ByteBuffer((int)data.Length, data.GetOrder());

        for (int i = 1; i < end; i++ )
        {
          uint sizeofElement = (uint)((ByteBuffer)m_list[i]).length();

          nOffsetCorrection += (uint) (sizeofElement + (((sizeofElement & 0x01) == 0x01) ? 9 : 8));

          mybuffy.Write((i * offsetSize), (int)nOffsetCorrection);
        }

        // set the data to return.
        data = mybuffy;
        data.Position = 0;
      }

      return data;
    }
    
    public override ByteBuffer GetDataFragment(int index, ByteOrder byteOrder)
    {
      if (index >= vm())
      {
        return null;
      }

      int offsetSize = Marshal.SizeOf(typeof(uint)),
        end = m_list.Count-1;

      ByteBuffer data = (ByteBuffer) m_list[index];

      if ((0 == index)
        &&  (this.tag() == org.dicomcs.dict.Tags.PixelData)
        &&  (data.length() == (end * offsetSize)))
      {
        uint nOffsetCorrection = 0;
        ByteBuffer mybuffy = new ByteBuffer((int)data.Length, data.GetOrder());

        for (int i = 1; i < end; i++ )
        {
          uint sizeofElement = (uint)((ByteBuffer)m_list[i]).length();

          nOffsetCorrection += (uint) (sizeofElement + (((sizeofElement & 0x01) == 0x01) ? 9 : 8));

          mybuffy.Write((i * offsetSize), (int)nOffsetCorrection);
        }

        // set the data to return.
        data = mybuffy;
        data.Position = 0;
      }

      if (data.GetOrder() != byteOrder)
      {
        SwapOrder(data);
      }
      return data;
    }
    
    public override int GetDataFragmentLength(int index)
    {
      if (index >= vm())
      {
        return 0;
      }
      ByteBuffer data = (ByteBuffer) m_list[index];
      return (data.length() + 1) & (~ 1);
    }
    
    public override String GetString(int index, Encoding encoding)
    {
      return GetBoundedString(Int32.MaxValue, index, encoding);
    }
    
    public override System.String GetBoundedString(int maxLen, int index, Encoding encoding)
    {
      if (index >= vm())
      {
        return null;
      }
      return StringUtils.PromptValue(vr(), GetDataFragment(index), maxLen);
    }
    
    public virtual System.String[] GetStrings(Encoding encoding)
    {
      return GetBoundedStrings(System.Int32.MaxValue, encoding);
    }
    
    public override System.String[] GetBoundedStrings(int maxLen, Encoding encoding)
    {
      System.String[] a = new System.String[vm()];
      for (int i = 0; i < a.Length; ++i)
        a[i] = StringUtils.PromptValue(vr(), GetDataFragment(i), maxLen);
      return a;
    }
    
    public virtual int CalcLength()
    {
      int len = 8;
      for (int i = 0, n = vm(); i < n; ++i)
        len += GetDataFragmentLength(i) + 8;
      return len;
    }
    
    public override void  AddDataFragment(ByteBuffer data)
    {
      m_list.Add(data != null?data:EMPTY_VALUE);
    }
    
    protected internal virtual void  SwapOrder(ByteBuffer data)
    {
      data.SetOrder(Swap(data.GetOrder()));
    }
    
    /// <summary>
    /// OB
    /// </summary>
    private sealed class OB : FragmentElement
    {
      internal OB(uint tag):base(tag)
      {
      }
      
      public override int vr()
      {
        return VRs.OB;
      }
    }
    
    public static DcmElement CreateOB(uint tag)
    {
      return new FragmentElement.OB(tag);
    }
    
    /*
    private sealed class OF:FragmentElement
    {
      internal OF(uint tag):base(tag)
      {
      }
      
      public int vr()
      {
        return VRs.OF;
      }
      
      public void  AddDataFragment(ByteBuffer data)
      {
        if ((data.length() & 3) != 0)
        {
          log.warn("Ignore odd length fragment of " + org.dicomcs.dict.Tags.toString(tag) + " OF #" + data.length());
          data = null;
        }
        base.AddDataFragment(data);
      }
      
      protected internal void  SwapOrder(ByteBuffer data)
      {
        SwapInts(data);
      }
    }
    
    public static DcmElement CreateOF(uint tag)
    {
      return new FragmentElement.OF(tag);
    }
    */
    
    /// <summary>
    /// OW
    /// </summary>
    private sealed class OW:FragmentElement
    {
      internal OW(uint tag):base(tag)
      {
      }
      
      public override int vr()
      {
        return VRs.OW;
      }
      
      public override void  AddDataFragment(ByteBuffer data)
      {
        if ((data.length() & 1) != 0)
        {
          log.Warn("Ignore odd length fragment of " + org.dicomcs.dict.Tags.ToHexString(tag()) + " OW #" + data.length());
          data = null;
        }
        base.AddDataFragment(data);
      }
      
      protected internal void  SwapOrder(ByteBuffer data)
      {
        SwapWords(data);
      }
    }
    
    public static DcmElement CreateOW(uint tag)
    {
      return new FragmentElement.OW(tag);
    }
    
    /// <summary>
    /// UN
    /// </summary>
    private sealed class UN : FragmentElement
    {
      internal UN(uint tag):base(tag)
      {
      }
      
      public override int vr()
      {
        return VRs.UN;
      }
    }
    
    public static DcmElement CreateUN(uint tag)
    {
      return new FragmentElement.UN(tag);
    }
    
    public override String ToString()
    {
      System.Text.StringBuilder sb = new StringBuilder(org.dicomcs.dict.Tags.ToHexString(tag()));
      sb.Append(",").Append(VRs.ToString(vr()));
      if (!IsEmpty())
      {
         for (int i = 0, n = vm(); i < n; ++i)
        {
          sb.Append("\n\tFrag-").Append(i + 1).Append(",#").Append(GetDataFragmentLength(i)).Append("[").Append(StringUtils.PromptValue(vr(), GetDataFragment(i), 64)).Append("]");
        }
      }
      return sb.ToString();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.