用c++写一个显示bmp的程序,可以植到linux windows unix dos
- User
用c++写一个显示bmp的程序,可以植到linux windows unix dos
- Assistant
以下是一个使用C++编写的显示BMP图片的程序,可以在Linux、Windows、Unix和DOS上运行:
#include <iostream> #include <fstream> #pragma pack(push, 1) // 禁用字节对齐 // BMP文件头结构体 struct BMPFileHeader { char signature[2]; uint32_t fileSize; uint32_t reserved; uint32_t dataOffset; }; // BMP信息头结构体 struct BMPInfoHeader { uint32_t headerSize; int32_t width; int32_t height; uint16_t colorPlanes; uint16_t bitsPerPixel; uint32_t compressionMethod; uint32_t imageSize; int32_t horizontalResolution; int32_t verticalResolution; uint32_t colorPaletteSize; uint32_t importantColors; }; // 像素结构体 struct RGBPixel { uint8_t blue; uint8_t green; uint8_t red; }; #pragma pack(pop) // 恢复字节对齐 // 函数声明 bool displayBMP(const std::string& filename); // 主函数 int main() { std::string filename = "image.bmp"; // 替换为你的BMP图片文件路径 if (!displayBMP(filename)) { std::cout << "无法显示BMP图片。\n"; } return 0; } bool displayBMP(const std::string& filename) { std::ifstream file(filename, std::ios::binary); if (!file) { return false; } BMPFileHeader fileHeader; file.read(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader)); if (fileHeader.signature[0] != 'B' || fileHeader.signature[1] != 'M') { return false; } BMPInfoHeader infoHeader; file.read(reinterpret_cast<char*>(&infoHeader), sizeof(infoHeader)); if (infoHeader.bitsPerPixel != 24 || infoHeader.compressionMethod != 0) { return false; } size_t imageSize = infoHeader.width * infoHeader.height; RGBPixel* pixels = new RGBPixel[imageSize]; file.seekg(fileHeader.dataOffset, std::ios::beg); file.read(reinterpret_cast<char*>(pixels), imageSize * sizeof(RGBPixel)); for (int row = infoHeader.height - 1; row >= 0; --row) { for (int col = 0; col < infoHeader.width; ++col) { RGBPixel pixel = pixels[row * infoHeader.width + col]; std::cout << "\033[48;2;" << static_cast<int>(pixel.red) << ";" << static_cast<int>(pixel.green) << ";" << static_cast<int>(pixel.blue) << "m "; } std::cout << "\033[0m\n"; } delete[] pixels; return true; } - User
用c写一个显示bmp的程序,可以植dos
- Assistant
以下是一个使用C语言编写的显示BMP图片的程序,可以在DOS环境下编译运行:
#include <stdio.h> #include <stdlib.h> #pragma pack(push, 1) // 禁用字节对齐 // BMP文件头结构体 struct BMPFileHeader { char signature[2]; unsigned int fileSize; unsigned int reserved; unsigned int dataOffset; }; // BMP信息头结构体 struct BMPInfoHeader { unsigned int headerSize; int width; int height; unsigned short colorPlanes; unsigned short bitsPerPixel; unsigned int compressionMethod; unsigned int imageSize; int horizontalResolution; int verticalResolution; unsigned int colorPaletteSize; unsigned int importantColors; }; // 像素结构体 struct RGBPixel { unsigned char blue; unsigned char green; unsigned char red; }; #pragma pack(pop) // 恢复字节对齐 // 函数声明 int displayBMP(const char* filename); // 主函数 int main() { const char* filename = "image.bmp"; // 替换为你的BMP图片文件路径 int result = displayBMP(filename); if (result != 0) { printf("无法显示BMP图片。\n"); } return 0; } int displayBMP(const char* filename) { FILE* file = fopen(filename, "rb"); if (file == NULL) { return -1; } struct BMPFileHeader fileHeader; fread(&fileHeader, sizeof(fileHeader), 1, file); if (fileHeader.signature[0] != 'B' || fileHeader.signature[1] != 'M') { fclose(file); return -1; } struct BMPInfoHeader infoHeader; fread(&infoHeader, sizeof(infoHeader), 1, file); if (infoHeader.bitsPerPixel != 24 || infoHeader.compressionMethod != 0) { fclose(file); return -1; } size_t imageSize = infoHeader.width * infoHeader.height; struct RGBPixel* pixels = (struct RGBPixel*)malloc(imageSize * sizeof(struct RGBPixel)); fseek(file, fileHeader.dataOffset, SEEK_SET); fread(pixels, sizeof(struct RGBPixel), imageSize, file); for (int row = infoHeader.height - 1; row >= 0; --row) { for (int col = 0; col < infoHeader.width; ++col) { struct RGBPixel pixel = pixels[row * infoHeader.width + col]; printf("\x1B[48;2;%d;%d;%dm ", pixel.red, pixel.green, pixel.blue); } printf("\x1B[0m\n"); } free(pixels); fclose(file); return 0; } - User
用c写一个彩色显示bmp的程序dos
- Assistant
以下是一个使用C语言编写的在DOS环境下彩色显示BMP图片的程序:
#include <stdio.h> #include <stdlib.h> #pragma pack(push, 1) // 禁用字节对齐 // BMP文件头结构体 struct BMPFileHeader { char signature[2]; unsigned int fileSize; unsigned int reserved; unsigned int dataOffset; }; // BMP信息头结构体 struct BMPInfoHeader { unsigned int headerSize; int width; int height; unsigned short colorPlanes; unsigned short bitsPerPixel; unsigned int compressionMethod; unsigned int imageSize; int horizontalResolution; int verticalResolution; unsigned int colorPaletteSize; unsigned int importantColors; }; // 像素结构体 struct RGBPixel { unsigned char blue; unsigned char green; unsigned char red; }; #pragma pack(pop) // 恢复字节对齐 // 函数声明 int displayBMP(const char* filename); // 主函数 int main() { const char* filename = "image.bmp"; // 替换为你的BMP图片文件路径 int result = displayBMP(filename); if (result != 0) { printf("无法显示BMP图片。\n"); } return 0; } int displayBMP(const char* filename) { FILE* file = fopen(filename, "rb"); if (file == NULL) { return -1; } struct BMPFileHeader fileHeader; fread(&fileHeader, sizeof(fileHeader), 1, file); if (fileHeader.signature[0] != 'B' || fileHeader.signature[1] != 'M') { fclose(file); return -1; } struct BMPInfoHeader infoHeader; fread(&infoHeader, sizeof(infoHeader), 1, file); if (infoHeader.bitsPerPixel != 24 || infoHeader.compressionMethod != 0) { fclose(file); return -1; } size_t imageSize = infoHeader.width * infoHeader.height; struct RGBPixel* pixels = (struct RGBPixel*)malloc(imageSize * sizeof(struct RGBPixel)); fseek(file, fileHeader.dataOffset, SEEK_SET); fread(pixels, sizeof(struct RGBPixel), imageSize, file); for (int row = infoHeader.height - 1; row >= 0; --row) { for (int col = 0; col < infoHeader.width; ++col) { struct RGBPixel pixel = pixels[row * infoHeader.width + col]; printf("\x1B[48;2;%d;%d;%dm ", pixel.red, pixel.green, pixel.blue); } printf("\x1B[0m\n"); } free(pixels); fclose(file); return 0; } - User
用ascii编写一个英文版支持原生dos的bmp显示程序
- Assistant
以下是一个使用ASCII字符显示英文版支持原生DOS的BMP图片的程序:
; bmp_display.asm ORG 100h SECTION .data filename: db "image.bmp",0 SECTION .text global _start _start: mov ah, 0x0e ; 设置文本模式 mov al, 0x05 ; 16色文本模式 int 0x10 mov ah, 0x09 ; 显示字符串 mov dx, welcome int 0x21 mov ah, 0x0a ; 读取键盘输入 int 0x21 mov al, dl ; 将输入的字符保存到al寄存器 cmp al, 'Y' ; 如果输入的字符是'Y',则继续执行 je load_bmp jmp exit load_bmp: mov ah, 0x0e ; 设置文本模式 mov al, 0x02 ; 2色文本模式 int 0x10 xor ax, ax ; 清空ax寄存器 mov ds, ax ; 设置段寄存器ds为0 mov ah, 0x0a ; 显示提示信息 mov dx, load_message int 0x21 mov ah, 0x48 ; 打开文件 mov al, 0x00 ; 只读模式 lea dx, [filename] int 0x21 jc exit ; 如果出现错误,跳转到退出 mov bx, ax ; 将文件句柄保存到bx寄存器 mov ah, 0x3f ; 读取文件 mov cx, 0xffff ; 读取整个文件 mov dx, buffer ; 数据缓冲区 mov al, 0x01 ; 文件函数号 int 0x21 cmp ax, cx ; 比较读取的字节数和请求的字节数 jne exit ; 如果不相等,表示读取失败,跳转到退出 mov ah, 0x0e ; 设置文本模式 mov al, 0x02 ; 2色文本模式 int 0x10 mov ah, 0x0a ; 显示提示信息 mov dx, display_message int 0x21 mov ax, 0x0000 ; 设置起始行 int 0x10 mov di, buffer ; 数据缓冲区的指针 display_loop: lodsb ; 加载一个字节到al寄存器 cmp al, 0 ; 比较al寄存器的值和0 je display_exit ; 如果等于0,表示遇到空字符,跳出循环 mov ah, 0x0e ; 写入字符到屏幕 mov bh, 0x00 ; 页面号 mov bl, 0x02 ; 文字属性 int 0x10 jmp display_loop display_exit: mov ah, 0x0a ; 换行显示 mov dx, crlf int 0x21 exit: mov ah, 0x4c ; 程序退出 xor al, al int 0x21 SECTION .data buffer: TIMES 64000 db 0 ; 数据缓冲区大小设为64000字节 welcome: db "Press 'Y' to display BMP image:", 0 load_message: db "Loading BMP image...", 0 display_message: db "Displaying BMP image:", 0 crlf: db 0x0D, 0x0A, 0x00 - User
[filename] 这里写文件名带不带【】
- Assistant
在代码中的
[filename]不应该带有方括号。方括号只是用来表示变量的占位符,在实际运行程序时,应该将[filename]替换为实际的文件名。换句话说,应该将[filename]替换为文件名的字符串,例如"image.bmp"。以下是一个修正后的示例:; bmp_display.asm ORG 100h SECTION .data filename db "image.bmp",0 SECTION .text global _start _start: mov ah, 0x0e ; 设置文本模式 mov al, 0x05 ; 16色文本模式 int 0x10 mov ah, 0x09 ; 显示字符串 mov dx, welcome int 0x21 mov ah, 0x0a ; 读取键盘输入 int 0x21 mov al, dl ; 将输入的字符保存到al寄存器 cmp al, 'Y' ; 如果输入的字符是'Y',则继续执行 je load_bmp jmp exit load_bmp: mov ah, 0x0e ; 设置文本模式 mov al, 0x02 ; 2色文