Asg 1: Greyscale and color image copies

Objective

Implement basic greyscale and color image structs that you can use to copy (read in/read out) PGM and PPM images. The structs should each be in their own pgm.h and ppm.h header files. Each pgm.h and ppm.h header file should also include function prototypes to allocate, free, read, and write each of the PGM and PPM images.

Assignment

  1. Create two modules, pgm.c and ppm.c, each with its own header.
  2. Handle memory allocation and deallocation by providing these functions:
    1. PGM *pgm_alloc(int r, int c);
    2. void pgm_free(PGM **img);
    3. PPM *ppm_alloc(int r, int c);
    4. void ppm_free(PPM **img);
  3. Handle file (image) i/o by providing read and write functions:
    1. PGM *pgm_read(const char *file);
    2. void pgm_write(PGM *img, const char *file);
    3. PPM *ppm_read(const char *file);
    4. void ppm_write(PPM *img, const char *file);
  4. Image data members should at least include: Image data should be stored as one 1D array: or as three 1D arrays:
  5. Write a driver program pgmtest.c to test the PGM module:
    1. calling img = pgm_read("mandrill.pgm");
    2. calling pgm_write(img,"copy.pgm");
    3. calling pgm_free(&img);
    and similarly for the PPM module:
    1. calling img = ppm_read("mandrill.ppm");
    2. calling ppm_write(img,"copy.ppm");
    3. calling ppm_free(&img);

Further Suggestions

  1. The bulk of the code for this project sits in the I/O functions for reading and writing PGM and PPM image files.
  2. You should develop this project incrementally where you first write code for the PGM image, test it by reading and writing the image you just read in—if you can write out an image just read in without distorting it in any way, your I/O routines are working.

Example Program Input

mandril.pgm
mandril.ppm


(click to save mandrill.pgm)

Supplemental

  1. If you call your source code files pgmtest.c and ppmtest.c here is a Makefile that you can use to try to compile the project:
    CC = gcc
    
    INCLUDE = -I.
    
    CFLAGS = -g
    
    LDFLAGS = -L. -L/usr/lib
    
    LDLIBS = -lc -lm
    
    .c.o:
    	$(CC) $(INCLUDE) $(CFLAGS) -c -o $@ $<
    
    all: pgmtest ppmtest
    
    pgmtest: pgm.o pgmtest.o
    	$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
    
    ppmtest: ppm.o ppmtest.o
    	$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
    
    pgmtest.o: pgmtest.c
    ppmtest.o: ppmtest.c
    pgm.o: pgm.c pgm.h
    ppm.o: ppm.c ppm.h
    
    clean:
    	rm -f *.o
    	rm -rf pgmtest ppmtest
    	
    Note that each line underneath the targets is indented by a tab, not just spaces, this is important!

Turn in

Turn in all of your code, in one tar.gz archive of your asg##/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    4. Lessons learned, identified interesting features of your program
    5. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .c source)
  4. object code (do a make clean before tar)

How to hand in

See handin notes

Grading scheme