Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Bitmap loading in windows

[b][red]This message was edited by the Gamekeeper at 2002-6-2 6:10:4[/red][/b][hr]

Comments

  • [b][red]This message was edited by the Gamekeeper at 2002-6-2 6:10:14[/red][/b][hr]

  • [b][red]This message was edited by the Gamekeeper at 2002-7-17 8:42:18[/red][/b][hr]
    #include
    #include

    bool ReadBMP(char* filename, char** data, int* width, int* height);
    bool WriteBMP(char* filename, char* data, int width, int height);

    bool ReadBMP(char* filename, char** data, int* width, int* height)
    {
    FILE* file;
    BITMAPFILEHEADER bfh;
    BITMAPINFOHEADER bih;
    int nbytes;
    unsigned char extrabytes;
    char reserved[4];
    char* p2;
    unsigned int scanbitcount;
    file = fopen(filename, "rb");
    if (!file) return false;
    fread((void*)&bfh, sizeof(bfh), 1, file);
    if (bfh.bfType != (('M' << 8) + 'B'))
    {
    fclose(file);
    perror("This is not a valid bitmap!");
    return false;
    }
    fread((void*)&bih, sizeof(bih), 1, file);
    if (bih.biCompression != BI_RGB)
    {
    fclose(file);
    perror("Can't handle compression!");
    return false;
    }
    if (bih.biBitCount != 24)
    {
    fclose(file);
    perror("Too few colors!");
    return true;
    }
    nbytes = bih.biWidth * bih.biHeight * 3;
    scanbitcount = bih.biWidth * 3;
    extrabytes = scanbitcount % 4;
    *data = new char[nbytes];
    fseek(file, bfh.bfOffBits, 0);
    if (extrabytes)
    {
    extrabytes = 4 - extrabytes;
    p2 = *data;
    for (int i = 0; i < nbytes; i += scanbitcount)
    {
    fread((void*)&p2[i], scanbitcount, 1, file);
    fread((void*)reserved, 1, extrabytes, file);
    }
    }
    else
    {
    fread((void*)*data, nbytes, 1, file);
    }
    fclose(file);
    *width = bih.biWidth;
    *height = bih.biHeight;
    return true;
    }

    bool WriteBMP(char* filename, char* data, int width, int height)
    {
    FILE* file;
    BITMAPFILEHEADER bfh;
    BITMAPINFOHEADER bih;
    unsigned long nbytes = width * height * 3;
    unsigned short oh_size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    unsigned char extrabytes = (width * 3) % 4;
    unsigned char reserved = 0;
    unsigned short scanbitcount;
    bfh.bfType = ('M' << 8) + 'B';
    bfh.bfSize = oh_size + nbytes;
    bfh.bfOffBits = oh_size;
    bfh.bfReserved1 = 0;
    bfh.bfReserved2 = 0;
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biWidth = width;
    bih.biHeight = height;
    bih.biPlanes = 1;
    bih.biBitCount = 24;
    bih.biCompression = BI_RGB;
    bih.biSizeImage = 0;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    bih.biClrUsed = 0;
    bih.biClrImportant = 0;
    file = fopen(filename, "wb");
    if (!file) return false;
    fwrite((void*)&bfh, sizeof(bfh), 1, file);
    fwrite((void*)&bih, sizeof(bih), 1, file);
    if (extrabytes)
    {
    extrabytes = 4 - extrabytes;
    for (int i = 0; i < nbytes; i += scanbitcount)
    {
    fwrite(&data[i], scanbitcount, 1, file);
    fwrite(&reserved, 1, extrabytes, file);
    }
    }
    else
    {
    fwrite(data, nbytes, 1, file);
    }
    fclose(file);
    return true;
    }
  • Justin BibJustin Bib USAMember Posts: 0

    ________ ( http://forcoder.org ) free video tutorials and ebooks about // C++ Assembly PL/SQL Visual Basic Java Python C JavaScript Go PHP R Swift Perl C# Objective-C Visual Basic .NET Scratch Delphi Ruby MATLAB Ada Lua Clojure ABAP Bash Erlang Hack Transact-SQL Kotlin F# Alice Scheme FoxPro Awk Apex ML LabVIEW Julia Prolog COBOL Rust Fortran Crystal SAS Logo Dart Lisp D Scala VBScript // ______________

Sign In or Register to comment.