docs: RenderedImage createSimpleIndexImage() {
          ColorModel srcCM = src.getColorModel();
          if(srcCM instanceof IndexColorModel) {
              return src;
          }
          
          Raster srcRaster = src.getData();
          boolean hasTransparent = srcCM.getTransparency() != Transparency.OPAQUE;
          int maxColorCount = 256;
          if(hasTransparent) {
              maxColorCount = 255;
          }
          
          int num = 0;
          Set<Integer> colorSet = new HashSet<Integer>();
          int[] colorArrays = new int[maxColorCount];
          for(int x = 0; x < src.getWidth(); x++) {
              for(int y = 0; y < src.getHeight(); y++) {
                  int rgb = srcCM.getRGB(srcRaster.getDataElements(x, y, null)) & 0xFFFFFF;
                  if(!colorSet.contains(rgb)) {
                      if(num == maxColorCount) {
                          return null;
                      }
                      
                      colorArrays[num++] = rgb;
                      colorSet.add(rgb);
                  }
              }
          }
          if(num == 0) {
              throw new IllegalStateException("This image has no color");
          }
          
          int[] colors = null;
          if(hasTransparent) {
              colors = new int[num + 1];
              System.arraycopy(colorArrays, 0, colors, 1, num);
              colors[0] = 0;
          } else {
              colors = new int[num];
              System.arraycopy(colorArrays, 0, colors, 0, num);
          }
          
          Arrays.sort(colors);
          
          byte[] red = new byte[colors.length];
          byte[] green = new byte[colors.length];
          byte[] blue = new byte[colors.length];
          for(int i = 0; i < colors.length; i++) {
              red[i] = (byte)((colors[i] >> 16) & 0xFF);
              green[i] = (byte)((colors[i] >> 8) & 0xFF);
              blue[i] = (byte)((colors[i]) & 0xFF);
          }
  
          IndexColorModel destCM = null;
          if(hasTransparent) {
              destCM = new IndexColorModel(8, colors.length, red, green, blue, 0);
          } else {
              destCM = new IndexColorModel(8, colors.length, red, green, blue);
          }
          
          BufferedImage image = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_INDEXED, destCM);
          WritableRaster raster = image.getRaster();
          for (int x = 0; x < src.getWidth(); x++) {
              for (int y = 0; y < src.getHeight(); y++) {
                  int argb = srcCM.getRGB(srcRaster.getDataElements(x, y, null));
                  if(hasTransparent && ((argb & 0xFF000000) != 0xFF000000)) {
                      raster.setSample(x, y, 0, 0);
                  } else {
                      int index = Arrays.binarySearch(colors, (argb & 0x00FFFFFF));
                      if(index < 0) {
                          throw new IllegalStateException("Some colors not indexed");
                      }
                      raster.setSample(x, y, 0, index);
                  }
              }
          }
  
          return image;
      }