|
| 1 | +// |
| 2 | +// Created by marco on 16/11/2024. |
| 3 | +// |
| 4 | + |
| 5 | +#include "../includes/BitmapConverter.h" |
| 6 | +#include <iostream> |
| 7 | +#include <sstream> |
| 8 | + |
| 9 | +std::vector<BYTE> BitmapConverter::HBitmapToRGB(HBITMAP hBitmap, int width, int height) { |
| 10 | + BITMAP bmp; |
| 11 | + GetObject(hBitmap, sizeof(BITMAP), &bmp); |
| 12 | + |
| 13 | + BITMAPINFOHEADER bi; |
| 14 | + bi.biSize = sizeof(BITMAPINFOHEADER); |
| 15 | + bi.biWidth = width; |
| 16 | + bi.biHeight = -height; // Negative height to avoid flipping |
| 17 | + bi.biPlanes = 1; |
| 18 | + bi.biBitCount = 24; // Use 24-bit RGB |
| 19 | + bi.biCompression = BI_RGB; |
| 20 | + |
| 21 | + std::vector<BYTE> pixels(width * height * 3); |
| 22 | + HDC hdc = GetDC(nullptr); |
| 23 | + GetDIBits(hdc, hBitmap, 0, height, pixels.data(), reinterpret_cast<BITMAPINFO *>(&bi), DIB_RGB_COLORS); |
| 24 | + ReleaseDC(nullptr, hdc); |
| 25 | + |
| 26 | + return pixels; |
| 27 | +} |
| 28 | + |
| 29 | +std::vector<BYTE> BitmapConverter::ResizeImage(const std::vector<BYTE> &pixels, const int originalWidth, |
| 30 | + const int originalHeight, const int newWidth, const int newHeight) { |
| 31 | + std::vector<BYTE> resized(newWidth * newHeight * 3); |
| 32 | + |
| 33 | + for (int y = 0; y < newHeight; ++y) { |
| 34 | + for (int x = 0; x < newWidth; ++x) { |
| 35 | + int nearestX = static_cast<int>(x * (originalWidth / static_cast<float>(newWidth))); |
| 36 | + int nearestY = static_cast<int>(y * (originalHeight / static_cast<float>(newHeight))); |
| 37 | + |
| 38 | + int originalIdx = (nearestY * originalWidth + nearestX) * 3; |
| 39 | + int resizedIdx = (y * newWidth + x) * 3; |
| 40 | + |
| 41 | + resized[resizedIdx] = pixels[originalIdx]; |
| 42 | + resized[resizedIdx + 1] = pixels[originalIdx + 1]; |
| 43 | + resized[resizedIdx + 2] = pixels[originalIdx + 2]; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return resized; |
| 48 | +} |
| 49 | + |
| 50 | +std::vector<int> BitmapConverter::CategorizePixels(const std::vector<BYTE> &pixels, int width, int height) { |
| 51 | + std::vector<int> categorizedPixels(width * height); |
| 52 | + |
| 53 | + for (int y = 0; y < height; ++y) { |
| 54 | + for (int x = 0; x < width; ++x) { |
| 55 | + int idx = (y * width + x) * 3; |
| 56 | + BYTE blue = pixels[idx]; |
| 57 | + BYTE green = pixels[idx + 1]; |
| 58 | + BYTE red = pixels[idx + 2]; |
| 59 | + |
| 60 | + // convert to grayscale using the luminance formula |
| 61 | + BYTE gray = static_cast<BYTE>(0.299 * red + 0.587 * green + 0.114 * blue); |
| 62 | + |
| 63 | + // categorize based on grayscale value |
| 64 | + int category; |
| 65 | + if (gray < 63.75) { |
| 66 | + category = 0; // Off |
| 67 | + } else if (gray < 127.5) { |
| 68 | + category = 1; // Red |
| 69 | + } else if (gray < 191.25) { |
| 70 | + category = 3; // Green |
| 71 | + } else { |
| 72 | + category = 2; // Orange |
| 73 | + } |
| 74 | + |
| 75 | + categorizedPixels[y * width + x] = category; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return categorizedPixels; |
| 80 | +} |
| 81 | + |
| 82 | +std::string BitmapConverter::PixelsToString(const std::vector<int>& pixels) { |
| 83 | + std::ostringstream oss; |
| 84 | + for (const int value : pixels) { |
| 85 | + oss << value; |
| 86 | + } |
| 87 | + return oss.str(); |
| 88 | +} |
0 commit comments