IMG_SW.C


 /*************************************************************************** * * img_sw.c - Image Convolution Software Processes and Testbench * * Copyright (c) 2004 by Green Mountain Computing Systems, Inc. * All rights reserved. *  ***************************************************************************/ #include <stdio.h> #include "co.h" #include "cosim_log.h" #include "img.h" #ifndef USE_FPGA #define USE_FPGA 1 #endif extern co_architecture co_initialize(void *); #include "test.h" static void prn_img(int width, int height, int rowwidth, uint16 *datain, int invert) {     int x, y;    unsigned int data;    uint16 *bp;    bp = datain;    for ( y = 0; y < height; y++ ) {             for ( x = 0; x < width; x++ ) {                     data  = ((*bp) >> 8) & 0xf8;                    data += ((*bp) >> 3) & 0xf8;                    data += (*(bp++) << 2) & 0xfc;                    data /= 3;                    if (invert) data = 255 - data;                    data = (data & 0x10) ? (data >> 5) + 1 : data >> 5;                    switch ( data ) {                     case 8:                    case 7:                          printf("#");                          break;                    case 6:                          printf("&");                          break;                    case 5:                          printf("G");                          break;                    case 4:                          printf("O");                          break;                    case 3:                          printf("o");                          break;                    case 2:                          printf(",");                          break;                    case 1:                          printf(".");                          break;                    case 0:                          printf(" ");                          break;                 }               }               bp += rowwidth - width;              printf("\ n");   }  }  void call_fpga(co_memory imgmem, co_signal start, co_signal end) {    IF_NSIM(long dur;)    int i, j, k;    uint16 *inp, *datain, *dataout;    uint32 data;    IF_SIM(cosim_logwindow log;)    IF_SIM(log = cosim_logwindow_create("call_fpga");)    datain = co_memory_ptr(imgmem);    k = 0;    for ( i = 0; i < IMG_HEIGHT; i++ ) {              for ( j = 0; j < IMG_WIDTH; j++ ) {                      if ( (j%32) == 0 ) inp = testimage16 + (i % 32) * 32;                     datain[k++] = *(inp++);           }    }    printf("Image in:\ n");    prn_img(32, 32, IMG_WIDTH, datain, 1);    printf("Running...\ n");    IF_SIM(cosim_logwindow_write(log, "Running...\ n");)    co_signal_post(start, 0);    co_signal_wait(end, &data);    printf("Done\ n");    IF_SIM(cosim_logwindow_write(log, "Done\ n");)    dataout = co_memory_ptr(imgmem);    printf("Image out:\ n");    prn_img(32, 32, IMG_WIDTH, dataout, 0); } #if !USE_FPGA void filter() {     IF_NSIM(long dur;)    int i, j, k;    uint16 *dp, *datain, *dataout;    uint16 p00, p01, p02, p10, p11, p12, p20, p21, p22, d0;    uint32 data;    dataout = malloc((IMG_HEIGHT + 1) * IMG_WIDTH * sizeof(uint16));    datain = dataout + IMG_WIDTH;    k = 0;    for ( i = 0; i < IMG_HEIGHT; i++ ) {             for ( j = 0; j < IMG_WIDTH; j++ ) {                     if ( (j%32) == 0 )                        dp = testimage16 + (i % 32) * 32;                    datain[k++] = *(dp++);            }    }    printf("Image in:\ n");    prn_img(32, 32, IMG_WIDTH, datain, 1);    printf("Running...\ n");    dp = dataout;    k = IMG_WIDTH; for ( i = 1; i < IMG_HEIGHT - 2; i++ ) {          for ( j = 0; j < IMG_WIDTH; j++ ) {                  p00 = datain[k - (IMG_WIDTH + 1)];                 p01 = datain[k - IMG_WIDTH];                 p02 = datain[k - (IMG_WIDTH - 1)];                 p10 = datain[k - 1];                 p11 = datain[k];                 p12 = datain[k + 1];                 p20 = datain[k + (IMG_WIDTH - 1)];                 p21 = datain[k + IMG_WIDTH];                 p22 = datain[k + (IMG_WIDTH + 1)];                 /* red */                 d0  = ((p11 >> 8) & 0xf8) << 3; /* center * 8 */                 d0 -= (p00 >> 8) & 0xf8;                 d0 -= (p01 >> 8) & 0xf8;                 d0 -= (p02 >> 8) & 0xf8;                 d0 -= (p10 >> 8) & 0xf8;                 d0 -= (p12 >> 8) & 0xf8;                 d0 -= (p20 >> 8) & 0xf8;                 d0 -= (p21 >> 8) & 0xf8;                 d0 -= (p22 >> 8) & 0xf8;                 d0 &= (d0 >> 15) - 1;                 data = (d0 & 0xf8) << 8;                 /* green */                 d0  = ((p11 >> 3) & 0xf8) << 3; /* center * 8 */                 d0 -= (p00 >> 3) & 0xf8;                 d0 -= (p01 >> 3) & 0xf8;                 d0 -= (p02 >> 3) & 0xf8;                 d0 -= (p10 >> 3) & 0xf8;                 d0 -= (p12 >> 3) & 0xf8;                 d0 -= (p20 >> 3) & 0xf8;                 d0 -= (p21 >> 3) & 0xf8;                 d0 -= (p22 >> 3) & 0xf8;                 d0 &= (d0 >> 15) - 1;                 data |= (d0 & 0xf8) << 3;                 /* blue */                 d0  = (p11 & 0x3f) << 5; /* center * 8 */                 d0 -= (p00 & 0x3f) << 2;                 d0 -= (p01 & 0x3f) << 2;                 d0 -= (p02 & 0x3f) << 2;                 d0 -= (p10 & 0x3f) << 2;                 d0 -= (p12 & 0x3f) << 2;                 d0 -= (p20 & 0x3f) << 2;                 d0 -= (p21 & 0x3f) << 2;                 d0 -= (p22 & 0x3f) << 2;                 d0 &= (d0 >> 15) - 1;                 data |= (d0 & 0xfc) >> 2;                 (*dp++) = data;                  k++;            }     }     printf("Done\ n");    printf("Image out:\ n");    prn_img(32, 32, IMG_WIDTH, dataout, 0); }  #endif int main(int argc, char *argv[]) {     IF_SIM(int c;)    co_architecture my_arch;    printf("Edge Detect Demo\ n");    printf("----------------\ n"); #if USE_FPGA    my_arch = co_initialize(NULL);    co_execute(my_arch); #else    filter(); #endif    IF_SIM(printf("Press Enter key to continue...\ n");)    IF_SIM(c = getc(stdin);)    return(0); }  



    Practical FPGA Programming in C
    Practical FPGA Programming in C
    ISBN: 0131543180
    EAN: 2147483647
    Year: 2005
    Pages: 208

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net