Exemple #1
0
        public static int GetStorageRequirements(int width, int height, SquishMethod method)
        {
            int blockcount = ((width + 3) / 4) * ((height + 3) / 4);
            int blocksize  = (method == SquishMethod.Dxt1) ? 8 : 16;

            return(blockcount * blocksize);
        }
Exemple #2
0
        public static byte[] DecompressImage(int width, int height, byte[] blocks, SquishMethod method)
        {
            byte[] rgba = new byte[4 * width * height];

            unsafe
            {
                fixed(byte *pSourceBlockBase = blocks, pRgba = rgba)
                {
                    byte *pSourceBlock  = pSourceBlockBase;
                    int   bytesPerBlock = method == SquishMethod.Dxt1 ? 8 : 16;

                    // loop over blocks
                    for (int y = 0; y < height; y += 4)
                    {
                        for (int x = 0; x < width; x += 4)
                        {
                            // decompress the block
                            byte *pTargetRgba = stackalloc byte[4 * 16];

                            Native.SquishDecompress((IntPtr)pTargetRgba, (IntPtr)pSourceBlock, (int)method);

                            // write the decompressed pixels to the correct image locations
                            byte *pSourcePixel = pTargetRgba;

                            for (int py = 0; py < 4; ++py)
                            {
                                for (int px = 0; px < 4; ++px)
                                {
                                    // get the target location
                                    int sx = x + px;
                                    int sy = y + py;
                                    if (sx < width && sy < height)
                                    {
                                        byte *pTargetPixel = pRgba + 4 * (width * sy + sx);

                                        // copy the rgba value
                                        for (int i = 0; i < 4; ++i)
                                        {
                                            *pTargetPixel++ = *pSourcePixel++;
                                        }
                                    }
                                    else
                                    {
                                        // skip this pixel as its outside the image
                                        pSourcePixel += 4;
                                    }
                                }
                            }

                            // advance
                            pSourceBlock += bytesPerBlock;
                        }
                    }
                }
            }

            return(rgba);
        }
Exemple #3
0
        public static byte[] CompressImage(byte[] rgba, int width, int height, SquishMethod method, SquishFit fit, SquishMetric metric, SquishExtra extra)
        {
            byte[] blocks = new byte[GetStorageRequirements(width, height, method)];

            unsafe
            {
                // initialise the block output
                fixed(byte *pTargetBlockBase = blocks, pRgba = rgba)
                {
                    byte *pTargetBlock  = pTargetBlockBase;
                    int   bytesPerBlock = method == SquishMethod.Dxt1 ? 8 : 16;

                    // loop over blocks
                    for (int y = 0; y < height; y += 4)
                    {
                        for (int x = 0; x < width; x += 4)
                        {
                            byte *pSourceRgba  = stackalloc byte[16 * 4];
                            byte *pTargetPixel = pSourceRgba;
                            int   mask         = 0;

                            for (int py = 0; py < 4; ++py)
                            {
                                for (int px = 0; px < 4; ++px)
                                {
                                    // get the source pixel in the image
                                    int sx = x + px;
                                    int sy = y + py;

                                    // enable if we're in the image
                                    if (sx < width && sy < height)
                                    {
                                        // copy the rgba value
                                        byte *pSourcePixel = pRgba + 4 * (width * sy + sx);

                                        for (int i = 0; i < 4; ++i)
                                        {
                                            *pTargetPixel++ = *pSourcePixel++;
                                        }

                                        // enable this pixel
                                        mask |= (1 << (4 * py + px));
                                    }
                                    else
                                    {
                                        // Skip this pixel as its outside the image
                                        pTargetPixel += 4;
                                    }
                                }
                            }

                            try
                            {
                                Native.SquishCompressMasked((IntPtr)pSourceRgba, mask, (IntPtr)pTargetBlock,
                                                            (int)method | (int)fit | (int)metric | (int)extra);
                            }
                            catch (DllNotFoundException)
                            {
                                throw new DllNotFoundException("Shared library Squish2 not found");
                            }

                            // advance
                            pTargetBlock += bytesPerBlock;
                        }
                    }
                }
            }

            return(blocks);
        }
