-5

I have tried to open bg.png file , but didn't work. There is no error , 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
    `getimage` and `putimage` are not standard C++, so this lacks information needed to diagnose the problem. – Baum mit Augen May 20 '16 at 00:27
  • So what should i do @BaummitAugen ?? – Bayarjargal Jargalsaikhan May 20 '16 at 00:28
  • I'd suspect that something is not working in either `getimage` or `putimage` (whatever they are), and I'm sure they return some sort of success/failure value (which you're not bothering to check). In fact, you're doing no sucess/failure checks on any of your function calls, so you have no way of knowing which one of them is failing. As you've been told already, there is not enough information in your question for us to help. – Ken White May 20 '16 at 00:29
  • 3
    You should [edit] the question to provide a [mcve] and tell us what libraries you are using. – Baum mit Augen May 20 '16 at 00:29
  • graphics.h , iostream – Bayarjargal Jargalsaikhan May 20 '16 at 00:31
  • why use Turbo C++ and graphics.h? They're not C++ – phuclv May 20 '16 at 08:20
  • @LưuVĩnhPhúc depends on which turbo C++ (the new one complies with standart... more or less :) ) , but He never stated what compiler is he using. BGI emulators works also on MSVCPP ... and are still used on schools do not know why as BGI was not good even in the old days. – Spektre May 20 '16 at 08:23

3 Answers3

2

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.

Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304
  • +1 ... btw I wrote him the same thing in the [previous question](http://stackoverflow.com/q/37325141/2521214) – Spektre May 20 '16 at 08:20
1

The graphics.h is ancient BGI lib from Borland I found this doc:

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:

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:

And look here PCX file-format specification

Community
  • 1
  • 1
Spektre
  • 41,942
  • 8
  • 91
  • 312
-1

Are you sure the bg.png is on the same folder where you are running your program? If the answer is 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");
    }
mhyst
  • 289
  • 1
  • 7
  • If you have to ask for clarification in your answer, it probably should not have been an answer. In general, only write answers for questions that are in an answerable state, chatty guesswork does not make good answers for this format. – Baum mit Augen May 20 '16 at 00:48
  • Good to know. That's OK. – mhyst May 20 '16 at 00:53
  • Actually, I just read the docs and my modifications are useless with just one initwindow. So my answer is useless also. Except for the other recommendation. Look if the image file is accessable and in your work folder. – mhyst May 20 '16 at 00:55