2015-04-25 2 views
1

Je travaille sur le microprocesseur DE12-115 d'Altera à l'aide de Quartus. Afin d'afficher une image BMP sur un moniteur en utilisant les connexions VGA intégrées, je dois d'abord transformer l'image BMP dans son format MIF. Le format MIF ne sera rien d'autre qu'une table de recherche qui spécifie l'adresse de chaque pixel et l'alias de chaque couleur en utilisant le code de couleur RVB. Un fichier MIF échantillon aura la forme suivanteExtraction du fichier d'initialisation de la mémoire (MIF) à partir d'une photo BMP

DEPTH = 32;     -- The size of data in bits 
WIDTH = 8;     -- The size of memory in words 
ADDRESS_RADIX = HEX;   -- The radix for address values 
DATA_RADIX = BIN;    -- The radix for data values 
CONTENT      -- start of (address : data pairs) 
BEGIN 

00 : 00000000;    -- memory address : data 
01 : 00000001; 
02 : 00000010; 
03 : 00000011; 
04 : 00000100; 
05 : 00000101; 
06 : 00000110; 
07 : 00000111; 
08 : 00001000; 
09 : 00001001; 
0A : 00001010; 
0B : 00001011; 
0C : 00001100; 

END; 

Je n'ai pas trouvé un logiciel qui me permettra de transformer mes propres images dans le format ci-dessus. Cependant, j'ai trouvé un code C qui le fait. Comme je ne connais pas C, je me demandais si quelqu'un pouvait m'aider à comprendre le code, les importations de la bibliothèque, etc ... pour que je puisse le transformer en JAVA. Ce serait bien aussi que quelqu'un m'explique comment extraire le format MIF d'une photo et que je puisse écrire mon propre code à partir de rien. Le code C est ci-dessous. Merci tout à l'avance

// PicTest.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

//#ifndef LaserMaze_Bitmap_h 
//#define LaserMaze_Bitmap_h 


#pragma pack(2) // Add this 

typedef struct 
{ 
    unsigned short bfType; 
    unsigned int bfSize; 
    unsigned short bfReserved1; 
    unsigned short bfReserved2; 
    unsigned int bfOffBits; 
} BITMAPFILEHEADER; 

#pragma pack() // and this 


# define BF_TYPE 0x4D42    /* "MB" */ 

typedef struct      /**** BMP file info structure ****/ 
{ 
    unsigned int biSize;   /* Size of info header */ 
    int   biWidth;   /* Width of image */ 
    int   biHeight;   /* Height of image */ 
    unsigned short biPlanes;   /* Number of color planes */ 
    unsigned short biBitCount;  /* Number of bits per pixel */ 
    unsigned int biCompression; /* Type of compression to use */ 
    unsigned int biSizeImage;  /* Size of image data */ 
    int   biXPelsPerMeter; /* X pixels per meter */ 
    int   biYPelsPerMeter; /* Y pixels per meter */ 
    unsigned int biClrUsed;  /* Number of colors used */ 
    unsigned int biClrImportant; /* Number of important colors */ 

    unsigned int RedMask;  /* Mask identifying bits of red component */ 
    unsigned int GreenMask;  /* Mask identifying bits of green component */ 
    unsigned int BlueMask;  /* Mask identifying bits of blue component */ 
    unsigned int AlphaMask;  /* Mask identifying bits of alpha component */ 
    unsigned int CSType;  /* Color space type */ 
    long RedX;   /* X coordinate of red endpoint */ 
    long RedY;   /* Y coordinate of red endpoint */ 
    long RedZ;   /* Z coordinate of red endpoint */ 
    long GreenX;  /* X coordinate of green endpoint */ 
    long GreenY;  /* Y coordinate of green endpoint */ 
    long GreenZ;  /* Z coordinate of green endpoint */ 
    long BlueX;   /* X coordinate of blue endpoint */ 
    long BlueY;   /* Y coordinate of blue endpoint */ 
    long BlueZ;   /* Z coordinate of blue endpoint */ 
    unsigned int GammaRed;  /* Gamma red coordinate scale value */ 
    unsigned int GammaGreen; /* Gamma green coordinate scale value */ 
    unsigned int GammaBlue;  /* Gamma blue coordinate scale value */ 
} BITMAPINFOHEADER; 

