8人参与 • 2025-03-06 • C/C++
c++ 将jpg图片变成16位565bmp图片
// consoleapplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include<string> #include <atlimage.h> bool convertjpgto16bitbmp565(const char* input_path, const char* output_path) { cimage srcimage, dstimage; // 1. 加载源图像 srcimage.load(input_path); int width = srcimage.getwidth(); int height = srcimage.getheight(); int srcbpp = srcimage.getbpp(); int srcpitch = srcimage.getpitch(); byte* srcbits = static_cast<byte*>(srcimage.getbits()); // 2. 创建目标图像(负高度表示自上而下存储) dword masks[] = { 0xf800, 0x07e0, 0x001f }; dstimage.createex(width, height, 16, bi_bitfields, masks); // 3. 获取目标图像参数(实际步长由api返回) int dstpitch = dstimage.getpitch(); byte* dstbits = static_cast<byte*>(dstimage.getbits()); // 5. 处理像素数据(无需反转行顺序) for (int y = 0; y < height; ++y) { byte* srcrow = srcbits + y * srcpitch; byte* dstrow = dstbits + y * dstpitch; for (int x = 0; x < width; ++x) { byte r, g, b; // 解析源像素颜色 if (srcbpp == 24) { // 24位bgr byte* p = srcrow + x * 3; b = p[0]; g = p[1]; r = p[2]; } else if (srcbpp == 32) { // 32位bgrx byte* p = srcrow + x * 4; b = p[0]; g = p[1]; r = p[2]; } else { return false; } // 转换为rgb565 word rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); // 安全写入目标像素 dstrow[x * 2] = static_cast<byte>(rgb565 & 0xff); // 低位字节 dstrow[x * 2 + 1] = static_cast<byte>((rgb565 >> 8)); // 高位字节 } } dstimage.save(output_path, gdiplus::imageformatbmp); return true; } int main() { if (__argc < 2) return 0; for (int i = 1; i < __argc; i++) { std::string h = __argv[i]; if (strcmp(h.substr(h.length() - 3).c_str(), "jpg")) continue; h=h.substr(0, h.length() - 3); h += "bmp"; printf(h.c_str()); convertjpgto16bitbmp565(__argv[i], h.c_str()); } return 0; }
特殊情况使用这个,这个的效果是将图片的前8位和后8位进行对调
#include <iostream> #include<string> #include <atlimage.h> bool convertjpgto16bitbmp565(const char* input_path, const char* output_path) { cimage srcimage, dstimage; // 1. 加载源图像 srcimage.load(input_path); int width = srcimage.getwidth(); int height = srcimage.getheight(); int srcbpp = srcimage.getbpp(); int srcpitch = srcimage.getpitch(); byte* srcbits = static_cast<byte*>(srcimage.getbits()); // 2. 创建目标图像(负高度表示自上而下存储) dword masks[] = { 0xf800, 0x07e0, 0x001f }; dstimage.createex(width, height, 16, bi_bitfields, masks); // 3. 获取目标图像参数(实际步长由api返回) int dstpitch = dstimage.getpitch(); byte* dstbits = static_cast<byte*>(dstimage.getbits()); // 5. 处理像素数据(无需反转行顺序) for (int y = 0; y < height; ++y) { byte* srcrow = srcbits + y * srcpitch; byte* dstrow = dstbits + y * dstpitch; for (int x = 0; x < width; ++x) { byte r, g, b; // 解析源像素颜色 if (srcbpp == 24) { // 24位bgr byte* p = srcrow + x * 3; b = p[0]; g = p[1]; r = p[2]; } else if (srcbpp == 32) { // 32位bgrx byte* p = srcrow + x * 4; b = p[0]; g = p[1]; r = p[2]; } else { return false; } // 转换为rgb565 word rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); // 安全写入目标像素 dstrow[x * 2] = static_cast<byte>((rgb565 >> 8)); // 低位字节 dstrow[x * 2 + 1] = static_cast<byte>(rgb565 & 0xff); // 高位字节 } } dstimage.save(output_path, gdiplus::imageformatbmp); return true; } int main() { if (__argc < 2) return 0; for (int i = 1; i < __argc; i++) { std::string h = __argv[i]; if (strcmp(h.substr(h.length() - 3).c_str(), "jpg")) continue; h=h.substr(0, h.length() - 3); h += "bmp"; printf(h.c_str()); convertjpgto16bitbmp565(__argv[i], h.c_str()); } return 0; }
方法补充
下面小编为大家整理了c++实现图片转换不同格式的方法,需要的可以了解下
#include <opencv2/opencv.hpp> #include <iostream> int main() { // 输入文件路径和输出文件路径 std::string inputimagepath, outputimagepath; std::cout << "enter the input image file path: "; std::cin >> inputimagepath; std::cout << "enter the output image file path (e.g., output.png, output.jpg): "; std::cin >> outputimagepath; // 读取图片 cv::mat image = cv::imread(inputimagepath, cv::imread_unchanged); // 检查图片是否成功加载 if (image.empty()) { std::cerr << "error: could not open or find the image!" << std::endl; return -1; } // 保存图片到新格式 bool success = cv::imwrite(outputimagepath, image); if (success) { std::cout << "image successfully converted and saved to " << outputimagepath << std::endl; } else { std::cerr << "error: could not save the image!" << std::endl; } return 0; }
到此这篇关于c++实现图片jpg格式变成16位565bmp格式的文章就介绍到这了,更多相关c++图片jpg转bmp内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论