-
Array Program Help
Hi everyone. This is my first post.
I'm trying to malloc space for a color image in memory, fill it with a design like a colored rectangle, and then write it out into a PPM format file (which will be done in Unix).
As a note, I've had very little experience with 3D arrays. However, I know that the image in memory acts like a 3D array except that the pointers only let you access it one dimension. Any help would be greatly appreciated. I thought this would be a neat thing to try to do (I wanted to ultimately try to make a checkerboard in the long term, but I'm having trouble making a simple colored rectangle, haha).
Below is what I've written so far, I'm kind of stumped on where to go from there. I've commented a section where I believe that the code would go to generate the image.
Code:
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[]) {
unsigned char *image, *tempptr;
int x, y, c, width, height;
if(argc < 3) {
fprintf(stderr, "Usage: %s width height\n", argv[0]);
exit(1);
}
width = atoi(argv[1]);
height = atoi(argv[2]);
// ***** I believe this is where I should add the code to
generate the image and write it
out as a PPM file
return(0);
}
Thank you for your help,
James
-
> However, I know that the image in memory acts like a 3D array
Width and height is 2D - what's the 3rd?
-
Color would be the 3rd dimension of the array (e.g. a rectangle consisting of values for length and width would be two dimensions and the coloring of it would be the 3rd).
Thanks,
James
-
No, the color would be the array element value.
Code:
pixel[width][height] = someColor;
-
maybe width, height and depth?
Code:
// This is a 3d array.
pixel[width][height][depth] = someColor;
-
Depth is just the number of bits used to define a color. 32 bit vs 24 bit vs 16 bit etc.
-
Hi everyone,
Thank you all for following up with my question. I still am in need of help, but I did not know that depth would be used to define the color, that's very interesting (I stand corrected bithub ;) ).
Thanks again,
James
-
Hi,
I've made some progress. I can generate the color, but still can't generate the rectangle. Below is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
//drawPixel function
//added to simplify drawing loops below
void drawPixel(unsigned char *picPointer, int picWidth, int x, int y, unsigned char
rVal, unsigned char gVal, unsigned char bVal)
{
picPointer[3*y*picWidth + 3*x] = rVal; //+0 to access red byte
picPointer[3*y*picWidth + 3*x + 1] = gVal; //+1 to access green byte
picPointer[3*y*picWidth + 3*x + 2] = bVal; //+2 to access blue byte
return;
}
int main (int argc, char *argv[])
{
if (argc < 3)
{
fprintf(stderr, "Usage: %s width height\n", argv[0]);
exit(1);
}
const int wid = atoi(argv[1]);
const int ht = atoi(argv[2]);
unsigned char *picture = (unsigned char *) malloc(3*wid*ht*sizeof(unsigned char));
int x,y;
for(y=0;y<ht;y++)
{
for(x=0;x<wid;x++)
{
drawPixel(picture,wid,x,y,60,100,200); //fills whole image with a light blue
}
As always, any help is greatly appreciated.
Thanks.