2009-07-08 2 views
0

저레벨 프로그래밍에 대한 경험이 없으며 [StructLayout (LayoutKind.Explicit)]을 사용하지 않는 코드 조각이 필요합니다. 내 사이트는 공유 호스트와 중간 신뢰에서 실행됩니다. 이 코드가 있으면 실행되지 않습니다.[StructLayout]을 System.Runtime.InteropServices를 사용하지 않는 것으로 바꾸십시오?

업데이트 : Octree 내부에서이를 사용하여 png 파일을 퀀트 화합니다.

해결 방법이 있습니까? 여기 =>Is there any way to do Image Quantization safely and with no Marshalling?

/// <summary> 
     /// Struct that defines a 32 bpp colour 
     /// </summary> 
     /// <remarks> 
     /// This struct is used to read data from a 32 bits per pixel image 
     /// in memory, and is ordered in this manner as this is the way that 
     /// the data is layed out in memory 
     /// </remarks> 
     [StructLayout(LayoutKind.Explicit)] 
     public struct Color32 
     { 

      public Color32(IntPtr pSourcePixel) 
      { 
       this = (Color32)Marshal.PtrToStructure(pSourcePixel, typeof(Color32)); 

      } 

      /// <summary> 
      /// Holds the blue component of the colour 
      /// </summary> 
      [FieldOffset(0)] 
      public byte Blue; 
      /// <summary> 
      /// Holds the green component of the colour 
      /// </summary> 
      [FieldOffset(1)] 
      public byte Green; 
      /// <summary> 
      /// Holds the red component of the colour 
      /// </summary> 
      [FieldOffset(2)] 
      public byte Red; 
      /// <summary> 
      /// Holds the alpha component of the colour 
      /// </summary> 
      [FieldOffset(3)] 
      public byte Alpha; 

      /// <summary> 
      /// Permits the color32 to be treated as an int32 
      /// </summary> 
      [FieldOffset(0)] 
      public int ARGB; 

      /// <summary> 
      /// Return the color for this Color32 object 
      /// </summary> 
      public Color Color 
      { 
       get { return Color.FromArgb(Alpha, Red, Green, Blue); } 
      } 
     } 
+0

무엇을 하시겠습니까? Bitmap 클래스를 사용할 수 없습니까? –

+0

비 관리 코드를 사용하는 이유와 방법에 대해 약간의 배경 지식을 제공 할 수 있습니까? –

+0

그가 다른 바이트 순서로 다른 소스의 데이터와 상호 작용하려고 시도하는 것처럼 보일 것입니다 – ShuggyCoUk

답변

1

Marshal.PtrToStructure 방법 (IntPtr입니다, 유형) 어쨌든 작동하지 않습니다,하지만 당신은 사용하지 않고 그것을 할 것 육군 원수.
간단히하기 위해 uints로 이동하는 것이 좋습니다.

public struct Color32 
{ 
    const uint BlueMask = 0xFF000000;  
    const uint GreenMask = 0x00FF0000; 
    const uint RedMask = 0x0000FF00; 
    const uint AlphaMask = 0x000000FF; 
    const int BlueShift = 24;  
    const int GreenShift = 16; 
    const int RedShift = 8; 
    const int AlphaShift = 0; 

    private byte GetComponent(uint mask, int shift) 
    { 
     var b = (this.ARGB & mask); 
     return (byte) (b >> shift);    
    } 

    private void SetComponent(int shift, byte value) 
    { 
     var b = ((uint)value) << shift 
     this.ARGB |= b; 
    } 

    public byte Blue 
    { 
     get { return GetComponent(BlueMask, BlueShift); } 
     set { SetComponent(BlueShift, value); } 
    } 

    public byte Green 
    { 
     get { return GetComponent(GreenMask, GreenShift); } 
     set { SetComponent(GreenShift, value); } 
    } 

    public byte Red 
    { 
     get { return GetComponent(RedMask, RedShift); } 
     set { SetComponent(RedShift, value); } 
    } 

    public byte Alpha 
    { 
     get { return GetComponent(AlphaMask, AlphaShift); } 
     set { SetComponent(AlphaShift, value); } 
    } 

    /// <summary> 
    /// Permits the color32 to be treated as an UInt32 
    /// </summary> 
    public uint ARGB; 

    /// <summary> 
    /// Return the color for this Color32 object 
    /// </summary> 
    public Color Color 
    { 
     get { return Color.FromArgb(Alpha, Red, Green, Blue); } 
    } 
} 

그러나 원하는 것은 1 바이트 순서로 int32를 역으로 변환하는 것입니다. 그런 다음 간단한 Int32 값으로 사용자의 입력 데이터를 읽을 수 있도록 경우이 들어
당신은

System.Net.IPAddress.HostToNetworkOrder (, 당신은 단순히 당신이하고자하는 주문에 대한 구체적인 것보다 오히려 엔디안을 반대하는 데이 점에서 불쾌한 사용 사용하는) 한 do :

static Color FromBgra(int bgra) 
{ 
    return Color.FromArgb(System.Net.IPAddress.HostToNetworkOrder(bgra)); 
} 

이것은 훨씬 간단합니다.

+0

분명히 당신은 직접 수동으로 인라인을 할 수 있습니다. 만약 당신이 정말로 블리드 에지 스피드를 원한다면, 직접적으로 Color 구조체를 직접 생성하는 것이 링크 된 질문에서 실제 사용을 위해 직접적으로 주어지는 것이 더 좋을 것입니다. 너 – ShuggyCoUk

+0

고맙습니다. 이건 구조체를 없애 버렸지 만, 내 진짜 문제가 육군 원이라는 것을 깨달았습니다.그것은 중간 신뢰하에 운영되지 않습니다. –

+0

+1 구조체 질문에 답했기에 자격이 있습니다. –

0

당신은 단지 INT ARGB를 저장하고, 색상 반환을위한 4 바이트로 변환하는 BitConverter를 사용할 수

업데이트 새로운 질문입니다.

더 나은 점은 int를 저장하고 Color.FromArgb(Int32)을 사용하는 것입니다.

이러한 두 가지 방법 모두 개별 바이트를 저장할 필요가 없으므로 StructLayout 특성을 완전히 제거 할 수 있습니다.

1

네이티브 코드 및 메모리와 상호 운용하는 것은 본질적으로 안전하지 않은 작업입니다. 마샬 클래스에 대한 호출도 실패합니다. 전체 신뢰 수준을 갖고 있지 않으면 메모리에 액세스하거나 Interop을 수행 할 수 없습니다. 이

[SecurityPermissionAttribute(
    SecurityAction.LinkDemand, 
    Flags = SecurityPermissionFlag.UnmanagedCode)] 

당신은 자신을 안전하게 오프셋 처리 수를 필요로하기 때문에

+0

방금 ​​깨달았습니다. :-( –

관련 문제