C++實(shí)現(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)建目標(biāo)圖像(負(fù)高度表示自上而下存儲(chǔ)) DWORD masks[] = { 0xF800, 0x07E0, 0x001F }; dstImage.CreateEx(width, height, 16, BI_BITFIELDS, masks); // 3. 獲取目標(biāo)圖像參數(shù)(實(shí)際步長由API返回) int dstPitch = dstImage.GetPitch(); BYTE* dstBits = static_cast<BYTE*>(dstImage.GetBits()); // 5. 處理像素?cái)?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); // 安全寫入目標(biāo)像素 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; }
特殊情況使用這個(gè),這個(gè)的效果是將圖片的前8位和后8位進(jìn)行對(duì)調(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)建目標(biāo)圖像(負(fù)高度表示自上而下存儲(chǔ)) DWORD masks[] = { 0xF800, 0x07E0, 0x001F }; dstImage.CreateEx(width, height, 16, BI_BITFIELDS, masks); // 3. 獲取目標(biāo)圖像參數(shù)(實(shí)際步長由API返回) int dstPitch = dstImage.GetPitch(); BYTE* dstBits = static_cast<BYTE*>(dstImage.GetBits()); // 5. 處理像素?cái)?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); // 安全寫入目標(biāo)像素 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; }
方法補(bǔ)充
下面小編為大家整理了C++實(shí)現(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++實(shí)現(xiàn)圖片jpg格式變成16位565bmp格式的文章就介紹到這了,更多相關(guān)C++圖片jpg轉(zhuǎn)bmp內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法
這篇文章主要介紹了C語言中字符串與各數(shù)值類型之間的轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03探討:C++實(shí)現(xiàn)鏈?zhǔn)蕉鏄?用非遞歸方式先序,中序,后序遍歷二叉樹)
本篇文章是對(duì)用C++實(shí)現(xiàn)鏈?zhǔn)蕉鏄?用非遞歸方式先序,中序,后序遍歷二叉樹)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++內(nèi)存泄漏的檢測(cè)與實(shí)現(xiàn)詳細(xì)流程
內(nèi)存泄漏(memory leak) 是指由于疏忽或錯(cuò)誤造成了程序未能釋放掉不再使用的內(nèi)存的情況。內(nèi)存泄漏并非指內(nèi)存在物理上的消失,而是應(yīng)用程序分配某段內(nèi)存后,由于設(shè)計(jì)錯(cuò)誤,失去了對(duì)該段內(nèi)存的控制,因而造成了內(nèi)存的浪費(fèi)2022-08-08