|
-
Jan 24th, 2008, 12:46 PM
#1
Banned
Re: C/C++ - Loading Bitmap Files (Manually)
The reason why this is giving you so many errors is because the code contains them. Starting with a missing curly at the second line, continuing with a missing bracket...
-
Oct 13th, 2008, 12:07 PM
#2
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
Hey,
sry for my bad english, Im a german baby boy (;
So, I wrote a Bitmaploader, too. But in OOP (ObjectOrientatedProgramming)
The text's in the Messagebox's are in German, if you guys want it in
english i can also write it in english. so, have fun.
Code:
#include "fstream"
class Bitmap
{
public:
void *LoadBitmap(const char *Filename);
//BITMAPFILEHEADER
typedef struct
{
WORD bfType;
DWORD bfSize;
DWORD bfReserved;
DWORD bfOffBits;
}BITMAPFILEHEADER;
//BITMAPINFOHEADER
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
};
void *Bitmap::LoadBitmap(const char *Filename)
{
Bitmap o_Bitmap;
unsigned char *bitmap_Image;
FILE *File=NULL;
unsigned int ImageIdx=0;
if(!Filename)
{
MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
}
else
{
File=fopen(Filename,"rb");
}
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
if(FileHeader.bfType != 0x4D42)
{
MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
}
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
if(o_Bitmap.InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
}
if(!bitmap_Image)
{
free(bitmap_Image);
fclose(File);
}
if(bitmap_Image==NULL)
{
fclose(File);
}
for(ImageIdx=0;ImageIdx < o_Bitmap.InfoHeader.biSizeImage; ImageIdx+=3)
{
bitmap_Image[ImageIdx] = bitmap_Image[+2];
bitmap_Image[ImageIdx+2] = bitmap_Image[ImageIdx];
}
fclose(File);
return bitmap_Image;
};
Thanks
Last edited by si_the_geek; Oct 13th, 2008 at 05:53 PM.
Reason: removed advertising link
-
Oct 27th, 2008, 09:44 AM
#3
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
Hey guys,
i just wanted to say that I can now handle the RunLengthEncoding (RLE). To be precise the RLE4 (8 Bit) and RLE8 (24 Bit). If some guys are interested in the SourceCode, I can post it. But if there comes no feedback, I don't post it...So let me know if you want to see the full length of code!
Thanks!
-
Oct 29th, 2008, 05:12 AM
#4
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
Hi!
Yes I would like to see the entire code.
I was following this to, but didn't login yet.
Thx for the help i got here so far
-
Oct 29th, 2008, 12:46 PM
#5
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
Hey!
Very cool. So, how i said one time before, the Bitmapdecoder can read
all RLE's. I've added 2 more methods, in this the next operation i will
get starting to overgive the textureinformations to the OpenGL-Paraneters
to laod the texture. So, if you want i can also write from the
OpenGL-Operations in the Win32-API.
So, here is the Code:
Code:
#include "fstream"
class Bitmap
{
private:
unsigned long BPP;
unsigned long width;
unsigned long height;
unsigned long size;
unsigned char *bitmap_Image;
unsigned int bps;
GLuint KompressionFormat;
public:
Bitmap();
~Bitmap();
void *LoadBitmap(const char *Filename);
void Load8BitKompression(const char *Filename);
void Load24BitKompression(const char *Filename);
#define BI_RGB 0L
#define BI_RLE4 2L
#define BI_RLE8 4L
#pragma pack(push,1);
//BITMAPFILEHEADER
typedef struct
{
WORD bfType;
DWORD bfSize;
DWORD bfReserved;
DWORD bfOffBits;
}BITMAPFILEHEADER;
//BITMAPINFOHEADER
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;
#pragma pack(pop);
void Kompression();
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
};
Bitmap::Bitmap()
{
BPP=0;
width=0;
height=0;
bitmap_Image=NULL;
size=0;
bps=0;
KompressionFormat=0;
}
Bitmap::~Bitmap()
{
BPP=NULL;
width=NULL;
height=NULL;
delete[] bitmap_Image;
KompressionFormat=NULL;
size=NULL;
bps=NULL;
}
void *Bitmap::LoadBitmap(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
unsigned int ImageIdx=0;
unsigned char *bitmap_Image=NULL;
if(!Filename)
{
MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
return false;
}
else
{
File=fopen(Filename,"rb");
if(!File)
{
MessageBox(NULL,"'File' hat keinen Inhalt!","Error",MB_OK|MB_ICONERROR);
return 0;
}
}
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
if(ferror(File))
{
MessageBox(NULL,"'FileHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
if(ferror(File))
{
MessageBox(NULL,"'InfoHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}
fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
if(ferror(File))
{
MessageBox(NULL,"Zu 'bfOffBits' konnte nicht gesprungen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}
if(o_Bitmap.InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
}
if(o_Bitmap.FileHeader.bfType != 0x4D42)
{
MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}
if(!bitmap_Image)
{
free(bitmap_Image);
fclose(File);
}
if(bitmap_Image==NULL)
{
fclose(File);
}
switch(o_Bitmap.InfoHeader.biCompression)
{
case BI_RGB:
MessageBox(NULL,"Standardkompression ist aktiviert 'BI_RGB'!","Info",MB_OK);
break;
case BI_RLE4:
Load8BitKompression(Filename);
break;
case BI_RLE8:
Load24BitKompression(Filename);
break;
default:
MessageBox(NULL,"Keine Kompression vorhanden","Error",MB_OK|MB_ICONERROR);
}
fclose(File);
};
void Bitmap::Load8BitKompression(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
MessageBox(NULL,"'BI_RLE4'-Kompression ist aktiviert!","Info",MB_OK);
File=fopen(Filename,"rb");
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
if(FileHeader.bfType != 8)
{
MessageBox(NULL,"8-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}
BPP=o_Bitmap.InfoHeader.biBitCount;
width=o_Bitmap.InfoHeader.biHeight;
height=o_Bitmap.InfoHeader.biHeight;
KompressionFormat=GL_RGB;
if(InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
}
};
void Bitmap::Load24BitKompression(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
MessageBox(NULL,"'BI_RLE8'-Kompression ist aktiviert!","Info",MB_OK);
File=fopen(Filename,"rb");
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
if(o_Bitmap.FileHeader.bfType != 24)
{
MessageBox(NULL,"24-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}
BPP=o_Bitmap.InfoHeader.biBitCount;
width=o_Bitmap.InfoHeader.biHeight;
height=o_Bitmap.InfoHeader.biHeight;
KompressionFormat=GL_COLOR_INDEX;
};
So, i don't think that you can find an error or mistake. I hope you enjoy it and thanks for everything.
(Sry for my bad english, im german!)
-
Nov 3rd, 2008, 06:06 AM
#6
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
It still gives many error's .
I can generate a file whit it . But the file is not usefull.
Any idea how this can be ?
-
Nov 4th, 2008, 12:58 PM
#7
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
So, there can be a few reasons. So, you have to create a Win32-Application. It doesn't work if you just include it. I don't exactly what problem you have, but the Includes you have to include are:
Code:
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <glaux.h>
#include <iostream>
#include <conio.h>
using namespace std;
#include "Bitmap.h"
The path of the OpenGL's libs mustn't be right, i installed them on "gl\...". Which Compiler [IDE] do you use? I can write for you in Win32-API or in DirectX if you want...just say (:
Maybe the problem is just you don't know how to do this all, i just uploaded the whole project for you, here: (You must have OpenGL for this (-
http://www.megaupload.com/de/?d=OE2VLS8J
Another way (I think it's better), youre registering on my site:
(Please don't erase !!Mods!!!)
http://www.Online-Programmierung.com
So just register under "Forum" and write your problem in it, i or some guys will help you. In this subforum aren't accepted a big quantity of posts ;-).
I hope i have helped you, so if there a few more questions or problems, just register ;-).
-
Nov 5th, 2008, 05:59 AM
#8
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
Yes i have used win-32 projects before.
But i didnt realise that i needed to make one for this program to.
When i make a win-32 one, to add the gl/.. thing i need to do something the configs.
At the linker thing, the input => additional dependencies => and here i must put some type for code.
I forgot what it was. Do U think u know what it is.
So far you helped me allot, and sorry for my english to. I'm Flemish.
-
May 28th, 2017, 01:57 PM
#9
New Member
Re: C/C++ - Loading Bitmap Files (Manually)
i were working with your code, plus win32 system calls plus winbgim win api, this code also works, so:
#include <winbgim.h>
#include <string.h>
#include<iostream>
#include<windows.h>
#include<stdio.h>
#include <stdlib.h>
#include <fstream.h>
const char *Filename;
WORD bibitcount;
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()
{
char *p;
char *base;
DWORD buf;
Filename="C:\\path to file\\mybmp.BMP";
int gd = DETECT, gm;
int x = 320, y = 240, radius;
k=0;
initgraph(&gd, &gm, "C:\\path to BGI folder\\BGI");//download TC 3.0 for dos
int i;
int j;
char g[20];
FILE *File=NULL;
if(!Filename)
{
MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
}
else
{
File=fopen("C:\\path to my file\\mybmp.BMP","rb");
}
if(FileHeader.bfType != 0x4D42)
{
MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
exit(1);
}
fread(&FileHeader,sizeof(BITMAPFILEHEADER),1,File);
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);
fclose(File);
hArch = CreateFile("bar.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+buf;
k=0;
for (i=0;i<InfoHeader.biHeight;i++)
for(j=0;j<InfoHeader.biWidth;j++)
{
putpixel(j+6,InfoHeader.biHeight-i,COLOR(int(*(p+k)),int(*(p+k+2)),int(*(p+k+1))));
k=k+3;
}
getch();
}
-
May 28th, 2017, 07:04 PM
#10
New Member
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
-
May 28th, 2017, 07:44 PM
#11
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|