/* 
* Constants for the biCompression field... 
*/ 

# define BI_RGB  0    /* No compression - straight BGR data */ 
# define BI_RLE8  1    /* 8-bit run-length compression */ 
# define BI_RLE4  2    /* 4-bit run-length compression */ 
# define BI_BITFIELDS 3    /* RGB bitmap with RGB masks */ 

typedef struct      /**** Colormap entry structure ****/ 
{ 
    unsigned char rgbBlue;   /* Blue value */ 
    unsigned char rgbGreen;   /* Green value */ 
    unsigned char rgbRed;   /* Red value */ 
    unsigned char rgbReserved;  /* Reserved */ 
} RGBQUAD; 


unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader) 
{ 
    FILE *filePtr; //our file pointer 
    BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header 
    unsigned char *bitmapImage; //store image data int imageIdx=0; //image index counter 
    unsigned char tempRGB; //our swap variable 

    //open filename in read binary mode 
    filePtr = fopen(filename,"rb"); 
    if (filePtr == NULL) 
     return NULL; 

    //read the bitmap file header 
    fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr); 

    //verify that this is a bmp file by check bitmap id 
    if (bitmapFileHeader.bfType !=0x4D42) 
    { 
     fclose(filePtr); 
     return NULL; 
    } 

    //read the bitmap info header 
    fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr); 

    //move file point to the begging of bitmap data 
    fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET); 

    //allocate enough memory for the bitmap image data 
    bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage); 

    //verify memory allocation 
    if (!bitmapImage) 
    { 
     free(bitmapImage); 
     fclose(filePtr); 
     return NULL; 
    } 

    //read in the bitmap image data 
    fread(bitmapImage,bitmapInfoHeader->biSizeImage,1,filePtr); 

    //make sure bitmap image data was read 
    if (bitmapImage == NULL) 
    { 
     fclose(filePtr); 
     return NULL; 
    } 

    //swap the r and b values to get RGB (bitmap is BGR) 
    /*for (imageIdx = 0,imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3) 
    { 
     tempRGB = bitmapImage[imageIdx]; 
     bitmapImage[imageIdx] = bitmapImage[imageIdx + 2]; 
     bitmapImage[imageIdx + 2] = tempRGB; 
    }*/ 
    //close file and return bitmap iamge data 
    fclose(filePtr); 
    return bitmapImage; 
} 
double round(double d) 
{ 
    return floor(d + 0.5); 
} 


bool generateMIF(unsigned char *bitmapData, long tSize, char *file) 
{ 
    FILE * pFile; 
    pFile = fopen (file,"w"); 
    if (pFile==NULL) 
    { 
     printf("Unable to Open file to write \n"); 
     return 0; 
    } 
    char buff[40]; 
    sprintf(buff,"DEPTH = %d;\n",tSize/3); 
    fputs("WIDTH = 8;\n",pFile); 
    fputs(buff,pFile); 
    fputs("ADDRESS_RADIX = HEX;\n",pFile); 
    fputs("DATA_RADIX = HEX;\n",pFile); 
    fputs("CONTENT BEGIN\n",pFile); 
    long ind=0; 
    long addr=0; 
    for (ind=tSize-1;ind>=0; ind-=3) 
    { 

     unsigned char R=round(bitmapData[ind]/255.0*7.0); 
     unsigned char G=round(bitmapData[ind-1]/255.0*7.0); 
     unsigned char B=round(bitmapData[ind-2]/255.0*3.0); 

     unsigned char Var = R *32 + G *4 + B; 

     sprintf(buff,"%X : %X ;\n",addr,Var); 
     fputs(buff,pFile); 
     addr++; 
    } 
    fputs("END;\n",pFile); 
    fclose (pFile); 

} 


