// file: $USFS/class/image_analysis/v6.1/imaga_dct_01.cc
//

// system include files
//
#include <string.h>

// isip include files
//
#include "image_analysis.h"
#include "image_analysis_constants.h"

// method: dct_compute_cc
//
// arguments:
//  uint_1* blk_r: (input) buffer for red color
//  uint_1* blk_g: (input) buffer for green color
//  uint_1* blk_b: (input) buffer for blue color
//  int_4 offset_w: (input) offset in width for the image block
//  int_4 offset_h: (input) offset in height for the image block
//  int_4 width: (input) width of the image block
//  int_4 height: (input) height of the image block
//
// return:
//  a logical_1 value indicating status
//
// this method computes the dct-based features for each color on an image block
//
logical_1 Image_analysis::dct_compute_cc(uint_1* blk_r_a, uint_1* blk_g_a,
				         uint_1* blk_b_a, int_4 offset_w_a,
				         int_4 offset_h_a, int_4 width_a,
				         int_4 height_a) {

  // declare local variable
  //
  logical_1 process_block = ISIP_TRUE;

  // check if segmentation data needed
  //
  if (check_segflag_cc() == ISIP_TRUE) {
    
    // if no region corresponding to the transcription was found in the
    // image, no computation needed
    //
    if (polygon_list_d == (Link_list*)NULL) {
      process_block = ISIP_FALSE;      
    }
    else if ((polygon_list_d->get_head_cc()) == (Link_node*)NULL) {
      process_block = ISIP_FALSE;
    }

    // else check if the block is inside the prescribed region
    //
    else {

      // compute the coordinates for the block center
      //
      int_4 center_x = offset_w_a + width_a / 2;
      int_4 center_y = offset_h_a + height_a / 2;

      // if the center of the block is not inside the prescibed region, no
      // computation needed
      //
      if (is_inside_cc(center_x, center_y) == ISIP_FALSE) {
        process_block = ISIP_FALSE;
      }
    }
  }
  
  if (process_block == ISIP_TRUE) {

    // compute the cosine coefficients required for the DCT transform
    //
    if (dct_cos_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error computing the cosine coefficients");
      return ISIP_FALSE;
    }

    // compute the normalization matrix
    //
    if (dct_set_norm_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error computing the normalization matrix");
      return ISIP_FALSE;
    }
    
    // debias the dc value before coding
    //
    if (dct_debias_cc(blk_r_a, blk_g_a, blk_b_a, width_a, height_a)
	== ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error subtracting the DC value");
      return ISIP_FALSE;
    }
      
    // perform the DCT transform
    //
    if (dct_trans_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error computing the DCT transform");
      return ISIP_FALSE;
    }
    
    // normalize the DCT coefficients
    //
    if (dct_normalize_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error normalizing the DCT coefficients");
      return ISIP_FALSE;
    }

    // zigzag scan the DCT coefficients
    //
    if (dct_zigzag_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
			   (char_1*)"error performing the zigzag method");
      return ISIP_FALSE;
    }

    // compute the dct features
    //
    if (dct_filter_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"dct_compute_cc",
		       (char_1*)"error computing the dct features");
      return ISIP_FALSE;
    }
    
    // increment the counters
    //
    dct_r_nblks_d += (int_4)1;
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
