Team Slytherin - Software emulation source code
= Software Emulation
#include <convey/usr/cny_comp.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #include <stdio.h> //added #include <math.h> //added #undef DEBUG typedef unsigned long long uint64; /*-------STRUCTURES---------*/ typedef struct {int rows; int cols; unsigned char* data;} sImage; extern long cpVadd(); extern int cpXbar(); void usage (char *); /*-------PROTOTYPES---------*/ long getImageInfo(FILE*, long, int); void copyImageInfo(FILE* inputFile, FILE* outputFile); void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors); int main(int argc, char *argv[]) { FILE *bmpInput, *bmpOutput; sImage originalImage; sImage edgeImage; unsigned int X, Y; int I, J; long sumX, sumY; int nColors, SUM; unsigned long vectorSize; unsigned long fileSize; int GX[3][3]; int GY[3][3]; unsigned char *pChar, someChar; unsigned int row, col; long i; long size; uint64 *a1, *a2, *a3, *a4; uint64 act_sum; uint64 exp_sum=0; //Sobel someChar = '0'; pChar = &someChar; /* 3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */ GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1; GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2; GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1; /* 3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */ GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1; GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0; GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1; if(argc < 2) { printf("Usage: %s bmpInput.bmp\n", argv[0]); exit(0); }; // Get personality signature // The "pdk" personality is the PDK sample vadd personality cny_image_t sig2; cny_image_t sig; int stat; if (cny_get_signature) cny_get_signature("pdk", &sig, &sig2, &stat); else fprintf(stderr,"ERROR: cny_get_signature not found\n"); if (stat) { printf("***ERROR: cny_get_signature() Failure: %d\n", stat); exit(1); } // check interleave // this example requires binary interleave if (cny_cp_interleave() == CNY_MI_3131) { printf("ERROR - interleave set to 3131, this personality requires binary interleave\n"); exit (1); } // read AEG control register to see if crossbar is present int crossbar_enabled = 0; crossbar_enabled = i_copcall_fmt(sig, cpXbar, ""); #ifdef DEBUG printf("UserApp: crossbar_enabled = %d\n", crossbar_enabled); #endif /*-------DECLARE INPUT & OUTPUT FILES-------*/ bmpInput = fopen(argv[1], "rb"); bmpOutput = fopen("edgeSob.bmp", "wb"); /*---SET POINTER TO BEGINNING OF FILE----*/ fseek(bmpInput, 0L, SEEK_END); /*-------GET INPUT BMP DATA--------*/ fileSize = getImageInfo(bmpInput, 2, 4); originalImage.cols = (int)getImageInfo(bmpInput, 18, 4); originalImage.rows = (int)getImageInfo(bmpInput, 22, 4); edgeImage.rows = originalImage.rows; edgeImage.cols = originalImage.cols; size=originalImage.cols; // Allocate memory // if the crossbar is enabled in the hardware, use malloc // if no crossbar, the memory must be aligned on MC 0 if (crossbar_enabled) { if (cny_cp_malloc) { a1 = (uint64 *) (cny_cp_malloc)(size*8); a2 = (uint64 *) (cny_cp_malloc)(size*8); a3 = (uint64 *) (cny_cp_malloc)(size*8); a4 = (uint64 *) (cny_cp_malloc)(size*8); } else printf("malloc failed\n"); } else {
cny_cp_posix_memalign((void**)&a1, 512, size*8); cny_cp_posix_memalign((void**)&a2, 512, size*8); cny_cp_posix_memalign((void**)&a3, 512, size*8);
cny_cp_posix_memalign((void**)&a4, 512, size*8); } #ifdef DEBUG printf("a1 = %llx MC = %lld \n", a1, ((uint64)a1>>6)&7); printf("a2 = %llx MC = %lld \n", a2, ((uint64)a2>>6)&7); printf("a3 = %llx MC = %lld \n", a3, ((uint64)a3>>6)&7); #endif /*--------PRINT DATA TO SCREEN----------*/ printf("Width: %d\n", originalImage.cols); printf("Height: %d\n", originalImage.rows); printf("File size: %lu\n", fileSize); nColors = (int)getImageInfo(bmpInput, 46, 4); printf("nColors: %d\n", nColors); /*------ALLOCATE MEMORY FOR FILES--------*/ vectorSize = fileSize - (14+40+4*nColors); printf("vectorSize: %lu\n", vectorSize); edgeImage.data = (unsigned char*) malloc(vectorSize*sizeof(unsigned char)); if(edgeImage.data == NULL) {
printf("Failed to malloc edgeImage.data\n"); exit(0);
} printf("%lu bytes malloc'ed for edgeImage.data\n", vectorSize); originalImage.data = (unsigned char*) malloc(vectorSize*sizeof(unsigned char)); if(originalImage.data == NULL) {
printf("Failed to malloc originalImage.data\n"); exit(0);
} printf("%lu bytes malloc'ed for originalImage.datt\n", vectorSize); /*------COPY HEADER AND COLOR TABLE---------*/ copyImageInfo(bmpInput, bmpOutput); copyColorTable(bmpInput, bmpOutput, nColors); fseek(bmpInput, (14+40+4*nColors), SEEK_SET); fseek(bmpOutput, (14+40+4*nColors), SEEK_SET); for(row=0; row<=originalImage.rows-1; row++) {
for(col=0; col<=originalImage.cols-1; col++) { fread(pChar, sizeof(char), 1, bmpInput); *(originalImage.data + row*originalImage.cols + col) = *pChar; }
} // populate operand arrays, initialize a3 for (row=0; row < (originalImage.rows-2); row++){ for(i=0; i< size; i++){ a1[i]=*(originalImage.data + row*originalImage.cols +i); a2[i]=*(originalImage.data + (row+1)*originalImage.cols +i); a3[i]=*(originalImage.data + (row+2)*originalImage.cols +i);} // sobel copcall act_sum = l_copcall_fmt(sig, cpVSobel, "AAAAA", a1, a2, a3, a4, size); for(i=0; i< size; i++){ *(edgeImage.data +(row+1)*size+i)=a4[i];} } fwrite(a4, sizeof(char)*originalImage.cols*originalImage.rows, 1, bmpOutput); } return 0; } /*----------GET IMAGE INFO SUBPROGRAM--------------*/ long getImageInfo(FILE* inputFile, long offset, int numberOfChars) { unsigned char *ptrC; long value = 0L; unsigned char dummy; int i; dummy = '0'; ptrC = &dummy; fseek(inputFile, offset, SEEK_SET); for(i=1; i<=numberOfChars; i++) { fread(ptrC, sizeof(char), 1, inputFile); /* calculate value based on adding bytes */ value = (long)(value + (*ptrC)*(pow(256, (i-1)))); } return(value); } /* end of getImageInfo */ /*-------------COPIES HEADER AND INFO HEADER----------------*/ void copyImageInfo(FILE* inputFile, FILE* outputFile) { unsigned char *ptrC; unsigned char dummy; int i; dummy = '0'; ptrC = &dummy; fseek(inputFile, 0L, SEEK_SET); fseek(outputFile, 0L, SEEK_SET); for(i=0; i<=50; i++) { fread(ptrC, sizeof(char), 1, inputFile); fwrite(ptrC, sizeof(char), 1, outputFile); } } /*----------------COPIES COLOR TABLE-----------------------------*/ void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors) { unsigned char *ptrC; unsigned char dummy; int i; dummy = '0'; ptrC = &dummy; fseek(inputFile, 54L, SEEK_SET); fseek(outputFile, 54L, SEEK_SET); for(i=0; i<=(4*nColors); i++) /* there are (4*nColors) bytesin color table */ { fread(ptrC, sizeof(char), 1, inputFile); fwrite(ptrC, sizeof(char), 1, outputFile); } }