Exemplo n.º 1
0
 internal static void DecompressRle(IBinaryStream input, byte[] output, int step)
 {
     for (int i = 0; i < step; ++i)
     {
         sbyte ctl = input.ReadInt8();
         int   dst = i;
         while (ctl != 0)
         {
             if (ctl < 0)
             {
                 int count = -ctl;
                 while (count-- > 0)
                 {
                     output[dst] = input.ReadUInt8();
                     dst        += step;
                 }
             }
             else
             {
                 byte v     = input.ReadUInt8();
                 int  count = ctl;
                 while (count-- > 0)
                 {
                     output[dst] = v;
                     dst        += step;
                 }
             }
             ctl = input.ReadInt8();
         }
     }
 }
Exemplo n.º 2
0
        void UnpackRle(int dst, int pixel_size)
        {
            int remaining = m_input.ReadInt32();

            while (remaining > 0)
            {
                int count = m_input.ReadInt8();
                --remaining;
                if (count >= 0)
                {
                    for (int i = 0; i <= count; ++i)
                    {
                        m_output[dst] = m_input.ReadUInt8();
                        --remaining;
                        dst += pixel_size;
                    }
                }
                else
                {
                    count = 1 - count;
                    byte repeat = m_input.ReadUInt8();
                    --remaining;
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = repeat;
                        dst          += pixel_size;
                    }
                }
            }
        }
Exemplo n.º 3
0
        internal static object ReadField(this IBinaryStream input, FieldInfo field)
        {
            var field_type = field.FieldType;
            var type_code  = Type.GetTypeCode(field_type);

            switch (type_code)
            {
            case TypeCode.SByte:    return(input.ReadInt8());

            case TypeCode.Byte:     return(input.ReadUInt8());

            case TypeCode.Int16:    return(input.ReadInt16());

            case TypeCode.UInt16:   return(input.ReadUInt16());

            case TypeCode.Int32:    return(input.ReadInt32());

            case TypeCode.UInt32:   return(input.ReadUInt32());

            case TypeCode.Int64:    return(input.ReadInt64());

            case TypeCode.UInt64:   return(input.ReadUInt64());

            case TypeCode.Char:     return((char)input.ReadInt16());

            case TypeCode.String:
                var cstring_attr = field.GetCustomAttribute <CStringAttribute>();
                if (cstring_attr != null)
                {
                    var encoding = cstring_attr.Encoding ?? Encodings.cp932;
                    if (cstring_attr.IsLengthDefined)
                    {
                        return(input.ReadCString(cstring_attr.Length, encoding));
                    }
                    else
                    {
                        return(input.ReadCString(encoding));
                    }
                }
                throw new FormatException("Serialization method for string field is not defined.");

            case TypeCode.Object:
                object val = Activator.CreateInstance(field_type);
                ReadStruct(input, field_type, ref val);  // FIXME check object graph for cycles
                return(val);

            default:
                throw new NotSupportedException("Not supported serialization type.");
            }
        }
Exemplo n.º 4
0
        void Unpack24bpp()
        {
            int blocks_w = (int)m_info.Width / 2;
            int blocks_h = (int)m_info.Height / 2;
            int dst1     = 0;
            int dst2     = m_stride;

            for (int y = 0; y < blocks_h; ++y)
            {
                for (int x = 0; x < blocks_w; ++x)
                {
                    sbyte v1  = m_input.ReadInt8();
                    sbyte v2  = m_input.ReadInt8();
                    int   b   = (29145 * v2 - 21601 * v1) >> 14;
                    int   g   = (-5312 * v1 - 11083 * v2) >> 14;
                    int   r   = (3 * (v1 + 24 * (v1 + 2 * (v1 + (v1 << 7)))) + 10638 * v2) >> 14;
                    byte  x00 = m_input.ReadUInt8();
                    m_output[dst1++] = Clamp(x00 + b);
                    m_output[dst1++] = Clamp(x00 + g);
                    m_output[dst1++] = Clamp(x00 + r);
                    byte x01 = m_input.ReadUInt8();
                    m_output[dst1++] = Clamp(x01 + b);
                    m_output[dst1++] = Clamp(x01 + g);
                    m_output[dst1++] = Clamp(x01 + r);
                    byte x10 = m_input.ReadUInt8();
                    m_output[dst2++] = Clamp(x10 + b);
                    m_output[dst2++] = Clamp(x10 + g);
                    m_output[dst2++] = Clamp(x10 + r);
                    byte x11 = m_input.ReadUInt8();
                    m_output[dst2++] = Clamp(x11 + b);
                    m_output[dst2++] = Clamp(x11 + g);
                    m_output[dst2++] = Clamp(x11 + r);
                }
                dst1 += m_stride;
                dst2 += m_stride;
            }
        }
Exemplo n.º 5
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var  meta        = (Pb00MetaData)info;
            int  channels    = info.BPP / 8;
            var  pixels      = new byte[info.Width * info.Height * channels];
            long channel_pos = 0x20;

            for (int i = 0; i < channels; ++i)
            {
                file.Position = channel_pos;
                int length = meta.ChannelTable[i];
                channel_pos += length;
                int dst = RgbOrder[i];
                for (int j = 0; j < length; ++j)
                {
                    int b = file.ReadInt8();
                    if (b >= 0)
                    {
                        for (int count = b + 1; count > 0; --count)
                        {
                            pixels[dst] = file.ReadUInt8();
                            dst        += channels;
                            ++j;
                        }
                    }
                    else
                    {
                        byte c = file.ReadUInt8();
                        for (int count = 1 - b; count > 0; --count)
                        {
                            pixels[dst] = c;
                            dst        += channels;
                        }
                        ++j;
                    }
                }
            }
            PixelFormat format;

            if (4 == channels)
            {
                format = PixelFormats.Bgra32;
            }
            else
            {
                format = PixelFormats.Bgr24;
            }
            return(ImageData.Create(info, format, null, pixels));
        }
Exemplo n.º 6
0
        byte[] UnpackRLE()
        {
            var scanlines = new int[m_info.Channels, (int)m_info.Height];

            for (int ch = 0; ch < m_info.Channels; ++ch)
            {
                for (uint row = 0; row < m_info.Height; ++row)
                {
                    scanlines[ch, row] = Binary.BigEndian(m_input.ReadInt16());
                }
            }
            var pixels = new byte[m_info.Channels * m_channel_size];
            int dst    = 0;

            for (int ch = 0; ch < m_info.Channels; ++ch)
            {
                for (uint row = 0; row < m_info.Height; ++row)
                {
                    int line_count = scanlines[ch, row];
                    int n          = 0;
                    while (n < line_count)
                    {
                        int count = m_input.ReadInt8();
                        ++n;
                        if (count >= 0)
                        {
                            ++count;
                            m_input.Read(pixels, dst, count);
                            dst += count;
                            n   += count;
                        }
                        else if (count > -128)
                        {
                            count = 1 - count;
                            byte color = m_input.ReadUInt8();
                            ++n;
                            for (int i = 0; i < count; ++i)
                            {
                                pixels[dst++] = color;
                            }
                        }
                    }
                }
            }
            return(pixels);
        }
Exemplo n.º 7
0
        void LL5Decompress(IBinaryStream input, Stream output)
        {
            var buffer = new byte[0x100];

            while (input.PeekByte() != -1)
            {
                int count = input.ReadInt8();
                if (count < 0)
                {
                    count = -count;
                    input.Read(buffer, 0, count);
                    output.Write(buffer, 0, count);
                }
                else
                {
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        buffer[i] = v;
                    }
                    output.Write(buffer, 0, count);
                }
            }
        }