java - Extracting Memory Initialization File (MIF) from a BMP photo -
i working on de12-115 microprocessor altera using quartus. in order display bmp image onto monitor using built-in vga connections, must first transform bmp image mif format. mif format nothing lookup table specifies address of each pixel , alias of each color using rgb color code. sample mif file have following shape
depth = 32; -- size of data in bits width = 8; -- size of memory in words address_radix = hex; -- radix address values data_radix = bin; -- radix 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;
i haven't found software enable me transform own images format above. however, i've found c code it. since not acquainted c, wondering if me understand code, library imports etc... can transform java. great if explains me how extract mif format photo , write own code scratch. c code below. thank in advance
// pictest.cpp : defines entry point 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 typedef struct { unsigned short bftype; unsigned int bfsize; unsigned short bfreserved1; unsigned short bfreserved2; unsigned int bfoffbits; } bitmapfileheader; #pragma pack() // , # 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 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 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 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 bitmap file header fread(&bitmapfileheader, sizeof(bitmapfileheader),1,fileptr); //verify bmp file check bitmap id if (bitmapfileheader.bftype !=0x4d42) { fclose(fileptr); return null; } //read bitmap info header fread(bitmapinfoheader, sizeof(bitmapinfoheader),1,fileptr); //move file point begging of bitmap data fseek(fileptr, bitmapfileheader.bfoffbits, seek_set); //allocate enough memory bitmap image data bitmapimage = (unsigned char*)malloc(bitmapinfoheader->bisizeimage); //verify memory allocation if (!bitmapimage) { free(bitmapimage); fclose(fileptr); return null; } //read in bitmap image data fread(bitmapimage,bitmapinfoheader->bisizeimage,1,fileptr); //make sure bitmap image data read if (bitmapimage == null) { fclose(fileptr); return null; } //swap r , b values rgb (bitmap bgr) /*for (imageidx = 0,imageidx < bitmapinfoheader->bisizeimage;imageidx+=3) { temprgb = bitmapimage[imageidx]; bitmapimage[imageidx] = bitmapimage[imageidx + 2]; bitmapimage[imageidx + 2] = temprgb; }*/ //close file , 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 open file 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; (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 open file 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; (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; }
try use matlab. become easy. if yo use c, should define struct, try jump header of picture. meaningless. if use matlab, cam open picture , data! matlab code, hope you:
%mcode create mif file src = imread('lena.jpg'); gray = rgb2gray(src); [m,n] = size( gray ); %size od picture n = m*n; %your ram or rom depth。 word_len = 8; data = reshape(gray, 1, n);% reshape 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'); = 0 : n-1 fprintf(fid, '\t%d\t:\t%x;\n',i, data(i+1)); end fprintf(fid, 'end;\n'); % prinf end fclose(fid); % close file
Comments
Post a Comment