Results 1 to 32 of 32

Thread: C/C++ - Loading Bitmap Files (Manually)

Hybrid View

  1. #1
    New Member
    Join Date
    May 2017
    Posts
    3

    Post Re: C/C++ - Loading Bitmap Files (Manually)

    maybe in my program is necesary to write:initwindow(400,400) in the top of the main function to open the graphic window, it depends on the winbgim installation

  2. #2
    New Member
    Join Date
    May 2017
    Posts
    3

    Post Re: C/C++ - Loading Bitmap Files (Manually)

    thats the working code for 32 bit windows machine, in my example above i made a mistake, making an if without reading the file, in 64 bit machines it`s necesary to include iostream and fstream, that's how I load a BMP file of 24 bits per pixel, not for indexed coloured bitmaps, download winbgim from here: http://www.mediafire.com/file/z9wnl0...evCpp.zip/file, is in dev-c++, you probably have to load a couple of parameters for the linker, the path is: project, project options, parameters, and write: -lbgi -lgdi32 -luser32 -lcomdlg32 -luuid -loleaut32 -lole32 -mwindows -lwinmm, one thing more, after installing winbgim, go to dev c and create new project as console graphics application, this item must be in the project list if winbgim is properly installed, I wrote "write to shared memory", because I wrote the file also in memory although I did not access the file this way, but from the same file so:

    #include <winbgim.h>
    #include <string.h>
    #include<windows.h>
    #include<stdio.h>
    #include <stdlib.h>
    #include<fstream>
    #include <iostream>
    const char *Filename;

    BITMAPFILEHEADER FileHeader;
    BITMAPINFOHEADER InfoHeader;
    int k;
    typedef struct{
    BYTE colorr;
    BYTE colorg;
    BYTE colorb;
    }cine;

    cine color;

    HANDLE hArch, hProy;
    LPSTR base,puntero;
    DWORD tam;
    int main()
    {
    int gdriver=9;
    int gmode=2;
    // initgraph(&gdriver,&gmode, "");
    cleardevice();

    UnmapViewOfFile(base);
    CloseHandle(hProy);
    CloseHandle(hArch);
    char *p;
    char *base;
    DWORD buf;
    Filename="D:\\music\\IMSLP00795-BWV0971\\01.bmp";
    int gd = DETECT, gm;
    int x = 320, y = 240, radius;
    k=0;

    int i;
    int j;




    FILE *File=NULL;
    if(!Filename)
    {
    MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
    }
    else
    {
    File=fopen("D:\\music\\IMSLP00795-BWV0971\\01.bmp","rb");
    }

    fread(&FileHeader,sizeof(BITMAPFILEHEADER),1,File);
    if(FileHeader.bfType != 0x4D42)
    {
    MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
    exit(1);
    }
    printf("tamaño total del archivo %d\n",FileHeader.bfSize);
    printf("comienzo del mapa de bits (imagen en pixels) en bits %d\n",FileHeader.bfOffBits);

    buf=FileHeader.bfOffBits/8; //offset from the begining of BMP file (pixel array)
    printf("comienzo del mapa de bits en bytes desde el origen del archivo %d\n",buf);
    fread(&InfoHeader,sizeof(BITMAPINFOHEADER),1,File);

    printf("horizontal resolution in pixels por metro %li\n",InfoHeader.biWidth);
    printf("vertical resolution in pixels por metro %li\n",InfoHeader.biHeight);
    printf("numero de bits por pixel %d", InfoHeader.biBitCount);
    initwindow(InfoHeader.biWidth,InfoHeader.biHeight);


    hArch = CreateFile("D:\\music\\IMSLP00795-BWV0971\\01.bmp", /* file name */
    GENERIC_ALL , /* read/write access */
    0, /* no sharing of the file */
    NULL, /* default security */
    OPEN_ALWAYS, /* open new or existing file */
    FILE_ATTRIBUTE_NORMAL, /* routine file attributes */
    NULL); /* no file template */
    if (hArch==INVALID_HANDLE_VALUE){

    fprintf(stderr,"no puede abrirse el archivo");

    }

    hProy = CreateFileMapping(hArch, /* file handle */
    NULL, /* default security */
    PAGE_READWRITE, /* read/write access to mapped pages */
    0, /* map entire file */
    0,
    TEXT("SharedObject")); /* named shared memory object */

    /* write to shared memory */
    base=(LPSTR)MapViewOfFile(hProy,FILE_MAP_ALL_ACCESS,0,0,0);
    tam=GetFileSize(hArch,NULL);
    int cont=0;
    puntero=base;
    p=base+FileHeader.bfOffBits;
    k=0;int t=0,v,l;
    fseek(File,FileHeader.bfOffBits,SEEK_SET );
    int read=0,read2=0;
    k=0;
    for( i=0; i<InfoHeader.biWidth; i++ ) {
    fread(&color,sizeof(cine),1,File);
    read += sizeof(cine);
    printf( "Pixel %d: %3d %3d %3d\n", i+1, int(color.colorb), int(color.colorg), int(color.colorr) );
    }
    if( read % 4 != 0 ) {
    read2 = 4 - (read%4);
    printf( "Padding: %d bytes\n", read2 );
    //fread( &color, read2, 1, File );

    }
    fseek(File,FileHeader.bfOffBits,SEEK_SET );
    for (i=0;i<InfoHeader.biHeight;i++)
    for(j=0;j<InfoHeader.biWidth ;j++)

    {
    fread(&color,sizeof(cine),1,File);

    putpixel(j,InfoHeader.biHeight- i,COLOR(int(color.colorb),int(color.colorg),int(color.colorr)));
    if(j==InfoHeader.biWidth-1&&read2!=0)fseek(File,read2,SEEK_CUR);
    }
    fclose(File);

    UnmapViewOfFile(base);
    CloseHandle(hProy);
    CloseHandle(hArch);
    getch();
    }
    Last edited by Carl G; Sep 8th, 2021 at 02:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width