Ejemplo n.º 1
0
        public void Export(string FileName, int BPP)
        {
            int[] ucBitMask = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
            int i, ii, ulLen;
            bool Dim16Bit = false;
            int PBMHeader, uiPixelRep, uiRawPixels;
            Color[] tmpPixels;
            Color Pixel, LastPixel;
            PygmyPBMHeader PBM = new PygmyPBMHeader();
            FileStream fs = File.Create(FileName);
            UTF8Encoding utf8 = new UTF8Encoding();

            BinaryWriter bw = new BinaryWriter(fs, utf8);

            //toolStripStatusLabel1.Text = BPP.ToString();
            PBMHeader = PBM.ConvertBPP(BPP); Dim16Bit = false;
            if (ImageWidth > 255 || ImageHeight > 255)
            {
                PBMHeader |= PygmyPBMHeader.PYGMY_PBM_16BITD;
                Dim16Bit = true;
            }

            bw.Write((byte)(PBMHeader >> 8));
            bw.Write((byte)PBMHeader);
            if (Dim16Bit == true)
            {
                bw.Write((byte)(ImageWidth >> 8));
                bw.Write((byte)(ImageWidth));
                bw.Write((byte)(ImageHeight >> 8));
                bw.Write((byte)(ImageHeight));
            }
            else
            {
                bw.Write((byte)ImageWidth);
                bw.Write((byte)ImageHeight);
            }
            i = 0;
            ulLen = ImageWidth * ImageHeight;
            tmpPixels = new Color[ulLen];
            for (int y = 0; y < ImageHeight; y++)
            {
                for (int x = 0; x < ImageWidth; x++, i++)
                {
                    tmpPixels[i] = Pixels[x, y];
                }
            }

            for (i = 0; i < ulLen; )
            {
                LastPixel = tmpPixels[i];
                for (ii = 0, uiPixelRep = 0; i < ulLen; uiPixelRep++, ++i)// range 0-63
                {
                    Pixel = tmpPixels[i];
                    if (++ii == 65)
                    {
                        break;
                    }
                    if (Pixel.R != LastPixel.R || Pixel.G != LastPixel.G || Pixel.B != LastPixel.B)
                    {
                        break;
                    }
                }
                if (BPP == PygmyPBMHeader.PYGMY_PBM_1BPP)
                {
                    if (uiPixelRep > 7)
                    {
                        // RLE
                        if (LastPixel.R > 0 || LastPixel.G > 0 || LastPixel.B > 0)
                        {
                            bw.Write((byte)((uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON)); // BIT7 set for RLE, BIT6 set for pixel ON
                        }
                        else
                        {
                            bw.Write((byte)((uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE)); //BIT7 set for RLE ON, BIT6 clear for pixel off/transparent
                        }
                    }
                    else
                    {
                        // RAW
                        for (ii = 1, i -= uiPixelRep, uiRawPixels = 0; ii < 8 && i < ulLen; i++, ii++)
                        {
                            Pixel = tmpPixels[i];
                            if (Pixel.R > 0 || Pixel.G > 0 || Pixel.B > 0)
                            {
                                uiRawPixels |= ucBitMask[ii];
                            }
                        }
                        bw.Write((byte)uiRawPixels);
                    }
                }
                else if (BPP == PygmyPBMHeader.PYGMY_PBM_4BPP)
                {
                    if (uiPixelRep > 0)
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        bw.Write((byte)tmpPixels[i - 1].R << 4);
                    }
                    else
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        for (ii = 0, i -= uiPixelRep; ii < ((uiPixelRep / 2) * 2) && i < ulLen; )
                        {
                            bw.Write((byte)(tmpPixels[i++].R << 4) | (tmpPixels[i++].R & 0x0F));
                            ii += 2;
                        } // for
                        if (ii < uiPixelRep)
                        {
                            bw.Write((byte)(tmpPixels[i++].R << 4));
                        }
                    }
                }
                else if (BPP == PygmyPBMHeader.PYGMY_PBM_8BPP)
                {
                    // ToDo: Figure 8BPP
                    if (uiPixelRep > 0)
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        bw.Write((byte)((LastPixel.R) & 0xE0) | (((LastPixel.G >> 8) & 0xC0) >> 3) | ((LastPixel.B & 0xE0) >> 5));
                    }
                    else
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        for (ii = 0, i -= uiPixelRep; ii < uiPixelRep && i < ulLen; ii++, i++)
                        {
                            bw.Write((byte)(tmpPixels[i].R & 0xE0) | ((tmpPixels[i].G & 0xC0) >> 3) |
                                ((tmpPixels[i].B & 0xE0) >> 5));
                        }
                    }
                }
                else if (BPP == PygmyPBMHeader.PYGMY_PBM_12BPP)
                {
                    if (uiPixelRep > 0)
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        bw.Write((byte)(LastPixel.R >> 4) & 0x000F);
                        bw.Write((byte)((LastPixel.G) & 0x00F0) | ((LastPixel.B >> 4) & 0x000F));
                    }
                    else
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                        for (ii = 0, i -= uiPixelRep; ii < ((uiPixelRep / 2) * 2) && i < ulLen; )
                        {
                            bw.Write((byte)(tmpPixels[i].R & 0xF0) | ((tmpPixels[i].G >> 4) & 0x0F));
                            bw.Write((byte)(tmpPixels[i].B & 0xF0) | ((tmpPixels[i + 1].R >> 4) & 0x0F));
                            bw.Write((byte)(tmpPixels[i + 1].G & 0xF0) | ((tmpPixels[i + 1].B >> 4) & 0x0F));
                            ii += 2;
                            i += 2;
                        } // for
                        if (ii < uiPixelRep)
                        {
                            bw.Write((byte)(tmpPixels[i].R & 0xF0) | ((tmpPixels[i].G >> 4) & 0x0F));
                            bw.Write((byte)(tmpPixels[i].B & 0xF0));
                            ++i;
                        } // if
                    } // else
                }
                else if (BPP == PygmyPBMHeader.PYGMY_PBM_16BPP)
                {
                    if (uiPixelRep == 0)
                    {
                        bw.Write((byte)PygmyPBMHeader.PYGMY_PBM_PIXELON);

                    }
                    else
                    {
                        bw.Write((byte)((uiPixelRep - 1) | (PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON)));
                    }
                    bw.Write((byte)((LastPixel.B & 0xF8) | (LastPixel.G >> 5)));
                    bw.Write((byte)(((LastPixel.G & 0x1C) << 3) | ((LastPixel.R >> 3) & 0x1F)));

                    /*    bw.Write((byte)((tmpPixels[ i-1 ].R & 0xF8 ) | (tmpPixels[ i-1 ].G >> 5 ) ) );
                        bw.Write((byte)((tmpPixels[ i-1 ].G & 0x1C ) << 3 ) | (( tmpPixels[ i-1 ].B >> 3 ) & 0x1F ) );
                    }
                    else
                    {
                        bw.Write((byte)(uiPixelRep-1) | PYGMY_PBM_PIXELON );
                        for( ii = 0, i -= uiPixelRep; ii < uiPixelRep && i < ulLen; ii++, i++ )
                        {
                            bw.Write((byte)(tmpPixels[ i ].R & 0xF8 ) | (tmpPixels[ i ].G >> 5 ) );
                            bw.Write((byte) ((tmpPixels[ i ].G & 0x1C ) << 3 ) | ((tmpPixels[ i ].B >> 3 ) & 0x1F ) );
                        } // for
                    } // else
                    */
                }
                else if (BPP == PygmyPBMHeader.PYGMY_PBM_24BPP)
                {
                    if (uiPixelRep > 0)
                    {
                        bw.Write((byte)(uiPixelRep - 1) | PygmyPBMHeader.PYGMY_PBM_RLE | PygmyPBMHeader.PYGMY_PBM_PIXELON);
                    }
                    else
                    {
                        bw.Write((byte)PygmyPBMHeader.PYGMY_PBM_PIXELON);
                    }
                    bw.Write((byte)(LastPixel.R));
                    bw.Write((byte)(LastPixel.G));
                    bw.Write((byte)(LastPixel.B));
                    /*}
                    else
                    {
                        bw.Write((byte)(uiPixelRep-1) | PYGMY_PBM_PIXELON );
                        //for( ii = 0, i -= uiPixelRep; ii < uiPixelRep && i < ulLen; ii++, i++ )
                        for (ii = 0, i -= uiPixelRep; ii < uiPixelRep && i < ulLen; ii++, i++)
                        {
                            bw.Write((byte)tmpPixels[ i ].R );
                            bw.Write((byte)tmpPixels[ i ].G );
                            bw.Write((byte)tmpPixels[ i ].B );
                        }
                    } */
                }
            }
            bw.Flush();
            bw.Close();
            fs.Close();
        }
Ejemplo n.º 2
0
        public void Load(string FileName)
        {
            int PygmyHeader;
            bool Dim16Bit;
            PygmyPBMHeader PBM = new PygmyPBMHeader();
            FileStream fs = File.Create(FileName);
            UTF8Encoding utf8 = new UTF8Encoding();

            BinaryReader br = new BinaryReader(fs, utf8);

            // Re-Init the Vectors array
            Length = 0;
            SelectedIndex = 0;
            AddPoly();
            // End Re-Init the Vectors Array

            PygmyHeader = br.ReadByte() << 8;
            PygmyHeader |= br.ReadByte();
            Dim16Bit = false;
            if ((PygmyHeader & PygmyPBMHeader.PYGMY_PBM_16BITD) != 0)
            {
                Dim16Bit = true;
            }
            if (Dim16Bit == true)
            {
                ImageWidth = br.ReadByte() << 8;
                ImageWidth |= br.ReadByte();
                ImageHeight = br.ReadByte() << 8;
                ImageHeight |= br.ReadByte();
            }
            else
            {
                ImageWidth = br.ReadByte();
                ImageHeight = br.ReadByte();
            }
            for (int i = 0; ; i++)
            {
                int VectorPacketHeader = br.ReadByte();
                int VectorType = PBM.ExtractVectorPacketType(VectorPacketHeader);
                int VerticeCount = PBM.ExtractVerticeCount(VectorPacketHeader);
                if (VectorType == PygmyPBMHeader.PYGMY_VECTOR_END)
                {
                    break;
                }
                AddPacket(VectorType);
                for( int ii = 0; ii < VerticeCount; ii++ )
                {
                    int x, y;
                    if (Dim16Bit == true)
                    {
                        x = ( br.ReadByte() << 8 ) | br.ReadByte();
                        y = ( br.ReadByte() << 8 ) | br.ReadByte();
                    }
                    else
                    {
                        x = br.ReadByte();
                        y = br.ReadByte();
                    }
                    AddVertice(x, y);
                }
            }
            fs.Close();
        }