bool generateLUTMIF(char *file) 
{ 
    FILE * pFile; 
    pFile = fopen (file,"w"); 
    if (pFile==NULL) 
    { 
     printf("Unable to Open file to write \n"); 
     return 0; 
    } 
    char buff[40]; 
    fputs("WIDTH = 24;\n",pFile); 
    fputs("DEPTH = 256;\n",pFile); 
    fputs("ADDRESS_RADIX = HEX;\n",pFile); 
    fputs("DATA_RADIX = HEX;\n",pFile); 
    fputs("CONTENT BEGIN\n",pFile); 
    long ind=0; 
    long addr=0; 
    for (ind=0;ind<256; ind++) 
    { 
     unsigned char C=ind; 
     unsigned char R=C >> 5; 
     R = R* 255/7; 
     unsigned char G= (C >> 2)&0x07; 
     G= G* 255/7; 

     unsigned char B=C & 0x3; 
     B=B*255/3; 


     sprintf(buff,"%X : %02X%02X%02X ;\n",ind,R,G,B); 
     fputs(buff,pFile); 
     addr++; 
    } 
    fputs("END;\n",pFile); 
    fclose (pFile); 

} 



int _tmain(int argc, _TCHAR* argv[]) 
{ 
    printf("Reading Image... \n"); 
    BITMAPINFOHEADER bitmapInfoHeader; 

    unsigned char *bitmapData; 
    bitmapData = LoadBitmapFile("d:\\back_24.bmp",&bitmapInfoHeader); 
    long tSize= bitmapInfoHeader.biHeight *bitmapInfoHeader.biWidth * 3 ;//24 bps 
    generateMIF(bitmapData,tSize,"D:\\backMIF.txt"); 
    generateLUTMIF("D:\\lutMIF.mif"); 
    printf("Done !"); 
    return 0; 
} 

Répondre

1

Essayez d'utiliser Matlab. Cela deviendra si facile. Si vous utilisez c, vous devez définir une structure, essayez de sauter l'en-tête d'une image. c'est sans signification. Mais si vous utilisez Matlab, vous pouvez simplement ouvrir l'image et obtenir les données! Ceci est mon code Matlab, espoir pour vous aider:

%mcode to create a mif file 
    src = imread('lena.jpg'); 
    gray = rgb2gray(src); 
    [m,n] = size(gray); %size od your picture 

    N = m*n; %your ram or rom depth。 
    word_len = 8; 
    data = reshape(gray, 1, N);% reshape you picture's data 

    fid=fopen('gray_image.mif', 'w'); % open mif file 
    fprintf(fid, 'DEPTH=%d;\n', N); 
    fprintf(fid, 'WIDTH=%d;\n', word_len); 

    fprintf(fid, 'ADDRESS_RADIX = UNS;\n'); 
    fprintf(fid, 'DATA_RADIX = HEX;\n'); 
    fprintf(fid, 'CONTENT\t'); 
    fprintf(fid, 'BEGIN\n'); 
    for i = 0 : N-1 
    fprintf(fid, '\t%d\t:\t%x;\n',i, data(i+1)); 
    end 
    fprintf(fid, 'END;\n'); % prinf the end 
    fclose(fid); % close your file 
+0

Merci beaucoup! Cependant, je ne suis pas familier avec matlab, pourriez-vous me dire comment exactement je pourrais compiler le code et ajouter une image? Merci à un copain de groupe! – dou2abou

+0

@ dou2abou Si vous utilisez Matlab, vous n'avez pas besoin de compléter le code. il suffit d'ouvrir le code dans matlab, puis mettez votre image dans le chemin où le code est. et cliquez sur le bouton "Exécuter". bien sûr, vous shuold changer le nom du fichier. –

1

Je travaille sur un projet sur la configuration d'un FPGA Altera à un moniteur, donc je besoin d'un générateur de fichier MIF. J'ai vu qu'il n'y en a pas beaucoup en ligne, et certains ne compilent même pas.

J'ai donc décidé d'écrire le mien en Java, cela fonctionne et gère presque toutes les erreurs possibles. L'outil, cependant, va d'abord convertir l'image en niveaux de gris, et seulement alors le fichier MIF sera créé.

Vous pouvez trouver mon outil sur ma page sur GitHub.