コード例 #1
0
        public virtual FileMap EncodeTEX0Texture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

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

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + 0x40);

            try
            {
                //Build TEX header
                TEX0v1 *header = (TEX0v1 *)fileView.Address;
                *       header = new TEX0v1(w, h, RawFormat, mipLevels);

                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(header->PixelData, dib, src, dStep, sStep, i);
                    }

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #2
0
        public UnsafeBuffer GeneratePreview(Bitmap bmp)
        {
            //_blockCache.Clear();
            int w = bmp.Width, h = bmp.Height;
            int aw = w.Align(BlockWidth), ah = h.Align(BlockHeight);

            UnsafeBuffer buffer = new UnsafeBuffer((aw / 4) * (ah / 4) * 8);
            CMPRBlock *  bPtr   = (CMPRBlock *)buffer.Address;

            //using (Bitmap bmp = src.Clone(new Rectangle(0, 0, aw, ah), PixelFormat.Format32bppArgb))
            //{
            //    BitmapData data = bmp.LockBits(new Rectangle(0,0,aw,ah), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            //    for (int y1 = 0; y1 < ah; y1 += 8)
            //        for (int x1 = 0; x1 < aw; x1 += 8)
            //            for (int y = 0; y < 8; y += 4)
            //                for (int x = 0; x < 8; x += 4)
            //                {
            //                    ARGBPixel* ptr = (ARGBPixel*)dib.Scan0 + (((y1 + y) * aw) + (x1 + x));
            //                    CMPBlock block = CMPBlock.Encode(ptr, aw, false);
            //                    _blockCache.Add(block);
            //                    block.Decode(ptr, aw);
            //                }
            //}

            using (DIB dib = DIB.FromBitmap(bmp, BlockWidth, BlockHeight, PixelFormat.Format32bppArgb))
            {
                ARGBPixel *img = (ARGBPixel *)dib.Scan0;
                for (int y1 = 0; y1 < ah; y1 += 8)
                {
                    for (int x1 = 0; x1 < aw; x1 += 8)
                    {
                        for (int y = 0; y < 8; y += 4)
                        {
                            for (int x = 0; x < 8; x += 4)
                            {
                                *bPtr = NVDXT.compressDXT1a(img, x1 + x, y1 + y, aw, ah);
                                bPtr->Decode(img, x1 + x, y1 + y, aw, ah);
                                bPtr++;
                            }
                        }
                    }
                }

                dib.WriteBitmap(bmp, w, h);
            }

            return(buffer);
        }
コード例 #3
0
        public virtual FileMap EncodeTPLTexture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

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

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + TPLTextureHeader.Size);

            try
            {
                //Build TPL header
                TPLTextureHeader *tex = (TPLTextureHeader *)fileView.Address;
                tex->_wrapS      = 0;
                tex->_wrapT      = 0;
                tex->_minFilter  = 1;
                tex->_magFilter  = 1;
                tex->_minLOD     = 0;
                tex->_maxLOD     = (byte)(mipLevels - 1);
                tex->PixelFormat = RawFormat;
                tex->_width      = (ushort)w;
                tex->_height     = (ushort)h;
                tex->_data       = TPLTextureHeader.Size;

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

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

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #4
0
        public virtual FileMap EncodeTexture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;
            //int aw = w.Align(bw), ah = h.Align(bh);

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

            //int fileSize = GetMipOffset(w, h, mipLevels + 1) + 0x40;
            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels, false));

            //FileMap fileView = FileMap.FromTempFile(fileSize);
            try
            {
                //Build TEX header
                TEX0 *header = (TEX0 *)fileView.Address;
                *     header = new TEX0(w, h, RawFormat, mipLevels);

                int     sStep    = bw * Image.GetPixelFormatSize(fmt) / 8;
                int     dStep    = bw * bh * BitsPerPixel / 8;
                VoidPtr baseAddr = header->PixelData;

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

                return(fileView);
            }
            catch (Exception x)
            {
                //MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
        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);
            }
        }