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. C / C++ / MFC
  4. Writing 8-bit grayscale bitmap data

Writing 8-bit grayscale bitmap data

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsdata-structurestutorial
7 Posts 2 Posters 0 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 Offline
    M Offline
    Member 2116736
    wrote on last edited by
    #1

    Hi everybody, I'm currently working with 8-bit grayscale bitmap scanned from my HP LaserJet 3055. I am able to read the Bitmap Header and its data into an array of BYTE. I write the values directly into Text File, the result is great! However, I can't create another BMP file using the data array I have. For 24-bit BMP, what we have to write after the Header is the RGB data, but for the grayscale one we have to use the Color Table, right? How can I use the Color table properly? Please anyone helps me on how to create an 8-bit grayscale bitmap! Thanks,

    Ing LengIeng Software Developer

    A 1 Reply Last reply
    0
    • M Member 2116736

      Hi everybody, I'm currently working with 8-bit grayscale bitmap scanned from my HP LaserJet 3055. I am able to read the Bitmap Header and its data into an array of BYTE. I write the values directly into Text File, the result is great! However, I can't create another BMP file using the data array I have. For 24-bit BMP, what we have to write after the Header is the RGB data, but for the grayscale one we have to use the Color Table, right? How can I use the Color table properly? Please anyone helps me on how to create an 8-bit grayscale bitmap! Thanks,

      Ing LengIeng Software Developer

      A Offline
      A Offline
      Akt_4_U
      wrote on last edited by
      #2

      Hi, For an 8 bit grayscale image, between the header and pixel data, there will a color map of 256 colors. The color map will hold 256 shades of gray(ie R=G=B). The actual pixel data will be a reference this color map. So fo creating 8 bit grayscale image do the following steps. 1. Open a file in binary mode. 2. Write Bitmap File header to file. 3. Write Bitmap Info header to file. 4. Write Colormap to file. 5. Write pixel data to file. 6. Close file. Thanks Akt_4_U

      akt

      M 1 Reply Last reply
      0
      • A Akt_4_U

        Hi, For an 8 bit grayscale image, between the header and pixel data, there will a color map of 256 colors. The color map will hold 256 shades of gray(ie R=G=B). The actual pixel data will be a reference this color map. So fo creating 8 bit grayscale image do the following steps. 1. Open a file in binary mode. 2. Write Bitmap File header to file. 3. Write Bitmap Info header to file. 4. Write Colormap to file. 5. Write pixel data to file. 6. Close file. Thanks Akt_4_U

        akt

        M Offline
        M Offline
        Member 2116736
        wrote on last edited by
        #3

        Thanks very much, Akt! But how do I write the Colormap? I wrote the following code, but I didn't get the grayscale anymore; instead, I got a more green color for the new image.

        for(int c=0; c<256; c++)
        {
        fwrite((BYTE*)&bmpInfo.bmiColors[c], sizeof(RGBQUAD), 1, bmpFile);
        }

        Regards,

        Ing LengIeng Software Developer

        A 1 Reply Last reply
        0
        • M Member 2116736

          Thanks very much, Akt! But how do I write the Colormap? I wrote the following code, but I didn't get the grayscale anymore; instead, I got a more green color for the new image.

          for(int c=0; c<256; c++)
          {
          fwrite((BYTE*)&bmpInfo.bmiColors[c], sizeof(RGBQUAD), 1, bmpFile);
          }

          Regards,

          Ing LengIeng Software Developer

          A Offline
          A Offline
          Akt_4_U
          wrote on last edited by
          #4

          Hope you have read the ColorMap completely from source bitmap. That is you have to allocate enough memory for reading the complete ColorMap(256 colors) for source image. The following code will do this. BYTE* pbyThumbImage // This is your source bitmap buffer. const ULONG COLORMAP_START_OFFSET = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ); // Color map starts after Bitmap file header and Bitmap info header. UINT uColorMapSize = 256 * sizeof( RGBQUAD ); pBmpInfoHdr = reinterpret_cast( GlobalAllocPtr( GHND, sizeof( BITMAPINFOHEADER ) + uColorMapSize )); memcpy( pBmpInfoHdr->bmiColors, ( pbyThumbImage + COLORMAP_START_OFFSET ), uColorMapSize ); // Now complete colormap information is within your pBmpInfoHdr->bmiColors. Now you can write this colormap to destination file using fwrite with input buffer as pBmpInfoHdr->bmiColors and size to be written as uColorMapSize.

          akt

          M 1 Reply Last reply
          0
          • A Akt_4_U

            Hope you have read the ColorMap completely from source bitmap. That is you have to allocate enough memory for reading the complete ColorMap(256 colors) for source image. The following code will do this. BYTE* pbyThumbImage // This is your source bitmap buffer. const ULONG COLORMAP_START_OFFSET = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ); // Color map starts after Bitmap file header and Bitmap info header. UINT uColorMapSize = 256 * sizeof( RGBQUAD ); pBmpInfoHdr = reinterpret_cast( GlobalAllocPtr( GHND, sizeof( BITMAPINFOHEADER ) + uColorMapSize )); memcpy( pBmpInfoHdr->bmiColors, ( pbyThumbImage + COLORMAP_START_OFFSET ), uColorMapSize ); // Now complete colormap information is within your pBmpInfoHdr->bmiColors. Now you can write this colormap to destination file using fwrite with input buffer as pBmpInfoHdr->bmiColors and size to be written as uColorMapSize.

            akt

            M Offline
            M Offline
            Member 2116736
            wrote on last edited by
            #5

            But soon after I've finished reading the Colormap, I displayed the value of each RGB I can see that the color itself is of color more than the grayscale. I did so by comparing the RGB I get with the value in Microsoft Paint (mspaint.exe). That's why the result of the new image is rather a color one. How can I solve this?

            Ing LengIeng Software Developer

            A 1 Reply Last reply
            0
            • M Member 2116736

              But soon after I've finished reading the Colormap, I displayed the value of each RGB I can see that the color itself is of color more than the grayscale. I did so by comparing the RGB I get with the value in Microsoft Paint (mspaint.exe). That's why the result of the new image is rather a color one. How can I solve this?

              Ing LengIeng Software Developer

              A Offline
              A Offline
              Akt_4_U
              wrote on last edited by
              #6

              Which value did u take? R G and B from color map?

              akt

              M 1 Reply Last reply
              0
              • A Akt_4_U

                Which value did u take? R G and B from color map?

                akt

                M Offline
                M Offline
                Member 2116736
                wrote on last edited by
                #7

                Hi Akt, Now I've solved my problem. I make the Colormap again by myself through the loop, and write it into the new bmp. Thanks a lot! :) Regards,

                Ing LengIeng Software Developer

                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