dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: GDIPLUS: how can i set the ColorPalette to image?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,272

    GDIPLUS: how can i set the ColorPalette to image?

    see how i get the ColorPalette:
    Code:
    ColorPalette *imgPallet;
    //....................
    else
            {
                img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    i don't get any error here(DebugText() it's 1 function for i get the errors).
    now heres how i set it:
    Code:
    friend std::ostream& operator << (std::ostream& lhs, image& rhs)
    {
    //.............
     parameterValue = EncoderValueMultiFrame;
            Status            stat;
            //rhs.imgPallet->Flags=0;
            if(rhs.imgPallet==NULL)
                DebugText("palette not");
            stat=rhs.img->SetPalette((const ColorPalette*)rhs.imgPallet);
            if(stat!=0)
                DebugText("palette not changed!!!\t" + to_string(stat));
            if(rhs.img->Save(pIStream, &pngClsid, encoderParameters) != Ok)
    the palette isn't set. i get the error 2: Invalid Parameter. i don't understand what i'm doing wrong
    and i can't see any sample for SetPalette()(only GetPalette() on MSDN)
    what i'm doing wrong?
    why change the palette!?! i don't have sure if will resolve the problem on saving gif's files:
    Name:  my istream images.jpg
Views: 300
Size:  24.6 KB

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: GDIPLUS: how can i set the ColorPalette to image?

    What is the image& rhs you are passing in the function
    Code:
    friend std::ostream& operator << (std::ostream& lhs, image& rhs)
    and what does it contain?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by VictorN View Post
    What is the image& rhs you are passing in the function
    Code:
    friend std::ostream& operator << (std::ostream& lhs, image& rhs)
    and what does it contain?
    it's my own class, that have img GDIPLUS::Image pointer and more

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by Cambalinho View Post
    it's my own class, that have img GDIPLUS::Image pointer and more
    Well, but we don't see this class declaration nor know we how you use this class!
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Code:
    stat=rhs.img->SetPalette((const ColorPalette*)rhs.imgPallet);
    What is the type of imgPallet? Just because this is cast to const ColorPalette*, doesn't mean that this is a valid cast!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  6. #6
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by VictorN View Post
    Well, but we don't see this class declaration nor know we how you use this class!
    have several dependences class's, but i think that will do for you see the problem:
    cambalinho.zip
    it's a big class, but see the zip file.

  7. #7
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by 2kaud View Post
    Code:
    stat=rhs.img->SetPalette((const ColorPalette*)rhs.imgPallet);
    What is the type of imgPallet? Just because this is cast to const ColorPalette*, doesn't mean that this is a valid cast!
    imgPallet it's:
    Code:
    ColorPalette *imgPallet;

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: GDIPLUS: how can i set the ColorPalette to image?

    If you use SetPalette() right after the GetPalette() call as a test, does it work or do you still get an error?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by 2kaud View Post
    If you use SetPalette() right after the GetPalette() call as a test, does it work or do you still get an error?
    Code:
    ColorPalette *imgPallet;
    //......
    void readimagefile(string filename)
        {
    //..........
    }
            else
            {
                img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    no error.. seems ok.
    on SET:
    Code:
    friend std::ostream& operator << (std::ostream& lhs, image& rhs)
        {
            //Create an empty IStream:
            IStream* pIStream = nullptr;
            if(CreateStreamOnHGlobal(NULL, FALSE, (LPSTREAM*)&pIStream)!=S_OK)
                DebugText("error on creating an empty IStream");
    
            //Create the Encoder way:
            //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*)malloc(sizeof(EncoderParameters) +  (rhs.framecount-1)* sizeof(EncoderParameter));
            encoderParameters->Count = 1;
            ULONG parameterValue = 50;
           // the for loop is pointless and does nothing
            encoderParameters->Parameter[0].Guid = EncoderSaveFlag;
            encoderParameters->Parameter[0].Type = EncoderParameterValueTypeLong;
            encoderParameters->Parameter[0].NumberOfValues = 1;
            encoderParameters->Parameter[0].Value = &parameterValue;
    
            //save the 1st frame:
            parameterValue = EncoderValueMultiFrame;
            Status            stat;
            //rhs.imgPallet->Flags=0;
            if(rhs.imgPallet==NULL)
                DebugText("palette not");
            stat=rhs.img->SetPalette((const ColorPalette*)rhs.imgPallet);
            if(stat!=0)
                DebugText("palette not changed!!!\t" + to_string(stat));
            if(rhs.img->Save(pIStream, &pngClsid, encoderParameters) != Ok)
            {
                pIStream->Release();
                MessageBox("error on saving to IStream");
            }
    
            for(int i=1; i<rhs.framecount;i++)
            {
                //create a new Image for save the selected frame:
    
                Image *img2= rhs.img;
                stat=img2->SetPalette((const ColorPalette*)rhs.imgPallet);
                if(stat!=0)
                    DebugText("palette <1 not changed!!!\t" + to_string(stat));
                //Selecte the frame:
                UINT count = 0;
                count = img2->GetFrameDimensionsCount();
                vector<GUID> pDimensionIDs;
                pDimensionIDs.resize(count);
                img2->GetFrameDimensionsList(pDimensionIDs.data(), pDimensionIDs.size());
                img2->SelectActiveFrame(&pDimensionIDs[0],i);
    
                //save the next frames, from img2, on stream:
    
                if(i==rhs.framecount-1)
                    parameterValue = EncoderValueLastFrame;
                else
                    parameterValue = EncoderValueFrameDimensionTime;
                stat = rhs.img->SaveAdd(img2, encoderParameters);
            }
            //closing the stream:
            parameterValue = EncoderValueFlush;
            stat = rhs.img->SaveAdd(encoderParameters);
    the error happens: 2 - Invalid Parameter.
    i did that casting for try it avoid the error. but the error continues

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: GDIPLUS: how can i set the ColorPalette to image?

    I meant try this
    Code:
    ColorPalette *imgPallet;
    //......
    void readimagefile(string filename)
        {
    //..........
    }
            else
            {
                img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    
              stImage=img->SetPalette(imgPallet);
              if (stImage!= OK)
                Debugtext("error: palette not set " + to_string(stImage));
    If this works then between getting the palette here and using SetPalette() later, the contents of imgPallet are changed - and you'll need to trace through the code to see where.

    PS where do you free imgPallet?
    Last edited by 2kaud; October 2nd, 2016 at 12:13 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  11. #11
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by 2kaud View Post
    I meant try this
    Code:
    ColorPalette *imgPallet;
    //......
    void readimagefile(string filename)
        {
    //..........
    }
            else
            {
                img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    
              stImage=img->SetPalette(imgPallet);
              if (stImage!= OK)
                Debugtext("error: palette not set " + to_string(stImage));
    If this works then between getting the palette here and using SetPalette() later, the contents of imgPallet are changed - and you'll need to trace through the code to see where.

    PS where do you free imgPallet?
    same error. seems that the palette it's protected or something, because can't be changed
    for the moment i was not free it for see where was the problem.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: GDIPLUS: how can i set the ColorPalette to image?

    I haven't used the Image class, but it seems very strange that you can't set the palette to what has just been obtained - I would have thought that a get followed by a set would have worked? The get is working as the return status is OK.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  13. #13
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by 2kaud View Post
    I haven't used the Image class, but it seems very strange that you can't set the palette to what has just been obtained - I would have thought that a get followed by a set would have worked? The get is working as the return status is OK.
    i'm confused like you
    is there another way for fix that problem that you have seen on picture?
    my big objective is saving the animated gif without diferences between the original and what i'm saving

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: GDIPLUS: how can i set the ColorPalette to image?

    I've looked at the example provided by Microsoft for GetPalette() here https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    and it doesn't work for all files! Some I tried said there were 0 colours in the palette. However for a file which actually had a palette with some colours then SetPalette() worked!

    For those files that said there were 0 colours in the palette then SetPalette() gave an error of 2!

    So I think the problem is that for the file used, it has no palette and so SetPalette() fails.

    In your readimagefile(), try this
    Code:
    img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    
               DebugText("Colours: " + to_string(imgPallet->Count));
    If this shows that Colours are 0 then that is the issue.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  15. #15
    Join Date
    Apr 2009
    Posts
    1,272

    Re: GDIPLUS: how can i set the ColorPalette to image?

    Quote Originally Posted by 2kaud View Post
    I've looked at the example provided by Microsoft for GetPalette() here https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    and it doesn't work for all files! Some I tried said there were 0 colours in the palette. However for a file which actually had a palette with some colours then SetPalette() worked!

    For those files that said there were 0 colours in the palette then SetPalette() gave an error of 2!

    So I think the problem is that for the file used, it has no palette and so SetPalette() fails.

    In your readimagefile(), try this
    Code:
    img= Image::FromFile(towstring(filename).c_str(), FALSE);
                //free(imgPallet);
                UINT PaletteSize;
                PaletteSize=img->GetPaletteSize();
                imgPallet=(ColorPalette*)malloc(PaletteSize);
                if(imgPallet==NULL)
                    DebugText("error: palette not created!!!!");
                Status stImage;
                stImage=img->GetPalette(imgPallet,PaletteSize);
                if(stImage!=Ok)
                    DebugText("error: palette not geted!!!!");
    
               DebugText("Colours: " + to_string(imgPallet->Count));
    If this shows that Colours are 0 then that is the issue.
    yes it's zero

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)