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. Locking 24bpp bitmap portion

Locking 24bpp bitmap portion

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestionlounge
8 Posts 3 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    How to select correct width to lock from 24bpp bitmap of random width, that the locked bitmpa data has the stride equal to 3 * locked bitmap data width? bitmapData.stride == bitmapData.width * 3

    Чесноков

    U L 2 Replies Last reply
    0
    • C Chesnokov Yuriy

      How to select correct width to lock from 24bpp bitmap of random width, that the locked bitmpa data has the stride equal to 3 * locked bitmap data width? bitmapData.stride == bitmapData.width * 3

      Чесноков

      U Offline
      U Offline
      uraeu
      wrote on last edited by
      #2

      Depends how your bitmap is stored. If you have this structure:

      BITMAPINFOHEADER bmHeader

      it has a member

      //LONG biWidth;
      LONG width = bmHeader.biWidth

      C 1 Reply Last reply
      0
      • U uraeu

        Depends how your bitmap is stored. If you have this structure:

        BITMAPINFOHEADER bmHeader

        it has a member

        //LONG biWidth;
        LONG width = bmHeader.biWidth

        C Offline
        C Offline
        Chesnokov Yuriy
        wrote on last edited by
        #3

        I need to do it using GDI+ How do we need to decrease bitmap width, that after it was locked to read its RGB triplets, the stride in that array will be equal to (3 * width) e.g. 640x480 24bpp bitmap has stride 1920 = 3 * 640 but some 257x150 24bpp has stride not equal to 3 * 257 What is the formula to decrease 257 to fit into 3 * width = stride?

        Чесноков

        1 Reply Last reply
        0
        • C Chesnokov Yuriy

          How to select correct width to lock from 24bpp bitmap of random width, that the locked bitmpa data has the stride equal to 3 * locked bitmap data width? bitmapData.stride == bitmapData.width * 3

          Чесноков

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I believe the memory holding the pixel data is DWORD aligned. So wouldnt you just do:#define ALIGN(val)((val + 3) & ~3) DWORD iSize = (sizeof(RGBTRIPLE) * bitmapData.width); iSize = ALIGN(iSize);
          Am I right? Best Wishes, -David Delaune

          C 1 Reply Last reply
          0
          • L Lost User

            I believe the memory holding the pixel data is DWORD aligned. So wouldnt you just do:#define ALIGN(val)((val + 3) & ~3) DWORD iSize = (sizeof(RGBTRIPLE) * bitmapData.width); iSize = ALIGN(iSize);
            Am I right? Best Wishes, -David Delaune

            C Offline
            C Offline
            Chesnokov Yuriy
            wrote on last edited by
            #5

            say we have width = 257; what is the width should be to (width * 3) % 4 = 0

            Чесноков

            L 1 Reply Last reply
            0
            • C Chesnokov Yuriy

              say we have width = 257; what is the width should be to (width * 3) % 4 = 0

              Чесноков

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              (((257 * sizeof(RGBTRIPLE)) + 3) & ~3) == 772; Best Wishes, -David Delaune

              C 1 Reply Last reply
              0
              • L Lost User

                (((257 * sizeof(RGBTRIPLE)) + 3) & ~3) == 772; Best Wishes, -David Delaune

                C Offline
                C Offline
                Chesnokov Yuriy
                wrote on last edited by
                #7

                I do not need to estimate stride of bitmap given the width I need to estimate width of the bitmap for a stride. In the equation 257 is the x variable needed to be found: (((x * sizeof(RGBTRIPLE))) + 3) & ~3) = 772 we have width = 257 257 * 3 = 771, 771 / sizeof(DWORD) = 192.75 hence we need to decrease 257 the way the stride / 4 = integer 252 * 3 = 756, 756 / sizeof(DWORD) = 189 How can we found that width = 252 from original 257?

                Чесноков

                L 1 Reply Last reply
                0
                • C Chesnokov Yuriy

                  I do not need to estimate stride of bitmap given the width I need to estimate width of the bitmap for a stride. In the equation 257 is the x variable needed to be found: (((x * sizeof(RGBTRIPLE))) + 3) & ~3) = 772 we have width = 257 257 * 3 = 771, 771 / sizeof(DWORD) = 192.75 hence we need to decrease 257 the way the stride / 4 = integer 252 * 3 = 756, 756 / sizeof(DWORD) = 189 How can we found that width = 252 from original 257?

                  Чесноков

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I am not understanding the numbers you are showing me. For example:

                  Chesnokov Yuriy wrote:

                  257 * 3 = 771, 771 / sizeof(DWORD) = 192.75

                  Why are you dividing 771 by sizeof(DWORD)? This does not make any sense to me. Each pixel is represented by 3 bytes or sizeof(RGBTRIPLE). The padding will be at the end of the memory allocated. Like this: xxxxxxxxxxxxxxxxPADDING You should be able to calculate the original bitmap width by checking that stride modulo RGBTRIPLE is equal to zero. For finding the best width where width is equal to stride you can multiply width by RGBTRIPLE and check that it is equal to the alignment. Something like this:#define ALIGN(val)((val + 3) & ~3) #define ISSTRIDEALIGNED(val)((val * sizeof(RGBTRIPLE)) == ALIGN(val * sizeof(RGBTRIPLE))) DWORD OriginalWidth24(DWORD dwInStride) { if(0 != dwInStride % sizeof(RGBTRIPLE)) { --dwInStride; } return dwInStride / sizeof(RGBTRIPLE); } DWORD BestWidthEqualStride(DWORD dwWidth) { while(dwWidth && (sizeof(RGBTRIPLE) * dwWidth) != ALIGN(dwWidth * sizeof(RGBTRIPLE))) { --dwWidth; } return dwWidth; }
                  And you can check their values:DWORD dwBitmapWidth = 257; DWORD dwNonAlignedSize = (dwBitmapWidth * sizeof(RGBTRIPLE)); DWORD dwAlignedSize = ALIGN(dwBitmapWidth * sizeof(RGBTRIPLE)); DWORD dwOriginalWidth = OriginalWidth24(dwAlignedSize); DWORD dwWidthEqualsStride = BestWidthEqualStride(dwBitmapWidth);
                  Best Wishes, -David Delaune

                  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