// file: $USFS/class/image_segmentation/v1.0/imags_win_0.cc
//

// isip include files
//
#include "image_segmentation.h"
#include "image_segmentation_constants.h"

// method: pad_zero_cc
//
// arguments:
//  uint_1*& buf_red: (input & output) buffer for red color
//  uint_1*& buf_green: (input & output) buffer for green color
//  uint_1*& buf_blue: (input & output) buffer for blue color
//  int_4 offset_w: (input) offset in width for the window
//  int_4 offset_h: (input) offset in height for the window
//  int_4 width: (input) width of the window
//  int_4 height: (input) height of the window
//  int_4 actual_off_w: (input) offset in width for the actual image block
//                       read from the file
//  int_4 actual_off_h: (input) offset in height for the actual image block
//                       read from the file
//  int_4 actual_w: (input) width of the actual image block read from the file
//  int_4 actual_h: (input) height of the actual image block read from the file
//
// return:
//  a logical_1 value indicating status
//
// this method does zero padding for windowing a frame of image
//
logical_1 Image_segmentation::pad_zero_cc(uint_1*& buf_red_a,
					  uint_1*& buf_green_a,
					  uint_1*& buf_blue_a,
					  int_4 offset_w_a,
					  int_4 offset_h_a, int_4 width_a,
					  int_4 height_a, int_4 actual_off_w_a,
					  int_4 actual_off_h_a,
					  int_4 actual_w_a, int_4 actual_h_a) {

  // local variables
  //
  int_4 j = 0;
  int_4 buf_len = width_a * height_a;

  // declare temporary buffers
  //
  uint_1* temp_buf_r = new uint_1[buf_len];
  uint_1* temp_buf_g = new uint_1[buf_len];
  uint_1* temp_buf_b = new uint_1[buf_len];
  
  for (int_4 i=0; i<buf_len; i++) {

    // coordinates for pixels in the window
    //
    int_4 w1 = offset_w_a + i % width_a;
    int_4 h1 = offset_h_a + i / width_a;

    // coordinates for pixels in the actual block read from the image
    //
    int_4 w2 = actual_off_w_a + j % actual_w_a;
    int_4 h2 = actual_off_h_a + j / actual_w_a;

    // if both sets of coordinates match or not
    //
    if ((w1 == w2) && (h1 == h2) && (j < (actual_w_a * actual_h_a))) {
      temp_buf_r[i] = buf_red_a[j];
      temp_buf_g[i] = buf_green_a[j];
      temp_buf_b[i] = buf_blue_a[j];
      j++;      
    }
    else {
      temp_buf_r[i] = 0;
      temp_buf_g[i] = 0;
      temp_buf_b[i] = 0;
    }
  }
  
  // release memory
  //
  if (buf_red_a != (uint_1*)NULL) {
    delete [] buf_red_a;
  }

  if (buf_green_a != (uint_1*)NULL) {
    delete [] buf_green_a;
  }

  if (buf_blue_a != (uint_1*)NULL) {
    delete [] buf_blue_a;
  }
  
  buf_red_a = temp_buf_r;
  buf_green_a = temp_buf_g;
  buf_blue_a = temp_buf_b;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
