bmp einlesen - ich werd bekloppt.



  • moin, ich möchte eine .bmp datei einlesen.

    [cpp]main.cpp:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include "BMPStruct.h"

    using namespace std;

    class BMP
    {
    public:
    BMP(string);
    BMPHeader bmh;
    RGBTriple *BMPData;
    };

    BMP::BMP(string FileName)
    {
    ifstream ifFile;
    ifFile.open(FileName.c_str(), ios::binary);
    ifFile.read((char*)&bmh,sizeof(BMPHeader));
    ifFile.seekg(bmh.bfOffBits, ios::beg);

    BMPData= new RGBTriple[ bmh.biWidth*bmh.biHeight ];

    for( int iY= 0 ; iY <= bmh.biHeight; ++iY )
    {
    for( int iX= 0; iX <= bmh.biWidth; ++iX )
    {
    ifFile.read((char*)&BMPData[ iX * iY ],sizeof(RGBTriple));
    }

    iX= bmh.biWidth*3;
    while( iX % 4 != 0)
    {
    ifFile.seekg( 1 , ios::cur );
    iX++;
    }

    }

    ifFile.close();
    }

    int main(int argc, char *argv[])
    {

    BMP BMPFile("weiss.bmp");

    for(int i = 0; i<99; i++)
    cout << i << ": " <<int(BMPFile.BMPData[i].rgbGreen) << endl;

    return 0;

    }[/cpp]

    BMPStruct.h:
    #pragma pack(1)
    typedef struct {   // bmfh
        char    bfType[2];				// File Format (BM)
        unsigned long   bfSize;			// Filesize
        unsigned short    bfReserved1;	// Null
        unsigned short    bfReserved2;	// Null
        unsigned long   bfOffBits;		// Headersize
        unsigned long   biSize;			// Headersize (from here)
        long    biWidth;				// Width (in px)
        long    biHeight;				// Height (in px)
        unsigned short    biPlanes;		// Amount of Layers
        unsigned short    biBitCount;	// Color Deep
        unsigned long   biCompression;	// Compression Method
        unsigned long   biSizeImage;	// Filesize (without header)
        long    biXPelsPerMeter;		// Horizontal Resolution
        long    biYPelsPerMeter;		// Vertical Resolution
        unsigned long   biClrUsed;		// Amout of used colors
        unsigned long   biClrImportant;	// Amount of important colors
    } BMPHeader;
    
    typedef struct {   // rgbt
        unsigned char   rgbBlue; //unsigned char (kompletter struct)
        unsigned char   rgbGreen;
        unsigned char   rgbRed;
    } RGBTriple;
    #pragma pack()
    

    Ausgabe:
    0: 255
    1: 255
    2: 255
    3: 255
    4: 255
    5: 255
    6: 255
    7: 255
    8: 255
    9: 255
    10: 255
    11: 205
    12: 255
    13: 205
    14: 255
    15: 255
    16: 255
    17: 205
    ...

    wie mann sieht, kommt bei der ausgebe hin und wieder 205 anstatt 255. da es sich bei weiss.bmp aber um ein komplett weisses .bmp handelt kann das ja eigentlich nicht sein. weis einer von euch woran das liegen könnte?



  • schau dir mal bei www.wotsit.org das fileformat von bmp an... vlei kommste drauf...



  • Du hast einen Fehler beim einlesen:

    for( int iY= 0 ; iY <= bmh.biHeight; ++iY ) 
    { 
        for( int iX= 0; iX <= bmh.biWidth; ++iX ) 
        { 
            ifFile.read((char*)&BMPData[ iX * iY ],sizeof(RGBTriple));// <--
        }
    //....
    

    es muss heißen

    ifFile.read((char*)&BMPData[ iX * iY + iX ],sizeof(RGBTriple));
    


  • Nun hab ich selbst einen Fehler gemacht, muss natürlich

    ifFile.read((char*)&BMPData[ iY * bmh.biWidth + iX ],sizeof(RGBTriple));
    

    heißen!!



  • thx, es klappt!


Anmelden zum Antworten