How do we store images in memory? Well there are two basic ways, one is via a pointer to a 1-D array and there is also the possibility of having a multidimensional array. The possiblities are discussed below.
Pointer to 1D Array, Greyscale ImageProblem: Have an Image of any arbitrary size and want to store
it. You can use a defined 2-D array like int Image[10][10]; But this only works if you have a 10x10 image. Solution: You should use the concept of a pointer and
dynamically allocate memory to it. In this case, you will be representing
a 2D image by a 1D array. First, Lets review the use of Pointers in C/C++.
Where W = Image_Width |
2D Array Greyscale ImageAnother possibility is to create a multi-dimensional array I[r][c] dynamically. See See 3240 discussion of arrays. Also, take a look at the following exercise that has some code for creating such arrays.. it is not complete...it is an exercise. |
Pointer to 1D Array , Color ImageNow consider a color image instead of a greyscale image.
unsigned char *I; I =
malloc(Image_Width*Image_Height*3); /*to access Red value for Pixel (r,c) */ red_at_r_c =
I[r*Image_Width*3 + c*3]; /*to access Green value for Pixel (r,c) */ green_at_r_c =
I[r*Image_Width*3 + c*3 + 1]; /*to access Blue value for Pixel (r,c) */ blue_at_r_c =
I[r*Image_Width*3 + c*3 + 2]; This can be viewed as follows: |
|