bmp转mif c语言,【原创】bmp转mif、coe或hex软件发布及使用介绍

有时候,想用FPGA在显示器上显示一幅图像,需要将一定尺寸的图像转换为mif、coe或hex文件(它们都是ROM的一种存储文件格式),于是需要一款可以实现bmp转mif、coe或hex文件的软件。在网上倒是可以找到几款相应的软件,但用得非常不爽,要么需要几款软件结合起来用(如Image2Lcd、C2Mif),要么无法输出RGB888、RGB565数据格式的mif文件(如Pic2Mif)、要么无法生成coe文件(Xilinx的用户需要)等等。基于对这些情况的不满,笔者自己动手开发了一款方便使用且集多功能于一身的BMP转Mif、Coe或Hex文件的软件,如图1所示。

dbd9dd2bfcdf02284d0bea7372d8d284.png

图1  BMP2MifOrCoe软件

打开BMP2MifOrCoe软件,其主界面如图2所示。

a105f0215a7bf2f27c75c2e41613a4e4.png

图2  BMP2Mif软件主界面

点击“帮助”菜单可以帮助用户使用该软件,如图3所示。

648469de6bf7749be023439499240a48.png

图3  “帮助(H)”菜单

点击“关于(A)”菜单可以了解该软件的相关信息,如图4所示。

151f038950408232fabe086a1d1d9d0f.png

图4  “关于(A)”菜单

点击“加载...”按钮可进行8位或24位bmp图片的加载,加载成功后按钮上的“加载...”将变成“加载成功”,在位图属性框里则显示图像的名称和大小等,如图5所示。

a8d41feb615e424aeb9edf5ee00e8699.png

图5 bmp图片加载

接着,就可以选择输出图像数据的格式。有RGB888(24位真彩色)、RGB565(16位真彩色)、Gray8(灰度)和1Bit(单色)四种,可以选择其中一种进行输出,如果不选,默认输出RGB888格式。

然后,选择文件类型。有Mif、Coe和Hex三种文件类型,选择其中一种进行输出,如果不选,默认输出Mif文件。

最后,点击“一键转换”按钮生成文件。如果文件类型选为Mif,则在电脑桌面生成CrazyBird.mif,如图6所示;如果文件类型选为Coe,则在电脑桌面生成CrazyBird.coe,如图7所示;如果文件类型选为Hex,则在桌面生成CrazyBird.hex,如图8所示。生成的各类文件,如图9所示。

155a102de51c79c47a9fe8c3209d2c24.png

图6  生成mif文件

a9a9910edd2e1abc5f6161b731b4b7d4.png

图7  生成coe文件

d99752c028cd3600ccb84dde9b20e506.png

图8  生成hex文件

91977a90c51f88a633ea15f06f67d34b.png

图6  在桌面生成各类文件

最后的最后,给出测试效果,如图7和图8所示。

4866e2d3af37a17d0522fab47e228b29.png

图7  RGB565图像显示测试

dfb0cb204f823b60f37c2aa958e89cce.png

图8  单色图像显示测试

BMP2MifOrCoe软件下载:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bmp To Mif 换器 // (karimov 2005) // This program was originnaly written by one of the ECE241 students to convert an image // supplied in a BMP file into an MIF file format for use with Quartus II. // // This program has recently been modified to work with the new VGA controller used with the DE2 // board. // // What to do: // 1. Create an image in Microsoft paint (or other program). The image must be 160 pixels wide, 120 pixels high and // use 24-bits to represent colour information. // 2. Once you create the image you need, flip it up-side down. Then save the BMP file. (example: foo.bmp) // 3. Run this converter in command prompt using the name of the BMP file you created as a command-line parameter. // For example: // bmp2mif foo.bmp // 4. The program generates two files: // image.colour.mif - an MIF file with 3 bits colour information obtained from the BMP file you supplied // image.mono.mif - an MIF file containing 1 bit of colour for every pixel on the screen. The dot will either be // black or white. // You can change the file names once they are created, but they should still have the .mif extension. // // 5. Copy the proper MIF file to the directory where your design is located and include it in your project. // 6. Change the BACKGROUND_IMAGE parameter of the VgaAdapter to indicate your MIF file. // 7. The COLOR_CHANNEL_DEPTH parameter must be set to 1 to work with the image.colour.mif file. #include #include #define FLIP_INT(c) ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <> 8) | ((c & 0x00FF) << 8) typedef struct s_header { unsigned short bfType; unsigned int bfSize; unsigned short reserved1; unsigned short reserved2; unsigned int offset; } t_bmp_header; typedef struct s_bmp_info { unsigned int biSize; unsigned int biWidth; unsigned int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; unsigned int biXPelsPerMeter; unsigned int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } t_bmp_info; int faprint(FILE *fcol, FILE *fm, const char *pattern) { fprintf(fcol, pattern); return fprintf(fm, pattern); } int main(int argc, char* argv[]) { FILE *f, *fcol, *fm; int y; unsigned int x, c, r, g, b; unsigned int width, height; if (argc != 2) { printf("Usage: bmp2mif \n"); return 0; } else { printf("Input file is: %s\n", argv[1]); } printf("This program converts n x m 24-bit .BMP image to MIF file\n"); printf("There are 2 files produced:\n"); printf("\timage.colour.mif - 8-colour channel, n x m x 3\n"); printf("\timage.mono.mif - black and white image, n x m x 1\n\n"); f = fopen(argv[1], "rb"); fcol = fopen("image.colour.mif", "wb"); fm = fopen("image.mono.mif", "wb"); if (f) { t_bmp_header header; t_bmp_info info; fread(&header, 14, 1, f); /* sizeof(t_bmp_header) returns 16 instead of 14. Should be 14. */ fread(&info, sizeof(t_bmp_info), 1, f); #if !defined (WIN32) header.bfSize = FLIP_INT(header.bfSize); header.bfType = FLIP_SHORT(header.bfType); header.offset = FLIP_INT(header.offset); header.reserved1 = FLIP_SHORT(header.reserved1); header.reserved2 = FLIP_SHORT(header.reserved2); info.biSize = FLIP_INT(info.biSize); info.biWidth = FLIP_INT(info.biWidth); info.biHeight = FLIP_INT(info.biHeight); info.biPlanes = FLIP_SHORT(info.biPlanes); info.biBitCount = FLIP_SHORT(info.biBitCount); info.biCompression = FLIP_INT(info.biCompression); info.biSizeImage = FLIP_INT(info.biSizeImage); info.biXPelsPerMeter = FLIP_INT(info.biXPelsPerMeter); info.biYPelsPerMeter = FLIP_INT(info.biYPelsPerMeter); info.biClrUsed = FLIP_INT(info.biClrUsed); info.biClrImportant = FLIP_INT(info.biClrImportant); #endif printf("Input file is %ix%i %i-bit depth\n", info.biWidth, info.biHeight, info.biBitCount); if (info.biBitCount == 24) { char temp[100]; width = info.biWidth; height = info.biHeight; printf("Converting...\n"); sprintf(temp, "Depth = %i;\r\n",width*height); faprint(fcol, fm, temp); fprintf(fcol, "Width = 3;\r\n"); fprintf(fm, "Width = 1;\r\n"); faprint(fcol, fm, "Address_radix=dec;\r\n"); faprint(fcol, fm, "Data_radix=bin;\r\n"); faprint(fcol, fm, "Content\r\n"); faprint(fcol, fm, "BEGIN\r\n"); sprintf(temp, "\t[0..%i] : 000;\r\n", width*height - 1); fprintf(fcol, temp); sprintf(temp, "\t[0..%i] : 0;\r\n", width*height - 1); fprintf(fm, temp); fseek(f, 54, SEEK_SET); for(y=height-1; y >=0; y--) { x = 0; fprintf(fcol, "\t%i :", y*width+x); fprintf(fm, "\t%i :", y*width+x); for(x=0; x 0) && ((x % 40) == 0)) { fprintf(fcol, ";\r\n\t%i :", y*width + x); fprintf(fm, ";\r\n\t%i :", y*width + x); } #if defined (WIN32) c = ((c >> 24) & 0x000000FF) | ((c & 0x00FF0000) >> 8) | ((c & 0x0000FF00) << 8) | ((c & 0x000000FF) <>= 8; b = (c & 0xFF0000) >> 16; g = (c & 0x00FF00) >> 8; r = (c & 0x0000FF); c = r + g + b; c /= 3; r = (r >= 128 ? 1 : 0); g = (g >= 128 ? 1 : 0); b = (b >= 128 ? 1 : 0); c = (c >= 128 ? 1 : 0); fprintf(fcol, " %i%i%i", r, g, b); fprintf(fm, " %i", c); } faprint(fcol, fm, ";\r\n"); if ((x*3) % 4 != 0) { fread(&c, 4-((x*3) % 4), 1, f); } } faprint(fcol, fm, "End;\r\n"); } else printf("Input file image.bmp is not in a 24-bit colour format!\n"); fclose(fm); fclose(fcol); fclose(f); printf("All done.\n"); } else printf("Cannot open input file. Check for input.bmp\n"); }
BMP是一种常见的位图图片格式,而MIF是一种用于描述存储器初始化数据的文件格式。将一个BMP图像换成MIF文件,可以使该图像能够被存储到FPGA等硬件设备中。 Python是一种功能强大的编程语言,它提供了许多库和工具,可以帮助我们完成这个任务。下面是一个大致的步骤来实现这个换过程: 1. 导入必要的库,比如PIL库(Python Imaging Library)。 2. 读取BMP图像文件,可以使用PIL库的`Image.open()`函数。 3. 将图像换成灰度图像,可以使用PIL库的`convert()`函数。 4. 获取图像的宽度和高度,可以使用PIL库的`size`属性。 5. 创建MIF文件,并写入必要的MIF文件头和格式。 6. 遍历图像的每个像素,并将其换为MIF文件中的格式。对于每个像素,可以将其灰度值换为二进制并写入MIF文件。 7. 关闭MIF文件。 下面是一个简单的示例代码: ```python from PIL import Image # 读取BMP图像文件 image = Image.open('input.bmp') # 将图像换成灰度图像 gray_image = image.convert('L') # 获取图像的宽度和高度 width, height = gray_image.size # 创建MIF文件 mif_file = open('output.mif', 'w') # 写入MIF文件头和格式 mif_file.write('DEPTH = %d;\n' % width*height) mif_file.write('WIDTH = 8;\n') mif_file.write('ADDRESS_RADIX = HEX;\n') mif_file.write('DATA_RADIX = BIN;\n') mif_file.write('CONTENT\n') mif_file.write('BEGIN\n') # 遍历图像的每个像素,并将其换为MIF文件中的格式 for y in range(height): for x in range(width): pixel_value = gray_image.getpixel((x, y)) binary_value = bin(pixel_value)[2:].zfill(8) mif_file.write('%04X : %s;\n' % (y*width + x, binary_value)) # 关闭MIF文件 mif_file.write('END;\n') mif_file.close() ``` 这段代码中,我们首先使用PIL库打开BMP图像文件,并将其换成灰度图像。然后,我们获取图像的宽度和高度,并创建一个新的MIF文件。接下来,我们遍历图像的每个像素,将其换为二进制格式,并写入MIF文件中。最后,我们关闭MIF文件。运行这段代码后,将会生成一个名为"output.mif"的MIF文件,其中包含了用于初始化存储器的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值