Skip to content

A simple way to read pixel values from a .bmp image

Notifications You must be signed in to change notification settings

wwwqr-000/bmp_reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

BMP Image Reader

#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#include <vector>

using namespace Gdiplus;

#include "classes/texturemap.hpp"

//Linkers: -lgdi32 -lgdiplus

ULONG_PTR gdiplusToken;
std::vector<texturemap> textureMaps;

void addTexturemap(const wchar_t* path, std::string name) {
    texturemap tmpMap;
    tmpMap.setMap(new Bitmap(path));
    Rect box(0, 0, tmpMap.getMap()->GetWidth(), tmpMap.getMap()->GetHeight());
    tmpMap.getMap()->LockBits(&box, ImageLockModeRead, PixelFormat24bppRGB, &tmpMap.getMapData());
    tmpMap.setMapPixels(static_cast<BYTE*>(tmpMap.getMapData().Scan0));
    tmpMap.getName() = name;
    textureMaps.emplace_back(tmpMap);
}

void loadTexturemaps() {
    addTexturemap(L"pfp.bmp", "Profile Picture");
}

int main() {
    GdiplusStartupInput gdiplusStartupInput = {0};
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    loadTexturemaps();
    //Get rgb value from pixel on coord 10, 10
    int y = 10;
    int x = 10;
    int index = (y * textureMaps[0].getMapData().Stride + (x * 3));

    int red = static_cast<int>(textureMaps[0].getMapPixels()[index + 2]);
    int green = static_cast<int>(textureMaps[0].getMapPixels()[index + 1]);
    int blue = static_cast<int>(textureMaps[0].getMapPixels()[index]);

    std::cout << "RGB value from '" << textureMaps[0].getName() << "' on x: " << x << ", y: " << y << " is RGB(" << red << ", " << green << ", " << blue << ")\n";
    //

    GdiplusShutdown(gdiplusToken);
    return 0;
}

A simple yet practical bmp reader for c++

List of required includes
#include <iostream>
#include <windows.h>
#include <gdiplus.h>
#include <vector>

About

A simple way to read pixel values from a .bmp image

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages