Example #1
0
        public static void Clamp(this ColorPalette pal, WiiPaletteFormat format)
        {
            switch (format)
            {
            case WiiPaletteFormat.IA8:
            {
                for (int i = 0; i < pal.Entries.Length; i++)
                {
                    pal.Entries[i] = (Color)(IA8Pixel)pal.Entries[i];
                }

                break;
            }

            case WiiPaletteFormat.RGB565:
            {
                for (int i = 0; i < pal.Entries.Length; i++)
                {
                    pal.Entries[i] = (Color)(wRGB565Pixel)pal.Entries[i];
                }

                break;
            }

            case WiiPaletteFormat.RGB5A3:
            {
                for (int i = 0; i < pal.Entries.Length; i++)
                {
                    pal.Entries[i] = (Color)(wRGB5A3Pixel)pal.Entries[i];
                }

                break;
            }
            }
        }
Example #2
0
 public virtual FileMap EncodeTPLTextureIndexed(Bitmap src, int numColors, WiiPaletteFormat format,
                                                QuantizationAlgorithm algorithm, out FileMap paletteFile)
 {
     using (Bitmap indexed = src.Quantize(algorithm, numColors, RawFormat, format, null))
     {
         return(EncodeTPLTextureIndexed(indexed, 1, format, out paletteFile));
     }
 }
Example #3
0
 public virtual FileMap EncodeREFTTextureIndexed(Bitmap src, int mipLevels, int numColors,
                                                 WiiPaletteFormat format, QuantizationAlgorithm algorithm)
 {
     using (Bitmap indexed = src.Quantize(algorithm, numColors, RawFormat, format, null))
     {
         return(EncodeREFTTextureIndexed(indexed, mipLevels, format));
     }
 }
Example #4
0
        public virtual FileMap EncodeREFTTextureIndexed(Bitmap src, int mipLevels, WiiPaletteFormat format)
        {
            if (!src.IsIndexed())
            {
                throw new ArgumentException("Source image must be indexed.");
            }

            return(EncodeREFTTexture(src, mipLevels, format));
        }
Example #5
0
        public virtual FileMap EncodeTPLTextureIndexed(Bitmap src, int mipLevels, WiiPaletteFormat format, out FileMap paletteFile)
        {
            if (!src.IsIndexed())
            {
                throw new ArgumentException("Source image must be indexed.");
            }

            FileMap texMap = EncodeTPLTexture(src, mipLevels);

            paletteFile = EncodeTPLPalette(src.Palette, format);
            return(texMap);
        }
Example #6
0
        public PLT0v1(int length, WiiPaletteFormat format)
        {
            _bresEntry._tag        = Tag;
            _bresEntry._size       = (length * 2) + Size;
            _bresEntry._version    = 1;
            _bresEntry._bresOffset = 0;

            _headerLen      = Size;
            _stringOffset   = 0;
            _pixelFormat    = (uint)format;
            _numEntries     = (short)length;
            _pad            = 0;
            _origPathOffset = 0;
        }
Example #7
0
        public PLT0v1(int length, WiiPaletteFormat format)
        {
            _bresEntry._tag = Tag;
            _bresEntry._size = (length * 2) + Size;
            _bresEntry._version = 1;
            _bresEntry._bresOffset = 0;

            _headerLen = 0x40;
            _stringOffset = 0;
            _pixelFormat = (uint)format;
            _numEntries = (short)length;
            _pad = 0;
            _origPathOffset = 0;
        }
Example #8
0
        protected override bool OnInitialize()
        {
            base.OnInitialize();

            _unk       = Header->_unknown;
            _format    = (WiiPixelFormat)Header->_format;
            _pltFormat = (WiiPaletteFormat)Header->_pltFormat;
            numColors  = Header->_colorCount;
            _imgLen    = (int)Header->_imagelen;
            _width     = Header->_width;
            _height    = Header->_height;

            return(false);
        }
Example #9
0
        protected override bool OnInitialize()
        {
            base.OnInitialize();

            _palette = null;

            if ((_name == null) && (Header->_stringOffset != 0))
            {
                _name = Header->ResourceString;
            }

            //_numColors = Header->_numEntries;
            _format = Header->PaletteFormat;

            return(false);
        }
Example #10
0
        private MedianCut(Bitmap bmp, WiiPixelFormat texFormat, WiiPaletteFormat palFormat)
        {
            //Set output format
            if (texFormat == WiiPixelFormat.CI4)
            {
                _outFormat = PixelFormat.Format4bppIndexed;
            }
            else if (texFormat == WiiPixelFormat.CI8)
            {
                _outFormat = PixelFormat.Format8bppIndexed;
            }
            else
            {
                throw new ArgumentException("Invalid pixel format.");
            }

            //Set conversion functions
            if (palFormat == WiiPaletteFormat.IA8)
            {
                _idFunc = IA8Handler;
                _idConv = IA8Converter;
            }
            else if (palFormat == WiiPaletteFormat.RGB565)
            {
                _idFunc = RGB565Handler;
                _idConv = RGB565Converter;
            }
            else
            {
                _idFunc = RGB5A3Handler;
                _idConv = RGB5A3Converter;
            }

            //Lock/set source data
            _srcBmp  = bmp;
            _width   = bmp.Width;
            _height  = bmp.Height;
            _size    = _width * _height;
            _srcData = bmp.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly,
                                    PixelFormat.Format32bppArgb);
            _srcPixels = (ARGBPixel *)_srcData.Scan0;

            //Create buffers
            _boxes      = (ColorBox *)Marshal.AllocHGlobal(256 * sizeof(ColorBox));
            _groupTable = (ColorEntry **)Marshal.AllocHGlobal(65536 * sizeof(void *));
        }
Example #11
0
        public override bool OnInitialize()
        {
            base.OnInitialize();

            _unk       = Header->_unknown;
            _format    = (WiiPixelFormat)Header->_format;
            _pltFormat = (WiiPaletteFormat)Header->_pltFormat;
            numColors  = Header->_colorCount;
            _imgLen    = (int)Header->_imagelen;
            _width     = Header->_width;
            _height    = Header->_height;
            _pltLen    = (int)Header->_pltSize;
            _lod       = Header->_mipmap + 1;
            _minFltr   = Header->_min_filt;
            _magFltr   = Header->_mag_filt;

            return(false);
        }
