convertRgbTensorBufferToImage static method

Image convertRgbTensorBufferToImage(
  1. TensorBuffer buffer
)

Implementation

static Image convertRgbTensorBufferToImage(TensorBuffer buffer) {
  List<int> shape = buffer.getShape();
  ColorSpaceType rgb = ColorSpaceType.RGB;
  rgb.assertShape(shape);

  int h = rgb.getHeight(shape);
  int w = rgb.getWidth(shape);
  Image image = Image(w, h);

  List<int> rgbValues = buffer.getIntList();
  assert(rgbValues.length == w * h * 3);

  for (int i = 0, j = 0, wi = 0, hi = 0; j < rgbValues.length; i++) {
    int r = rgbValues[j++];
    int g = rgbValues[j++];
    int b = rgbValues[j++];
    image.setPixelRgba(wi, hi, r, g, b);
    wi++;
    if (wi % w == 0) {
      wi = 0;
      hi++;
    }
  }

  return image;
}