private final void shadowColor(Color c, int lightlevel, int[] shadowscale) {
   int scale = shadowscale[lightlevel];
   if (scale < 256)
     c.setRGBA(
         (c.getRed() * scale) >> 8,
         (c.getGreen() * scale) >> 8,
         (c.getBlue() * scale) >> 8,
         c.getAlpha());
 }
 private void doScaleWithBilinear(int[] argb, int[] zargb, int width, int height) {
   Color c1 = new Color();
   /* Now, compute zoomed tile - bilinear filter 2x2 -> 1x1 */
   for (int y = 0; y < height; y += 2) {
     for (int x = 0; x < width; x += 2) {
       int red = 0;
       int green = 0;
       int blue = 0;
       int alpha = 0;
       for (int yy = y; yy < y + 2; yy++) {
         for (int xx = x; xx < x + 2; xx++) {
           c1.setARGB(argb[(yy * width) + xx]);
           red += c1.getRed();
           green += c1.getGreen();
           blue += c1.getBlue();
           alpha += c1.getAlpha();
         }
       }
       c1.setRGBA(red >> 2, green >> 2, blue >> 2, alpha >> 2);
       zargb[(y * width / 4) + (x / 2)] = c1.getARGB();
     }
   }
 }
  private void applySmoothLighting(
      HDPerspectiveState ps, HDShaderState ss, Color incolor, Color[] outcolor, int[] shadowscale) {
    int[] xyz = ps.getSubblockCoord();
    int scale = (int) ps.getScale();
    int mid = scale / 2;
    BlockStep s1, s2;
    int w1, w2;
    /* Figure out which directions to look */
    switch (ps.getLastBlockStep()) {
      case X_MINUS:
      case X_PLUS:
        if (xyz[1] < mid) {
          s1 = BlockStep.Y_MINUS;
          w1 = mid - xyz[1];
        } else {
          s1 = BlockStep.Y_PLUS;
          w1 = xyz[1] - mid;
        }
        if (xyz[2] < mid) {
          s2 = BlockStep.Z_MINUS;
          w2 = mid - xyz[2];
        } else {
          s2 = BlockStep.Z_PLUS;
          w2 = xyz[2] - mid;
        }
        break;
      case Z_MINUS:
      case Z_PLUS:
        if (xyz[0] < mid) {
          s1 = BlockStep.X_MINUS;
          w1 = mid - xyz[0];
        } else {
          s1 = BlockStep.X_PLUS;
          w1 = xyz[0] - mid;
        }
        if (xyz[1] < mid) {
          s2 = BlockStep.Y_MINUS;
          w2 = mid - xyz[1];
        } else {
          s2 = BlockStep.Y_PLUS;
          w2 = xyz[1] - mid;
        }
        break;
      default:
        if (xyz[0] < mid) {
          s1 = BlockStep.X_MINUS;
          w1 = mid - xyz[0];
        } else {
          s1 = BlockStep.X_PLUS;
          w1 = xyz[0] - mid;
        }
        if (xyz[2] < mid) {
          s2 = BlockStep.Z_MINUS;
          w2 = mid - xyz[2];
        } else {
          s2 = BlockStep.Z_PLUS;
          w2 = xyz[2] - mid;
        }
        break;
    }
    /* Now get the 3 needed light levels */
    LightLevels skyemit0 = ps.getCachedLightLevels(0);
    ps.getLightLevels(skyemit0);
    LightLevels skyemit1 = ps.getCachedLightLevels(1);
    ps.getLightLevelsAtStep(s1, skyemit1);
    LightLevels skyemit2 = ps.getCachedLightLevels(2);
    ps.getLightLevelsAtStep(s2, skyemit2);

    /* Get light levels */
    int ll0 = getLightLevel(skyemit0, true);
    int ll1 = getLightLevel(skyemit1, true);
    int weight = 0;
    if (ll1 < ll0) weight -= w1;
    else if (ll1 > ll0) weight += w1;
    int ll2 = getLightLevel(skyemit2, true);
    if (ll2 < ll0) weight -= w2;
    else if (ll2 > ll0) weight += w2;
    outcolor[0].setColor(incolor);
    int cscale = 256;
    if (weight == 0) {
      cscale = shadowscale[ll0];
    } else if (weight < 0) {
        /* If negative, interpolate down */
      weight = -weight;
      if (ll0 > 0) {
        cscale = (shadowscale[ll0] * (scale - weight) + shadowscale[ll0 - 1] * weight) / scale;
      } else {
        cscale = shadowscale[ll0];
      }
    } else {
      if (ll0 < 15) {
        cscale = (shadowscale[ll0] * (scale - weight) + shadowscale[ll0 + 1] * weight) / scale;
      } else {
        cscale = shadowscale[ll0];
      }
    }
    if (cscale < 256) {
      Color c = outcolor[0];
      c.setRGBA(
          (c.getRed() * cscale) >> 8,
          (c.getGreen() * cscale) >> 8,
          (c.getBlue() * cscale) >> 8,
          c.getAlpha());
    }
    if (outcolor.length > 1) {
      ll0 = getLightLevel(skyemit0, false);
      ll1 = getLightLevel(skyemit1, false);
      weight = 0;
      if (ll1 < ll0) weight -= w1;
      else if (ll1 > ll0) weight += w1;
      ll2 = getLightLevel(skyemit2, false);
      if (ll2 < ll0) weight -= w2;
      else if (ll2 > ll0) weight += w2;
      outcolor[1].setColor(incolor);
      cscale = 256;
      if (weight == 0) {
        cscale = shadowscale[ll0];
      } else if (weight < 0) {
          /* If negative, interpolate down */
        weight = -weight;
        if (ll0 > 0) {
          cscale = (shadowscale[ll0] * (scale - weight) + shadowscale[ll0 - 1] * weight) / scale;
        } else {
          cscale = shadowscale[ll0];
        }
      } else {
        if (ll0 < 15) {
          cscale = (shadowscale[ll0] * (scale - weight) + shadowscale[ll0 + 1] * weight) / scale;
        } else {
          cscale = shadowscale[ll0];
        }
      }
      if (cscale < 256) {
        Color c = outcolor[1];
        c.setRGBA(
            (c.getRed() * cscale) >> 8,
            (c.getGreen() * cscale) >> 8,
            (c.getBlue() * cscale) >> 8,
            c.getAlpha());
      }
    }
  }
  protected void scan(
      World world,
      int seq,
      boolean isnether,
      final Color result,
      final Color result_day,
      MapChunkCache.MapIterator mapiter) {
    int lightlevel = 15;
    int lightlevel_day = 15;
    result.setTransparent();
    if (result_day != null) result_day.setTransparent();
    for (; ; ) {
      if (mapiter.y < 0) {
        return;
      }
      int id = mapiter.getBlockTypeID();
      int data = 0;
      if (isnether) {
          /* Make bedrock ceiling into air in nether */
        if (id != 0) {
          /* Remember first color we see, in case we wind up solid */
          if (result.isTransparent())
            if (colorScheme.colors[id] != null) result.setColor(colorScheme.colors[id][seq]);
          id = 0;
        } else isnether = false;
      }
      if (id != 0) {
          /* No update needed for air */
        if (colorScheme.datacolors[id] != null) {
            /* If data colored */
          data = mapiter.getBlockData();
        }
        if ((shadowscale != null) && (mapiter.y < 127)) {
          /* Find light level of previous chunk */
          switch (seq) {
            case 0:
            case 2:
              mapiter.incrementY();
              break;
            case 1:
              mapiter.incrementX();
              break;
            case 3:
              mapiter.decrementZ();
              break;
          }
          lightlevel = lightlevel_day = mapiter.getBlockSkyLight();
          if (lightscale != null) lightlevel = lightscale[lightlevel];
          if ((lightlevel < 15) || (lightlevel_day < 15)) {
            int emitted = mapiter.getBlockEmittedLight();
            lightlevel = Math.max(emitted, lightlevel);
            lightlevel_day = Math.max(emitted, lightlevel_day);
          }
          switch (seq) {
            case 0:
            case 2:
              mapiter.decrementY();
              break;
            case 1:
              mapiter.decrementX();
              break;
            case 3:
              mapiter.incrementZ();
              break;
          }
        }
      }

      switch (seq) {
        case 0:
          mapiter.decrementX();
          break;
        case 1:
        case 3:
          mapiter.decrementY();
          break;
        case 2:
          mapiter.incrementZ();
          break;
      }

      seq = (seq + 1) & 3;

      if (id != 0) {
        if (highlightBlocks.contains(id)) {
          result.setColor(highlightColor);
          return;
        }
        Color[] colors;
        if (data != 0) colors = colorScheme.datacolors[id][data];
        else colors = colorScheme.colors[id];
        if (colors != null) {
          Color c = colors[seq];
          if (c.getAlpha() > 0) {
            /* we found something that isn't transparent, or not doing transparency */
            if ((!transparency) || (c.getAlpha() == 255)) {
              /* it's opaque - the ray ends here */
              result.setARGB(c.getARGB() | 0xFF000000);
              if (lightlevel < 15) {
                  /* Not full light? */
                shadowColor(result, lightlevel);
              }
              if (result_day != null) {
                if (lightlevel_day == lightlevel) /* Same light = same result */
                  result_day.setColor(result);
                else {
                  result_day.setColor(c);
                  if (lightlevel_day < 15) shadowColor(result_day, lightlevel_day);
                }
              }
              return;
            }

            /* this block is transparent, so recurse */
            scan(world, seq, isnether, result, result_day, mapiter);

            int cr = c.getRed();
            int cg = c.getGreen();
            int cb = c.getBlue();
            int ca = c.getAlpha();
            if (lightlevel < 15) {
              int scale = shadowscale[lightlevel];
              cr = (cr * scale) >> 8;
              cg = (cg * scale) >> 8;
              cb = (cb * scale) >> 8;
            }
            cr *= ca;
            cg *= ca;
            cb *= ca;
            int na = 255 - ca;
            result.setRGBA(
                (result.getRed() * na + cr) >> 8,
                (result.getGreen() * na + cg) >> 8,
                (result.getBlue() * na + cb) >> 8,
                255);
            /* Handle day also */
            if (result_day != null) {
              cr = c.getRed();
              cg = c.getGreen();
              cb = c.getBlue();
              if (lightlevel_day < 15) {
                int scale = shadowscale[lightlevel_day];
                cr = (cr * scale) >> 8;
                cg = (cg * scale) >> 8;
                cb = (cb * scale) >> 8;
              }
              cr *= ca;
              cg *= ca;
              cb *= ca;
              result_day.setRGBA(
                  (result_day.getRed() * na + cr) >> 8,
                  (result_day.getGreen() * na + cg) >> 8,
                  (result_day.getBlue() * na + cb) >> 8,
                  255);
            }
            return;
          }
        }
      }
    }
  }
  protected void scan(
      DynmapWorld world,
      int seq,
      boolean isnether,
      final Color result,
      final Color result_day,
      MapIterator mapiter) {
    int lightlevel = 15;
    int lightlevel_day = 15;
    BiomeMap bio = null;
    double rain = 0.0;
    double temp = 0.0;
    result.setTransparent();
    if (result_day != null) result_day.setTransparent();
    for (; ; ) {
      if (mapiter.getY() < 0) {
        return;
      }
      int id = mapiter.getBlockTypeID();
      int data = 0;
      if (isnether) {
          /* Make bedrock ceiling into air in nether */
        if (id != 0) {
          /* Remember first color we see, in case we wind up solid */
          if (result.isTransparent()) {
            try {
              if (colorScheme.colors[id] != null) {
                result.setColor(colorScheme.colors[id][seq]);
              }
            } catch (ArrayIndexOutOfBoundsException aioobx) {
              colorScheme.resizeColorArray(id);
            }
          }
          id = 0;
        } else isnether = false;
      }
      if (id != 0) {
          /* No update needed for air */
        switch (biomecolored) {
          case NONE:
            try {
              if (colorScheme.datacolors[id] != null) {
                  /* If data colored */
                data = mapiter.getBlockData();
              }
            } catch (ArrayIndexOutOfBoundsException aioobx) {
              colorScheme.resizeColorArray(id);
            }
            break;
          case BIOME:
            bio = mapiter.getBiome();
            break;
          case RAINFALL:
            rain = mapiter.getRawBiomeRainfall();
            break;
          case TEMPERATURE:
            temp = mapiter.getRawBiomeTemperature();
            break;
        }
        if ((shadowscale != null) && (mapiter.getY() < (mapiter.getWorldHeight() - 1))) {
          /* Find light level of previous chunk */
          BlockStep last = mapiter.unstepPosition();
          lightlevel = lightlevel_day = mapiter.getBlockSkyLight();
          if (lightscale != null) lightlevel = lightscale[lightlevel];
          if ((lightlevel < 15) || (lightlevel_day < 15)) {
            int emitted = mapiter.getBlockEmittedLight();
            lightlevel = Math.max(emitted, lightlevel);
            lightlevel_day = Math.max(emitted, lightlevel_day);
          }
          mapiter.stepPosition(last);
        }
      }

      switch (seq) {
        case 0:
          mapiter.stepPosition(BlockStep.X_MINUS);
          break;
        case 1:
        case 3:
          mapiter.stepPosition(BlockStep.Y_MINUS);
          break;
        case 2:
          mapiter.stepPosition(BlockStep.Z_PLUS);
          break;
      }

      seq = (seq + 1) & 3;

      if (id != 0) {
        if (highlightBlocks.contains(id)) {
          result.setColor(highlightColor);
          return;
        }
        Color[] colors = null;
        switch (biomecolored) {
          case NONE:
            try {
              if (data != 0) colors = colorScheme.datacolors[id][data];
              else colors = colorScheme.colors[id];
            } catch (ArrayIndexOutOfBoundsException aioobx) {
              colorScheme.resizeColorArray(id);
            }
            break;
          case BIOME:
            if (bio != null) colors = colorScheme.biomecolors[bio.ordinal()];
            break;
          case RAINFALL:
            colors = colorScheme.getRainColor(rain);
            break;
          case TEMPERATURE:
            colors = colorScheme.getTempColor(temp);
            break;
        }
        if (colors != null) {
          Color c = colors[seq];
          if (c.getAlpha() > 0) {
            /* we found something that isn't transparent, or not doing transparency */
            if ((!transparency) || (c.getAlpha() == 255)) {
              /* it's opaque - the ray ends here */
              result.setARGB(c.getARGB() | 0xFF000000);
              if (lightlevel < 15) {
                  /* Not full light? */
                shadowColor(result, lightlevel);
              }
              if (result_day != null) {
                if (lightlevel_day == lightlevel) /* Same light = same result */
                  result_day.setColor(result);
                else {
                  result_day.setColor(c);
                  if (lightlevel_day < 15) shadowColor(result_day, lightlevel_day);
                }
              }
              return;
            }

            /* this block is transparent, so recurse */
            scan(world, seq, isnether, result, result_day, mapiter);

            int cr = c.getRed();
            int cg = c.getGreen();
            int cb = c.getBlue();
            int ca = c.getAlpha();
            if (lightlevel < 15) {
              int scale = shadowscale[lightlevel];
              cr = (cr * scale) >> 8;
              cg = (cg * scale) >> 8;
              cb = (cb * scale) >> 8;
            }
            cr *= ca;
            cg *= ca;
            cb *= ca;
            int na = 255 - ca;
            result.setRGBA(
                (result.getRed() * na + cr) >> 8,
                (result.getGreen() * na + cg) >> 8,
                (result.getBlue() * na + cb) >> 8,
                255);
            /* Handle day also */
            if (result_day != null) {
              cr = c.getRed();
              cg = c.getGreen();
              cb = c.getBlue();
              if (lightlevel_day < 15) {
                int scale = shadowscale[lightlevel_day];
                cr = (cr * scale) >> 8;
                cg = (cg * scale) >> 8;
                cb = (cb * scale) >> 8;
              }
              cr *= ca;
              cg *= ca;
              cb *= ca;
              result_day.setRGBA(
                  (result_day.getRed() * na + cr) >> 8,
                  (result_day.getGreen() * na + cg) >> 8,
                  (result_day.getBlue() * na + cb) >> 8,
                  255);
            }
            return;
          }
        }
      }
    }
  }