Team Gryffindor - Sobel Source Code: Difference between revisions

From Cpre584
Jump to navigation Jump to search
New page: == UserApp.c == <nowiki>#include <convey/usr/cny_comp.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include "image.h" #undef DEBUG typedef unsigned ...
 
Line 60: Line 60:


== image.h ==
== image.h ==
<nowiki>
typedef struct {int rows; int cols; unsigned char* data;} sImage;


void writeImage(char *filename, sImage *edgeImage);
void readImage(char* filename, sImage *originalImage, sImage *edgeImage);
</nowiki>


== image.c ==
== image.c ==

Revision as of 23:43, 20 February 2012

UserApp.c

#include <convey/usr/cny_comp.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "image.h"

#undef DEBUG

typedef unsigned long long uint64;
extern void cpSobel();
extern int cpXbar();
void usage (char *);

int main(int argc, char *argv[])
{
	// Check arguments
	if (argc != 2) {
		printf("Usage: %s bmpInput.bmp\n", argv[0]);
    	exit(1);	
	}

    // Get personality signature
    cny_image_t sig, sig2;
    int stat;
    cny_get_signature("pdk", &sig, &sig2, &stat);

    if (stat) {
        printf("***ERROR: cny_get_signature() Failure: %d\n", stat);
        exit(1);
    }

    // Check memory interleave
    if (cny_cp_interleave() == CNY_MI_3131) {
        printf("ERROR - interleave set to 3131, this personality requires binary interleave\n");
        exit(1);
    }

    // Check for crossbar support
    if (!i_copcall_fmt(sig, cpXbar, ""))    {
	    printf("ERROR - Crossbar not enabled!");
	    exit(1);
    }

	// Read in bitmap data
	sImage original, edge;
	readImage(argv[1], &original, &edge);

    // Coprocessor Call
    copcall_fmt(sig, cpSobel, "AAAA", original.data, edge.data, original.rows, original.cols);

	// Write bitmap data
	writeImage(argv[1], &edge);

    return 0;
}

image.h

typedef struct {int rows; int cols; unsigned char* data;} sImage;

void writeImage(char *filename, sImage *edgeImage);
void readImage(char* filename, sImage *originalImage, sImage *edgeImage);

image.c

/*
  FILE: edgeSob.c - WORKS!!
  AUTH: Chad Nelson (originally by Bill Green)
  DESC: 2 3x3 Sobel masks for edge detection
  DATE: 02/20/2012
  REFS: edgeLap.c
*/

#include <convey/usr/cny_comp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "image.h"

/*-------PROTOTYPES---------*/
long getImageInfo(FILE*, long, int);
void copyImageInfo(FILE* inputFile, FILE* outputFile);
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors);



void readImage(char* filename, sImage *originalImage, sImage *edgeImage)
{
    FILE          *bmpInput;
    unsigned long vectorSize;
    unsigned long fileSize;
    unsigned char *pChar, someChar;
    unsigned int  row, col, nColors;

    someChar = '0'; pChar = &someChar;

    printf("Reading filename: %s\n", filename);

    /*-------DECLARE INPUT & OUTPUT FILES-------*/
    bmpInput = fopen(filename, "rb");

    /*---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;

    /*--------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 = farmalloc(vectorSize*sizeof(unsigned char));
    edgeImage->data = (unsigned char *) (cny_cp_malloc)(vectorSize*sizeof(unsigned char));

// originalImage.data = farmalloc(vectorSize*sizeof(unsigned char));
    originalImage->data = (unsigned char *) (cny_cp_malloc)(vectorSize*sizeof(unsigned char));

    if(edgeImage->data == NULL || originalImage->data == NULL) {
	    printf("Failed to cny_cp_malloc image space\n");
	    exit(0);
    }
    printf("%lu bytes malloc'ed for image data\n", vectorSize);

    fseek(bmpInput, (14+40+4*nColors), SEEK_SET);

    /* Read input.bmp and store it's raster data into originalImage.data */
    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;
	    }
    }

	fclose(bmpInput);
}


void writeImage(char *filename, sImage *edgeImage) {
	FILE *bmpInput, *bmpOutput;
    int   nColors;
    int   X, Y;

	/*-------DECLARE INPUT & OUTPUT FILES-------*/
    bmpInput = fopen(filename, "rb");
    bmpOutput = fopen("edgeSob.bmp", "wb");

    /*---SET POINTER TO BEGINNING OF FILE----*/
    fseek(bmpInput, 0L, SEEK_END);

    /*-------GET INPUT BMP DATA--------*/
    nColors = (int)getImageInfo(bmpInput, 46, 4);

	/*------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(Y=0; Y<=(edgeImage->rows-1); Y++)  {
	    for(X=0; X<=(edgeImage->cols-1); X++)  {
	        fwrite((edgeImage->data + X + Y*edgeImage->cols), sizeof(char), 1, bmpOutput);
        }
    }

    fclose(bmpInput);
    fclose(bmpOutput);

    printf("See edgeSob.bmp for results\n");
}


/*----------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);
  }

}

==