void ScaleTransform()

in caffe2_customized_ops/video/customized_video_io.cc [574:650]


void ScaleTransform(
    const float* clip_data,
    const int channels,
    const int length,
    const int height,
    const int width,
    const int max_size,
    const int min_size,
    float*& buffer,
    std::mt19937* randgen,
    int & new_height,
    int & new_width)
    {
      int side_length;

      if (min_size == max_size)
      {
        side_length = min_size;
      }
      else
      {
        side_length =
          std::uniform_int_distribution<>(min_size, max_size)(*randgen);
      }
      new_height = height;
      new_width  = width;

      float ratio = 1;

      buffer = nullptr;

      if (height > width)
      {
        ratio = (float)side_length / (float)width;
      }
      else
      {
        ratio = (float)side_length / (float)height;
      }
      new_height = (int)((float)height * ratio);
      new_width = (int)((float)width * ratio);

      cv::Mat img(cv::Size(new_width, new_height), CV_8UC3);
      cv::Mat img_origin(cv::Size(width, height), CV_8UC3);

      int image_size = new_height * new_width;
      int channel_size = image_size * length;
      int data_size = channel_size * 3;
      buffer = new float[data_size];
      int offset = 0;

      for (int l = 0; l < length; ++l)
      {
        for (int c = 0; c < 3; ++c)
        {
          for (int h = 0; h < height; ++h)
          {
            for (int w = 0; w < width; ++w)
            {
              int data_index = ((c * length + l) * height  + h) * width  + w;
              float tnum = clip_data[data_index];
              img_origin.at<cv::Vec3b>(h, w)[c] = (uchar)(tnum);
              // img_origin.at<cv::Vec3b>(h, w)[2 - c] = (uchar)(tnum);
            } // w
          } // h
        } // c
        cv::resize(img_origin, img, cv::Size(new_width, new_height));

        for (int c = 0; c < 3; c++) {
          ImageChannelToBuffer(&img, buffer + c * channel_size + offset, c);
        } // c
        offset += image_size;
      } // l
      CAFFE_ENFORCE(offset == channel_size, "Wrong offset size");
      img_origin.release();
      img.release();
    } // ScaleTransform