public void maskRect(int x, int y, int w, int h, int[] pix, byte[] mask) {
    assert data instanceof int[];
    int maskBytesPerRow = (w + 7) / 8;

    for (int j = 0; j < h; j++) {
      int cy = y + j;

      if (cy < 0 || cy >= height_)
        continue;

      for (int i = 0; i < w; i++) {
        int cx = x + i;

        if (cx < 0 || cx >= width_)
          continue;

        int byte_ = j * maskBytesPerRow + i / 8;
        int bit = 7 - i % 8;

        if ((mask[byte_] & (1 << bit)) != 0)
         ((int[])data)[cy * width_ + cx] = pix[j * w + i];
      }
    }
  }