Example #12
0
        public static FileMap EncodePalette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2));

            try
            {
                EncodePalette(fileView.Address, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
Example #13
0
        public PLT0(int length, WiiPaletteFormat format)
        {
            _bresEntry._tag        = Tag;
            _bresEntry._size       = (length * 2) + Size;
            _bresEntry._version    = 1;
            _bresEntry._bresOffset = 0;

            _headerLen    = 0x40;
            _stringOffset = 0;
            _pixelFormat  = (uint)format;
            _numEntries   = (short)length;
            _unk          = 0;

            fixed(uint *p = _padding)
            for (int i = 0; i < 8; i++)
            {
                p[i] = 0;
            }
        }
Example #14
0
        public static ColorPalette DecodePalette(VoidPtr address, int count, WiiPaletteFormat format)
        {
            ColorPalette pal = ColorPaletteExtension.CreatePalette(ColorPaletteFlags.HasAlpha, count);

            switch (format)
            {
            case WiiPaletteFormat.IA8:
            {
                IA8Pixel *sPtr = (IA8Pixel *)address;
                for (int i = 0; i < count; i++)
                {
                    pal.Entries[i] = (Color)sPtr[i];
                }

                break;
            }

            case WiiPaletteFormat.RGB565:
            {
                wRGB565Pixel *sPtr = (wRGB565Pixel *)address;
                for (int i = 0; i < count; i++)
                {
                    pal.Entries[i] = (Color)sPtr[i];
                }

                break;
            }

            case WiiPaletteFormat.RGB5A3:
            {
                wRGB5A3Pixel *sPtr = (wRGB5A3Pixel *)address;
                for (int i = 0; i < count; i++)
                {
                    pal.Entries[i] = (Color)(ARGBPixel)sPtr[i];
                }

                break;
            }
            }

            return(pal);
        }
Example #15
0
        public override bool OnInitialize()
        {
            base.OnInitialize();

            _palette = null;

            if ((_name == null) && (Header1->_stringOffset != 0))
            {
                _name = Header1->ResourceString;
            }

            _format = Header1->PaletteFormat;

            if (_version == 3)
            {
                (_userEntries = new UserDataCollection()).Read(Header3->UserData);
            }

            return(false);
        }
        public static void EncodePalette(VoidPtr destAddr, ColorPalette pal, WiiPaletteFormat format)
        {
            int count = pal.Entries.Length;

            PLT0 *header = (PLT0 *)destAddr;

            *header = new PLT0(count, format);

            switch (format)
            {
            case WiiPaletteFormat.IA8:
            {
                IA8Pixel *dPtr = (IA8Pixel *)header->PaletteData;
                for (int i = 0; i < count; i++)
                {
                    dPtr[i] = (IA8Pixel)pal.Entries[i];
                }
                break;
            }

            case WiiPaletteFormat.RGB565:
            {
                wRGB565Pixel *dPtr = (wRGB565Pixel *)header->PaletteData;
                for (int i = 0; i < count; i++)
                {
                    dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                }
                break;
            }

            case WiiPaletteFormat.RGB5A3:
            {
                wRGB5A3Pixel *dPtr = (wRGB5A3Pixel *)header->PaletteData;
                for (int i = 0; i < count; i++)
                {
                    dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                }
                break;
            }
            }
        }
Example #17
0
        public static FileMap EncodePLT0Palette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0x40);

            try
            {
                PLT0v1 *header = (PLT0v1 *)fileView.Address;
                *       header = new PLT0v1(pal.Entries.Length, format);

                EncodePalette(fileView.Address + 0x40, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
        private MedianCut(Bitmap bmp, WiiPixelFormat texFormat, WiiPaletteFormat palFormat)
        {
            //Set output format
            if (texFormat == WiiPixelFormat.CI4)
                _outFormat = PixelFormat.Format4bppIndexed;
            else if (texFormat == WiiPixelFormat.CI8)
                _outFormat = PixelFormat.Format8bppIndexed;
            else
                throw new ArgumentException("Invalid pixel format.");

            //Set conversion functions
            if (palFormat == WiiPaletteFormat.IA8)
            {
                _idFunc = IA8Handler;
                _idConv = IA8Converter;
            }
            else if (palFormat == WiiPaletteFormat.RGB565)
            {
                _idFunc = RGB565Handler;
                _idConv = RGB565Converter;
            }
            else
            {
                _idFunc = RGB5A3Handler;
                _idConv = RGB5A3Converter;
            }

            //Lock/set source data
            _srcBmp = bmp;
            _width = bmp.Width;
            _height = bmp.Height;
            _size = _width * _height;
            _srcData = bmp.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            _srcPixels = (ARGBPixel*)_srcData.Scan0;

            //Create buffers
            _boxes = (ColorBox*)Marshal.AllocHGlobal(256 * sizeof(ColorBox));
            _groupTable = (ColorEntry**)Marshal.AllocHGlobal(65536 * 4);
        }
Example #19
0
        public static FileMap EncodeTPLPalette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0xC);

            try
            {
                TPLPaletteHeader *header = (TPLPaletteHeader *)fileView.Address;
                header->_format     = (uint)format;
                header->_numEntries = (ushort)pal.Entries.Length;
                header->_data       = 0xC;

                EncodePalette(fileView.Address + 0xC, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
 public static void Clamp(this ColorPalette pal, WiiPaletteFormat format)
 {
     switch (format)
     {
         case WiiPaletteFormat.IA8:
             {
                 for (int i = 0; i < pal.Entries.Length; i++)
                     pal.Entries[i] = (Color)(IA8Pixel)pal.Entries[i];
                 break;
             }
         case WiiPaletteFormat.RGB565:
             {
                 for (int i = 0; i < pal.Entries.Length; i++)
                     pal.Entries[i] = (Color)(wRGB565Pixel)pal.Entries[i];
                 break;
             }
         case WiiPaletteFormat.RGB5A3:
             {
                 for (int i = 0; i < pal.Entries.Length; i++)
                     pal.Entries[i] = (Color)(wRGB5A3Pixel)pal.Entries[i];
                 break;
             }
     }
 }
        public virtual FileMap EncodeTPLTextureIndexed(Bitmap src, int mipLevels, WiiPaletteFormat format, out FileMap paletteFile)
        {
            if (!src.IsIndexed())
                throw new ArgumentException("Source image must be indexed.");

            FileMap texMap = EncodeTPLTexture(src, mipLevels);
            paletteFile = EncodeTPLPalette(src.Palette, format);
            return texMap;
        }
Example #22
0
        public virtual FileMap EncodeREFTTexture(Bitmap src, int mipLevels, WiiPaletteFormat format)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

            ColorPalette pal = src.Palette;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + 0x20 + (pal != null ? (pal.Entries.Length * 2) : 0));

            try
            {
                //Build REFT image header
                REFTImageHeader *header = (REFTImageHeader *)fileView.Address;
                *header = new REFTImageHeader((ushort)w, (ushort)h, (byte)RawFormat, (byte)format, (ushort)(pal != null ? pal.Entries.Length : 0), (uint)fileView.Length - 0x20 - (uint)(pal != null ? (pal.Entries.Length * 2) : 0), (byte)(mipLevels - 1));

                int sStep = bw * Image.GetPixelFormatSize(fmt) / 8;
                int dStep = bw * bh * BitsPerPixel / 8;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel((VoidPtr)header + 0x20, dib, src, dStep, sStep, i);
                    }

                if (pal != null)
                {
                    int count = pal.Entries.Length;

                    switch (format)
                    {
                    case WiiPaletteFormat.IA8:
                    {
                        IA8Pixel *dPtr = (IA8Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (IA8Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB565:
                    {
                        wRGB565Pixel *dPtr = (wRGB565Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB5A3:
                    {
                        wRGB5A3Pixel *dPtr = (wRGB5A3Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                        }
                        break;
                    }
                    }
                }

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
 public virtual FileMap EncodeREFTTextureIndexed(Bitmap src, int mipLevels, int numColors, WiiPaletteFormat format, QuantizationAlgorithm algorithm)
 {
     using (Bitmap indexed = src.Quantize(algorithm, numColors, RawFormat, format, null))
         return EncodeREFTTextureIndexed(indexed, mipLevels, format);
 }
Example #24
0
        //public static unsafe ColorPalette GeneratePalette(this Bitmap bmp, QuantizationAlgorithm mode, int numColors)
        //{
        //    ColorInformation info = bmp.GetColorInformation();

        //    ColorPalette pal = ColorPaletteExtension.CreatePalette(ColorPaletteFlags.None, numColors);
        //    if (info.UniqueColors.Length <= numColors)
        //    {
        //        //Use original colors
        //        for (int i = 0; i < info.UniqueColors.Length; i++)
        //            pal.Entries[i] = (Color)info.UniqueColors[i];
        //    }
        //    else
        //    {
        //        switch (mode)
        //        {
        //            case QuantizationAlgorithm.WeightedAverage:
        //                {
        //                    pal = WeightedAverage.Process(bmp, numColors);
        //                    break;
        //                }
        //            //case QuantizationAlgorithm.MedianCut:
        //            //    {
        //            //        MedianCut.Quantize(bmp, numColors);
        //            //        break;
        //            //    }
        //        }
        //    }
        //    return pal;
        //}

        public static Bitmap Quantize(this Bitmap bmp, QuantizationAlgorithm algorithm, int numColors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
        {
            return(MedianCut.Quantize(bmp, numColors, texFormat, palFormat, progress));
        }
        public override bool OnInitialize()
        {
            base.OnInitialize();

            _unk = Header->_unknown;
            _format = (WiiPixelFormat)Header->_format;
            _pltFormat = (WiiPaletteFormat)Header->_pltFormat;
            numColors = Header->_colorCount;
            _imgLen = (int)Header->_imagelen;
            _width = Header->_width;
            _height = Header->_height;
            _pltLen = (int)Header->_pltSize;
            _lod = Header->_mipmap + 1;
            _minFltr = Header->_min_filt;
            _magFltr = Header->_mag_filt;

            return false;
        }
        public override bool OnInitialize()
        {
            base.OnInitialize();

            _palette = null;

            if ((_name == null) && (Header1->_stringOffset != 0))
                _name = Header1->ResourceString;

            _version = Header1->_bresEntry._version;
            //_numColors = Header->_numEntries;
            _format = Header1->PaletteFormat;

            if (_version == 3)
                (_userEntries = new UserDataCollection()).Read(Header3->UserData);

            return false;
        }
 public static Bitmap Quantize(Bitmap bmp, int colors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
 {
     using (MedianCut mc = new MedianCut(bmp, texFormat, palFormat))
         return mc.Quantize(colors, progress);
 }
 //public static unsafe ColorPalette GeneratePalette(this Bitmap bmp, QuantizationAlgorithm mode, int numColors)
 //{
 //    ColorInformation info = bmp.GetColorInformation();
 //    ColorPalette pal = ColorPaletteExtension.CreatePalette(ColorPaletteFlags.None, numColors);
 //    if (info.UniqueColors.Length <= numColors)
 //    {
 //        //Use original colors
 //        for (int i = 0; i < info.UniqueColors.Length; i++)
 //            pal.Entries[i] = (Color)info.UniqueColors[i];
 //    }
 //    else
 //    {
 //        switch (mode)
 //        {
 //            case QuantizationAlgorithm.WeightedAverage:
 //                {
 //                    pal = WeightedAverage.Process(bmp, numColors);
 //                    break;
 //                }
 //            //case QuantizationAlgorithm.MedianCut:
 //            //    {
 //            //        MedianCut.Quantize(bmp, numColors);
 //            //        break;
 //            //    }
 //        }
 //    }
 //    return pal;
 //}
 public static Bitmap Quantize(this Bitmap bmp, QuantizationAlgorithm algorithm, int numColors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
 {
     return MedianCut.Quantize(bmp, numColors, texFormat, palFormat, progress);
 }
 public static ColorPalette DecodePalette(VoidPtr address, int count, WiiPaletteFormat format)
 {
     ColorPalette pal = ColorPaletteExtension.CreatePalette(ColorPaletteFlags.HasAlpha, count);
     switch (format)
     {
         case WiiPaletteFormat.IA8:
             {
                 IA8Pixel* sPtr = (IA8Pixel*)address;
                 for (int i = 0; i < count; i++)
                     pal.Entries[i] = (Color)sPtr[i];
                 break;
             }
         case WiiPaletteFormat.RGB565:
             {
                 wRGB565Pixel* sPtr = (wRGB565Pixel*)address;
                 for (int i = 0; i < count; i++)
                     pal.Entries[i] = (Color)sPtr[i];
                 break;
             }
         case WiiPaletteFormat.RGB5A3:
             {
                 wRGB5A3Pixel* sPtr = (wRGB5A3Pixel*)address;
                 for (int i = 0; i < count; i++)
                     pal.Entries[i] = (Color)(ARGBPixel)sPtr[i];
                 break;
             }
     }
     return pal;
 }
        public virtual FileMap EncodeREFTTexture(Bitmap src, int mipLevels, WiiPaletteFormat format)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

            ColorPalette pal = src.Palette;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + 0x20 + (pal != null ? (pal.Entries.Length * 2) : 0));
            try
            {
                //Build REFT image header
                REFTImageHeader* header = (REFTImageHeader*)fileView.Address;
                *header = new REFTImageHeader((ushort)w, (ushort)h, (byte)RawFormat, (byte)format, (ushort)(pal != null ? pal.Entries.Length : 0), (uint)fileView.Length - 0x20 - (uint)(pal != null ? (pal.Entries.Length * 2) : 0), (byte)(mipLevels - 1));

                int sStep = bw * Image.GetPixelFormatSize(fmt) / 8;
                int dStep = bw * bh * BitsPerPixel / 8;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                        EncodeLevel((VoidPtr)header + 0x20, dib, src, dStep, sStep, i);

                if (pal != null)
                {
                    int count = pal.Entries.Length;

                    switch (format)
                    {
                        case WiiPaletteFormat.IA8:
                            {
                                IA8Pixel* dPtr = (IA8Pixel*)header->PaletteData;
                                for (int i = 0; i < count; i++)
                                    dPtr[i] = (IA8Pixel)pal.Entries[i];
                                break;
                            }
                        case WiiPaletteFormat.RGB565:
                            {
                                wRGB565Pixel* dPtr = (wRGB565Pixel*)header->PaletteData;
                                for (int i = 0; i < count; i++)
                                    dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                                break;
                            }
                        case WiiPaletteFormat.RGB5A3:
                            {
                                wRGB5A3Pixel* dPtr = (wRGB5A3Pixel*)header->PaletteData;
                                for (int i = 0; i < count; i++)
                                    dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                                break;
                            }
                    }
                }

                return fileView;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return null;
            }
        }
 public virtual FileMap EncodeTPLTextureIndexed(Bitmap src, int numColors, WiiPaletteFormat format, QuantizationAlgorithm algorithm, out FileMap paletteFile)
 {
     using (Bitmap indexed = src.Quantize(algorithm, numColors, RawFormat, format, null))
         return EncodeTPLTextureIndexed(indexed, 1, format, out paletteFile);
 }
        public static FileMap EncodeTPLPalette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0xC);
            try
            {
                TPLPaletteHeader* header = (TPLPaletteHeader*)fileView.Address;
                header->_format = (uint)format;
                header->_numEntries = (ushort)pal.Entries.Length;
                header->_data = 0xC;

                EncodePalette(fileView.Address + 0xC, pal, format);
                return fileView;
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw x;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
        //public virtual void GeneratePreviewIndexed(Bitmap src, Bitmap dst, int numColors, WiiPaletteFormat format)
        //{
        //    _cachedPalette = src.GeneratePalette(QuantizationAlgorithm.WeightedAverage, numColors);
        //    _paletteFormat = format;
        //    _cachedPalette.Clamp(format);

        //    src.CopyTo(dst);
        //    dst.Clamp(_cachedPalette);
        //}

        //public virtual void GeneratePreview(Bitmap src, Bitmap dst)
        //{
        //    src.CopyTo(dst);
        //    dst.Clamp(RawFormat);
        //}

        public virtual FileMap EncodeTextureIndexed(Bitmap src, int mipLevels, int numColors, WiiPaletteFormat format, QuantizationAlgorithm algorithm, out FileMap paletteFile)
        {
            using (Bitmap indexed = src.Quantize(algorithm, numColors, RawFormat, format, null))
            {
                return(EncodeTextureIndexed(indexed, mipLevels, format, out paletteFile));
            }
            //ColorPalette pal = src.GeneratePalette(algorithm, numColors);
            //pal.Clamp(format);
            //return EncodeTextureIndexed(src, mipLevels, pal, format, out paletteFile);
        }
        public static FileMap EncodePLT0Palette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0x40);
            try
            {
                PLT0v1* header = (PLT0v1*)fileView.Address;
                *header = new PLT0v1(pal.Entries.Length, format);

                EncodePalette(fileView.Address + 0x40, pal, format);
                return fileView;
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw x;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
        public static void EncodePalette(VoidPtr destAddr, ColorPalette pal, WiiPaletteFormat format)
        {
            int count = pal.Entries.Length;

            switch (format)
            {
                case WiiPaletteFormat.IA8:
                    {
                        IA8Pixel* dPtr = (IA8Pixel*)destAddr;
                        for (int i = 0; i < count; i++)
                            dPtr[i] = (IA8Pixel)pal.Entries[i];
                        break;
                    }
                case WiiPaletteFormat.RGB565:
                    {
                        wRGB565Pixel* dPtr = (wRGB565Pixel*)destAddr;
                        for (int i = 0; i < count; i++)
                            dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                        break;
                    }
                case WiiPaletteFormat.RGB5A3:
                    {
                        wRGB5A3Pixel* dPtr = (wRGB5A3Pixel*)destAddr;
                        for (int i = 0; i < count; i++)
                            dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                        break;
                    }
            }
        }
 public static FileMap EncodePalette(ColorPalette pal, WiiPaletteFormat format)
 {
     FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2));
     try
     {
         EncodePalette(fileView.Address, pal, format);
         return fileView;
     }
     catch (Exception x)
     {
         fileView.Dispose();
         throw x;
         //MessageBox.Show(x.ToString());
         //fileView.Dispose();
         //return null;
     }
 }
        public virtual FileMap EncodeREFTTextureIndexed(Bitmap src, int mipLevels, WiiPaletteFormat format)
        {
            if (!src.IsIndexed())
                throw new ArgumentException("Source image must be indexed.");

            return EncodeREFTTexture(src, mipLevels, format);
        }
        public virtual FileMap EncodeTextureIndexed(Bitmap src, int mipLevels, WiiPaletteFormat format, out FileMap paletteFile)
        {
            if (!src.IsIndexed())
            {
                throw new ArgumentException("Source image must be indexed.");
            }

            FileMap texMap = EncodeTexture(src, mipLevels);

            paletteFile = EncodePalette(src.Palette, format);
            return(texMap);

            //int w = src.Width, h = src.Height;
            //int bw = BlockWidth, bh = BlockHeight;
            //int aw = w.Align(bw), ah = h.Align(bh);

            //paletteFile = null;
            //FileMap texFile = FileMap.FromTempFile(GetFileSize(w, h, mipLevels));
            //try
            //{
            //    //Build TEX header
            //    TEX0* header = (TEX0*)texFile.Address;
            //    *header = new TEX0(w, h, RawFormat, mipLevels);

            //    int sStep = bw * Image.GetPixelFormatSize(src.PixelFormat) / 8;
            //    int dStep = bw * bh * BitsPerPixel / 8;
            //    VoidPtr baseAddr = header->PixelData;
            //    using (DIB dib = DIB.FromBitmap(src, bw, bh, src.PixelFormat))
            //    {
            //        for (int i = 1; i <= mipLevels; i++)
            //        {
            //            int mw = w, mh = h;
            //            VoidPtr dAddr = baseAddr;
            //            //int mw = dib.Width, mh = dib.Height, aw = mw.Align(BlockWidth);
            //            //VoidPtr dstAddr = header->PixelData;
            //            if (i != 1)
            //            {
            //                dAddr += GetMipOffset(ref mw, ref mh, i);
            //                using (Bitmap mip = src.GenerateMip(i))
            //                {
            //                    dib.ReadBitmap(mip, mw, mh);
            //                }
            //            }

            //            mw = mw.Align(bw);
            //            mh = mh.Align(bh);

            //            int bStride = mw * BitsPerPixel / 8;
            //            for (int y = 0; y < mh; y += bh)
            //            {
            //                VoidPtr sPtr = (int)dib.Scan0 + (y * dib.Stride);
            //                VoidPtr dPtr = dAddr + (y * bStride);
            //                for (int x = 0; x < mw; x += bw, dPtr += dStep, sPtr += sStep)
            //                    EncodeBlock((ARGBPixel*)sPtr, dPtr, aw);
            //            }
            //        }
            //    }

            //    paletteFile = EncodePalette(src.Palette, format);
            //    return texFile;
            //}
            //catch (Exception x)
            //{
            //    texFile.Dispose();
            //    return null;
            //}
        }
        public virtual FileMap EncodeREFTTexture(Bitmap src, int mipLevels, WiiPaletteFormat format, bool usePalette)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;
            //int aw = w.Align(bw), ah = h.Align(bh);
            ColorPalette pal = src.Palette;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            //int fileSize = GetMipOffset(w, h, mipLevels + 1) + 0x20;
            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels, true) + (usePalette ? (pal.Entries.Length * 2) : 0));

            //FileMap fileView = FileMap.FromTempFile(fileSize);
            try
            {
                //Build REFT image header
                REFTData *header = (REFTData *)fileView.Address;
                *         header = new REFTData((ushort)w, (ushort)h, (byte)RawFormat);
                header->_imagelen = (uint)fileView.Length - 0x20;

                int     sStep    = bw * Image.GetPixelFormatSize(fmt) / 8;
                int     dStep    = bw * bh * BitsPerPixel / 8;
                VoidPtr baseAddr = (byte *)header + 0x20;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel(baseAddr, dib, src, dStep, sStep, i);
                    }

                if (usePalette)
                {
                    int count = pal.Entries.Length;

                    header->_colorCount = (ushort)count;
                    header->_pltFormat  = (byte)format;

                    switch (format)
                    {
                    case WiiPaletteFormat.IA8:
                    {
                        IA8Pixel *dPtr = (IA8Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (IA8Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB565:
                    {
                        wRGB565Pixel *dPtr = (wRGB565Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB5A3:
                    {
                        wRGB5A3Pixel *dPtr = (wRGB5A3Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                        }
                        break;
                    }
                    }
                }

                return(fileView);
            }
            catch (Exception x)
            {
                //MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
 public static Bitmap Quantize(Bitmap bmp, int colors, WiiPixelFormat texFormat, WiiPaletteFormat palFormat, IProgressTracker progress)
 {
     using (MedianCut mc = new MedianCut(bmp, texFormat, palFormat))
         return(mc.Quantize(colors, progress));
 }
        public override bool OnInitialize()
        {
            base.OnInitialize();

            _palette = null;

            _name = "Palette";

            //_numColors = Header->_numEntries;
            _format = Header->PaletteFormat;

            if (_replaced)
                _dataAddr = WorkingUncompressed.Address + Header->_data;

            return false;
        }