【问题标题】:how to decompose integer array to a byte array (pixel codings)如何将整数数组分解为字节数组(像素编码)
【发布时间】:2008-12-25 00:08:41
【问题描述】:

您好,很抱歉重新表述我的问题让我很恼火,但我正准备找到我的答案。

我有一个由 RGB 值组成的 int 数组,我需要将该 int 数组分解为一个字节数组,但它应该按 BGR 顺序排列。

由 RGB 值组成的 int 数组是这样创建的:

pix[index++] = (255 << 24) | (red << 16) | blue;

【问题讨论】:

    标签: c# arrays rgb pixels


    【解决方案1】:

    C#代码

    
            // convert integer array representing [argb] values to byte array representing [bgr] values
            private byte[] convertArray(int[] array)
            {
                byte[] newarray = new byte[array.Length * 3];
    for (int i = 0; i < array.Length; i++) {
    newarray[i * 3] = (byte)array[i]; newarray[i * 3 + 1] = (byte)(array[i] >> 8); newarray[i * 3 + 2] = (byte)(array[i] >> 16);
    } return newarray; }

    【讨论】:

      【解决方案2】:
      #define N something
      unsigned char bytes[N*3];
      unsigned int  ints[N];
      
      for(int i=0; i<N; i++) {
          bytes[i*3]   = ints[i];       // Blue
          bytes[i*3+1] = ints[i] >> 8;  // Green
          bytes[i*3+2] = ints[i] >> 16; // Red
      }
      

      【讨论】:

      • 问题没有说明具体的语言,我现在只看到了c#标签。
      【解决方案3】:

      使用 Linq:

              pix.SelectMany(i => new byte[] { 
                  (byte)(i >> 0),
                  (byte)(i >> 8),
                  (byte)(i >> 16),
              }).ToArray();
      

      或者

              return (from i in pix
                      from x in new[] { 0, 8, 16 }
                      select (byte)(i >> x)
                     ).ToArray();
      

      【讨论】:

        【解决方案4】:

        尝试使用缓冲类

        byte[] bytes = new byte[ints.Length*4];
        Buffer.BlockCopy(ints, 0, bytes, 0, ints.Length * 4);
        

        【讨论】:

          【解决方案5】:

          r = (pix[index] &gt;&gt; 16) &amp; 0xFF

          其余类似,只需将 16 改为 8 或 24 即可。

          【讨论】:

          • & 如果 r 是字符,则 0xFF 不是必需的。
          猜你喜欢
          • 1970-01-01
          • 2010-12-14
          • 1970-01-01
          • 2012-07-11
          • 2011-10-04
          • 1970-01-01
          • 2021-09-17
          • 1970-01-01
          相关资源
          最近更新 更多