示例#1
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);
        }
示例#2
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;
        }