示例#1
0
        private static byte[] DecodeImage(byte[] Img, int w, int h, int ScanLine)
        {
            using (TCompressor Cmp = new TCompressor())
            {
                if (w <= 0)
                {
                    return(new byte[2]);
                }
                byte[] Result = new byte[h * ScanLine];
                using (MemoryStream InflatedStream = new MemoryStream())
                {
                    unchecked
                    {
                        Cmp.Inflate(Img, 0, InflatedStream);
                        InflatedStream.Position = 0;

                        Sh.Read(InflatedStream, Result, 0, h * ScanLine);
                        return(UnFilter(Result, ScanLine, h));
                    }
                }
            }
        }
示例#2
0
文件: UXlsWmf.cs 项目: mwilian/demos
        //[MethodImpl(MethodImplOptions.NoInlining)] //We don't want to check for ZLib until we are here.
        internal static void ToWMF(byte[] XlsData, int Offset, Stream OutStream, bool IsEMF)
        {
            if (!IsEMF)
            {
                WmfHeader WmfHead = new WmfHeader();
                WmfHead.Key = 0x9AC6CDD7;

                //On Xls format coords are 32 bits, on wmf file format they are 16. That's why we have to convert.
                //catch overflows. We could use unchecked here, but this way we setup the final result.
                try{ WmfHead.Left = (Int16)BitConverter.ToInt32(XlsData, Offset + 4 + 0); }
                catch (OverflowException) { WmfHead.Left = 0; }
                try{ WmfHead.Top = (Int16)BitConverter.ToInt32(XlsData, Offset + 4 + 4); }
                catch (OverflowException) { WmfHead.Top = 0; }
                try{ WmfHead.Right = (Int16)BitConverter.ToInt32(XlsData, Offset + 4 + 8); }
                catch (OverflowException) { WmfHead.Right = 0xFFF; }
                try{ WmfHead.Bottom = (Int16)BitConverter.ToInt32(XlsData, Offset + 4 + 12); }
                catch (OverflowException) { WmfHead.Bottom = 0xFFF; }

                WmfHead.Inch     = 96;
                WmfHead.CheckSum = ComputeAldusCheckSum(WmfHead.Data);
                OutStream.Write(WmfHead.Data, 0, WmfHead.Data.Length);
            }
            //Common part on EMF and WMF
            int IsCompressed = XlsData[Offset + 32];

#if (FRAMEWORK30 || !COMPACTFRAMEWORK)
            if (IsCompressed == 0)  //Data is compressed.
            {
                using (TCompressor Cmp = new TCompressor())
                {
                    Cmp.Inflate(XlsData, Offset + 34, OutStream);
                }
            }
#else
            throw new FlexCelException("Operation not supported in CF 2.0");
#endif
        }
