コード例 #1
0
        public void Process(
            TinyBinaryWriter writer)
        {
            writer.WriteUInt32((UInt32)_bitmap.Width);
            writer.WriteUInt32((UInt32)_bitmap.Height);

            writer.WriteUInt16(0x00);   // flags

            var tinyImageFormat = GetTinytImageFormat(_bitmap.RawFormat);

            if (tinyImageFormat != 0)
            {
                writer.WriteByte(0x01);     // bpp
                writer.WriteByte(tinyImageFormat);
                _bitmap.Save(writer.BaseStream, _bitmap.RawFormat);
            }
            else
            {
                writer.WriteByte(0x10);     // bpp
                writer.WriteByte(tinyImageFormat);

                var rect = new Rectangle(Point.Empty, _bitmap.Size);
                using (var convertedBitmap =
                    _bitmap.Clone(new Rectangle(Point.Empty, _bitmap.Size),
                        PixelFormat.Format16bppRgb565))
                {
                    var bitmapData = convertedBitmap.LockBits(
                        rect, ImageLockMode.ReadOnly, convertedBitmap.PixelFormat);

                    var buffer = new Int16[bitmapData.Stride * convertedBitmap.Height / sizeof(Int16)];
                    System.Runtime.InteropServices.Marshal.Copy(
                        bitmapData.Scan0, buffer, 0, buffer.Length);

                    convertedBitmap.UnlockBits(bitmapData);
                    foreach (var item in buffer)
                    {
                        writer.WriteInt16(item);
                    }
                }
            }
        }
