private byte[] getROIData(ROI roi, Rectangle rectIMG) {
    byte[] dataROI;
    PlanarImage roiIMG = roi.getAsImage();
    Rectangle rectROI = roiIMG.getBounds();
    // Forcing to component colormodel in order to avoid packed bits
    ImageWorker w = new ImageWorker();
    w.setImage(roiIMG);
    w.forceComponentColorModel();
    RenderedImage img = w.getRenderedImage();
    //
    BufferedImage test =
        new BufferedImage(rectIMG.width, rectIMG.height, BufferedImage.TYPE_BYTE_GRAY);
    ImageLayout2 layout = new ImageLayout2(test);
    layout.setMinX(img.getMinX());
    layout.setMinY(img.getMinY());
    layout.setWidth(img.getWidth());
    layout.setHeight(img.getHeight());
    // Lookup
    byte[] lut = new byte[256];
    lut[255] = 1;
    lut[1] = 1;
    LookupTableJAI table = new LookupTableJAI(lut);
    RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
    RenderedOp transformed = LookupDescriptor.create(img, table, hints);

    Graphics2D gc2d = null;
    // Translation parameters in order to position the ROI data correctly in the Raster Space
    int trX = -rectIMG.x + rectROI.x - rectIMG.x;
    int trY = -rectIMG.y + rectROI.y - rectIMG.y;
    try {
      gc2d = test.createGraphics();
      gc2d.drawRenderedImage(transformed, AffineTransform.getTranslateInstance(trX, trY));
    } finally {
      gc2d.dispose();
    }
    Rectangle testRect = new Rectangle(rectIMG.width, rectIMG.height);
    DataBufferByte dbRoi = (DataBufferByte) test.getData(testRect).getDataBuffer();
    dataROI = dbRoi.getData();
    // BufferedImage is stored in memory so the planarImage chain before can be disposed
    ImageUtilities.disposePlanarImageChain(transformed);
    // Flush of the BufferedImage
    test.flush();

    return dataROI;
  }
  protected RenderedImage convert(RenderedImage renderedImage) throws Exception {

    int height = renderedImage.getHeight();
    int width = renderedImage.getWidth();

    SampleModel sampleModel = renderedImage.getSampleModel();
    ColorModel colorModel = renderedImage.getColorModel();

    Raster raster = renderedImage.getData();

    DataBuffer dataBuffer = raster.getDataBuffer();

    if (colorModel instanceof IndexColorModel) {
      IndexColorModel indexColorModel = (IndexColorModel) colorModel;

      int mapSize = indexColorModel.getMapSize();

      byte[][] data = new byte[4][mapSize];

      indexColorModel.getReds(data[0]);
      indexColorModel.getGreens(data[1]);
      indexColorModel.getBlues(data[2]);
      indexColorModel.getAlphas(data[3]);

      LookupTableJAI lookupTableJAI = new LookupTableJAI(data);

      renderedImage = LookupDescriptor.create(renderedImage, lookupTableJAI, null);
    } else if (sampleModel.getNumBands() == 2) {
      List<Byte> bytesList = new ArrayList<Byte>(height * width * _NUM_OF_BANDS);

      List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);

      for (int i = 0; i < dataBuffer.getSize(); i++) {
        int mod = (i + 1) % 2;

        int elemPos = i;

        if (mod == 0) {
          tempBytesList.add((byte) dataBuffer.getElem(elemPos - 1));
          tempBytesList.add((byte) dataBuffer.getElem(elemPos - 1));
        }

        tempBytesList.add((byte) dataBuffer.getElem(elemPos));

        if (mod == 0) {
          Collections.reverse(tempBytesList);

          bytesList.addAll(tempBytesList);

          tempBytesList.clear();
        }
      }

      byte[] data = ArrayUtil.toArray(bytesList.toArray(new Byte[bytesList.size()]));

      DataBuffer newDataBuffer = new DataBufferByte(data, data.length);

      renderedImage = createRenderedImage(renderedImage, height, width, newDataBuffer);
    } else if (colorModel.getTransparency() != Transparency.TRANSLUCENT) {
      List<Byte> bytesList = new ArrayList<Byte>(height * width * _NUM_OF_BANDS);

      List<Byte> tempBytesList = new ArrayList<Byte>(_NUM_OF_BANDS);

      for (int i = 0; i < dataBuffer.getSize(); i++) {
        int mod = (i + 1) % 3;

        int elemPos = i;

        tempBytesList.add((byte) dataBuffer.getElem(elemPos));

        if (mod == 0) {
          tempBytesList.add((byte) 255);

          Collections.reverse(tempBytesList);

          bytesList.addAll(tempBytesList);

          tempBytesList.clear();
        }
      }

      byte[] data = ArrayUtil.toArray(bytesList.toArray(new Byte[bytesList.size()]));

      DataBuffer newDataBuffer = new DataBufferByte(data, data.length);

      renderedImage = createRenderedImage(renderedImage, height, width, newDataBuffer);
    }

    return renderedImage;
  }