示例#3
0
        //We can't crop the image here, without changing the filters.
        private static void SuppressAlpha(TPdfPngData OutData, byte IntBytes)
        {
            using (TCompressor Cmp = new TCompressor())
            {
                using (TCompressor Cmp2 = new TCompressor())
                {
                    using (TCompressor Cmp3 = new TCompressor())
                    {
                        const int BuffLen = 4096;

                        bool MaskEmpty    = true;
                        bool IsOneBitMask = true;

                        int h = (int)OutData.Height;
                        int w = (int)OutData.Width;

#if (FRAMEWORK20 || ICSHARP) && !COMPACTFRAMEWORK
                        byte[] NewData    = new byte[BuffLen + IntBytes];
                        byte[] SMask      = new byte[BuffLen + 1];
                        byte[] OneBitMask = new byte[BuffLen + 1];
#else
                        sbyte[] NewData    = new sbyte[BuffLen + IntBytes];
                        sbyte[] SMask      = new sbyte[BuffLen + 1];
                        sbyte[] OneBitMask = new sbyte[BuffLen + 1];
#endif

                        using (MemoryStream InflatedStream = new MemoryStream())
                        {
                            unchecked
                            {
                                int NewDataPos    = 0;
                                int SMaskPos      = 0;
                                int OneBitMaskPos = 0;

                                Cmp.Inflate(OutData.Data.ToArray(), 0, InflatedStream);
                                InflatedStream.Position = 0;

                                Cmp.BeginDeflate();
                                Cmp2.BeginDeflate();
                                Cmp3.BeginDeflate();

                                OutData.Data.SetLength(0); Stream OutStream = OutData.Data;
                                using (MemoryStream SMaskStream = new MemoryStream())
                                {
                                    using (MemoryStream OneBitMaskStream = new MemoryStream())
                                    {
                                        int OneBitMaskInnerPos = 128;
                                        for (int r = 0; r < h; r++)
                                        {
                                            byte LastSMask = 0;

#if (FRAMEWORK20 || ICSHARP) && !COMPACTFRAMEWORK
                                            byte RowFilter = (byte)InflatedStream.ReadByte();
#else
                                            sbyte RowFilter = (sbyte)InflatedStream.ReadByte();
#endif

                                            #region inlined                                             //for speed on debug mode
                                            if (NewDataPos >= BuffLen)
                                            {
                                                Cmp.IncDeflate(NewData, 0, NewDataPos, OutStream);
                                                NewDataPos = 0;
                                            }
                                            if (SMaskPos >= BuffLen)
                                            {
                                                Cmp2.IncDeflate(SMask, 0, SMaskPos, SMaskStream);
                                                SMaskPos = 0;
                                            }
                                            if (OneBitMaskPos >= BuffLen)
                                            {
                                                Cmp3.IncDeflate(OneBitMask, 0, OneBitMaskPos, OneBitMaskStream);
                                                OneBitMaskPos             = 0;
                                                OneBitMask[OneBitMaskPos] = 0;
                                                OneBitMaskInnerPos        = 128;
                                            }
                                            #endregion

                                            NewData[NewDataPos++] = RowFilter;
                                            SMask[SMaskPos++]     = RowFilter;
                                            if (IsOneBitMask)
                                            {
                                                if (OneBitMaskInnerPos < 128)
                                                {
                                                    OneBitMaskPos++;                                                     //finish row.
                                                    OneBitMask[OneBitMaskPos] = 0;
                                                    OneBitMaskInnerPos        = 128;
                                                }
                                            }

                                            for (int c = 0; c < w; c++)
                                            {
                                                #region inlined                                                 //for speed on debug mode
                                                if (NewDataPos >= BuffLen)
                                                {
                                                    Cmp.IncDeflate(NewData, 0, NewDataPos, OutStream);
                                                    NewDataPos = 0;
                                                }
                                                if (SMaskPos >= BuffLen)
                                                {
                                                    Cmp2.IncDeflate(SMask, 0, SMaskPos, SMaskStream);
                                                    SMaskPos = 0;
                                                }
                                                if (OneBitMaskPos >= BuffLen)
                                                {
                                                    Cmp3.IncDeflate(OneBitMask, 0, OneBitMaskPos, OneBitMaskStream);
                                                    OneBitMaskPos             = 0;
                                                    OneBitMask[OneBitMaskPos] = 0;
                                                    OneBitMaskInnerPos        = 128;
                                                }
                                                #endregion

                                                for (int b = 0; b < IntBytes; b++)
#if (FRAMEWORK20 || ICSHARP) && !COMPACTFRAMEWORK
                                                {
                                                    NewData[NewDataPos++] = (byte)InflatedStream.ReadByte();
                                                }

                                                byte SMaskData = (byte)InflatedStream.ReadByte();
                                                SMask[SMaskPos++] = SMaskData;
#else
                                                {
                                                    NewData[NewDataPos++] = (sbyte)InflatedStream.ReadByte();
                                                }

                                                byte SMaskData = (byte)InflatedStream.ReadByte();
                                                SMask[SMaskPos++] = (sbyte)SMaskData;
#endif
                                                if (MaskEmpty && SMaskData != 0xFF)
                                                {
                                                    MaskEmpty = false;
                                                }

                                                if (IsOneBitMask)
                                                {
                                                    //SMaskData might have been flushed, and so contain invalid data.
                                                    //So we need a separate LastSMask for sub filter, and the whole last scanline for the others.
                                                    //As this is only an optimization (it will work the same with an SMask), we will only contemplate filters 0 and 1.
                                                    if (RowFilter == 1)                                                     //sub
                                                    {
                                                        unchecked { SMaskData += LastSMask; }
                                                        LastSMask = SMaskData;
                                                    }

                                                    if (RowFilter > 1 || (SMaskData != 0xFF && SMaskData != 0))
                                                    {
                                                        IsOneBitMask = false;
                                                    }
                                                    else
                                                    {
#if (FRAMEWORK20 || ICSHARP) && !COMPACTFRAMEWORK
                                                        OneBitMask[OneBitMaskPos] |= (byte)(~SMaskData & OneBitMaskInnerPos);
#else
                                                        OneBitMask[OneBitMaskPos] = (sbyte)((byte)OneBitMask[OneBitMaskPos] | (~(byte)SMaskData & OneBitMaskInnerPos));
#endif
                                                        if (OneBitMaskInnerPos > 1)
                                                        {
                                                            OneBitMaskInnerPos >>= 1;
                                                        }
                                                        else
                                                        {
                                                            OneBitMaskPos++;
                                                            OneBitMask[OneBitMaskPos] = 0;
                                                            OneBitMaskInnerPos        = 128;
                                                        }
                                                    }
                                                }
                                            }
                                        }


                                        #region inlined                                         //for speed on debug mode
                                        if (NewDataPos > 0)
                                        {
                                            Cmp.IncDeflate(NewData, 0, NewDataPos, OutStream);
                                            NewDataPos = 0;
                                        }
                                        if (SMaskPos > 0)
                                        {
                                            Cmp2.IncDeflate(SMask, 0, SMaskPos, SMaskStream);
                                            SMaskPos = 0;
                                        }
                                        if (OneBitMaskPos > 0)
                                        {
                                            if (OneBitMaskInnerPos < 128)
                                            {
                                                OneBitMaskPos++;                                                                       //finish row.
                                            }
                                            Cmp3.IncDeflate(OneBitMask, 0, OneBitMaskPos, OneBitMaskStream);
                                            OneBitMaskPos             = 0;
                                            OneBitMask[OneBitMaskPos] = 0;
                                        }
                                        #endregion

                                        Cmp.EndDeflate(OutStream);
                                        Cmp2.EndDeflate(SMaskStream);
                                        Cmp3.EndDeflate(OneBitMaskStream);

                                        if (!MaskEmpty)
                                        {
                                            if (IsOneBitMask)
                                            {
                                                OutData.OneBitMask = OneBitMaskStream.ToArray();
                                            }
                                            OutData.SMask = SMaskStream.ToArray();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }