コード例 #1
0
        private void WriteLogicalScreenDescriptor(Image2 image, EndianBinaryWriter writer, int tranparencyIndex)
        {
            GifLogicalScreenDescriptor descriptor = new GifLogicalScreenDescriptor {
                Width  = (short)image.Width,
                Height = (short)image.Height,
                GlobalColorTableFlag = false,                 // Always false for now.
                GlobalColorTableSize = bitDepth - 1,
                BackgroundColorIndex = (byte)(tranparencyIndex > -1 ? tranparencyIndex : 255)
            };

            writer.Write((ushort)descriptor.Width);
            writer.Write((ushort)descriptor.Height);
            PackedField field = new PackedField();

            field.SetBit(0, descriptor.GlobalColorTableFlag);     // 1   : Global color table flag = 1 || 0 (GCT used/ not used)
            field.SetBits(1, 3, descriptor.GlobalColorTableSize); // 2-4 : color resolution
            field.SetBit(4, false);                               // 5   : GCT sort flag = 0
            field.SetBits(5, 3, descriptor.GlobalColorTableSize); // 6-8 : GCT size. 2^(N+1)

            // Reduce the number of writes
            byte[] arr =
            {
                field.Byte, descriptor.BackgroundColorIndex, // Background Color Index
                descriptor.PixelAspectRatio                  // Pixel aspect ratio. Assume 1:1
            };
            writer.Write(arr);
        }
コード例 #2
0
        private void WriteGraphicalControlExtension(ImageBase image, EndianBinaryWriter writer,
                                                    int transparencyIndex)
        {
            // TODO: Check transparency logic.
            bool           hasTransparent         = transparencyIndex > -1;
            DisposalMethod disposalMethod         = hasTransparent ? DisposalMethod.RestoreToBackground : DisposalMethod.Unspecified;
            GifGraphicsControlExtension extension = new GifGraphicsControlExtension()
            {
                DisposalMethod    = disposalMethod,
                TransparencyFlag  = hasTransparent,
                TransparencyIndex = transparencyIndex,
                DelayTime         = image.FrameDelay
            };

            // Reduce the number of writes.
            byte[] intro =
            {
                GifConstants.extensionIntroducer, GifConstants.graphicControlLabel, 4                 // size
            };
            writer.Write(intro);
            PackedField field = new PackedField();

            field.SetBits(3, 3, (int)extension.DisposalMethod);              // 1-3 : Reserved, 4-6 : Disposal

            // TODO: Allow this as an option.
            field.SetBit(6, false);                      // 7 : User input - 0 = none
            field.SetBit(7, extension.TransparencyFlag); // 8: Has transparent.
            writer.Write(field.Byte);
            writer.Write((ushort)extension.DelayTime);
            writer.Write((byte)(extension.TransparencyIndex == -1 ? 255 : extension.TransparencyIndex));
            writer.Write(GifConstants.terminator);
        }
コード例 #3
0
        private void WriteImageDescriptor(ImageBase image, EndianBinaryWriter writer)
        {
            writer.Write(GifConstants.imageDescriptorLabel); // 2c
            // TODO: Can we capture this?
            writer.Write((ushort)0);                         // Left position
            writer.Write((ushort)0);                         // Top position
            writer.Write((ushort)image.Width);
            writer.Write((ushort)image.Height);
            PackedField field = new PackedField();

            field.SetBit(0, true);             // 1: Local color table flag = 1 (LCT used)
            field.SetBit(1, false);            // 2: Interlace flag 0
            field.SetBit(2, false);            // 3: Sort flag 0
            field.SetBits(5, 3, bitDepth - 1); // 4-5: Reserved, 6-8 : LCT size. 2^(N+1)
            writer.Write(field.Byte);
        }