3 Replies - 467 Views - Last Post: 20 July 2016 - 01:34 PM Rate Topic: ****- 2 Votes

#1 Cambalinho  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 646
  • Joined: 04-March 12

how overloading input and output operators?

Posted 17 July 2016 - 01:50 PM

see these code:
//write HBITMAP:
    friend std::ostream& operator << (std::ostream& lhs, BitmapDC &hBitmap)
    {
        //get hbitmap size:
        BITMAP bm;
        GetObject(hBitmap.bitmapcurrent, sizeof(BITMAP), &bm);
        LONG lWidth=bm.bmWidth;
        LONG lHeight=bm.bmHeight;
        BYTE* pBitmapBits;
        BitBlt(GetWindowDC(ActivatedForm), 100, 100, bm.bmWidth, bm.bmHeight, hBitmap.hdcbitmap, 0, 0, SRCCOPY);


        WORD wBitsPerPixel=bm.bmBitsPixel;
        unsigned long pixel_data_size = lHeight * ( ( lWidth * ( wBitsPerPixel / 8 ) ) + 0 );
        // Some basic bitmap parameters
        unsigned long headers_size = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );

        BITMAPINFOHEADER bmpInfoHeader = {0};

        // Set the size
        bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);

        // Bit count
        bmpInfoHeader.biBitCount = wBitsPerPixel;

        // Use all colors
        bmpInfoHeader.biClrImportant = 0;

        // Use as many colors according to bits per pixel
        bmpInfoHeader.biClrUsed = 0;

        // Store as un Compressed
        bmpInfoHeader.biCompression = BI_RGB;

        // Set the height in pixels
        bmpInfoHeader.biHeight = lHeight;

        // Width of the Image in pixels
        bmpInfoHeader.biWidth = lWidth;

        // Default number of planes
        bmpInfoHeader.biPlanes = 1;

        // Calculate the image size in bytes
        bmpInfoHeader.biSizeImage = pixel_data_size;

        //getting the HBitmap pixel data from HDC:
        pBitmapBits=new BYTE[bmpInfoHeader.biSizeImage];
        GetDIBits(hBitmap.hdcbitmap , hBitmap.bitmapcurrent, 0, bm.bmHeight, pBitmapBits, (BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);

        BITMAPFILEHEADER bfh = {0};

        // This value should be values of BM letters i.e 0x4D42
        // 0x4D = M 042 = B storing in reverse order to match with endian
        bfh.bfType = 0x4D42;
        //bfh.bfType = 'B'+('M' << 8);

        // <<8 used to shift M to end  */

        // Offset to the RGBQUAD
        bfh.bfOffBits = headers_size;

        // Total size of image including size of headers
        bfh.bfSize =  headers_size +  bmpInfoHeader.biSizeImage ;

        // Write the File header:
        lhs.write((char*)&bfh, sizeof(bfh));

        //Write the bitmap info header:
        lhs.write((char*)&bmpInfoHeader,sizeof(bmpInfoHeader));

        //write pixel data:
        lhs.write((char*)pBitmapBits, bmpInfoHeader.biSizeImage);

        delete []pBitmapBits;

        return lhs;
    }

    //read HBITMAP:
    friend std::istream& operator >> (std::istream& lhs, BitmapDC &hBitmap)
    {
        MessageBox("read it");
        BITMAPFILEHEADER bfh = {0};
        lhs.read((char*)&bfh, sizeof(bfh));

        BITMAPINFOHEADER bmpInfoHeader = {0};
        bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
        lhs.read((char*)&bmpInfoHeader,sizeof(bmpInfoHeader));
        BYTE* pBitmapBits=new BYTE[bmpInfoHeader.biSizeImage];
        lhs.read((char*)pBitmapBits, bmpInfoHeader.biSizeImage);
        hBitmap.init(bmpInfoHeader.biWidth, bmpInfoHeader.biHeight);
        SetDIBitsToDevice(hBitmap.hdcbitmap, 0, 0, bmpInfoHeader.biWidth, bmpInfoHeader.biHeight, 0, 0, 0, bmpInfoHeader.biHeight, pBitmapBits,(BITMAPINFO*)&bmpInfoHeader, DIB_RGB_COLORS);
        BitBlt(GetWindowDC(ActivatedForm), 200, 100, bmpInfoHeader.biWidth, bmpInfoHeader.biHeight, hBitmap.hdcbitmap, 0, 0, SRCCOPY);
        delete []pBitmapBits;
        return lhs;
    }

void save(string FileName)
    {
        ofstream WriteOnFile(FileName.c_str(), ios::out | ios::binary);
        WriteOnFile << bitmapcurrent;
        WriteOnFile.close();
        ifstream ReadOnFile(FileName.c_str(), ios::in | ios::binary);
        ReadOnFile >>bitmapcurrent;
        ReadOnFile.close();
    }

these code is making me very confused :(
if i'm overloading the input and output operators why i get these errors?
"cannot bind 'std::basic_istream<char>' lvalue to 'std::basic_istream<char>&&'"
bitmapcurrent it's a BitmapDC member. seems that i can't use 'this'.
i'm trying testing for read. on write works fine

Is This A Good Question/Topic? 0
  • +

Replies To: how overloading input and output operators?

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,667
  • Joined: 19-February 09

Re: how overloading input and output operators?

Posted 17 July 2016 - 03:16 PM

View PostCambalinho, on 17 July 2016 - 09:50 PM, said:

... bitmapcurrent it's a BitmapDC member. ...


Do the objects differ?

The functions read from and to a BitmapDC object, and the calling statement reads and writes to bitmapcurrent object, a member of BitmapDC.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5955
  • View blog
  • Posts: 20,408
  • Joined: 05-May 12

Re: how overloading input and output operators?

Posted 17 July 2016 - 08:36 PM

As an aside, what happened to your code from October 2015 where you were also using operator overloads to save and load images?
Was This Post Helpful? 0
  • +
  • -

#4 Cambalinho  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 646
  • Joined: 04-March 12

Re: how overloading input and output operators?

Posted 20 July 2016 - 01:34 PM

Skydiver i was getting problems, very problems, on code. so i used the last saved *.h for help me fix some errors.
now that i can use the Debugger, normaly, i fixed several errors.
now i can see the image:
friend std::ostream& operator << (std::ostream& lhs, image& rhs)
    {

        //Create an empty IStream:
        IStream* pIStream = nullptr;
        if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");

        //choose image format for save it on IStream:
        // Get encoder class id for jpg compression
        // for other compressions use
        //    image/bmp
        //    image/jpeg
        //    image/gif
        //    image/tiff
        //    image/png
        CLSID pngClsid;
        GetEncoderClsid(L"image/gif", &pngClsid);

        // Setup encoder parameters
        EncoderParameters encoderParameters;
        encoderParameters.Count = 1;
        encoderParameters.Parameter[0].Guid = EncoderQuality;
        encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
        encoderParameters.Parameter[0].NumberOfValues = 1;
        // setup compression level
        ULONG quality = 50;
        encoderParameters.Parameter[0].Value = &quality;

        //  Save the image to the stream

        if(rhs.img->Save(pIStream, &pngClsid, &encoderParameters) != Ok)
        {
            pIStream->Release();
            DebugText("error on saving to IStream");
        }

        //getting the stream size:

        STATSTG sts;
        pIStream->Stat(&sts, STATFLAG_DEFAULT);
        ULARGE_INTEGER uli = sts.cbSize;
        LARGE_INTEGER zero;
        zero.QuadPart = 0;
        int size = (int)uli.QuadPart;
        char* bits = new char[size];
        DebugText("write: " + to_string(size));
        ULONG written;
        pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
        pIStream->Read(bits, size, &written);

        //write the stream size on file
        lhs.write(reinterpret_cast<char*>(&size),sizeof(int));

        //write pBuff data on file
        lhs.write(reinterpret_cast<char*>(bits),size);

        //clean resources
        delete[] bits;

        pIStream->Release();

        return lhs;
    }

    friend std::istream& operator >> (std::istream& lhs, image& rhs)
    {
        //getting IStream size:
        int streamsize;
        lhs.read(reinterpret_cast<char*>(&streamsize), sizeof(int));

        // getting IStream data:
        IStream* pIStream = nullptr;

        char* p =new char[streamsize];
        lhs.read(reinterpret_cast<char*>(p),streamsize);

        if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");


        LARGE_INTEGER zero;
        zero.QuadPart = 0;
        pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
        ULONG written;
        pIStream->Write(p,streamsize, &written);
        //reading IStream to Image class:
        rhs.img=Image::FromStream(pIStream);
        Status stat = rhs.img->GetLastStatus();
        if(stat!=S_OK)
            DebugText("error reading stream to Image: "  + to_string(stat));
        else
        {
            DebugText("reading stream to Image sucessfull");
        }
        //realease resources:
        pIStream->Release();

        //prepare the image to be drawed:
        rhs.imageweight=rhs.img->GetWidth();
        rhs.imageheight=rhs.img->GetHeight();
        rhs.intSelectFrame=0;
        rhs.resize(rhs.imageweight,rhs.imageheight);
        Gdiplus::Graphics graphics(rhs.HBitmap);
        Matrix rotatepoint;
        PointF rotatezeropoint={(REAL)rhs.imageweight/2,(REAL)rhs.imageheight/2};
        rotatepoint.RotateAt(rhs.intRotate,rotatezeropoint);

        graphics.SetTransform(&rotatepoint);
        rhs.BeforeDrawImage(rhs.intSelectFrame);
        graphics.DrawImage(rhs.img, 0,0,rhs.imageweight,rhs.imageheight);
        //BitBlt(GetWindowDC(ActivatedForm),100,100,rhs.imageweight,rhs.imageheight,rhs.HBitmap,0,0,SRCCOPY);
        UINT count = 0;
        count = rhs.img->GetFrameDimensionsCount();
        vector<GUID> pDimensionIDs;
        pDimensionIDs.resize(count);
        rhs.img->GetFrameDimensionsList(pDimensionIDs.data(), pDimensionIDs.size());
        rhs.framecount=rhs.img->GetFrameCount(pDimensionIDs.data());
        rhs.framedelay =rhs.img->GetPropertyItemSize(PropertyTagFrameDelay);
        DebugText("frame count: " + to_string(rhs.framecount));
        if(rhs.framecount>1)
        {

            rhs.tmrAnimation.timerprocedure=[&]()
            {
                static int intFrame=0;
                intFrame=intFrame+1;
                if(intFrame==rhs.framecount)
                    intFrame=0;
                rhs.SelectFrame=intFrame;

            };
            rhs.tmrAnimation.Interval=rhs.framedelay;
            rhs.tmrAnimation.Start();
        }
        rhs.UpdateReturnBitmap();
        rhs.strfilename="istream";
        //BitBlt(GetWindowDC(ActivatedForm),100,100,rhs.imageweight,rhs.imageheight,rhs.hbmMask,0,0,SRCCOPY);
        //DrawHBITMAPtoHDC(rhs.hbmMask,GetWindowDC(ActivatedForm),200,200);
        //these is for do the reset option:
        rhs.OriginalBitmap=rhs.HBitmap;
        rhs.DrawImage(rhs.intSelectFrame);
        rhs.AfterDrawImage(rhs.intSelectFrame);
        if(rhs.framecount>1)
        {
            rhs.tmrAnimation.timerprocedure=[&]()
            {
                static int intFrame=0;
                intFrame=intFrame+1;
                if(intFrame==rhs.framecount)
                    intFrame=0;
                rhs.SelectFrame=intFrame;
            };
            rhs.tmrAnimation.Interval=rhs.framedelay;
            rhs.tmrAnimation.Start();
        }
        return lhs;
    }

please see these little code:
//Create an empty IStream:
        IStream* pIStream = nullptr;
        if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");

        //choose image format for save it on IStream:
        // Get encoder class id for jpg compression
        // for other compressions use
        //    image/bmp
        //    image/jpeg
        //    image/gif
        //    image/tiff
        //    image/png
        CLSID pngClsid;
        GetEncoderClsid(L"image/gif", &pngClsid);

        // Setup encoder parameters
        EncoderParameters encoderParameters;
        encoderParameters.Count = 1;
        encoderParameters.Parameter[0].Guid = EncoderQuality;
        encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
        encoderParameters.Parameter[0].NumberOfValues = 1;
        // setup compression level
        ULONG quality = 50;
        encoderParameters.Parameter[0].Value = &quality;

        //  Save the image to the stream

        if(rhs.img->Save(pIStream, &pngClsid, &encoderParameters) != Ok)

why is saved the actual frame to pIStream, instead all frame?
(it's an animated gif, but i only know save the actual frames :(/> )

This post has been edited by Cambalinho: 20 July 2016 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1