#include <stdlib.h>
#include "sobel_func.h"

#ifdef __MICROBLAZE__
volatile unsigned char *src_ptr = (volatile unsigned char *)0xA0000000;
volatile unsigned char *dest_ptr = (volatile unsigned char *)0xA0100000;
#else
#include "imageIO.h"
#endif

void gradient( const int *a1, const int *a2, const int *a3, const int *a4, const int *a5, const int *a6, int *output ){

    *output = ( ((*a4)+((*a5)<<1)+(*a6)) - ((*a1)+((*a2)<<1)+(*a3)) );

}


void absVal( const int *x, const int *y, int *output ) {

    *output = ( (abs(*x)+abs(*y))/4 );

}



void readPixel( int *output ) {
#ifdef __MICROBLAZE__
    static int addr = 0;
    *output = src_ptr[addr++];
#else
    int pp;
	static FILE *fh = NULL;

	if (fh == NULL) {
           fh = mrOpen("car_gray.B");
	}

	pp = mGetc(fh);
	*output =  pp;
#endif
}

void  writePixel( const int *pp ) {
#ifdef __MICROBLAZE__
    static int addr = 0;
    dest_ptr[addr++] = *pp;
#else
	static FILE *fh = NULL;

	if (fh == NULL) {
           fh = mwOpen("car_gray_sobel.raw");
	}

	mPutc(fh, *pp);
#endif
}