Exemple #4
0
        public static byte[] CompressImage(byte[] rgba, int width, int height, SquishMethod method, SquishFit fit, SquishMetric metric, SquishExtra extra)
        {
            byte[] blocks = new byte[GetStorageRequirements(width, height, method)];

            unsafe
            {
                // initialise the block output
                fixed (byte* pTargetBlockBase = blocks, pRgba = rgba)
                {
                    byte* pTargetBlock = pTargetBlockBase;
                    int bytesPerBlock = method == SquishMethod.Dxt1 ? 8 : 16;

                    // loop over blocks
                    for( int y = 0; y < height; y += 4 )
                    {
                        for( int x = 0; x < width; x += 4 )
                        {
                            byte* pSourceRgba = stackalloc byte[16 * 4];
                            byte* pTargetPixel = pSourceRgba;
                            int mask = 0;

                            for( int py = 0; py < 4; ++py )
                            {
                                for( int px = 0; px < 4; ++px )
                                {
                                    // get the source pixel in the image
                                    int sx = x + px;
                                    int sy = y + py;

                                    // enable if we're in the image
                                    if( sx < width && sy < height )
                                    {
                                        // copy the rgba value
                                        byte* pSourcePixel = pRgba + 4 * (width * sy + sx);

                                        for (int i = 0; i < 4; ++i)
                                            *pTargetPixel++ = *pSourcePixel++;

                                        // enable this pixel
                                        mask |= (1 << (4 * py + px));
                                    }
                                    else
                                    {
                                        // Skip this pixel as its outside the image
                                        pTargetPixel += 4;
                                    }
                                }
                            }

                            try
                            {
                                Native.SquishCompressMasked((IntPtr)pSourceRgba, mask, (IntPtr)pTargetBlock,
                                    (int)method | (int)fit | (int)metric | (int)extra);
                            }
                            catch (DllNotFoundException)
                            {
                                throw new DllNotFoundException("Shared library Squish2 not found");
                            }

                            // advance
                            pTargetBlock += bytesPerBlock;
                        }
                    }
                }
            }

            return blocks;
        }
Exemple #5
0
        public static int GetStorageRequirements(int width, int height, SquishMethod method)
        {
            int blockcount = ((width + 3) / 4 ) * ((height + 3) / 4);
            int blocksize = (method == SquishMethod.Dxt1) ? 8 : 16;

            return blockcount * blocksize;
        }
Exemple #6
0
        public static byte[] DecompressImage(int width, int height, byte[] blocks, SquishMethod method)
        {
            byte[] rgba = new byte[4 * width * height];

            unsafe
            {
                fixed (byte* pSourceBlockBase = blocks, pRgba = rgba)
                {
                    byte* pSourceBlock = pSourceBlockBase;
                    int bytesPerBlock = method == SquishMethod.Dxt1 ? 8 : 16;

                    // loop over blocks
                    for (int y = 0; y < height; y += 4)
                    {
                        for (int x = 0; x < width; x += 4 )
                        {
                            // decompress the block
                            byte* pTargetRgba = stackalloc byte[4 * 16];

                            Native.SquishDecompress((IntPtr)pTargetRgba, (IntPtr)pSourceBlock, (int)method);

                            // write the decompressed pixels to the correct image locations
                            byte* pSourcePixel = pTargetRgba;

                            for( int py = 0; py < 4; ++py )
                            {
                                for( int px = 0; px < 4; ++px )
                                {
                                    // get the target location
                                    int sx = x + px;
                                    int sy = y + py;
                                    if( sx < width && sy < height )
                                    {
                                        byte* pTargetPixel = pRgba + 4*(width * sy + sx);

                                        // copy the rgba value
                                        for (int i = 0; i < 4; ++i)
                                            *pTargetPixel++ = *pSourcePixel++;
                                    }
                                    else
                                    {
                                        // skip this pixel as its outside the image
                                        pSourcePixel += 4;
                                    }
                                }
                            }

                            // advance
                            pSourceBlock += bytesPerBlock;
                        }
                    }
                }
            }

            return rgba;
        }