Help in creating a ppm image in C

Page 1 of 1

3 Replies - 1291 Views - Last Post: 11 October 2010 - 12:03 PM Rate Topic: -----

#1 Heathie89  Icon User is offline

  • New D.I.C Head

Reputation: -7
  • View blog
  • Posts: 1
  • Joined: 09-October 10

Help in creating a ppm image in C

Posted 10 October 2010 - 04:25 PM

s

This post has been edited by Heathie89: 11 October 2010 - 09:57 AM

Is This A Good Question/Topic? -7
  • +

Replies To: Help in creating a ppm image in C

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2131
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: Help in creating a ppm image in C

Posted 11 October 2010 - 01:41 AM

I'm not surprised you're confused.

> 2. print_Row ( int yValue, // to specify which row to print , int width, int height)
> - makes a row of pixels from column start to column finish.
The prototype has "width" and "height"
The description talks about start/end columns.

Ask your tutor for clarification.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7007
  • View blog
  • Posts: 14,660
  • Joined: 16-October 07

Re: Help in creating a ppm image in C

Posted 11 October 2010 - 05:22 AM

Actually, your prototypes are pretty miserable.

Let's first go look at spec: http://en.wikipedia....i/Netpbm_format

Hmm... you probably want P3. More importantly, sending output to stdout may not be that helpful. Let's change that up a little:
// 1. make_header(int w, int h)
// yes, I stole this from WikiPedia
void make_header(FILE *fh, int width, int height) {
	fprintf(fh, "P3\n");
	fprintf(fh, "# The P3 means colors are in ASCII, then %d columns and %d rows,\n", width, height);
	fprintf(fh, "# then 255 for max color, then RGB triplets\n");
	fprintf(fh, "%d %d\n255\n", width, height);
}



Again, referencing the spec, this one isn't bad:
// 3. make_pixel(unsigned char r, unsigned char g, unsigned char B)/>
void make_pixel(FILE *fh, unsigned char r, unsigned char g, unsigned char b ) {
	fprintf(fh, "%3d %3d %3d   ", r, g, b );
}



It's that print_row that's confusing. It pretty much takes things out of context. Let's write a simple test instead:
void make_red_box(FILE *fh, int width, int height) {
	int x, y;
	make_header(fh, width, height);
	for(y=0; y<height; y++) {
		for(x=0; x<width; x++) {
			make_pixel(fh,255,0,0);
		}
		fprintf(fh, "\n"); // end the row
	}
}



What would you have to do for your assignment? You have four colors; you need to figure where they land. You'll probably have at least a loop like the one above. It would be easier to have an array with all the pixel values and print that out. Perhaps print_Row assumes you do? Globals are evil, btw.

A couple of structs could do you a world of good here:
typedef struct {
	unsigned char r, g, b ;
} Pixel;

typedef struct {
	int width, int height;
	Pixel *data; // or Pixel **data
} Image;



Maybe some functions could be:
void writeImage(FILE *, Image *);
void setPixel(Image *, int x, int y, Pixel);
Pixel getPixel(Image *, int x, int y);
Pixel makePixel(unsigned char r, unsigned char g, unsigned char b );



Hope this helps.
Was This Post Helpful? 3
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: Help in creating a ppm image in C

Posted 11 October 2010 - 12:03 PM

Topic closed. Do NOT remove the contents of your posts, as others may benefit from them in the future.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1