C++ to turn jpg pictures into 16-bit 565bmp pictures
// : This file contains the "main" function. Program execution will begin and end here.// #include <iostream> #include<string> #include <> bool ConvertJpgTo16BitBmp565(const char* input_path, const char* output_path) { CImage srcImage, dstImage; // 1. Load the source image (input_path); int width = (); int height = (); int srcBpp = (); int srcPitch = (); BYTE* srcBits = static_cast<BYTE*>(()); // 2. Create target image (negative height means top-down storage) DWORD masks[] = { 0xF800, 0x07E0, 0x001F }; (width, height, 16, BI_BITFIELDS, masks); // 3. Get the target image parameters (the actual step size is returned by the API) int dstPitch = (); BYTE* dstBits = static_cast<BYTE*>(()); // 5. Process pixel data (no need to reverse the line order) 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; // parse the source pixel color if (srcBpp == 24) { // 24-bit BGR BYTE* p = srcRow + x * 3; b = p[0]; g = p[1]; r = p[2]; } else if (srcBpp == 32) { // 32-bit BGRX BYTE* p = srcRow + x * 4; b = p[0]; g = p[1]; r = p[2]; } else { return false; } // Convert to RGB565 WORD rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); // Safely write to the target pixel dstRow[x * 2] = static_cast<BYTE>(rgb565 & 0xFF); // Low byte dstRow[x * 2 + 1] = static_cast<BYTE>((rgb565 >> 8)); // High bytes } } (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((() - 3).c_str(), "jpg")) continue; h=(0, () - 3); h += "bmp"; printf(h.c_str()); ConvertJpgTo16BitBmp565(__argv[i], h.c_str()); } return 0; }
Use this in special cases. The effect of this is to compare the first 8 and the last 8 bits of the picture.
#include <iostream> #include<string> #include <> bool ConvertJpgTo16BitBmp565(const char* input_path, const char* output_path) { CImage srcImage, dstImage; // 1. Load the source image (input_path); int width = (); int height = (); int srcBpp = (); int srcPitch = (); BYTE* srcBits = static_cast<BYTE*>(()); // 2. Create target image (negative height means top-down storage) DWORD masks[] = { 0xF800, 0x07E0, 0x001F }; (width, height, 16, BI_BITFIELDS, masks); // 3. Get the target image parameters (the actual step size is returned by the API) int dstPitch = (); BYTE* dstBits = static_cast<BYTE*>(()); // 5. Process pixel data (no need to reverse the line order) 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; // parse the source pixel color if (srcBpp == 24) { // 24-bit BGR BYTE* p = srcRow + x * 3; b = p[0]; g = p[1]; r = p[2]; } else if (srcBpp == 32) { // 32-bit BGRX BYTE* p = srcRow + x * 4; b = p[0]; g = p[1]; r = p[2]; } else { return false; } // Convert to RGB565 WORD rgb565 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); // Safely write to the target pixel dstRow[x * 2] = static_cast<BYTE>((rgb565 >> 8)); // Low byte dstRow[x * 2 + 1] = static_cast<BYTE>(rgb565 & 0xFF); // High bytes } } (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((() - 3).c_str(), "jpg")) continue; h=(0, () - 3); h += "bmp"; printf(h.c_str()); ConvertJpgTo16BitBmp565(__argv[i], h.c_str()); } return 0; }
Method supplement
#include <opencv2/> #include <iostream> int main() { // Input file path and output file path std::string inputImagePath, outputImagePath; std::cout << "Enter the input image file path: "; std::cin >> inputImagePath; std::cout << "Enter the output image file path (., , ): "; std::cin >> outputImagePath; // Read pictures cv::Mat image = cv::imread(inputImagePath, cv::IMREAD_UNCHANGED); // Check whether the image loads successfully if (()) { std::cerr << "Error: Could not open or find the image!" << std::endl; return -1; } // Save the picture to the new format 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; }
This is the article about C++ realizing the image jpg format to become 16-bit 565bmp format. For more related C++ image jpg to bmp content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!