How to navigate memory using pointers?
-
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++; } } }
-
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++; } } }
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:
-
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:
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?
-
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++; } } }
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 byscan - 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.
-
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?
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: