예제 #1
1
파일: WldTex2.cs 프로젝트: Glain/FFTPatcher
 private void PopulateProperties( GraphicStructure paletteStructure, IList<GraphicStructure> pixelStructures )
 {
     Palette = new Palette( paletteStructure.Pixels, Palette.ColorDepth._16bit );
     List<byte> pix = new List<byte>();
     pixelStructures.ForEach( p => pix.AddRange( p.Pixels ) );
     Pixels = pix.AsReadOnly();
 }
예제 #2
0
        public PalettedImage8bpp( 
            string name, 
            int width, int height, 
            int numPalettes, Palette.ColorDepth depth,
            PatcherLib.Iso.KnownPosition imagePosition, 
            PatcherLib.Iso.KnownPosition palettePosition,
            bool ignoreAssert = false)
            : base(name, width, height)
        {
            this.position = imagePosition;
            this.palettePosition = palettePosition;
            this.depth = depth;

            if (!ignoreAssert)
            {
                System.Diagnostics.Debug.Assert(palettePosition.Length == 256 * (int)depth * numPalettes);
            }

            if ( position is PatcherLib.Iso.PsxIso.KnownPosition )
            {
                var pos = position as PatcherLib.Iso.PsxIso.KnownPosition;
                saveFileName = string.Format( "{0}_{1}_{2}.bmp", pos.Sector, pos.StartLocation, pos.Length );
            }
            else if ( position is PatcherLib.Iso.PspIso.KnownPosition )
            {
                var pos = position as PatcherLib.Iso.PspIso.KnownPosition;
                saveFileName = string.Format( "{0}_{1}_{2}.bmp", pos.SectorEnum, pos.StartLocation, pos.Length );
            }
        }
예제 #3
0
        public static void CopyRectangleToPointNonIndexed( this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette sourcePalette, bool flip )
        {
            BitmapData bmdSource = source.LockBits( new Rectangle( 0, 0, source.Width, source.Height ), ImageLockMode.ReadOnly, source.PixelFormat );
            BitmapData bmdDest = destination.LockBits( new Rectangle( 0, 0, destination.Width, destination.Height ), ImageLockMode.WriteOnly, destination.PixelFormat );
            if( flip )
            {
                for( int col = 0; col < sourceRectangle.Width; col++ )
                {
                    for( int row = 0; row < sourceRectangle.Height; row++ )
                    {
                        int index = bmdSource.GetPixel( col + sourceRectangle.X, row + sourceRectangle.Y );
                        Color c = sourcePalette.Colors[index % 16];
                        if( c.A != 0 )
                        {
                            bmdDest.SetPixel32bpp( destinationPoint.X + (sourceRectangle.Width - col - 1), destinationPoint.Y + row, c );
                        }
                    }
                }
            }
            else
            {
                for( int col = 0; col < sourceRectangle.Width; col++ )
                {
                    for( int row = 0; row < sourceRectangle.Height; row++ )
                    {
                        int index = bmdSource.GetPixel( col + sourceRectangle.X, row + sourceRectangle.Y );
                        Color c = sourcePalette.Colors[index % 16];

                        if( c.A != 0 )
                        {
                            bmdDest.SetPixel32bpp( destinationPoint.X + col, destinationPoint.Y + row, c );
                        }
                    }
                }
            }
            source.UnlockBits( bmdSource );
            destination.UnlockBits( bmdDest );
        }
예제 #4
0
        /// <summary>
        /// Creates a colection of palettes from a PAL file.
        /// </summary>
        public static Palette[] FromPALFile( IList<byte> bytes )
        {
            Palette[] result = new Palette[16];
            for( int i = 0; i < 16; i++ )
            {
                result[i] = Palette.FromPALFiledata( bytes.Sub( 24 + 4 * 16 * i, 24 + 4 * 16 * (i + 1) - 1 ) );
            }

            return result;
        }
예제 #5
0
            public Segment(IList<byte> bytes)
            {
                System.Diagnostics.Debug.Assert(bytes.Count == NumBytes);
                Unknown0x0000 = new List<byte>(bytes.Sub(0, 1920 - 1)).AsReadOnly();
                //Unknown0x0880 = new List<byte>(bytes.Sub(0x880, 0x880 + 384 - 1)).AsReadOnly();
                Unknown0x6E00 = new List<byte>(bytes.Sub(0x6e00, 0x6e00 + 2560 - 1)).AsReadOnly();

                System.Diagnostics.Debug.Assert(Unknown0x6E00.TrueForAll(b => b == 0xFF) || Unknown0x6E00.TrueForAll(b => b == 0x00));

                Palettes = new Palette[numPalettes];
                for (int i = 0; i < numPalettes; i++)
                {
                    Palettes[i] = new Palette(bytes.Sub(1920 + i * 32, 1920 + (i + 1) * 32 - 1));
                }
                Pixels = new byte[width * height];
                for (int i = 0; i < (width * height / 2); i++)
                {
                    Pixels[i * 2] = bytes[i + 1920 + 256 + 384].GetLowerNibble();
                    Pixels[i * 2 + 1] = bytes[i + 1920 + 256 + 384].GetUpperNibble();
                }
            }
예제 #6
0
        protected override System.Drawing.Bitmap GetImageFromIsoInner(System.IO.Stream iso)
        {
            Palette palette = new Palette(palettePositions[0].ReadIso(iso), Palette.ColorDepth._16bit);

            System.Drawing.Bitmap result = new System.Drawing.Bitmap(496, 368);

            for (int i = 0; i < 4; i++)
            {
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sizes[i].Width, sizes[i].Height))
                {
                    List<byte> bytes = new List<byte>();
                    isoPositions[i].ForEach(kp => bytes.AddRange(kp.ReadIso(iso)));
                    WritePixelsToBitmap(palette, bytes, bmp);
                    CopyBitmapToBitmap(bmp, result, positions[i]);
            #if DEBUG
                    bmp.Save(string.Format("worldmap{0}.bmp", i), System.Drawing.Imaging.ImageFormat.Bmp);
            #endif
                }
            }

            return result;
        }
예제 #7
0
        public WLDFACE( IList<byte> bytes )
        {
            Images = new Image[160];
            Palettes = new Palette[64*4];

            //byte[][] pixels = new byte[160][];
            //for ( int i = 0; i < 160; i++ )
            //{
            //    pixels[i] = new byte[32 * 48];
            //}

            //for ( int i = 0; i < bytes.Count; i++ )
            //{
            //    byte left = bytes[i].GetLowerNibble();
            //    byte right = bytes[i].GetUpperNibble();
            //}

            List<byte> truePixels = new List<byte>( portraitHeight * portraitWidth * portraitsPerPage * 4 );
            truePixels.AddRange( GetTruePixels( bytes.Sub( 0, portraitsPerPage * portraitWidth * portraitHeight / 2 - 1 ) ) );
            truePixels.AddRange( GetTruePixels( bytes.Sub( 32768, 32768 + portraitsPerPage * portraitWidth * portraitHeight / 2 - 1 ) ) );
            truePixels.AddRange( GetTruePixels( bytes.Sub( 65536, 65536 + portraitsPerPage * portraitWidth * portraitHeight / 2 - 1 ) ) );
            truePixels.AddRange( GetTruePixels( bytes.Sub( 98304, 98304 + portraitsPerPage * portraitWidth * portraitHeight / 2 - 1 ) ) );
            for ( int page = 0; page < 4; page++ )
            {
                int start = page * 32768 + portraitHeight * portraitWidth * portraitsPerPage / 2;
                for ( int pal = 0; pal < 64; pal++ )
                {
                    Palettes[pal + 64 * page] = new Palette( bytes.Sub( start + pal * 32, start + ( pal + 1 ) * 32 - 1 ) );
                }
            }

            Bitmap b = new Bitmap( portraitWidth * colsPerPage, portraitHeight * rowsPerPage * 4, PixelFormat.Format4bppIndexed );
            BitmapData bmd = b.LockBits( new Rectangle( Point.Empty, b.Size ), ImageLockMode.WriteOnly, PixelFormat.Format4bppIndexed );
            for ( int i = 0; i < truePixels.Count; i++ )
            {
                bmd.SetPixel4bpp( i % ( colsPerPage * portraitWidth ), i / colsPerPage / portraitWidth, truePixels[i] );
            }
            b.UnlockBits( bmd );

            for ( int i = 0; i < Palettes.Length; i++ )
            {
                ColorPalette pal = b.Palette;
                Palette.FixupColorPalette( pal, Palettes, i, 0 );
                b.Palette = pal;
                b.Save( string.Format( "{0}.png", i ), ImageFormat.Png );
            }


            for ( int i = 0; i < 4; i++ )
            {
                ProcessPage( i, bytes.Sub( pageSize * i, pageSize * ( i + 1 ) - 1 ) );

                //for ( int row = 0; row < 5; row++ )
                //{
                //    for ( int col = 0; col < 8; col++ )
                //    {
                //        Palettes[i * row * col] = new Palette( bytes.Sub( ( i + 1 ) * 32 * 48 / 2 * 5 * 8 + ( row * 8 * col * 32 ), ( i + 1 ) * 32 * 48 / 2 * 5 * 8 + ( row * 8 * col * 32 ) + 32 - 1 ) );
                //        using ( Bitmap b = new Bitmap( 32, 48, System.Drawing.Imaging.PixelFormat.Format4bppIndexed ) )
                //        {
                //            BitmapData bmd = b.LockBits( new Rectangle( Point.Empty, b.Size ), ImageLockMode.WriteOnly, PixelFormat.Format4bppIndexed );
                //            for ( int x = 0; x < 32; x++ )
                //            {
                //                for ( int y = 0; y < 48; y++ )
                //                {
                //                }
                //            }
                //            b.UnlockBits( bmd );
                //        }
                //    }
                //}
            }

            Pixels = BuildPixels( bytes.Sub( 0x0000, 0x77FF ) );

            Palettes = new Palette[64];
            for( int i = 0; i < 64; i++ )
            {
                Palettes[i] = new Palette( bytes.Sub( 0x7800 + 32 * i, 0x7800 + 32 * (i + 1) - 1 ) );
            }
        }
예제 #8
0
        public override void SaveImage(System.IO.Stream iso, System.IO.Stream output)
        {
            if (ImportExport8bpp)
            {
                SaveImage8bpp(iso, output);
                return;
            }

            List<byte> imageBytes = GetAllPixelBytes(iso);
            int imageByteIndex = 0;
            bool useHighNibble = false;

            PatcherLib.Iso.KnownPosition newPalettePosition = PalettePosition.AddOffset(CurrentPalette * PalettePosition.Length, 0);
            Palette p = new Palette(newPalettePosition.ReadIso(iso), FFTPatcher.SpriteEditor.Palette.ColorDepth._16bit, true);

            using (Bitmap bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format4bppIndexed))
            {
                System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
                for (int i = 0; i < p.Colors.Length; i++)
                {
                    pal.Entries[i] = p.Colors[i];
                }
                bmp.Palette = pal;

                System.Drawing.Imaging.BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format4bppIndexed);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        byte currentByte = imageBytes[imageByteIndex];
                        int index = useHighNibble ? currentByte.GetUpperNibble() : currentByte.GetLowerNibble();

                        bmd.SetPixel4bpp(x, y, index);

                        useHighNibble = !useHighNibble;
                        imageByteIndex = useHighNibble ? imageByteIndex : (imageByteIndex + 1);
                    }
                }
                bmp.UnlockBits(bmd);

                // Write that shit
                bmp.Save(output, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
예제 #9
0
        /// <summary>
        /// Copies the rectangle to point.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="destinationPoint">The destination point.</param>
        /// <param name="flip">if set to <c>true</c> [flip].</param>
        public static void CopyRectangleToPoint(this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette palette, bool reverseX, bool reverseY)
        {
            // Check if rectangle is out of bounds
            if ((source.Width < (sourceRectangle.Width + sourceRectangle.X)) || (source.Height < (sourceRectangle.Height + sourceRectangle.Y)))
            {
                return;
            }

            BitmapData bmdSource = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            BitmapData bmdDest   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, destination.PixelFormat);

            int        width  = sourceRectangle.Width;
            int        height = sourceRectangle.Height;
            int        x      = destinationPoint.X;
            int        y      = destinationPoint.Y;
            CalcOffset calcX  = reverseX ?
                                (CalcOffset)(col => (width - col - 1)) :
                                (CalcOffset)(col => col);
            CalcOffset calcY = reverseY ?
                               (CalcOffset)(row => (height - row - 1)) :
                               (CalcOffset)(row => row);

            for (int col = 0; col < sourceRectangle.Width; col++)
            {
                for (int row = 0; row < sourceRectangle.Height; row++)
                {
                    int index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                    if (palette.Colors[index % 16].A != 0)
                    {
                        bmdDest.SetPixel8bpp(
                            x + calcX(col),
                            y + calcY(row),
                            index);
                    }
                }
            }

            source.UnlockBits(bmdSource);
            destination.UnlockBits(bmdDest);
        }
예제 #10
0
        public virtual void ImportBitmap4bpp(int paletteIndex, IList <byte> importBytes, IList <byte> originalPaletteBytes)
        {
            using (System.IO.Stream stream = new System.IO.MemoryStream(importBytes.ToArray()))
            {
                using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
                {
                    if (image.PixelFormat != PixelFormat.Format4bppIndexed)
                    {
                        throw new BadImageFormatException("Image is not an 4bpp paletted bitmap!");
                    }
                    if (image.Width != 256)
                    {
                        throw new BadImageFormatException("Image is not 256 pixels wide!");
                    }

                    const int singlePaletteBytes = 32;
                    Palettes = new Palette[16];

                    for (int i = 0; i < 16; i++)
                    {
                        int          startIndex = singlePaletteBytes * i;
                        int          endIndex   = startIndex + singlePaletteBytes - 1;
                        IList <byte> bytes      = originalPaletteBytes.Sub(startIndex, endIndex);
                        Palettes[i] = new Palette(bytes, Palette.ColorDepth._16bit);
                    }
                    for (int i = 0; i < image.Palette.Entries.Length; i++)
                    {
                        Color c = image.Palette.Entries[i];
                        Palettes[paletteIndex][i] = Color.FromArgb(c.R & 0xF8, c.G & 0xF8, c.B & 0xF8);
                        if ((i == 0) && c.ToArgb() == Color.Black.ToArgb())
                        {
                            Palettes[paletteIndex][i] = Color.Transparent;
                        }
                    }

                    Pixels.InitializeElements();

                    int combinedWidth = (image.Width + 1) / 2;
                    int stride        = (((Width * 4) + 31) / 32) * 4;

                    byte[] resultData      = new byte[image.Height * stride];
                    int    imageDataOffset = importBytes[10] | (importBytes[11] << 8) | (importBytes[12] << 16) | (importBytes[13] << 24);

                    for (int rowIndex = 0; rowIndex < image.Height; rowIndex++)
                    {
                        for (int colIndex = 0; colIndex < combinedWidth; colIndex++)
                        {
                            int  currentByteIndex = imageDataOffset + (rowIndex * stride) + colIndex;
                            int  pixelIndex       = ((image.Height - rowIndex - 1) * image.Width) + (colIndex * 2);
                            byte currentByte      = importBytes[currentByteIndex];

                            if (pixelIndex < Pixels.Count)
                            {
                                Pixels[pixelIndex] = (byte)((currentByte & 0xF0) >> 4);
                                if ((colIndex < image.Width) && ((pixelIndex + 1) < Pixels.Count))
                                {
                                    Pixels[pixelIndex + 1] = (byte)(currentByte & 0x0F);
                                }
                            }
                        }
                    }
                }
            }

            BitmapDirty = true;
            FirePixelsChanged();
        }
예제 #11
0
        /// <summary>
        /// Imports a bitmap and tries to convert it to a FFT sprite.
        /// </summary>
        public virtual void ImportBitmap( Bitmap bmp, out bool foundBadPixels )
        {
            foundBadPixels = false;

            if( bmp.PixelFormat != PixelFormat.Format8bppIndexed )
            {
                throw new BadImageFormatException();
            }
            if( bmp.Width != 256 )
            {
                throw new BadImageFormatException();
            }

            Palettes = new Palette[16];
            for( int i = 0; i < 16; i++ )
            {
                Palettes[i] = new Palette( bmp.Palette.Entries.Sub( 16 * i, 16 * (i + 1) - 1 ) );
            }

            Pixels.InitializeElements();

            BitmapData bmd = bmp.LockBits( new Rectangle( 0, 0, bmp.Width, bmp.Height ), ImageLockMode.ReadWrite, bmp.PixelFormat );
            for( int i = 0; (i < Pixels.Count) && (i / 256 < bmp.Height); i++ )
            {
                Pixels[i] = (byte)bmd.GetPixel( i % 256, i / 256 );
                if( Pixels[i] >= 16 )
                {
                    foundBadPixels = true;
                }
            }

            bmp.UnlockBits( bmd );

            ThumbnailDirty = true;
            BitmapDirty = true;

            FirePixelsChanged();
        }
예제 #12
0
        protected override System.Drawing.Bitmap GetImageFromIsoInner( System.IO.Stream iso )
        {
            Palette p = new Palette( palettePosition.ReadIso( iso ), depth );
            //IList<byte> bytes = position.ReadIso( iso );
            IList<byte> bytes = GetIsoBytes(iso);

            Bitmap result = new Bitmap( Width, Height );

            for (int i = 0; i < Width * Height; i++)
            {
                result.SetPixel( i % Width, i / Width, p[bytes[i]] );
            }

            return result;
        }
예제 #13
0
        //public void SaveImageSpecific( System.IO.Stream iso, System.IO.Stream output, Bitmap originalImage = null, bool isSource4bpp = false )
        public void SaveImageSpecific(System.IO.Stream iso, System.IO.Stream output, bool isSource4bpp = false, IList<byte> imageBytes = null)
        {
            //imageBytes = imageBytes ?? position.ReadIso(iso);
            imageBytes = imageBytes ?? GetIsoBytes(iso);

            if (isSource4bpp)
            {
                List<byte> newImageBytes = new List<byte>();
                foreach (byte imageByte in imageBytes)
                {
                    newImageBytes.Add((byte)(imageByte & 0x0F));
                    newImageBytes.Add((byte)((imageByte & 0xF0) >> 4));
                }
                imageBytes = newImageBytes;
            }

            // Get colors
            //Set<Color> colors = GetColors( iso );
            PatcherLib.Iso.KnownPosition newPalettePosition = palettePosition.AddOffset(CurrentPalette * palettePosition.Length, 0);
            Palette p = new Palette(newPalettePosition.ReadIso(iso), depth, true);

            // Convert colors to indices
            //if (originalImage == null)
            //    originalImage = GetImageFromIso(iso);

            using ( Bitmap bmp = new Bitmap( Width, Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed ) )
            {
                ColorPalette pal = bmp.Palette;
                for (int i = 0; i < p.Colors.Length; i++)
                {
                    pal.Entries[i] = p.Colors[i];
                }
                bmp.Palette = pal;

                var bmd = bmp.LockBits( new Rectangle( 0, 0, Width, Height ), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed );
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        bmd.SetPixel8bpp(x, y, imageBytes[(y * Width) + x]);
                    }
                }
                bmp.UnlockBits( bmd );

                // Write that shit
                //bmp.Save( output, System.Drawing.Imaging.ImageFormat.Gif );
                bmp.Save(output, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
예제 #14
0
        protected override System.Drawing.Bitmap GetImageFromIsoInner( System.IO.Stream iso )
        {
            PatcherLib.Iso.KnownPosition newPalettePosition = palettePosition.AddOffset(CurrentPalette * palettePosition.Length, 0);
            Palette p = new Palette(newPalettePosition.ReadIso(iso), depth);

            byte[] bytes = GetIsoBytes( iso );
            List<byte> splitBytes = new List<byte>( bytes.Length * 2 );
            foreach (byte b in bytes.Sub( 0, Height * Width / 2 - 1 ))
            {
                splitBytes.Add( b.GetLowerNibble() );
                splitBytes.Add( b.GetUpperNibble() );
            }

            Bitmap result = new Bitmap( Width, Height );

            for (int i = 0; i < Width * Height; i++)
            {
                result.SetPixel( i % Width, i / Width, p[splitBytes[i]] );
            }

            return result;
        }
예제 #15
0
        /// <summary>
        /// Creates a palette from a portion of a PAL file.
        /// </summary>
        public static Palette FromPALFiledata( IList<byte> bytes )
        {
            Palette result = new Palette();
            result.Colors = new Color[16];

            for( int i = 0; i < 16 * 4; i += 4 )
            {
                result.Colors[i / 4] = Color.FromArgb( bytes[i] & 0xF8, bytes[i + 1] & 0xF8, bytes[i + 2] & 0xF8 );
            }

            if( result.Colors[0].ToArgb() == 0 )
            {
                result.Colors[0] = Color.Transparent;
            }

            return result;
        }
예제 #16
0
        public void SaveImageSpecific(System.IO.Stream iso, System.IO.Stream output, bool isSource4bpp = false)
        {
            IList<byte> imageBytes = GetAllPixelBytes(iso);
            if (isSource4bpp)
            {
                List<byte> newImageBytes = new List<byte>();
                foreach (byte imageByte in imageBytes)
                {
                    newImageBytes.Add((byte)(imageByte & 0x0F));
                    newImageBytes.Add((byte)((imageByte & 0xF0) >> 4));
                }
                imageBytes = newImageBytes;
            }

            // Get colors
            Palette p = new Palette(PalettePosition.ReadIso(iso), FFTPatcher.SpriteEditor.Palette.ColorDepth._16bit, true);

            // Convert colors to indices
            using (Bitmap bmp = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed))
            {
                System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
                for (int i = 0; i < p.Colors.Length; i++)
                {
                    pal.Entries[i] = p.Colors[i];
                }
                bmp.Palette = pal;

                var bmd = bmp.LockBits(new Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        bmd.SetPixel8bpp(x, y, imageBytes[(y * Width) + x]);
                    }
                }
                bmp.UnlockBits(bmd);

                // Write that shit
                bmp.Save(output, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
예제 #17
0
        public static void CopyRectangleToPointNonIndexed(this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette sourcePalette, bool flip)
        {
            BitmapData bmdSource = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            BitmapData bmdDest   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, destination.PixelFormat);

            if (flip)
            {
                for (int col = 0; col < sourceRectangle.Width; col++)
                {
                    for (int row = 0; row < sourceRectangle.Height; row++)
                    {
                        int   index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                        Color c     = sourcePalette.Colors[index % 16];
                        if (c.A != 0)
                        {
                            bmdDest.SetPixel32bpp(destinationPoint.X + (sourceRectangle.Width - col - 1), destinationPoint.Y + row, c);
                        }
                    }
                }
            }
            else
            {
                for (int col = 0; col < sourceRectangle.Width; col++)
                {
                    for (int row = 0; row < sourceRectangle.Height; row++)
                    {
                        int   index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                        Color c     = sourcePalette.Colors[index % 16];

                        if (c.A != 0)
                        {
                            bmdDest.SetPixel32bpp(destinationPoint.X + col, destinationPoint.Y + row, c);
                        }
                    }
                }
            }
            source.UnlockBits(bmdSource);
            destination.UnlockBits(bmdDest);
        }
예제 #18
0
        public override void SaveImage(System.IO.Stream iso, System.IO.Stream output)
        {
            List <List <byte> > quadrantBytes = new List <List <byte> >();

            for (int i = 0; i < 4; i++)
            {
                quadrantBytes.Add(new List <byte>());
                foreach (PatcherLib.Iso.PsxIso.KnownPosition pos in isoPositions[i])
                {
                    quadrantBytes[i].AddRange(pos.ReadIso(iso));
                }
            }

            List <byte> totalBytes = new List <byte>();

            for (int rowIndex = 0; rowIndex < 240; rowIndex++)
            {
                int byteIndex0 = rowIndex * 240;
                int byteIndex1 = rowIndex * 256;
                totalBytes.AddRange(quadrantBytes[0].Sub(byteIndex0, byteIndex0 + 239));
                totalBytes.AddRange(quadrantBytes[1].Sub(byteIndex1, byteIndex1 + 255));
            }
            for (int rowIndex = 0; rowIndex < 128; rowIndex++)
            {
                int byteIndex2 = rowIndex * 240;
                int byteIndex3 = rowIndex * 256;
                totalBytes.AddRange(quadrantBytes[2].Sub(byteIndex2, byteIndex2 + 239));
                totalBytes.AddRange(quadrantBytes[3].Sub(byteIndex3, byteIndex3 + 255));
            }
            byte[] imageBytes = totalBytes.ToArray();

            // Get colors
            Palette p = new Palette(palettePositions[0].ReadIso(iso), FFTPatcher.SpriteEditor.Palette.ColorDepth._16bit, true);

            // Convert colors to indices
            System.Drawing.Bitmap originalImage = GetImageFromIso(iso);

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed))
            {
                System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
                for (int i = 0; i < p.Colors.Length; i++)
                {
                    pal.Entries[i] = p.Colors[i];
                }
                bmp.Palette = pal;

                var bmd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        bmd.SetPixel8bpp(x, y, imageBytes[(y * Width) + x]);
                    }
                }
                bmp.UnlockBits(bmd);

                // Write that shit
                //bmp.Save( output, System.Drawing.Imaging.ImageFormat.Gif );
                bmp.Save(output, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
예제 #19
0
        protected override System.Drawing.Bitmap GetImageFromIsoInner( System.IO.Stream iso )
        {
            List<byte> pixels = GetAllPixelBytes( iso );
            //var palette = GetPalette( iso );

            PatcherLib.Iso.KnownPosition newPalettePosition = PalettePosition.AddOffset(CurrentPalette * PalettePosition.Length, 0);
            Palette palette = new Palette(newPalettePosition.ReadIso(iso), FFTPatcher.SpriteEditor.Palette.ColorDepth._16bit, true);

            Bitmap result = new Bitmap( Width, Height );

            var mypixels = new List<byte>();
            foreach ( byte p in pixels )
            {
                mypixels.Add( p.GetLowerNibble() );
                mypixels.Add( p.GetUpperNibble() );
            }
            pixels = mypixels;

            for ( int i = 0; i < Width * Height; i++ )
            {
                result.SetPixel( i % Width, i / Width, palette[pixels[i]] );
            }

            return result;
        }
예제 #20
0
        /// <summary>
        /// Imports a bitmap and tries to convert it to a FFT sprite.
        /// </summary>
        public virtual void ImportBitmap( Bitmap bmp, out bool foundBadPixels )
        {
            foundBadPixels = false;

            if( bmp.PixelFormat != PixelFormat.Format8bppIndexed )
            {
                throw new BadImageFormatException();
            }
            if( bmp.Width != 256 )
            {
                throw new BadImageFormatException();
            }

            Palettes = new Palette[16];

            for( int i = 0; i < 16; i++ )
            {
                Palettes[i] = Palette.EmptyPalette;
            }

            for ( int i = 0; i < bmp.Palette.Entries.Length; i++ )
            {
                Color c = bmp.Palette.Entries[i];
                Palettes[i / 16][i % 16] = Color.FromArgb( c.R & 0xF8, c.G & 0xF8, c.B & 0xF8 );
                if ( i % 16 == 0 && c.ToArgb() == Color.Black.ToArgb() )
                {
                    Palettes[i / 16][i % 16] = Color.Transparent;
                }
            }

            Pixels.InitializeElements();

            BitmapData bmd = bmp.LockBits( new Rectangle( 0, 0, bmp.Width, bmp.Height ), ImageLockMode.ReadWrite, bmp.PixelFormat );
            for( int i = 0; (i < Pixels.Count) && (i / 256 < bmp.Height); i++ )
            {
                Pixels[i] = (byte)bmd.GetPixel( i % 256, i / 256 );
                if( Pixels[i] >= 16 )
                {
                    foundBadPixels = true;
                }
            }

            bmp.UnlockBits( bmd );

            BitmapDirty = true;

            FirePixelsChanged();
        }
예제 #21
0
        private void ProcessPage( int page, IList<byte> bytes )
        {
            int startIndex = page * portraitsPerPage;
            IList<byte> truePixels = GetTruePixels( bytes.Sub( 0, portraitsPerPage * portraitWidth * portraitHeight / 2 ) );

            for ( int i = 0; i < portraitsPerPage; i++ )
            {
                Palettes[page * portraitsPerPage + i] = new Palette(
                    bytes.Sub(
                        i * paletteSize + portraitsPerPage * portraitWidth * portraitHeight / 2,
                        ( i + 1 ) * paletteSize + portraitsPerPage * portraitWidth * portraitHeight / 2 - 1 ) );
            }

            for ( int row = 0; row < rowsPerPage; row++ )
            {
                for ( int col = 0; col < colsPerPage; col++ )
                {
                    Bitmap b = new Bitmap( portraitWidth, portraitHeight, PixelFormat.Format4bppIndexed );
                    ColorPalette p = b.Palette;
                    Palette.FixupColorPalette( p, Palettes, startIndex + row * col, 0 );
                    b.Palette = p;
                    BitmapData bmd = b.LockBits( new Rectangle( Point.Empty, b.Size ), ImageLockMode.WriteOnly, PixelFormat.Format4bppIndexed );

                    int xOffset = col * portraitWidth;
                    int yOffset = row * portraitHeight;

                    for ( int x = 0; x < portraitWidth; x++ )
                    {
                        for ( int y = 0; y < portraitHeight; y++ )
                        {
                            bmd.SetPixel4bpp( x, y, truePixels[xOffset + x + ( yOffset + y ) * portraitWidth * colsPerPage] );
                        }
                    }
                    b.UnlockBits( bmd );
                    Images[startIndex + row * colsPerPage + col] = b;
                    b.Save( string.Format( "{0}.png", startIndex + row * colsPerPage + col ), ImageFormat.Png );
                }
            }

        }
예제 #22
0
        protected static Palette[] BuildPalettes( IList<byte> paletteBytes )
        {
            Palette[] result = new Palette[16];
            for( int i = 0; i < 16; i++ )
            {
                result[i] = new Palette( paletteBytes.Sub( i * 32, (i + 1) * 32 - 1 ) );
            }

            return result;
        }
예제 #23
0
 public PalettedImage8bppSectioned(
     string name, int width, int height, int numPalettes, Palette.ColorDepth depth, PatcherLib.Iso.KnownPosition imagePosition, PatcherLib.Iso.KnownPosition palettePosition, 
     bool ignoreAssert = false)
     : base(name, width, height, numPalettes, depth, imagePosition, palettePosition, ignoreAssert)
 {
 }
        protected override System.Drawing.Bitmap GetImageFromIsoInner( System.IO.Stream iso )
        {
            Palette p = new Palette( palettePosition.ReadIso( iso ), depth );
            IList<byte> bytes = position.ReadIso( iso );
            IList<byte> splitBytes = new List<byte>( bytes.Count * 2 );
            foreach (byte b in bytes.Sub( 0, Height * Width / 2 - 1 ))
            {
                splitBytes.Add( b.GetLowerNibble() );
                splitBytes.Add( b.GetUpperNibble() );
            }

            Bitmap result = new Bitmap( VisiblePortion.Width, VisiblePortion.Height );

            for(int x = 0; x < VisiblePortion.Width; x++)
            {
                for (int y =0 ; y < VisiblePortion.Height; y++)
                {
                    result.SetPixel( x, y, p[splitBytes[(y + VisiblePortion.Y) * FullSize.Width + (x + VisiblePortion.X)]] );
                }
            }

            return result;
        }
예제 #25
0
        public override void SaveImage(System.IO.Stream iso, System.IO.Stream output)
        {
            List<List<byte>> quadrantBytes = new List<List<byte>>();
            for (int i = 0; i < 4; i++)
            {
                quadrantBytes.Add(new List<byte>());
                foreach (PatcherLib.Iso.PsxIso.KnownPosition pos in isoPositions[i])
                {
                    quadrantBytes[i].AddRange(pos.ReadIso(iso));
                }
            }

            List<byte> totalBytes = new List<byte>();
            for (int rowIndex = 0; rowIndex < 240; rowIndex++)
            {
                int byteIndex0 = rowIndex * 240;
                int byteIndex1 = rowIndex * 256;
                totalBytes.AddRange(quadrantBytes[0].Sub(byteIndex0, byteIndex0 + 239));
                totalBytes.AddRange(quadrantBytes[1].Sub(byteIndex1, byteIndex1 + 255));
            }
            for (int rowIndex = 0; rowIndex < 128; rowIndex++)
            {
                int byteIndex2 = rowIndex * 240;
                int byteIndex3 = rowIndex * 256;
                totalBytes.AddRange(quadrantBytes[2].Sub(byteIndex2, byteIndex2 + 239));
                totalBytes.AddRange(quadrantBytes[3].Sub(byteIndex3, byteIndex3 + 255));
            }
            byte[] imageBytes = totalBytes.ToArray();

            // Get colors
            Palette p = new Palette(palettePositions[0].ReadIso(iso), FFTPatcher.SpriteEditor.Palette.ColorDepth._16bit, true);

            // Convert colors to indices
            System.Drawing.Bitmap originalImage = GetImageFromIso(iso);

            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed))
            {
                System.Drawing.Imaging.ColorPalette pal = bmp.Palette;
                for (int i = 0; i < p.Colors.Length; i++)
                {
                    pal.Entries[i] = p.Colors[i];
                }
                bmp.Palette = pal;

                var bmd = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Width, Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        bmd.SetPixel8bpp(x, y, imageBytes[(y * Width) + x]);
                    }
                }
                bmp.UnlockBits(bmd);

                // Write that shit
                //bmp.Save( output, System.Drawing.Imaging.ImageFormat.Gif );
                bmp.Save(output, System.Drawing.Imaging.ImageFormat.Bmp);
            }
        }
        private Bitmap GetFullImageFromIso( System.IO.Stream iso )
        {
            Palette p = new Palette( palettePosition.ReadIso( iso ), depth );
            IList<byte> bytes = position.ReadIso( iso );
            IList<byte> splitBytes = new List<byte>( bytes.Count * 2 );
            foreach (byte b in bytes.Sub( 0, Height * Width / 2 - 1 ))
            {
                splitBytes.Add( b.GetLowerNibble() );
                splitBytes.Add( b.GetUpperNibble() );
            }

            Bitmap bmp = new Bitmap( FullSize.Width, FullSize.Height );
            for (int x = 0; x < FullSize.Width; x++)
            {
                for (int y = 0; y > FullSize.Height; y++)
                {
                    bmp.SetPixel( x, y, p[splitBytes[y * FullSize.Width + x]] );
                }
            }
            return bmp;
        }
예제 #27
0
 private static void WritePixelsToBitmap( Palette palette, IList<byte> pixels, System.Drawing.Bitmap destination )
 {
     int width = destination.Width;
     int height = destination.Height;
     for ( int i = 0; i < pixels.Count; i++ )
     {
         destination.SetPixel( i % width, i / width, palette.Colors[pixels[i]] );
     }
 }
예제 #28
0
        protected IList<byte> GetPaletteBytes( Set<Color> colors, Palette.ColorDepth depth )
        {
            List<byte> result = new List<byte>();
            if ( depth == Palette.ColorDepth._16bit )
            {
                foreach ( Color c in colors )
                {
                    result.AddRange(Palette.ColorToBytes(c));
                }
            }
            else if ( depth == Palette.ColorDepth._32bit )
            {
                foreach ( Color c in colors )
                {
                    result.Add( c.R );
                    result.Add( c.G );
                    result.Add( c.B );
                    result.Add( c.A );
                }
            }

            result.AddRange( new byte[Math.Max( 0, (int)depth * 256 - result.Count )] );
            return result;
        }