コード例 #2
0
        public void Process(
            TinyBinaryWriter writer)
        {
            writer.WriteUInt32((UInt32)_bitmap.Width);
            writer.WriteUInt32((UInt32)_bitmap.Height);

            writer.WriteUInt16(0x00);   // flags

            var tinyImageFormat = GetTinytImageFormat(_bitmap.RawFormat);

            if (tinyImageFormat != 0)
            {
                writer.WriteByte(0x01);     // bpp
                writer.WriteByte(tinyImageFormat);
                _bitmap.Save(writer.BaseStream, _bitmap.RawFormat);
            }
            else
            {
                writer.WriteByte(0x10);     // bpp
                writer.WriteByte(tinyImageFormat);

                var rect = new Rectangle(Point.Empty, _bitmap.Size);
                using (var convertedBitmap =
                           _bitmap.Clone(new Rectangle(Point.Empty, _bitmap.Size),
                                         PixelFormat.Format16bppRgb565))
                {
                    var bitmapData = convertedBitmap.LockBits(
                        rect, ImageLockMode.ReadOnly, convertedBitmap.PixelFormat);

                    var buffer = new Int16[bitmapData.Stride * convertedBitmap.Height / sizeof(Int16)];
                    System.Runtime.InteropServices.Marshal.Copy(
                        bitmapData.Scan0, buffer, 0, buffer.Length);

                    convertedBitmap.UnlockBits(bitmapData);
                    foreach (var item in buffer)
                    {
                        writer.WriteInt16(item);
                    }
                }
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public void Write(
            TinyBinaryWriter writer)
        {
            var orderedResources = new SortedDictionary <Int16, Tuple <ResourceKind, Byte[]> >();

            foreach (var item in _resources.OfType <EmbeddedResource>())
            {
                var count = 0U;
                using (var reader = new ResourceReader(item.GetResourceStream()))
                {
                    foreach (DictionaryEntry resource in reader)
                    {
                        String resourceType;
                        Byte[] resourceData;
                        var    resourceName = resource.Key.ToString();

                        reader.GetResourceData(resourceName, out resourceType, out resourceData);

                        var kind = GetResourceKind(resourceType, resourceData);

                        if (kind == ResourceKind.Bitmap)
                        {
                            using (var stream = new MemoryStream(resourceData.Length))
                            {
                                var bitmapProcessor = new TinyBitmapProcessor((Bitmap)resource.Value);
                                bitmapProcessor.Process(writer.GetMemoryBasedClone(stream));
                                resourceData = stream.ToArray();
                            }
                        }

                        orderedResources.Add(GenerateIdFromResourceName(resourceName),
                                             new Tuple <ResourceKind, Byte[]>(kind, resourceData));

                        ++count;
                    }
                }

                _context.ResourceFileTable.AddResourceFile(item, count);
            }

            foreach (var item in orderedResources)
            {
                var kind  = item.Value.Item1;
                var bytes = item.Value.Item2;

                var padding = 0;
                switch (kind)
                {
                case ResourceKind.String:
                    var stringLength = (Int32)bytes[0];
                    if (stringLength < 0x7F)
                    {
                        bytes = bytes.Skip(1).Concat(Enumerable.Repeat((Byte)0, 1)).ToArray();
                    }
                    else
                    {
                        bytes = bytes.Skip(2).Concat(Enumerable.Repeat((Byte)0, 1)).ToArray();
                    }
                    break;

                case ResourceKind.Bitmap:
                    padding = _context.ResourceDataTable.AlignToWord();
                    break;

                case ResourceKind.Binary:
                    bytes = bytes.Skip(4).ToArray();
                    break;

                case ResourceKind.Font:
                    padding = _context.ResourceDataTable.AlignToWord();
                    bytes   = bytes.Skip(32).ToArray();   // File size + resource header size
                    break;
                }

                // Pre-process font data (swap endiannes if needed).
                if (kind == ResourceKind.Font)
                {
                    using (var stream = new MemoryStream(bytes.Length))
                    {
                        var fontProcessor = new TinyFontProcessor(bytes);
                        fontProcessor.Process(writer.GetMemoryBasedClone(stream));
                        bytes = stream.ToArray();
                    }
                }

                writer.WriteInt16(item.Key);
                writer.WriteByte((Byte)kind);
                writer.WriteByte((Byte)padding);
                writer.WriteInt32(_context.ResourceDataTable.CurrentOffset);

                _context.ResourceDataTable.AddResourceData(bytes);
            }

            if (orderedResources.Count != 0)
            {
                writer.WriteInt16(0x7FFF);
                writer.WriteByte((Byte)ResourceKind.None);
                writer.WriteByte(0x00);
                writer.WriteInt32(_context.ResourceDataTable.CurrentOffset);
            }
        }
コード例 #4
0
        /// <summary>
        /// Processes original data and writes processed data into output writer.
        /// </summary>
        /// <param name="writer">Endianness-aware binary writer.</param>
        public void Process(
            TinyBinaryWriter writer)
        {
            using (var stream = new MemoryStream(_fontResouce, false))
            using (var reader = new BinaryReader(stream))
            {
                // CLR_GFX_FontDescription

                {
                    // CLR_GFX_FontMetrics

                    writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontMetrics.m_height
                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_offset

                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_ascent
                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_descent

                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_internalLeading
                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_externalLeading

                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_aveCharWidth
                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontMetrics.m_aveCharWidth
                }

                var rangesCount = reader.ReadInt16();
                writer.WriteInt16(rangesCount); // CLR_GFX_FontDescription.m_ranges
                var charactersCount = reader.ReadInt16();
                writer.WriteInt16(charactersCount); // CLR_GFX_FontDescription.m_characters

                var flags = reader.ReadInt16();
                writer.WriteInt16(flags); // CLR_GFX_FontDescription.m_flags
                writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontDescription.m_pad

                // CLR_GFX_BitmapDescription

                var width = reader.ReadUInt32();
                writer.WriteUInt32(width); // CLR_GFX_BitmapDescription.m_width
                var height = reader.ReadUInt32();
                writer.WriteUInt32(height); // CLR_GFX_BitmapDescription.m_height

                writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_BitmapDescription.m_flags

                var bitsPerPixel = reader.ReadByte();
                writer.WriteByte(bitsPerPixel); // CLR_GFX_BitmapDescription.m_bitsPerPixel
                writer.WriteByte(reader.ReadByte()); // CLR_GFX_BitmapDescription.m_type

                for (var i = 0; i <= rangesCount; ++i) // Including sentinel range
                {
                    // CLR_GFX_FontCharacterRange

                    writer.WriteUInt32(reader.ReadUInt32()); // CLR_GFX_FontCharacterRange.m_indexOfFirstFontCharacter

                    writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacterRange.m_firstChar
                    writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacterRange.m_lastChar

                    writer.WriteUInt32(reader.ReadUInt32()); // CLR_GFX_FontCharacterRange.m_rangeOffset
                }

                for (var i = 0; i <= charactersCount; ++i) // Including sentinel character
                {
                    // CLR_GFX_FontCharacter

                    writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacter.m_offset

                    writer.WriteByte(reader.ReadByte()); // CLR_GFX_FontCharacter.m_marginLeft
                    writer.WriteByte(reader.ReadByte()); // CLR_GFX_FontCharacter.m_marginRight
                }

                if (bitsPerPixel == 0)
                {
                    bitsPerPixel = 16; // Native value, rest calculations are same
                }
                var totalSizeInWords = ((width * bitsPerPixel + 31) / 32) * height;
                for (var i = 0; i < totalSizeInWords; ++i)
                {
                    writer.WriteUInt32(reader.ReadUInt32());
                }

                if ((flags & FLAG_FONT_EX) == FLAG_FONT_EX)
                {
                    // TODO: implement it according original idea if needed
                }

                while (stream.Position < stream.Length)
                {
                    writer.WriteByte(reader.ReadByte());
                }
            }
        }
コード例 #5
0
ファイル: CodeWriter.cs プロジェクト: leeholder/Netduino_SDK
        private void WriteOperand(
            Instruction instruction)
        {
            var opcode      = instruction.OpCode;
            var operandType = opcode.OperandType;

            if (operandType == OperandType.InlineNone)
            {
                return;
            }

            var operand = instruction.Operand;

            if (operand == null)
            {
                throw new ArgumentException();
            }

            switch (operandType)
            {
            case OperandType.InlineSwitch:
            {
                var targets = (Instruction[])operand;
                _writer.WriteByte((Byte)targets.Length);
                var diff = instruction.Offset + opcode.Size + 2 * targets.Length + 1;
                foreach (var item in targets)
                {
                    _writer.WriteInt16((Int16)(GetTargetOffset(item) - diff));
                }
                break;
            }

            case OperandType.ShortInlineBrTarget:
            {
                var target = (Instruction)operand;
                _writer.WriteSByte((SByte)
                                   (GetTargetOffset(target) - (instruction.Offset + opcode.Size + 1)));
                break;
            }

            case OperandType.InlineBrTarget:
            {
                var target = (Instruction)operand;
                _writer.WriteInt16((Int16)
                                   (GetTargetOffset(target) - (instruction.Offset + opcode.Size + 2)));
                break;
            }

            case OperandType.ShortInlineVar:
                _writer.WriteByte((byte)GetVariableIndex((VariableDefinition)operand));
                break;

            case OperandType.ShortInlineArg:
                _writer.WriteByte((byte)GetParameterIndex((ParameterDefinition)operand));
                break;

            case OperandType.InlineVar:
                _writer.WriteInt16((short)GetVariableIndex((VariableDefinition)operand));
                break;

            case OperandType.InlineArg:
                _writer.WriteInt16((short)GetParameterIndex((ParameterDefinition)operand));
                break;

            case OperandType.InlineSig:
                // TODO: implement this properly after finding when such code is generated
                //WriteMetadataToken (GetStandAloneSignature ((CallSite) operand));
                break;

            case OperandType.ShortInlineI:
                if (opcode == OpCodes.Ldc_I4_S)
                {
                    _writer.WriteSByte((SByte)operand);
                }
                else
                {
                    _writer.WriteByte((Byte)operand);
                }
                break;

            case OperandType.InlineI:
                _writer.WriteInt32((Int32)operand);
                break;

            case OperandType.InlineI8:
                _writer.WriteInt64((Int64)operand);
                break;

            case OperandType.ShortInlineR:
                _writer.WriteSingle((Single)operand);
                break;

            case OperandType.InlineR:
                _writer.WriteDouble((Double)operand);
                break;

            case OperandType.InlineString:
                var stringReferenceId = _stringTable.GetOrCreateStringId((String)operand, false);
                _writer.WriteUInt16(stringReferenceId);
                break;

            case OperandType.InlineMethod:
                _writer.WriteUInt16(_context.GetMethodReferenceId((MethodReference)operand));
                break;

            case OperandType.InlineType:
                _writer.WriteUInt16(GetTypeReferenceId((TypeReference)operand));
                break;

            case OperandType.InlineField:
                _writer.WriteUInt16(GetFieldReferenceId((FieldReference)operand));
                break;

            case OperandType.InlineTok:
                _writer.WriteUInt32(GetMetadataToken((IMetadataTokenProvider)operand));
                break;

            default:
                throw new ArgumentException();
            }
        }
コード例 #6
0
        /// <summary>
        /// Processes original data and writes processed data into output writer.
        /// </summary>
        /// <param name="writer">Endianness-aware binary writer.</param>
        public void Process(
            TinyBinaryWriter writer)
        {
            using (var stream = new MemoryStream(_fontResouce, false))
                using (var reader = new BinaryReader(stream))
                {
                    // CLR_GFX_FontDescription

                    {
                        // CLR_GFX_FontMetrics

                        writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontMetrics.m_height
                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_offset

                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_ascent
                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_descent

                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_internalLeading
                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_externalLeading

                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_aveCharWidth
                        writer.WriteInt16(reader.ReadInt16());   // CLR_GFX_FontMetrics.m_aveCharWidth
                    }

                    var rangesCount = reader.ReadInt16();
                    writer.WriteInt16(rangesCount);     // CLR_GFX_FontDescription.m_ranges
                    var charactersCount = reader.ReadInt16();
                    writer.WriteInt16(charactersCount); // CLR_GFX_FontDescription.m_characters

                    var flags = reader.ReadInt16();
                    writer.WriteInt16(flags);              // CLR_GFX_FontDescription.m_flags
                    writer.WriteInt16(reader.ReadInt16()); // CLR_GFX_FontDescription.m_pad

                    // CLR_GFX_BitmapDescription

                    var width = reader.ReadUInt32();
                    writer.WriteUInt32(width);               // CLR_GFX_BitmapDescription.m_width
                    var height = reader.ReadUInt32();
                    writer.WriteUInt32(height);              // CLR_GFX_BitmapDescription.m_height

                    writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_BitmapDescription.m_flags

                    var bitsPerPixel = reader.ReadByte();
                    writer.WriteByte(bitsPerPixel);        // CLR_GFX_BitmapDescription.m_bitsPerPixel
                    writer.WriteByte(reader.ReadByte());   // CLR_GFX_BitmapDescription.m_type

                    for (var i = 0; i <= rangesCount; ++i) // Including sentinel range
                    {
                        // CLR_GFX_FontCharacterRange

                        writer.WriteUInt32(reader.ReadUInt32()); // CLR_GFX_FontCharacterRange.m_indexOfFirstFontCharacter

                        writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacterRange.m_firstChar
                        writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacterRange.m_lastChar

                        writer.WriteUInt32(reader.ReadUInt32()); // CLR_GFX_FontCharacterRange.m_rangeOffset
                    }

                    for (var i = 0; i <= charactersCount; ++i) // Including sentinel character
                    {
                        // CLR_GFX_FontCharacter

                        writer.WriteUInt16(reader.ReadUInt16()); // CLR_GFX_FontCharacter.m_offset

                        writer.WriteByte(reader.ReadByte());     // CLR_GFX_FontCharacter.m_marginLeft
                        writer.WriteByte(reader.ReadByte());     // CLR_GFX_FontCharacter.m_marginRight
                    }

                    if (bitsPerPixel == 0)
                    {
                        bitsPerPixel = 16; // Native value, rest calculations are same
                    }
                    var totalSizeInWords = ((width * bitsPerPixel + 31) / 32) * height;
                    for (var i = 0; i < totalSizeInWords; ++i)
                    {
                        writer.WriteUInt32(reader.ReadUInt32());
                    }

                    if ((flags & FLAG_FONT_EX) == FLAG_FONT_EX)
                    {
                        // TODO: implement it according original idea if needed
                    }

                    while (stream.Position < stream.Length)
                    {
                        writer.WriteByte(reader.ReadByte());
                    }
                }
        }
コード例 #7
0
        /// <inheritdoc/>
        public void Write(
            TinyBinaryWriter writer)
        {
            var orderedResources = new SortedDictionary<Int16, Tuple<ResourceKind, Byte[]>>();
            foreach (var item in _resources.OfType<EmbeddedResource>())
            {
                var count = 0U;
                using (var reader = new ResourceReader(item.GetResourceStream()))
                {
                    foreach (DictionaryEntry resource in reader)
                    {
                        String resourceType;
                        Byte[] resourceData;
                        var resourceName = resource.Key.ToString();

                        reader.GetResourceData(resourceName, out resourceType, out resourceData);

                        var kind = GetResourceKind(resourceType, resourceData);

                        if (kind == ResourceKind.Bitmap)
                        {
                            using (var stream = new MemoryStream(resourceData.Length))
                            {
                                var bitmapProcessor = new TinyBitmapProcessor((Bitmap)resource.Value);
                                bitmapProcessor.Process(writer.GetMemoryBasedClone(stream));
                                resourceData = stream.ToArray();
                            }
                        }

                        orderedResources.Add(GenerateIdFromResourceName(resourceName),
                            new Tuple<ResourceKind, Byte[]>(kind, resourceData));

                        ++count;
                    }
                }

                _context.ResourceFileTable.AddResourceFile(item, count);
            }

            foreach (var item in orderedResources)
            {
                var kind = item.Value.Item1;
                var bytes = item.Value.Item2;

                var padding = 0;
                switch (kind)
                {
                    case ResourceKind.String:
                        var stringLength = (Int32)bytes[0];
                        if (stringLength < 0x7F)
                        {
                            bytes = bytes.Skip(1).Concat(Enumerable.Repeat((Byte)0, 1)).ToArray();
                        }
                        else
                        {
                            bytes = bytes.Skip(2).Concat(Enumerable.Repeat((Byte)0, 1)).ToArray();
                        }
                        break;
                    case ResourceKind.Bitmap:
                        padding = _context.ResourceDataTable.AlignToWord();
                        break;
                    case ResourceKind.Binary:
                        bytes = bytes.Skip(4).ToArray();
                        break;
                    case ResourceKind.Font:
                        padding = _context.ResourceDataTable.AlignToWord();
                        bytes = bytes.Skip(32).ToArray(); // File size + resource header size
                        break;
                }

                // Pre-process font data (swap endiannes if needed).
                if (kind == ResourceKind.Font)
                {
                    using (var stream = new MemoryStream(bytes.Length))
                    {
                        var fontProcessor = new TinyFontProcessor(bytes);
                        fontProcessor.Process(writer.GetMemoryBasedClone(stream));
                        bytes = stream.ToArray();
                    }
                }

                writer.WriteInt16(item.Key);
                writer.WriteByte((Byte)kind);
                writer.WriteByte((Byte)padding);
                writer.WriteInt32(_context.ResourceDataTable.CurrentOffset);

                _context.ResourceDataTable.AddResourceData(bytes);
            }

            if (orderedResources.Count != 0)
            {
                writer.WriteInt16(0x7FFF);
                writer.WriteByte((Byte)ResourceKind.None);
                writer.WriteByte(0x00);
                writer.WriteInt32(_context.ResourceDataTable.CurrentOffset);
            }
        }