Ejemplo n.º 1
0
 public AnimationConverter()
 {
     InputFile = new BMAFormat();
 }
Ejemplo n.º 2
0
 public AnimationConverter(ColorOrder colorOder, PixelOrder pixelOrder, DataType dataType)
 {
     InputFile = new BMAFormat();
     SetInputFileOptions(colorOder, pixelOrder, dataType);
 }
Ejemplo n.º 3
0
        public bool Encode(string FilePath, int StartFrame, int EndFrame, ColorOrder colorOrder, PixelOrder pixelOrder, DataType dataType)
        {
            //if (Lock) return false;
            int   x, y, data_pos = 0;
            Frame frame;

            OutputFile             = new BMAFormat();
            OutputFile.colorOrder  = colorOrder;
            OutputFile.dataType    = dataType;
            OutputFile.pixelOrder  = pixelOrder;
            OutputFile.fps         = (byte)animation.Fps;
            OutputFile.FramesCount = EndFrame - StartFrame + 1;
            OutputFile.Width       = (ushort)animation.SizeX;
            OutputFile.Height      = (ushort)animation.SizeY;

            bool CT_Enable = (OutputFile.dataType == DataType.WithColorTable);

            if (CT_Enable)
            {
                if (!CreateColorTable())
                {
                    return(false);
                }
            }

            OutputFile.Data = new byte[OutputFile.FramesCount * OutputFile.Height * OutputFile.Width * 3];
            for (int frame_num = StartFrame; frame_num <= EndFrame; frame_num++)
            {
                frame = animation.frames[frame_num];
                if (frame.bitmap == null)
                {
                    FrameRender(frame);
                }
                switch (OutputFile.pixelOrder)
                {
                case PixelOrder.VS_TR:
                    for (x = animation.SizeX - 1; x >= 0; x--)
                    {
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        x--;
                        if (x < 0)
                        {
                            break;
                        }
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VS_TL:
                    for (x = 0; x < animation.SizeX; x++)
                    {
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        x++;
                        if (x == animation.SizeX)
                        {
                            break;
                        }
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VS_BR:
                    for (x = animation.SizeX - 1; x >= 0; x--)
                    {
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        x--;
                        if (x < 0)
                        {
                            break;
                        }
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VS_BL:
                    for (x = 0; x < animation.SizeX; x++)
                    {
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        x++;
                        if (x == animation.SizeX)
                        {
                            break;
                        }
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HS_TL:
                    for (y = 0; y < animation.SizeY; y++)
                    {
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        y++;
                        if (y == animation.SizeY)
                        {
                            break;
                        }
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HS_TR:
                    for (y = 0; y < animation.SizeY; y++)
                    {
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        y++;
                        if (y == animation.SizeY)
                        {
                            break;
                        }
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HS_BL:
                    for (y = animation.SizeY - 1; y >= 0; y--)
                    {
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        y--;
                        if (y < 0)
                        {
                            break;
                        }
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HS_BR:
                    for (y = animation.SizeY - 1; y >= 0; y--)
                    {
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                        y--;
                        if (y < 0)
                        {
                            break;
                        }
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HL_TL:
                    for (y = 0; y < animation.SizeY; y++)
                    {
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HL_TR:
                    for (y = 0; y < animation.SizeY; y++)
                    {
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HL_BL:
                    for (y = animation.SizeY - 1; y >= 0; y--)
                    {
                        for (x = 0; x < animation.SizeX; x++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.HL_BR:
                    for (y = animation.SizeY - 1; y >= 0; y--)
                    {
                        for (x = animation.SizeX - 1; x >= 0; x--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VL_TR:
                    for (x = animation.SizeX - 1; x >= 0; x--)
                    {
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VL_TL:
                    for (x = 0; x < animation.SizeX; x++)
                    {
                        for (y = 0; y < animation.SizeY; y++)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VL_BR:
                    for (x = animation.SizeX - 1; x >= 0; x--)
                    {
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                case PixelOrder.VL_BL:
                    for (x = 0; x < animation.SizeX; x++)
                    {
                        for (y = animation.SizeY - 1; y >= 0; y--)
                        {
                            WriteDataToOutput(x, y, frame.bitmap.GetPixel(x, y), CT_Enable, ref data_pos);
                        }
                    }
                    break;

                default: break;
                }
            }


            //*****************Save Animation to file*******************
            FileInfo f_info = new FileInfo(FilePath);

            switch (f_info.Extension)
            {
            case ".bma":
                using (BinaryWriter writer = new BinaryWriter(File.Open(FilePath, FileMode.OpenOrCreate)))
                {
                    writer.Flush();
                    writer.Seek(0, SeekOrigin.Begin);
                    writer.Write('b');
                    writer.Write('m');
                    writer.Write('a');
                    writer.Write((byte)OutputFile.version);
                    writer.Write((byte)OutputFile.pixelOrder);
                    writer.Write((byte)OutputFile.colorOrder);
                    writer.Write((byte)OutputFile.dataType);
                    writer.Write(OutputFile.FramesCount);
                    writer.Write(OutputFile.fps);
                    writer.Write(OutputFile.Width);
                    writer.Write(OutputFile.Height);
                    if (OutputFile.dataType == DataType.Default)
                    {
                        writer.Write(OutputFile.Data, 0, (int)(OutputFile.FramesCount * OutputFile.Width * OutputFile.Height * 3));
                    }
                    else if (OutputFile.dataType == DataType.WithColorTable)
                    {
                        writer.Write((ushort)(OutputFile.ColorTableSize * 3));
                        for (int ct_pos = 0; ct_pos < OutputFile.ColorTableSize; ct_pos++)
                        {
                            writer.Write(OutputFile.ColorTable[ct_pos, 0]);
                            writer.Write(OutputFile.ColorTable[ct_pos, 1]);
                            writer.Write(OutputFile.ColorTable[ct_pos, 2]);
                        }
                        writer.Write(OutputFile.Data, 0, (int)(OutputFile.FramesCount * OutputFile.Width * OutputFile.Height));
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(true);

            default: return(false);
            }
        }