// file: $USFS/class/ppm/v1.0/ppm_diff_0.cc
//

// isip include files
//
#include "ppm.h"

#include <integral_constants.h>
#include "ppm_constants.h"

// method: diff_cc
//
// arguments: 
//  float_4 alpha_a: (input) scale factor
//  float_4 beta_a: (input) scale factor
//
//  int_4 width_a: (input) width of image
//  int_4 height_a: (input) height of image
//
//  uint_1* b_red: (output) red buffer for the difference
//  uint_1* b_green: (output) green buffer for the difference
//  uint_1* b_blue: (output) blue buffer for the difference
//
//  uint_1* b_r_0: (input) red buffer of the operand0 image
//  uint_1* b_gr_0: (input) green buffer of the operand0 image
//  uint_1* b_bl_0: (input) blue buffer of the operand0 image
//
//  uint_1* b_r_1: (input) red buffer of the operand1 image
//  uint_1* b_gr_1: (input) green buffer of the operand1 image
//  uint_1* b_bl_1: (input) blue buffer of the operand1 image
//
// return: a logical_1 value indicating status
//     
// this method compares two images pixel by pixel and outputs the difference
// of the two:
//
//  b_red[i] = alpha * b_r_0[i] - beta * b_r_1[i]
//  ...
//
// the user must allocated space for the output.
//
logical_1 Ppm::diff_cc(float_4 alpha_a, float_4 beta_a,
		       int_4 width_a, int_4 height_a,
		       uint_1* b_r_a, uint_1* b_g_a, uint_1* b_bl_a,
		       uint_1* b_r_0_a, uint_1* b_gr_0_a, uint_1* b_bl_0_a,
		       uint_1* b_r_1_a, uint_1* b_gr_1_a, uint_1* b_bl_1_a) {

  // loop over all pixels
  //
  int_4 num_pixels = width_a * height_a;
  float_4 sum;
    
  for (int_4 i=0; i<num_pixels; i++) {
    
    // difference the red value
    //
    sum = alpha_a * b_r_0_a[i] - beta_a * b_r_1_a[i];
    if (sum > PPM_MAX_COLOR) {
      sum = PPM_MAX_COLOR;
    }
    else if (sum < (uint_1)0) {
      sum = (uint_1)0;
    }
    b_r_a[i] = (uint_1)rint(sum);

    // difference the green value
    //
    sum = alpha_a * b_gr_0_a[i] - beta_a * b_gr_1_a[i];
    if (sum > PPM_MAX_COLOR) {
      sum = PPM_MAX_COLOR;
    }
    else if (sum < (uint_1)0) {
      sum = (uint_1)0;
    }
    b_g_a[i] = (uint_1)rint(sum);

    // difference the blue value
    //
    sum = alpha_a * b_bl_0_a[i] - beta_a * b_bl_1_a[i];
    if (sum > PPM_MAX_COLOR) {
      sum = PPM_MAX_COLOR;
    }
    else if (sum < (uint_1)0) {
      sum = (uint_1)0;
    }
    b_bl_a[i] = (uint_1)rint(sum);
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
  



















