opening image file on c++ , PNG , JPEG [closed]
This:
ifstream image("bg.png");
… opens the file in text mode, where, in Windows and on old Macs, certain byte sequences denoting end-of-line and end-of-text, are changed on input and output.
You don't want that.
Specify binary mode.
That said, the closest you get to “standard” image handling in C++ is the Boost Image library.
The graphics.h
is ancient BGI lib from Borland I found this doc:
- getimage
So this just copies image from your screen to some memory buffer (to allow back buffering techniques). You are calling it with ifstream
that is wrong. You are overwriting the pointer with the memory buffer or the area where its pointer points to (depends on the getimage
behavior). If you really want to use BGI for this then decode the image to memory and use putimage
to view it. But I am afraid it is not encoded in RAW and safer would be draw the image directly with pixel access instead.
As I wrote to your other question You need to use some lib or code the image decoding your self. iostream
is not a good way for this.
For MS-DOS use OS api (int 21h
if you do not have functions covering it properly) ... for Windows and Borland environment use FileOpen FileRead FileClose
. For non Borland environment use WinAPI or relevant API from target OS (if not windows). To decode image you first need to chose file-format to decode I recommend to start with PCX,BMP (restricted to single pixelformat) or TGA. Fileformats like JPG,PNG,GIF are really too much for a rookie coder to start with. For example My GIF decoder/encoder in C++ is about 36 KByte
of source code + another 15 KBytes
for file CACHE buffering and multi-threaded RT encoding scheduler. In comparison to PCX with around 1.5KByte
of code to load/decode it.
This is small example of how I load textures into my GL engine in Borland/Embarcadero VCL C++ Windows:
int picture_load(Graphics::TBitmap *bmp,AnsiString name,int *_alpha)
{
if (bmp==NULL) { _errorlog+="picture_load bmp is NULL\n"; return 0; }
if (!FileExists(name)){ _errorlog+="picture_load file \""+name+"\" dont exist\n"; return 0; }
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
AnsiString ext=ExtractFileExt(name).LowerCase();
for(;;)
{
if (ext==".bmp")
{
bmp->LoadFromFile(name);
break;
}
if (ext==".jpg")
{
TJPEGImage *jpg=new TJPEGImage;
#ifdef _mmap_h
if (jpg) mmap_new('GL',jpg,sizeof(TJPEGImage));
#endif
if (jpg==NULL) { _errorlog+="picture_load not enough memory\n"; return 0; }
jpg->LoadFromFile(name);
bmp->Assign(jpg);
#ifdef _mmap_h
mmap_del('GL',jpg);
#endif
delete jpg;
break;
}
if (ext==".png")
{
TPNGObject *png=new TPNGObject;
#ifdef _mmap_h
if (png) mmap_new('GL',png,sizeof(TJPEGImage));
#endif
if (png==NULL) { _errorlog+="picture_load not enough memory\n"; return 0; }
png->LoadFromFile(name);
bmp->Assign(png);
#ifdef _mmap_h
mmap_del('GL',png);
#endif
delete png;
break;
}
if ((ext==".sgi")||(ext==".rgb"))
{
sgi sss;
sss.load(name);
bmp->Width=sss.rgba->Width;
bmp->Height=sss.rgba->Height;
bmp->Canvas->Draw(0,0,sss.rgba);
break;
}
if (ext==".pcx")
{
unsigned int *p,c;
int x,y,adr;
int hnd,siz,l,xs,ys;
unsigned int pal[256],r,g,b;
Byte *dat;
for(;;)
{
hnd=FileOpen(name,fmOpenRead);
if (hnd<0) { _errorlog+="picture_load file \""+name+"\" dont exist\n"; return 0; }
siz=FileSeek(hnd,0,2);
FileSeek(hnd,0,0);
dat=new Byte[siz];
#ifdef _mmap_h
if (dat) mmap_new('GL',dat,siz*sizeof(BYTE));
#endif
if (dat==NULL) { FileClose(hnd); _errorlog+="picture_load not enough memory\n"; return 0; }
FileRead(hnd,dat,siz);
FileClose(hnd);
adr=siz-3*256;
for (l=0;l<256;l++)
{
r=dat[adr]; adr++; r&=255;
g=dat[adr]; adr++; g&=255;
b=dat[adr]; adr++; b&=255;
c=(r<<16)|(g<<8)|(b);
c&=0x00FFFFFF;
pal[l]=c;
}
xs=int(dat[ 8])-int(dat[4])+((int(dat[ 9])-int(dat[5]))<<8)+1;
ys=int(dat[10])-int(dat[6])+((int(dat[11])-int(dat[7]))<<8)+1;
bmp->HandleType=bmDIB;
bmp->PixelFormat=pf32bit;
bmp->Width=xs;
bmp->Height=ys;
xs=bmp->Width;
ys=bmp->Height;
adr=128;
for (y=0;y<ys;y++)
{
p=(unsigned int*)bmp->ScanLine[y];
for (x=0;x<xs;)
{
c=dat[adr];
if (c<192) l=1;
else{
l=c&63;
adr++;
c=dat[adr];
}
adr++;
for (;l>0;l--)
{
if (x>=xs) break;
p[x]=pal[c];
x++;
}
}
}
#ifdef _mmap_h
mmap_del('GL',dat);
#endif
delete[] dat;
break;
}
break;
}
if (ext==".dds")
{
DDS::load(bmp,name);
_errorlog+=DDS::_errorlog;
DDS::_errorlog="";
break;
}
_errorlog+="picture_load unsuported file extension \""+ext+"\"\n";
return 0;
}
bmp->HandleType=bmDIB;
if (_alpha) _alpha[0]=(bmp->PixelFormat==pf32bit);
bmp->PixelFormat=pf32bit;
return 1;
}
As your code looks like you are in MS-DOS this will not work. but you can extract the PCX part which does not use any lib. Just port the code (replace AnsiString
and File access with the functions you have at disposal)
The Graphics::TBitmap
is VCL/GDI bitmap to understand its use see:
- Display an array of color in C
So you can port this to your BGI. Also you can ignore all the #ifdef _mmap_h
stuff it is just leftover from memory leak tracking/debugging related to this:
- traceback a pointer in c++ code gdb
- bds 2006 C hidden memory manager conflicts
And look here PCX file-format specification