Image: how save and read IStream from a file?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

48 Replies - 4239 Views - Last Post: 21 October 2015 - 12:23 PM Rate Topic: ****- 3 Votes

#1 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Image: how save and read IStream from a file?

Posted 08 October 2015 - 11:40 AM

i'm overloaing the std::ostream& operator << and std::istream& operator >> for save the Image data into a file:
friend std::ostream& operator << (std::ostream& lhs, const 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:

        HGLOBAL hg;
        GetHGlobalFromStream(pIStream,&hg);
        int buffsize=GlobalSize(hg);
        if(buffsize==0)
            DebugText("error on getting IStream size");
        char *pBuff = new char[buffsize];
        ULONG ulBytesRead;

        //Read the stream to pBuff
        if(pIStream->Read(pBuff,buffsize, &ulBytesRead)!=S_OK)
            DebugText("error on saving IStream to buffer");

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

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

        //clean resources
        delete[] pBuff;
        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(streamsize));

        IStream* pIStream = nullptr;

        // Create stream with 0 size
        HGLOBAL hg;
        void* p =GlobalLock(hg);
        lhs.read(reinterpret_cast<char*>(p),sizeof(streamsize));
        GlobalUnlock(hg);

        if(CreateStreamOnHGlobal(hg, TRUE, (LPSTREAM*)&pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");
        //reading IStream on Image class:
        if(rhs.img->FromStream(pIStream,TRUE)!=S_OK)
            DebugText("error reading stream to Image");

        //realease resources:
        pIStream->Release();
        return lhs;
    }

the IStream is totaly new for me. i don't get a compiler errors, just a memory leak :(
can anyone advice me... the is so confused to me :(

Is This A Good Question/Topic? 1
  • +

Replies To: Image: how save and read IStream from a file?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13401
  • View blog
  • Posts: 53,498
  • Joined: 12-June 08

Re: Image: how save and read IStream from a file?

Posted 08 October 2015 - 11:42 AM

Not really a 'discussion'.. so moving to the general help for c++ area.
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2129
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: Image: how save and read IStream from a file?

Posted 08 October 2015 - 12:31 PM

> lhs.write(reinterpret_cast<char*>(&pBuff),buffsize);
&pBuff is definitely wrong here.
You want to write what it points to, not a copy of the pointer.

> HGLOBAL hg;
> void* p =GlobalLock(hg);
What is hg initialised to here?
Was This Post Helpful? 1
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 09 October 2015 - 07:22 AM

Again,

weird way of doing things from you Cambalinho

why not just use GdipSaveImageToFile to save the image and GdipCreateBitmapFromFile to load the image ?

why do this with streams at all ??

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 11 October 2015 - 07:07 AM

View PostSalem_c, on 08 October 2015 - 12:31 PM, said:

> lhs.write(reinterpret_cast<char*>(&pBuff),buffsize);
&pBuff is definitely wrong here.
You want to write what it points to, not a copy of the pointer.

> HGLOBAL hg;
> void* p =GlobalLock(hg);
What is hg initialised to here?

is for save directly to pIStream.


snoopy11: "why do this with streams at all ??"
because i need save it, like 1 part of the file. i will save more information on file.

Moderator: or it's me or i don't recive mail notifications :(
'Enable email notification of replies?' is checked
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 12 October 2015 - 04:46 AM

I think i see what youre trying to do.

Are you trying to create a compound file ?
Was This Post Helpful? 1
  • +
  • -

#7 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 12 October 2015 - 12:41 PM

View Postsnoopy11, on 12 October 2015 - 04:46 AM, said:

I think i see what youre trying to do.

Are you trying to create a compound file ?

'compound file'? i don't know these term: https://msdn.microso...v=vs.85%29.aspx
but yes. i'm. but these is so complex what seems easy :(
if i continue with problems(yes i'm with these code on weeks :( ), i will do my own class for IStream, because is more
easy to test and then to use ;)

Moderator: thanks. i recived the mail notification. thanks
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 12 October 2015 - 04:07 PM

Look at this implementation from msdn on compound files using IStorage


Status MakeCompoundFile()
{
   IStorage* pIStorage = NULL;
   IStream* pIStream1 = NULL;
   IStream* pIStream2 = NULL;
   HRESULT hr;
   Status stat = Ok;

   // Create two Image objects from existing files.
   Image image1(L"Crayons.jpg");
   Image image2(L"Mosaic.png");

   hr = CoInitialize(NULL);
   if(FAILED(hr))
      goto Exit;

   // Create a compound file object, and get
   // a pointer to its IStorage interface.
   hr = StgCreateDocfile(
      L"CompoundFile.cmp", 
      STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE, 
      0, 
      &pIStorage);

   if(FAILED(hr))
      goto Exit;

   // Create a stream in the compound file.
   hr = pIStorage->CreateStream(
      L"StreamImage1",
      STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
      0,
      0,
      &pIStream1);

   if(FAILED(hr))
      goto Exit;

   // Create a second stream in the compound file.
   hr = pIStorage->CreateStream(
      L"StreamImage2",
      STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
      0,
      0,
      &pIStream2);

   if(FAILED(hr))
      goto Exit;

   // Get the class identifier for the JPEG encoder.
   CLSID jpgClsid;
   GetEncoderClsid(L"image/jpeg", &jpgClsid);

   // Get the class identifier for the PNG encoder.
   CLSID pngClsid;
   GetEncoderClsid(L"image/png", &pngClsid);

   // Save image1 as a stream in the compound file.
   stat = image1.Save(pIStream1, &jpgClsid);
   if(stat != Ok)
      goto Exit;

   // Save image2 as a stream in the compound file.
   stat = image2.Save(pIStream2, &pngClsid);

Exit:
   if(pIStream1)
      pIStream1->Release(); 
   if(pIStream2)
      pIStream2->Release();
   if(pIStorage)
      pIStorage->Release();

   if(stat != Ok || FAILED(hr))
      return GenericError;

   return Ok;
}



from this webpage

https://msdn.microso...6(v=vs.85).aspx

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#9 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 11:51 AM

View Postsnoopy11, on 12 October 2015 - 04:07 PM, said:

Look at this implementation from msdn on compound files using IStorage


Status MakeCompoundFile()
{
   IStorage* pIStorage = NULL;
   IStream* pIStream1 = NULL;
   IStream* pIStream2 = NULL;
   HRESULT hr;
   Status stat = Ok;

   // Create two Image objects from existing files.
   Image image1(L"Crayons.jpg");
   Image image2(L"Mosaic.png");

   hr = CoInitialize(NULL);
   if(FAILED(hr))
      goto Exit;

   // Create a compound file object, and get
   // a pointer to its IStorage interface.
   hr = StgCreateDocfile(
      L"CompoundFile.cmp", 
      STGM_READWRITE|STGM_CREATE|STGM_SHARE_EXCLUSIVE, 
      0, 
      &pIStorage);

   if(FAILED(hr))
      goto Exit;

   // Create a stream in the compound file.
   hr = pIStorage->CreateStream(
      L"StreamImage1",
      STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
      0,
      0,
      &pIStream1);

   if(FAILED(hr))
      goto Exit;

   // Create a second stream in the compound file.
   hr = pIStorage->CreateStream(
      L"StreamImage2",
      STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
      0,
      0,
      &pIStream2);

   if(FAILED(hr))
      goto Exit;

   // Get the class identifier for the JPEG encoder.
   CLSID jpgClsid;
   GetEncoderClsid(L"image/jpeg", &jpgClsid);

   // Get the class identifier for the PNG encoder.
   CLSID pngClsid;
   GetEncoderClsid(L"image/png", &pngClsid);

   // Save image1 as a stream in the compound file.
   stat = image1.Save(pIStream1, &jpgClsid);
   if(stat != Ok)
      goto Exit;

   // Save image2 as a stream in the compound file.
   stat = image2.Save(pIStream2, &pngClsid);

Exit:
   if(pIStream1)
      pIStream1->Release(); 
   if(pIStream2)
      pIStream2->Release();
   if(pIStorage)
      pIStorage->Release();

   if(stat != Ok || FAILED(hr))
      return GenericError;

   return Ok;
}



from this webpage

https://msdn.microso...6(v=vs.85).aspx

Regards

Snoopy.

i'm sorry, but on these code i only see 2 things:
1 - how create IStream;
2 - how save the Image on IStream.
i need to understand how save it on a file and read it.
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 12:13 PM

Right lets clear this up,

What are you trying to save exactly is it an image with text ?

Or something else entirely ?
Was This Post Helpful? 1
  • +
  • -

#11 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 12:18 PM

View Postsnoopy11, on 13 October 2015 - 12:13 PM, said:

Right lets clear this up,

What are you trying to save exactly is it an image with text ?

Or something else entirely ?

the IStream have all Image that(using the Save()), right?
so i need to save that IStream on file(overloading the >>\<< stream operators, for save it on actual seek)(the file must have several data (images\string\int's). but, for now, i'm trying correct these code for try save the IStream on file. but for that i must convert it to void* or char*, right?
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 12:28 PM

Well the file either must have a uniform structure or it must be implemented with header identifiers as if not then you're not going to be able to read the information back in any sane way.

So which is it ?

uniform set in stone file structure or header i.d.'s ?
Was This Post Helpful? 1
  • +
  • -

#13 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 12:36 PM

View Postsnoopy11, on 13 October 2015 - 12:28 PM, said:

Well the file either must have a uniform structure or it must be implemented with header identifiers as if not then you're not going to be able to read the information back in any sane way.

So which is it ?

uniform set in stone file structure or header i.d.'s ?

i have use a header i.d.'s. unless you are tell me that i must save the size of IStream. yes i must save the size of IStream for read it then.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1317
  • View blog
  • Posts: 4,043
  • Joined: 20-March 10

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 12:52 PM

Its not just that the images/strings/ints

and other types if there are other types...

must have their own headers each identifying it as its appropriate type and how big it is otherwise you will not know when to stop reading it and when another type begins.

You can't simply make a compound file which is basically a new file type and expect it to sort itself out.

inside the compound file you would expect to see things like

BEG
// beginning of file
JPG SZ 5938
//jpg data 5938 bytes in length
INT SZ 43
// 43 numbers
STR SZ 100
// a string 100 chars
END
// end of file marker



Its really up to you which codes you use but the basic premise is contained in the above.

Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#15 Cambalinho  Icon User is offline

  • D.I.C Addict

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

Re: Image: how save and read IStream from a file?

Posted 13 October 2015 - 01:01 PM

mod: you don't need to quote the post right above yours. ;)
--
i'm sorry, but the IStream don't have all the info for Image read it?

This post has been edited by modi123_1: 13 October 2015 - 01:07 PM
Reason for edit:: removed excessive quote

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »