private byte PackBytePbmBgra8(byte[] image, UInt32 arrPos, int packSize, int threshold)
        {
            byte b     = 0x00;
            byte kk    = (byte)(0x80 >> packSize);
            int  shift = 0;

            for (byte k = 0x80; k != kk; k >>= 1)
            {
                if (AnymapEncoder.GrayscaleBgra8(image, (UInt32)((arrPos + shift) * 4)) <= threshold)
                {
                    b |= k;
                }
                ++shift;
            }
            return(b);
        }
        private void WriteP2(byte[] image, StringWriter stringWriter, UInt32 totalPixels, AnymapProperties properties)
        {
            Double ratio = 1.0;

            switch (properties.SourcePixelFormat)
            {
            case BitmapPixelFormat.Bgra8:
                ratio = (Double)properties.MaxValue / 255;
                if (properties.BytesPerColor == 1)
                {
                    stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleBgra8(image, 0) * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleBgra8(image, i * 4) * ratio));
                    }
                }
                else
                {
                    stringWriter.Write("{0} ", (UInt16)(AnymapEncoder.GrayscaleBgra8(image, 0) * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (UInt16)(AnymapEncoder.GrayscaleBgra8(image, i * 4) * ratio));
                    }
                }
                break;

            case BitmapPixelFormat.Gray16:
                if (properties.BytesPerColor == 1)
                {
                    ratio = (Double)properties.MaxValue / 65280;     // 255 / 256
                    stringWriter.Write("{0} ", (byte)(PackU16(image[0], image[1])) * ratio);
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (byte)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio));
                    }
                }
                else
                {
                    ratio = (Double)properties.MaxValue / 65535;
                    stringWriter.Write("{0} ", (UInt16)(PackU16(image[0], image[1]) * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (UInt16)(PackU16(image[i * 2], image[i * 2 + 1]) * ratio));
                    }
                }
                break;

            case BitmapPixelFormat.Gray8:
                ratio = (Double)properties.MaxValue / 255;
                if (properties.BytesPerColor == 1)
                {
                    stringWriter.Write("{0} ", (byte)(image[0] * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (byte)(image[i] * ratio));
                    }
                }
                else
                {
                    stringWriter.Write("{0} ", (UInt16)(image[0] * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (UInt16)(image[i] * ratio));
                    }
                }
                break;

            case BitmapPixelFormat.Rgba16:
                if (properties.BytesPerColor == 1)
                {
                    ratio = (Double)properties.MaxValue / 65280;     //  255 / 256;
                    byte[]   pixel = new byte[2];
                    UInt16[] temp  = new UInt16[3];
                    temp[0] = PackU16(image[0], image[1]);
                    temp[1] = PackU16(image[2], image[3]);
                    temp[2] = PackU16(image[4], image[5]);
                    stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) * ratio);
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        temp[0] = PackU16(image[i * 8], image[i * 8 + 1]);
                        temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]);
                        temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]);
                        stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba16(temp, i * 8) * ratio));
                    }
                }
                else
                {
                    ratio = (Double)properties.MaxValue / 65535;
                    byte[]   pixel = new byte[2];
                    UInt16[] temp  = new UInt16[3];
                    temp[0] = PackU16(image[0], image[1]);
                    temp[1] = PackU16(image[2], image[3]);
                    temp[2] = PackU16(image[4], image[5]);
                    stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) * ratio);
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        temp[0] = PackU16(image[i * 8], image[i * 8 + 1]);
                        temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]);
                        temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]);
                        stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, i * 8) * ratio);
                    }
                }
                break;

            case BitmapPixelFormat.Rgba8:
                ratio = (Double)properties.MaxValue / 255;
                if (properties.BytesPerColor == 1)
                {
                    stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, 0) * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, i * 4) * ratio));
                    }
                }
                else
                {
                    stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, 0) * ratio));
                    for (UInt32 i = 1; i < totalPixels; ++i)
                    {
                        if (i % properties.Width == 0)
                        {
                            stringWriter.WriteLine();
                        }
                        stringWriter.Write("{0} ", (byte)(AnymapEncoder.GrayscaleRgba8(image, i * 4) * ratio));
                    }
                }
                break;

            case BitmapPixelFormat.Nv12:
            case BitmapPixelFormat.Yuy2:
            case BitmapPixelFormat.Unknown:
                break;
            }
        }
        private void WriteP1(byte[] image, StringWriter stringWriter, UInt32 totalPixels, AnymapProperties properties)
        {
            UInt32 i = 0;

            switch (properties.SourcePixelFormat)
            {
            case BitmapPixelFormat.Bgra8:
                stringWriter.Write("{0} ", AnymapEncoder.GrayscaleBgra8(image, 0) <= properties.Threshold8 ? 1 : 0);
                for (i = 1; i < totalPixels; ++i)
                {
                    if (i % properties.Width == 0)
                    {
                        stringWriter.WriteLine();
                    }
                    stringWriter.Write("{0} ", AnymapEncoder.GrayscaleBgra8(image, i * 4) <= properties.Threshold8 ? 1 : 0);
                }
                break;

            case BitmapPixelFormat.Gray16:
                UInt16 pixel = PackU16(image[0], image[1]);
                stringWriter.Write("{0} ", pixel <= properties.Threshold16 ? 1 : 0);
                for (i = 1; i < totalPixels; ++i)
                {
                    if (i % properties.Width == 0)
                    {
                        stringWriter.WriteLine();
                    }
                    pixel = PackU16(image[i * 2], image[i * 2 + 1]);
                    stringWriter.Write("{0} ", pixel <= properties.Threshold16 ? 1 : 0);
                }
                break;

            case BitmapPixelFormat.Gray8:
                stringWriter.Write("{0} ", image[0] <= properties.Threshold8 ? 1 : 0);
                for (i = 1; i < totalPixels; ++i)
                {
                    if (i % properties.Width == 0)
                    {
                        stringWriter.WriteLine();
                    }
                    stringWriter.Write("{0} ", image[i] <= properties.Threshold8 ? 1 : 0);
                }
                break;

            case BitmapPixelFormat.Rgba16:
                UInt16[] temp = new UInt16[3];
                temp[0] = PackU16(image[0], image[1]);
                temp[1] = PackU16(image[2], image[3]);
                temp[2] = PackU16(image[4], image[5]);
                stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, 0) <= properties.Threshold16 ? 1 : 0);
                for (i = 1; i < totalPixels; ++i)
                {
                    if (i % properties.Width == 0)
                    {
                        stringWriter.WriteLine();
                    }
                    temp[0] = PackU16(image[i * 8], image[i * 8 + 1]);
                    temp[1] = PackU16(image[i * 8 + 2], image[i * 8 + 3]);
                    temp[2] = PackU16(image[i * 8 + 4], image[i * 8 + 5]);
                    stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba16(temp, i * 8) <= properties.Threshold16 ? 1 : 0);
                }
                break;

            case BitmapPixelFormat.Rgba8:
                stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba8(image, 0) <= properties.Threshold8 ? 1 : 0);
                for (i = 1; i < totalPixels; ++i)
                {
                    if (i % properties.Width == 0)
                    {
                        stringWriter.WriteLine();
                    }
                    stringWriter.Write("{0} ", AnymapEncoder.GrayscaleRgba8(image, i * 4) <= properties.Threshold8 ? 1 : 0);
                }
                break;

            case BitmapPixelFormat.Nv12:
            case BitmapPixelFormat.Yuy2:
            case BitmapPixelFormat.Unknown:
                break;
            }
        }