Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Problem extracting full palette from 8bit image

Problem extracting full palette from 8bit image

Scheduled Pinned Locked Moved Graphics
comgraphicstoolshelpquestion
14 Posts 2 Posters 7 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mark Salsbery

    This sounds kinda strange... Copying 1 byte from one location to another 768 times shouldn't take long (unless you have a REALLY slow machine. I'm pretty sure the original PC at 4.77 MHz could do it in under a millisecond :) If you break execution when it stalls is it really in the loop or is it maybe in the Tools.BitsFromPixelFormat(bmp.PixelFormat) call. Mark

    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

    S Offline
    S Offline
    Sepharo
    wrote on last edited by
    #5

    Machine can more than handle this that's why I posted the question because it's so strange... Yes it does stall during the loop not during the bitsfrompixel Bits from pixel is just a simply switch anyway public static int BitsFromPixelFormat(PixelFormat pixelFormat) { switch (pixelFormat) { case PixelFormat.Format1bppIndexed: return 1; case PixelFormat.Format4bppIndexed: return 4; case PixelFormat.Format8bppIndexed: return 8; ... etc.

    S M 2 Replies Last reply
    0
    • S Sepharo

      Machine can more than handle this that's why I posted the question because it's so strange... Yes it does stall during the loop not during the bitsfrompixel Bits from pixel is just a simply switch anyway public static int BitsFromPixelFormat(PixelFormat pixelFormat) { switch (pixelFormat) { case PixelFormat.Format1bppIndexed: return 1; case PixelFormat.Format4bppIndexed: return 4; case PixelFormat.Format8bppIndexed: return 8; ... etc.

      S Offline
      S Offline
      Sepharo
      wrote on last edited by
      #6

      Here's a picture of my actual breakpoints... http://wwwp.mirror.waffleimages.com/files/13/13e214fd705683442d70081bb7c901230aac708a.png With the wya they are setup in the picture it takes a while to manually step through 256times but it reaches the end eventually and all is fine... But only because it took some time to do manually. If I remove the breakpoints on i < bmp.Palette.Entries.Length; and i++; the stall happens directly between int i=0; and return rgbArray; There isn't any further question in my mind that something is going wrong during that palette copying inside that loop. But the real question is why? This machine is a beast and the rest of the code is fine just for somereason 256 iterations of rgbArray[i].rgbRed = bmp.Palette.Entries[i].R; rgbArray[i].rgbGreen= bmp.Palette.Entries[i].G; rgbArray[i].rgbBlue = bmp.Palette.Entries[i].B; stalls the program for 30 seconds or so.

      M 1 Reply Last reply
      0
      • S Sepharo

        Machine can more than handle this that's why I posted the question because it's so strange... Yes it does stall during the loop not during the bitsfrompixel Bits from pixel is just a simply switch anyway public static int BitsFromPixelFormat(PixelFormat pixelFormat) { switch (pixelFormat) { case PixelFormat.Format1bppIndexed: return 1; case PixelFormat.Format4bppIndexed: return 4; case PixelFormat.Format8bppIndexed: return 8; ... etc.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #7

        Hmmm... Is this managed C++? If so, does optimizing the loop help any?

        public static RGBQUAD[] RGBQUADFromColorArray(Bitmap bmp)
        {
        // Some programs as Axialis have problems with a reduced palette, so lets create a full palette
        int bits = Tools.BitsFromPixelFormat(bmp.PixelFormat);

        if (bits <= 8)
        {
        RGBQUAD[] rgbArray = new RGBQUAD[1 << bits];

          ColorPalette ^palette = bmp.Palette;
          array<Color>^palcolors = palette->Entries;
          for(int i=0; i < palcolors->Length; i++)
          {
             rgbArray\[i\].rgbRed = palcolors\[i\].R;
             rgbArray\[i\].rgbGreen= palcolors\[i\].G;
             rgbArray\[i\].rgbBlue = palcolors\[i\].B;
          }
        
          return rgbArray;
        

        }

        return 0;
        }

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        S 1 Reply Last reply
        0
        • S Sepharo

          Here's a picture of my actual breakpoints... http://wwwp.mirror.waffleimages.com/files/13/13e214fd705683442d70081bb7c901230aac708a.png With the wya they are setup in the picture it takes a while to manually step through 256times but it reaches the end eventually and all is fine... But only because it took some time to do manually. If I remove the breakpoints on i < bmp.Palette.Entries.Length; and i++; the stall happens directly between int i=0; and return rgbArray; There isn't any further question in my mind that something is going wrong during that palette copying inside that loop. But the real question is why? This machine is a beast and the rest of the code is fine just for somereason 256 iterations of rgbArray[i].rgbRed = bmp.Palette.Entries[i].R; rgbArray[i].rgbGreen= bmp.Palette.Entries[i].G; rgbArray[i].rgbBlue = bmp.Palette.Entries[i].B; stalls the program for 30 seconds or so.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #8

          I'm wondering if it's reallocating/recreating a new palette every time the Palette property is accessed. Maybe the optimized loop I posted below will help :) Mark

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          1 Reply Last reply
          0
          • M Mark Salsbery

            Hmmm... Is this managed C++? If so, does optimizing the loop help any?

            public static RGBQUAD[] RGBQUADFromColorArray(Bitmap bmp)
            {
            // Some programs as Axialis have problems with a reduced palette, so lets create a full palette
            int bits = Tools.BitsFromPixelFormat(bmp.PixelFormat);

            if (bits <= 8)
            {
            RGBQUAD[] rgbArray = new RGBQUAD[1 << bits];

              ColorPalette ^palette = bmp.Palette;
              array<Color>^palcolors = palette->Entries;
              for(int i=0; i < palcolors->Length; i++)
              {
                 rgbArray\[i\].rgbRed = palcolors\[i\].R;
                 rgbArray\[i\].rgbGreen= palcolors\[i\].G;
                 rgbArray\[i\].rgbBlue = palcolors\[i\].B;
              }
            
              return rgbArray;
            

            }

            return 0;
            }

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            S Offline
            S Offline
            Sepharo
            wrote on last edited by
            #9

            The language is C#

            M 2 Replies Last reply
            0
            • S Sepharo

              The language is C#

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #10

              You should be able to do the same thing. The point is, pull the Palette and Palette.Entries property accesses OUT of the loop and loop through the entries array only. Sorry I don't know the syntax - it should be simple for you I would think :) Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              S 1 Reply Last reply
              0
              • S Sepharo

                The language is C#

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #11

                I'll take a WILD stab at the C# syntax LOL...

                public static RGBQUAD[] RGBQUADFromColorArray(Bitmap bmp)
                {
                // Some programs as Axialis have problems with a reduced palette, so lets create a full palette
                int bits = Tools.BitsFromPixelFormat(bmp.PixelFormat);

                if (bits <= 8)
                {
                RGBQUAD[] rgbArray = new RGBQUAD[1 << bits];

                  ColorPalette palette = bmp.Palette;
                  Color\[\] palcolors = palette.Entries;
                  for(int i=0; i < palcolors.Length; i++)
                  {
                     rgbArray\[i\].rgbRed = palcolors\[i\].R;
                     rgbArray\[i\].rgbGreen= palcolors\[i\].G;
                     rgbArray\[i\].rgbBlue = palcolors\[i\].B;
                  }
                
                  return rgbArray;
                

                }

                return 0;
                }

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                S 1 Reply Last reply
                0
                • M Mark Salsbery

                  You should be able to do the same thing. The point is, pull the Palette and Palette.Entries property accesses OUT of the loop and loop through the entries array only. Sorry I don't know the syntax - it should be simple for you I would think :) Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  S Offline
                  S Offline
                  Sepharo
                  wrote on last edited by
                  #12

                  Wow that worked... You're a lifesaver and a genius. I wouldn't have thought that could fix it either but I was willing to try anything, so thanks for suggesting what I thought didn't matter. :D

                  M 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    I'll take a WILD stab at the C# syntax LOL...

                    public static RGBQUAD[] RGBQUADFromColorArray(Bitmap bmp)
                    {
                    // Some programs as Axialis have problems with a reduced palette, so lets create a full palette
                    int bits = Tools.BitsFromPixelFormat(bmp.PixelFormat);

                    if (bits <= 8)
                    {
                    RGBQUAD[] rgbArray = new RGBQUAD[1 << bits];

                      ColorPalette palette = bmp.Palette;
                      Color\[\] palcolors = palette.Entries;
                      for(int i=0; i < palcolors.Length; i++)
                      {
                         rgbArray\[i\].rgbRed = palcolors\[i\].R;
                         rgbArray\[i\].rgbGreen= palcolors\[i\].G;
                         rgbArray\[i\].rgbBlue = palcolors\[i\].B;
                      }
                    
                      return rgbArray;
                    

                    }

                    return 0;
                    }

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    S Offline
                    S Offline
                    Sepharo
                    wrote on last edited by
                    #13

                    Yep you got it, same thing I translated your C++ to. In case you didn't see my above reply... You got it fixed thanks so much.

                    1 Reply Last reply
                    0
                    • S Sepharo

                      Wow that worked... You're a lifesaver and a genius. I wouldn't have thought that could fix it either but I was willing to try anything, so thanks for suggesting what I thought didn't matter. :D

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #14

                      Cool! I had no idea using the properties in the loop would be that slow! Good to know! :) Mark

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups