BiomePainter.RegionUtil.RenderChunkTerrain C# (CSharp) Method

RenderChunkTerrain() private static method

private static RenderChunkTerrain ( Object state ) : void
state Object
return void
        private static void RenderChunkTerrain(Object state)
        {
            Object[] parameters = (Object[])state;

            Chunk c = (Chunk)parameters[0];
            Bitmap b = (Bitmap)parameters[1];
            int offsetX = (int)parameters[2];
            int offsetY = (int)parameters[3];

            TAG_Compound[] sections = new TAG_Compound[16];
            int highest = -1;
            foreach (TAG t in (TAG[])c.Root["Level"]["Sections"])
            {
                byte index = (byte)t["Y"];
                if (index > highest)
                    highest = index;
                sections[index] = (TAG_Compound)t;

            }

            if (c.ManualHeightmap == null)
            {
                c.ManualHeightmap = new int[256];
                for (int i = 0; i < c.ManualHeightmap.Length; i++)
                    c.ManualHeightmap[i] = -1;
            }

            //chunk exists but all blocks are air
            if (highest < 0)
            {
                if (Interlocked.Decrement(ref taskCount) == 0)
                    signal.Set();
                return;
            }

            Color[,] pixels = new Color[16, 16];

            highest = ((highest + 1) * 16) - 1;

            TAG biomes = null;
            c.Root["Level"].TryGetValue("Biomes", out biomes);

            for (int z = 0; z < 16; z++)
            {
                for (int x = 0; x < 16; x++)
                {
                    int y;
                    if (c.ManualHeightmap[x + z * 16] >= 0)
                        y = c.ManualHeightmap[x + z * 16];
                    else
                    {
                        y = GetHeight(sections, x, z, highest);
                        c.ManualHeightmap[x + z * 16] = y;
                    }

                    if (y < 0)
                        continue;
                    byte id, data;
                    GetBlock(sections, x, y, z, out id, out data);
                    byte biome = (byte)Biome.Unspecified;
                    if(biomes != null && Settings.BiomeFoliage)
                        biome = ((byte[])biomes)[x + z * 16];

                    Color color = ColorPalette.Lookup(id, data, biome);

                    if (Settings.Transparency)
                    {
                        y--;
                        while (color.A < 255 && y >= 0)
                        {
                            GetBlock(sections, x, y, z, out id, out data);
                            Color c2 = ColorPalette.Lookup(id, data, biome);
                            color = Blend(color, c2);
                            y--;
                        }
                    }
                    else
                        color = Color.FromArgb(255, color.R, color.G, color.B);

                    //brighten/darken by height; arbitrary value, but /seems/ to look okay
                    color = AddtoColor(color, (int)(y / 1.7 - 42));

                    pixels[x, z] = color;
                }
            }

            mutex.WaitOne();
            for (int z = 0; z < 16; z++)
            {
                for (int x = 0; x < 16; x++)
                {
                    b.SetPixel(offsetX + x, offsetY + z, pixels[x, z]);
                }
            }
            mutex.ReleaseMutex();

            if (Interlocked.Decrement(ref taskCount) == 0)
                signal.Set();
        }