// fifo_test.h
// 2016/09/26 by marsee
//
#ifndef __FIFO_TEST_H__
#define __FIFO_TEST_H__
//#define HORIZONTAL_PIXEL_WIDTH 800
//#define VERTICAL_PIXEL_WIDTH 600
#define HORIZONTAL_PIXEL_WIDTH 640
#define VERTICAL_PIXEL_WIDTH 480
#endif
// fifo_test.cpp
// 2016/09/26 by marsee
//
#include <stdio.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include "fifo_test.h"
int fifo_test(hls::stream<ap_uint<32> >& ins, hls::stream<ap_uint<32> >& outs){
ap_uint<32> buf[HORIZONTAL_PIXEL_WIDTH];
ap_uint<32> ind, outd;
int i, j;
input : for(i=0; i<HORIZONTAL_PIXEL_WIDTH; i++){
ins >> buf[i];
}
output : for(j=0; j<HORIZONTAL_PIXEL_WIDTH; j++){
while(i <= j) ; // i よりも j が等しいか大きい場合はwait
outs << buf[j];
}
return 0;
}
// fifo_test_tb.cpp
// 2016/09/26 by marsee
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ap_int.h>
#include <hls_stream.h>
#include <iostream>
#include <fstream>
#include "fifo_test.h"
#include "bmp_header.h"
int fifo_test(hls::stream<ap_uint<32> >& ins, hls::stream<ap_uint<32> >& outs);
#define BMP_FILE_NAME "road_1.bmp"
int main()
{
using namespace std;
hls::stream<ap_uint<32> > ins;
hls::stream<ap_uint<32> > outs;
ap_uint<32> pix, result;
BITMAPFILEHEADER bmpfhr; // BMPファイルのファイルヘッダ(for Read)
BITMAPINFOHEADER bmpihr; // BMPファイルのINFOヘッダ(for Read)
FILE *fbmpr, *fbmpw;
ap_uint<32> *rd_bmp, *result_copy;
int blue, green, red;
if ((fbmpr = fopen(BMP_FILE_NAME, "rb")) == NULL){ // BMP をオープン
fprintf(stderr, "Can't open test.bmp by binary read mode\n");
exit(1);
}
// bmpヘッダの読み出し
fread(&bmpfhr.bfType, sizeof(char), 2, fbmpr);
fread(&bmpfhr.bfSize, sizeof(long), 1, fbmpr);
fread(&bmpfhr.bfReserved1, sizeof(short), 1, fbmpr);
fread(&bmpfhr.bfReserved2, sizeof(short), 1, fbmpr);
fread(&bmpfhr.bfOffBits, sizeof(long), 1, fbmpr);
fread(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpr);
// ピクセルを入れるメモリをアロケートする
if ((rd_bmp =(ap_uint<32> *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate rd_bmp memory\n");
exit(1);
}
if ((result_copy =(ap_uint<32> *)malloc(sizeof(int) * (bmpihr.biWidth * bmpihr.biHeight))) == NULL){
fprintf(stderr, "Can't allocate result_copy memory\n");
exit(1);
}
// rd_bmp にBMPのピクセルを代入。その際に、行を逆転する必要がある
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
blue = fgetc(fbmpr);
green = fgetc(fbmpr);
red = fgetc(fbmpr);
rd_bmp[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] = (blue & 0xff) | ((green & 0xff)<<8) | ((red & 0xff)<<16);
}
}
fclose(fbmpr);
for(int j=0; j < bmpihr.biHeight; j++){
for(int i=0; i < bmpihr.biWidth; i++){
pix = rd_bmp[(j*bmpihr.biWidth)+i];
ins << pix;
}
}
// 1行容量の fifo_test を呼び出しながら、malloc 領域に書き込む
for(int j=0; j < bmpihr.biHeight; j++){
fifo_test(ins, outs);
for(int i=0; i < bmpihr.biWidth; i++){
outs >> result;
result_copy[(j*bmpihr.biWidth)+i] = result;
}
}
// ハードウェアのラプラシアンフィルタの結果を result_copy.bmp へ出力する
if ((fbmpw=fopen("result_copy.bmp", "wb")) == NULL){
fprintf(stderr, "Can't open result_copy.bmp by binary write mode\n");
exit(1);
}
// BMPファイルヘッダの書き込み
fwrite(&bmpfhr.bfType, sizeof(char), 2, fbmpw);
fwrite(&bmpfhr.bfSize, sizeof(long), 1, fbmpw);
fwrite(&bmpfhr.bfReserved1, sizeof(short), 1, fbmpw);
fwrite(&bmpfhr.bfReserved2, sizeof(short), 1, fbmpw);
fwrite(&bmpfhr.bfOffBits, sizeof(long), 1, fbmpw);
fwrite(&bmpihr, sizeof(BITMAPINFOHEADER), 1, fbmpw);
// RGB データの書き込み、逆順にする
for (int y=0; y<bmpihr.biHeight; y++){
for (int x=0; x<bmpihr.biWidth; x++){
blue = result_copy[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] & 0xff;
green = (result_copy[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x] >> 8) & 0xff;
red = (result_copy[((bmpihr.biHeight-1)-y)*bmpihr.biWidth+x]>>16) & 0xff;
fputc(blue, fbmpw);
fputc(green, fbmpw);
fputc(red, fbmpw);
}
}
fclose(fbmpw);
free(rd_bmp);
free(result_copy);
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | - | - | - | - | - |