ResizePixels() private static method

private static ResizePixels ( Color32 pixels, int width, int height, int newWidth, int newHeight ) : UnityEngine.Color32[]
pixels UnityEngine.Color32
width int
height int
newWidth int
newHeight int
return UnityEngine.Color32[]
Exemplo n.º 1
0
        public static void TGAToTexture(TexInfo Texture, bool mipmaps)
        {
            GameDatabase.TextureInfo texture = Texture.texture;
            TextureConverter.InitImageBuffer();
            FileStream tgaStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read);

            tgaStream.Position = 0;
            tgaStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE);
            tgaStream.Close();

            byte imgType = imageBuffer[2];
            int  width   = imageBuffer[12] | (imageBuffer[13] << 8);
            int  height  = imageBuffer[14] | (imageBuffer[15] << 8);

            if (Texture.loadOriginalFirst)
            {
                Texture.Resize(width, height);
            }
            int           depth     = imageBuffer[16];
            bool          alpha     = depth == 32 ? true : false;
            TextureFormat texFormat = depth == 32 ? TextureFormat.RGBA32 : TextureFormat.RGB24;

            if (texture.isNormalMap)
            {
                texFormat = TextureFormat.ARGB32;
            }
            bool convertToNormalFormat = texture.isNormalMap ? true : false;

            Texture2D tex = texture.texture;

            Color32[] colors = new Color32[width * height];
            int       n      = 18;

            if (imgType == 2)
            {
                for (int i = 0; i < width * height; i++)
                {
                    colors[i].b = imageBuffer[n++];
                    colors[i].g = imageBuffer[n++];
                    colors[i].r = imageBuffer[n++];
                    if (alpha)
                    {
                        colors[i].a = imageBuffer[n++];
                    }
                    else
                    {
                        colors[i].a = 255;
                    }
                    if (convertToNormalFormat)
                    {
                        colors[i].a = colors[i].r;
                        colors[i].r = colors[i].g;
                        colors[i].b = colors[i].g;
                    }
                }
            }
            else if (imgType == 10)
            {
                int i   = 0;
                int run = 0;
                while (i < width * height)
                {
                    run = imageBuffer[n++];
                    if ((run & 0x80) != 0)
                    {
                        run         = (run ^ 0x80) + 1;
                        colors[i].b = imageBuffer[n++];
                        colors[i].g = imageBuffer[n++];
                        colors[i].r = imageBuffer[n++];
                        if (alpha)
                        {
                            colors[i].a = imageBuffer[n++];
                        }
                        else
                        {
                            colors[i].a = 255;
                        }
                        if (convertToNormalFormat)
                        {
                            colors[i].a = colors[i].r;
                            colors[i].r = colors[i].g;
                            colors[i].b = colors[i].g;
                        }
                        i++;
                        for (int c = 1; c < run; c++, i++)
                        {
                            colors[i] = colors[i - 1];
                        }
                    }
                    else
                    {
                        run += 1;
                        for (int c = 0; c < run; c++, i++)
                        {
                            colors[i].b = imageBuffer[n++];
                            colors[i].g = imageBuffer[n++];
                            colors[i].r = imageBuffer[n++];
                            if (alpha)
                            {
                                colors[i].a = imageBuffer[n++];
                            }
                            else
                            {
                                colors[i].a = 255;
                            }
                            if (convertToNormalFormat)
                            {
                                colors[i].a = colors[i].r;
                                colors[i].r = colors[i].g;
                                colors[i].b = colors[i].g;
                            }
                        }
                    }
                }
            }
            else
            {
                ActiveTextureManagement.DBGLog("TGA format is not supported!");
            }


            if (Texture.needsResize)
            {
                colors = TextureConverter.ResizePixels(colors, width, height, Texture.resizeWidth, Texture.resizeHeight);
                width  = Texture.resizeWidth;
                height = Texture.resizeHeight;
            }
            tex.Resize((int)width, (int)height, texFormat, mipmaps);
            tex.SetPixels32(colors);
            tex.Apply(mipmaps, false);
        }
Exemplo n.º 2
0
        public static void MBMToTexture(TexInfo Texture, bool mipmaps)
        {
            GameDatabase.TextureInfo texture = Texture.texture;
            TextureConverter.InitImageBuffer();
            FileStream mbmStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read);

            mbmStream.Position = 4;

            uint width = 0, height = 0;

            for (int b = 0; b < 4; b++)
            {
                width >>= 8;
                width  |= (uint)(mbmStream.ReadByte() << 24);
            }
            for (int b = 0; b < 4; b++)
            {
                height >>= 8;
                height  |= (uint)(mbmStream.ReadByte() << 24);
            }
            mbmStream.Position = 12;
            bool convertToNormalFormat = false;

            if (mbmStream.ReadByte() == 1)
            {
                texture.isNormalMap = true;
            }
            else
            {
                convertToNormalFormat = texture.isNormalMap ? true : false;
            }

            mbmStream.Position = 16;
            int format = mbmStream.ReadByte();

            mbmStream.Position += 3;

            int           imageSize = (int)(width * height * 3);
            TextureFormat texformat = TextureFormat.RGB24;
            bool          alpha     = false;

            if (format == 32)
            {
                imageSize += (int)(width * height);
                texformat  = TextureFormat.ARGB32;
                alpha      = true;
            }
            if (texture.isNormalMap)
            {
                texformat = TextureFormat.ARGB32;
            }

            mbmStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE);
            mbmStream.Close();

            Texture2D tex = texture.texture;

            Color32[] colors = new Color32[width * height];
            int       n      = 0;

            for (int i = 0; i < width * height; i++)
            {
                colors[i].r = imageBuffer[n++];
                colors[i].g = imageBuffer[n++];
                colors[i].b = imageBuffer[n++];
                if (alpha)
                {
                    colors[i].a = imageBuffer[n++];
                }
                else
                {
                    colors[i].a = 255;
                }
                if (convertToNormalFormat)
                {
                    colors[i].a = colors[i].r;
                    colors[i].r = colors[i].g;
                    colors[i].b = colors[i].g;
                }
            }

            if (Texture.loadOriginalFirst)
            {
                Texture.Resize((int)width, (int)height);
            }

            if (Texture.needsResize)
            {
                colors = TextureConverter.ResizePixels(colors, (int)width, (int)height, Texture.resizeWidth, Texture.resizeHeight);
                width  = (uint)Texture.resizeWidth;
                height = (uint)Texture.resizeHeight;
            }
            tex.Resize((int)width, (int)height, texformat, mipmaps);
            tex.SetPixels32(colors);
            tex.Apply(mipmaps, false);
        }