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#
  4. How to navigate memory using pointers?

How to navigate memory using pointers?

Scheduled Pinned Locked Moved C#
csharpperformancetutorialquestionlounge
5 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
    CopperCircle
    wrote on last edited by
    #1

    Hi, I am very new to C# but I am modifying an image buffer directly within memory, I am scanning along moving the pointer to get the RGB of each pixel, but I need away to jump around the image memory and read the pixels at random rather than just p++ or p--? Thanks...

      unsafe 
                { 
                    byte\* p = (byte\*)(void\*)pBuffer;
                    for(int y = 0; y < Height; y++)
                    {
                        for(int x = 0; x < Width; x++)
                        {
                            //B
                            int srcB = p\[0\];
                            p++;
                            //G
                            int srcG = p\[0\];
                            p++;
                            //R
                            int srcR = p\[0\];
                            p++;
                        }
                    }
                }
    
    L G 2 Replies Last reply
    0
    • C CopperCircle

      Hi, I am very new to C# but I am modifying an image buffer directly within memory, I am scanning along moving the pointer to get the RGB of each pixel, but I need away to jump around the image memory and read the pixels at random rather than just p++ or p--? Thanks...

        unsafe 
                  { 
                      byte\* p = (byte\*)(void\*)pBuffer;
                      for(int y = 0; y < Height; y++)
                      {
                          for(int x = 0; x < Width; x++)
                          {
                              //B
                              int srcB = p\[0\];
                              p++;
                              //G
                              int srcG = p\[0\];
                              p++;
                              //R
                              int srcR = p\[0\];
                              p++;
                          }
                      }
                  }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, inside an unsafe block you can use pointers and jump around as you wish, provided you tell the compiler to let you do that. What is the specific problem? :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      C 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, inside an unsafe block you can use pointers and jump around as you wish, provided you tell the compiler to let you do that. What is the specific problem? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        C Offline
        C Offline
        CopperCircle
        wrote on last edited by
        #3

        Hi, the problem is I have only used Blitz Basic before and I could jump to a pixel, eg GetColor(129,280)"Width,Height", with pointers I have to scan along the image for the data, I dont know how to jump to the pixel data I want?

        L 1 Reply Last reply
        0
        • C CopperCircle

          Hi, I am very new to C# but I am modifying an image buffer directly within memory, I am scanning along moving the pointer to get the RGB of each pixel, but I need away to jump around the image memory and read the pixels at random rather than just p++ or p--? Thanks...

            unsafe 
                      { 
                          byte\* p = (byte\*)(void\*)pBuffer;
                          for(int y = 0; y < Height; y++)
                          {
                              for(int x = 0; x < Width; x++)
                              {
                                  //B
                                  int srcB = p\[0\];
                                  p++;
                                  //G
                                  int srcG = p\[0\];
                                  p++;
                                  //R
                                  int srcR = p\[0\];
                                  p++;
                              }
                          }
                      }
          
          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          For a 24 bit bitmap, the location of a pixel can be calculated with: scan0 + y * scan + x * 3 (Note that for a bitmap that is stored upside down, which is the common way, scan0 points to the last line, and scan is a negative value.) Your code for looping the image is incomplete. The lines might not be stored end-to-end in memory, you have to skip any extra bytes between the lines (calculated by scan - x * 3). Also, if the bitmap is stored upside down, you will either get the data upside down, or only getting garbage after the first line read.

          Despite everything, the person most likely to be fooling you next is yourself.

          1 Reply Last reply
          0
          • C CopperCircle

            Hi, the problem is I have only used Blitz Basic before and I could jump to a pixel, eg GetColor(129,280)"Width,Height", with pointers I have to scan along the image for the data, I dont know how to jump to the pixel data I want?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, you can perform calculations with pointer values: every time you add one you move to the next element of that type (same as in C or C++). Guffa's answer below gives you a detailed example for bitmapped images. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Fixturized forever. :confused:


            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