% Written by: Holguer A Becerra %fpgalover.com %image to mif A = imread('pasto1.bmp'); [h,w,channels]= size(A); nombre_modulo='pasto1' fID = fopen('pasto1.v','w'); transparent=hex2dec('000');% color que se desea transparente dir=0; b=A/17; color_table=[]; color_table_order=[]; color_num=1; for y = 1:(h) for x = 1:(w) % R G B pixel=uint16(b(y,x,1))*256+uint16(b(y,x,2))*16+uint16(b(y,x,3)); encontro=0; for i=1:length(color_table) if(color_table(i)==pixel) encontro=1; end end if(encontro==0) color_table(color_num)=pixel; color_table_order(color_num)=color_num; color_num=color_num+1; end end end disp('numero de colores de la imagen:') disp(color_num-1); fprintf(fID,'module %s(input clk,input [3:0]scale,input on_off,input [10:0]x_video,input [10:0]y_video,input [10:0]x_pos,input [10:0]y_pos,output image_on,output [11:0]color);\n\n',nombre_modulo); fprintf(fID,'\nwire [10:0]real_x=(x_video[10:0]>>scale[3:0])-(x_pos[10:0]>>scale[3:0]);\nwire [10:0]real_y=(y_video[10:0]>>scale[3:0])-(y_pos[10:0]>>scale[3:0]);\n'); m=(ceil(log(color_num-1)/log(2))); fprintf(fID,'\nwire [14:0]bit_pos=real_y[10:0]*%d+real_x[10:0];',w); fprintf(fID,'\nreg [%d:0]datos;\n',(ceil(log(color_num-1)/log(2)))-1); fprintf(fID,'assign image_on= (color[11:0]!=%d) & on_off & (real_x[10:0]>11''d0) & (real_x[10:0]<=11''d%d)& (real_y[10:0]>11''d0) & (real_y[10:0]<=11''d%d);\n\n',transparent,w,h); fprintf(fID,'always@(posedge clk)\nbegin\n case(bit_pos[14:0])\n'); for y = 1:(h) for x = 1:(w) % R G B pixel=uint16(b(y,x,1))*256+uint16(b(y,x,2))*16+uint16(b(y,x,3)); for i=1:length(color_table) if(color_table(i)==pixel) if(pixel==transparent) else dir=(y-1)*w+(x-1); fprintf(fID,'\n %d:datos[%d:0]<=%d''d',dir,(ceil(log(color_num-1)/log(2)))-1,(ceil(log(color_num-1)/log(2)))); fprintf(fID,'%d;',color_table_order(i)); end end end end end fprintf(fID,'\n default: datos[%d:0]<=%d''h%d;\n',(ceil(log(color_num-1)/log(2)))-1,(ceil(log(color_num-1)/log(2))),0); fprintf(fID,'\n endcase\n'); fprintf(fID,'end\n'); fprintf(fID,'\n//Map Color\nassign color[11:0]='); for i=1:length(color_table) fprintf(fID, ' (datos[%d:0]==%d)?12''d%d:\n',m-1,i,color_table(i)); end fprintf(fID, ' 12''d%d;\n\n',transparent); fprintf(fID,'\nendmodule\n'); fclose(fID);