private static int _writeIconRaster(@NotNull Icon icon, float scale, @NotNull Pointer memory, int offset, int totalMemoryBytes)
    throws Exception {
    final int w = Math.round(icon.getIconWidth() * scale);
    final int h = Math.round(icon.getIconHeight() * scale);

    if (w <= 0 || h <= 0) {
      throw new Exception("Incorrect icon sizes: " + icon.getIconWidth() + "x" + icon.getIconHeight() + ", scale=" + scale);
    }

    final int rasterSizeInBytes = w * h * 4;
    final int totalSize = rasterSizeInBytes + 4;

    if (offset + totalSize > totalMemoryBytes) {
      throw new Exception(
        "Incorrect memory offset: offset=" + offset + ", rasterSize=" + rasterSizeInBytes + ", totalMemoryBytes=" + totalMemoryBytes);
    }

    memory.setShort(offset, (short)w);
    offset += 2;
    memory.setShort(offset, (short)h);
    offset += 2;

    _drawIconIntoMemory(icon, scale, memory, offset);

    return totalSize;
  }
private static int _writeIconRaster(@NotNull Icon icon, float scale, @NotNull Pointer memory, int offset, int totalMemoryBytes)