// 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;
}