Opening an image file in C ++, PNG, JPEG

I tried to open the bg.png file but did not work. There are no errors, but nothing appears. Help me!

int main() { initwindow(600,600,"GAME"); ifstream image("bg.png"); getimage(50, 50 , 450 , 450 , image); putimage(50,50,image,COPY_PUT); system("pause"); } 
-4
c ++
source share
3 answers

It:

 ifstream image("bg.png"); 

& hellip; opens the file in text mode , where on Windows and on older Mac computers, some byte sequences indicating the end of the line and the end of the text change at the input and output.

You do not want this.

Specify binary mode .


However, the closest to standard C ++ image processing is the Boost Image library .

+1
source share

graphics.h is an ancient BGI lib from Borland . I found this document:

This way, it simply copies the image from your screen to the memory buffer (to return buffering methods). You call it using ifstream , this is wrong. You rewrite the pointer with a memory buffer or the area where its pointer points (depending on the behavior of getimage ). If you really want to use BGI for this, then decode the image into memory and use putimage to view it. But I'm afraid it is not RAW encoded, and it would be safer to draw the image directly using pixel access.

As I wrote to your other question, you need to use some lib libraries or code to decode the image. iostream not suitable for this.

For MS-DOS, use the api OS ( int 21h if you do not have functions that cover it correctly) ... for Windows and Using the Borland FileOpen FileRead FileClose . For environments without Borland, use WinAPI or the appropriate API from the target OS (if not windows). To decode an image, you first need to select the file format for decoding. I recommend starting with PCX, BMP (limited to one pixel) or TGA . Formats such as JPG, PNG, GIF are actually too many for a beginner encoder. For example, the My GIF decoder / encoder in C ++ is about 36 KByte source code + another 15 KBytes for CACHE buffering and a multi-threaded RT coding scheduler. Compared to PCX with around 1.5KByte code for loading / decoding it.

This is a 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; } 

How your code looks like you are on MS-DOS , this will not work. but you can extract the part of PCX that does not use lib. Just port the code (replace AnsiString and access the file with the functions you have)

Graphics::TBitmap is a VCL / GDI bitmap to understand its use:

  • Display an array of colors in C

So you can port this to BGI . In addition, you can ignore all #ifdef _mmap_h tags that have been left out of the tracking / debugging memory leak associated with this:

  • trace pointer in c ++ gdb code
  • bds 2006 C conflicts with hidden memory manager

And look here PCX file format specification

0
source share

Are you sure bg.png is in the same folder where you run your program? If yes, have you tried?

 int main() { int wHANDLE=initwindow(600,600,"GAME"); setcurrentwindow(wHANDLE); ifstream image("bg.png"); getimage(50, 50 , 450 , 450 , image); putimage(50,50,image,COPY_PUT); system("pause"); } 
-one
source share

All Articles