Thread: Array Program Help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    26

    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
    Last edited by wvu2005; 09-20-2005 at 09:22 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > However, I know that the image in memory acts like a 3D array
    Width and height is 2D - what's the 3rd?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    26
    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

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, the color would be the array element value.
    Code:
    pixel[width][height] = someColor;

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe width, height and depth?
    Code:
    // This is a 3d array.
    pixel[width][height][depth] = someColor;

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Depth is just the number of bits used to define a color. 32 bit vs 24 bit vs 16 bit etc.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    26
    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

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    26
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM