C++實現(xiàn)圖片jpg格式變成16位565bmp格式
更新時間:2025年03月06日 09:31:46 作者:萬能的小裴同學(xué)
這篇文章主要為大家詳細介紹了C++如何實現(xiàn)圖片jpg格式變成16位565bmp格式,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
C++ 將jpg圖片變成16位565bmp圖片
// ConsoleApplication1.cpp : 此文件包含 "main" 函數(shù)。程序執(zhí)行將在此處開始并結(jié)束。
//
#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. 創(chuàng)建目標圖像(負高度表示自上而下存儲)
DWORD masks[] = { 0xF800, 0x07E0, 0x001F };
dstImage.CreateEx(width, height, 16, BI_BITFIELDS, masks);
// 3. 獲取目標圖像參數(shù)(實際步長由API返回)
int dstPitch = dstImage.GetPitch();
BYTE* dstBits = static_cast<BYTE*>(dstImage.GetBits());
// 5. 處理像素數(shù)據(jù)(無需反轉(zhuǎn)行順序)
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;
}
// 轉(zhuǎn)換為RGB565
WORD rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
// 安全寫入目標像素
dstRow[x * 2] = static_cast<BYTE>(rgb565 & 0xFF); // 低位字節(jié)
dstRow[x * 2 + 1] = static_cast<BYTE>((rgb565 >> 8)); // 高位字節(jié)
}
}
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位進行對調(diào)
#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. 創(chuàng)建目標圖像(負高度表示自上而下存儲)
DWORD masks[] = { 0xF800, 0x07E0, 0x001F };
dstImage.CreateEx(width, height, 16, BI_BITFIELDS, masks);
// 3. 獲取目標圖像參數(shù)(實際步長由API返回)
int dstPitch = dstImage.GetPitch();
BYTE* dstBits = static_cast<BYTE*>(dstImage.GetBits());
// 5. 處理像素數(shù)據(jù)(無需反轉(zhuǎn)行順序)
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;
}
// 轉(zhuǎn)換為RGB565
WORD rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
// 安全寫入目標像素
dstRow[x * 2] = static_cast<BYTE>((rgb565 >> 8)); // 低位字節(jié)
dstRow[x * 2 + 1] = static_cast<BYTE>(rgb565 & 0xFF); // 高位字節(jié)
}
}
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++實現(xiàn)圖片轉(zhuǎn)換不同格式的方法,需要的可以了解下
#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;
}到此這篇關(guān)于C++實現(xiàn)圖片jpg格式變成16位565bmp格式的文章就介紹到這了,更多相關(guān)C++圖片jpg轉(zhuǎn)bmp內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法
這篇文章主要介紹了C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
探討:C++實現(xiàn)鏈式二叉樹(用非遞歸方式先序,中序,后序遍歷二叉樹)
本篇文章是對用C++實現(xiàn)鏈式二叉樹(用非遞歸方式先序,中序,后序遍歷二叉樹)的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05

