Example #1
0
        private void DecodeStrips()
        {
            int pStrip = 0;
            int size   = 0;

            tex = new Texture2D(ImageWidth, ImageLength, TextureFormat.RGBA32, false);
            Color[] colors = new Color[ImageWidth * ImageLength];

            if (Compression == 5)
            {
                int stripLength = ImageWidth * RowsPerStrip * BitsPerSample.Count * BitsPerSample[1] / 8;
                CompressionLZW.CreateBuffer(stripLength);
                if (Predictor == 1)
                {
                    int index = 0;

                    for (int y = 0; y < StripOffsets.Count; y++)
                    {
                        //Debug.Log("strip : "+i);
                        pStrip = StripOffsets[y];    //起始位置
                        size   = StripByteCounts[y]; //读取长度
                        byte[] Dval = CompressionLZW.Decode(data, pStrip, size);
                        //Debug.Log(Dval.Length);
                        for (int x = 0; x < ImageWidth; x++)
                        {
                            float R = GetFloat(Dval, x * PixelBytes);
                            float G = GetFloat(Dval, x * PixelBytes + 4);
                            float B = GetFloat(Dval, x * PixelBytes + 8);
                            float A = GetFloat(Dval, x * PixelBytes + 12);
                            //tex.SetPixel(x,y,new Color(R,G,B,A));//可以一个像素一个像素的设置颜色,也可以先读出来再一起设置颜色
                            colors[index++] = new Color(R, G, B, A);
                        }
                    }
                }
                else
                {
                    // for(int p = 0; p<Dval.Length-16; p++)
                    //     {
                    //         Dval[p+16] += Dval[p];
                    //     }
                }
            }
            tex.SetPixels(colors);
            tex.Apply();
        }
Example #2
0
        private void DecodeStrips()
        {
            if (BitsPerSample.Count != 0)
            {
                int k     = 0;
                int count = -1;
                for (int i = 0; i < BitsPerSample.Count; i++)
                {
                    if (BitsPerSample[i] != k)
                    {
                        count++;
                    }
                    k = BitsPerSample[i];
                }
                if (count > 0) //三个通道大小不一致
                {
                    return;    //暂不支持
                }
            }
            else
            {
                return;
            }
            int pStrip = 0;
            int size   = 0;
            //int f = 1;
            TextureFormat f = TextureFormat.RGBA32;

            if (SampleFormat.Count != 0)  //默认为unsigned整型
            {
                if (SampleFormat[0] == 2) //有符号整型
                {
                    return;               //暂不支持
                }
                if (SampleFormat[0] == 3) //float
                {
                    f = TextureFormat.RGBAFloat;
                }
            }
            tex = new Texture2D(ImageWidth, ImageLength, f, false);

            int PixelCount = ImageWidth * ImageLength;

            Color[] colors = new Color[PixelCount];
            int     index  = PixelCount;

            if (Compression == 5)
            {
                int stripLength = ImageWidth * RowsPerStrip * BitsPerSample.Count * BitsPerSample[0] / 8;
                CompressionLZW.CreateBuffer(stripLength);
                //Debug.Log("PlannarConfiguration : " + PlannarConfiguration);//2基本不用,等于2就不解了
                if (Predictor == 1)//没有差值处理
                {
                    for (int y = 0; y < StripOffsets.Count; y++)
                    {
                        pStrip = StripOffsets[y];    //起始位置
                        size   = StripByteCounts[y]; //读取长度
                        byte[] Dval = CompressionLZW.Decode(data, pStrip, size);

                        if (f == TextureFormat.RGBA32 && BitsPerSample[0] == 1)
                        {
                            for (int x = 0; x < ImageWidth * RowsPerStrip; x++)
                            {
                                float R = Dval[x * PixelBytes] / 255f;
                                float G = Dval[x * PixelBytes + 1] / 255f;
                                float B = Dval[x * PixelBytes + 2] / 255f;
                                float A = PixelBytes > 3 ? Dval[x * PixelBytes + 3] / 255f : 1.0f;
                                //tex.SetPixel(x,y,new Color(R,G,B,A));//可以一个像素一个像素的设置颜色,也可以先读出来再一起设置颜色
                                colors[--index] = new Color(R, G, B, A);//解出来的图像是反着的,改Unity.Color的顺序
                            }
                        }
                        else if (f == TextureFormat.RGBAFloat)
                        {
                            for (int x = 0; x < ImageWidth * RowsPerStrip; x++)
                            {
                                float R = GetFloat(Dval, x * PixelBytes);
                                float G = GetFloat(Dval, x * PixelBytes + 4);
                                float B = GetFloat(Dval, x * PixelBytes + 8);
                                float A = GetFloat(Dval, x * PixelBytes + 12);
                                colors[--index] = new Color(R, G, B, A);
                            }
                        }
                    }
                }
                else if (Predictor == 2)//差值处理
                {
                    if (f == TextureFormat.RGBA32 && BitsPerSample[0] == 8)
                    {
                        for (int y = 0; y < StripOffsets.Count; y++)
                        {
                            pStrip = StripOffsets[y];    //起始位置
                            size   = StripByteCounts[y]; //读取长度
                            byte[] Dval = CompressionLZW.Decode(data, pStrip, size);

                            for (int rows = 0; rows < RowsPerStrip; rows++)
                            {
                                byte[] last = new byte[4] {
                                    0, 0, 0, 0
                                };
                                byte max   = 255;
                                int  start = ImageWidth * rows;
                                int  end   = ImageWidth * (rows + 1);
                                for (int x = start; x < end; x++)
                                {
                                    byte[] rgba = new byte[4] {
                                        255, 255, 255, 255
                                    };

                                    for (int channel = 0; channel < BitsPerSample.Count; channel++)
                                    {
                                        rgba[channel]  = Dval[x * PixelBytes + channel];
                                        rgba[channel] += last[channel];
                                        last[channel]  = rgba[channel];
                                    }
                                    colors[--index] = new Color(rgba[0] / 255f, rgba[1] / 255f, rgba[2] / 255f, rgba[3] / 255f);
                                }
                            }
                        }
                    }
                }
            }
            tex.SetPixels(colors);
            tex.Apply();
        }