docs: public static int getCompatibleBufferedImageType(final BufferedImage image) {
          Validate.notNull(image, "image");
  
          WritableRaster raster = image.getRaster();
          SampleModel sm = raster.getSampleModel();
          int numBands = raster.getNumBands();
  
          ColorModel cm = image.getColorModel();
          ColorSpace cs = cm.getColorSpace();
          boolean isAlphaPre = cm.isAlphaPremultiplied();
          int csType = cs.getType();
  
          int dataType = raster.getDataBuffer().getDataType();
  
          if (csType != ColorSpace.TYPE_RGB) {
              if (csType == ColorSpace.TYPE_GRAY && cm instanceof ComponentColorModel) {
                  if (sm instanceof ComponentSampleModel && ((ComponentSampleModel) sm).getPixelStride() != numBands) {
                      return BufferedImage.TYPE_CUSTOM;
                  }
                  else if (dataType == DataBuffer.TYPE_BYTE && raster.getNumBands() == 1 &&
                          cm.getComponentSize(0) == 8 && ((ComponentSampleModel) sm).getPixelStride() == 1) {
                      return BufferedImage.TYPE_BYTE_GRAY;
                  }
                  else if (dataType == DataBuffer.TYPE_USHORT && raster.getNumBands() == 1 &&
                          cm.getComponentSize(0) == 16 && ((ComponentSampleModel) sm).getPixelStride() == 1) {
                      return BufferedImage.TYPE_USHORT_GRAY;
                  }
              }
              else {
                  return BufferedImage.TYPE_CUSTOM;
              }
          }
  
          if ((dataType == DataBuffer.TYPE_INT) && (numBands == 3 || numBands == 4)) {
              // Check if the raster params and the color model are correct
              int pixSize = cm.getPixelSize();
  
              if (cm instanceof DirectColorModel && sm.getNumDataElements() == 1 && (pixSize == 32 || pixSize == 24)) {
                  // Now check on the DirectColorModel params
                  DirectColorModel dcm = (DirectColorModel) cm;
                  int rmask = dcm.getRedMask();
                  int gmask = dcm.getGreenMask();
                  int bmask = dcm.getBlueMask();
  
                  if (rmask == DCM_RED_MASK && gmask == DCM_GREEN_MASK && bmask == DCM_BLUE_MASK) {
                      if (dcm.getAlphaMask() == DCM_ALPHA_MASK) {
                          return isAlphaPre ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_ARGB;
                      }
                      else if (!dcm.hasAlpha()) {
                          // No Alpha
                          return BufferedImage.TYPE_INT_RGB;
                      }
                  }
                  else if (rmask == DCM_BGR_RED_MASK && gmask == DCM_BGR_GRN_MASK && bmask == DCM_BGR_BLU_MASK) {
                      if (!dcm.hasAlpha()) {
                          return BufferedImage.TYPE_INT_BGR;
                      }
                  }
              }
          }
          else if ((cm instanceof IndexColorModel) && (numBands == 1) && (!cm.hasAlpha() || !isAlphaPre)) {
              IndexColorModel icm = (IndexColorModel) cm;
              int pixSize = icm.getPixelSize();
  
              if (dataType == DataBuffer.TYPE_BYTE && sm instanceof MultiPixelPackedSampleModel) {
                  return BufferedImage.TYPE_BYTE_BINARY;
              }
              if (dataType == DataBuffer.TYPE_BYTE && sm instanceof ComponentSampleModel) {
                  ComponentSampleModel csm = (ComponentSampleModel) sm;
  
                  if (csm.getPixelStride() == 1 && pixSize <= 8) {
                      return BufferedImage.TYPE_BYTE_INDEXED;
                  }
              }
          }
          else if ((dataType == DataBuffer.TYPE_USHORT) &&
                  (cm instanceof DirectColorModel) && (numBands == 3) && !cm.hasAlpha()) {
              DirectColorModel dcm = (DirectColorModel) cm;
  
              if (dcm.getRedMask() == DCM_565_RED_MASK &&
                      dcm.getGreenMask() == DCM_565_GRN_MASK && dcm.getBlueMask() == DCM_565_BLU_MASK) {
                  return BufferedImage.TYPE_USHORT_565_RGB;
              }
              else if (dcm.getRedMask() == DCM_555_RED_MASK &&
                      dcm.getGreenMask() == DCM_555_GRN_MASK && dcm.getBlueMask() == DCM_555_BLU_MASK) {
                  return BufferedImage.TYPE_USHORT_555_RGB;
              }
          }
          else if (dataType == DataBuffer.TYPE_BYTE && cm instanceof ComponentColorModel &&
                  raster.getSampleModel() instanceof PixelInterleavedSampleModel && (numBands == 3 || numBands == 4)) {
              ComponentColorModel ccm = (ComponentColorModel) cm;
              PixelInterleavedSampleModel csm = (PixelInterleavedSampleModel) raster.getSampleModel();
  
              int[] offs = csm.getBandOffsets();
              int[] nBits = ccm.getComponentSize();
              boolean is8bit = true;
  
              for (int i = 0; i < numBands; i++) {
                  if (nBits[i] != 8) {
                      is8bit = false;
                      break;
                  }
              }
  
              if (is8bit && csm.getPixelStride() == numBands &&
                      offs[0] == numBands - 1 && offs[1] == numBands - 2 && offs[2] == numBands - 3) {
                  if (numBands == 3 && !ccm.hasAlpha()) {
                      return BufferedImage.TYPE_3BYTE_BGR;
                  }
                  else if (offs[3] == 0 && ccm.hasAlpha()) {
                      return isAlphaPre ? BufferedImage.TYPE_4BYTE_ABGR_PRE : BufferedImage.TYPE_4BYTE_ABGR;
                  }
              }
          }
  
          return BufferedImage.TYPE_CUSTOM;
      }