Asg 2: Greyscale image copy

Objective

Implement a basic greyscale image pgm_t class that you can use to copy (read in/read out) a PGM image. The class should include the "big three", namely the copy constructor, destructor, and (copy) assignment operator.

Assignment

  1. Create one module, pgm.cpp with its own header.
  2. Handle file (image) i/o by providing overloaded friend stream operators:
    1. friend std::istream& operator>>(std::istream& s, pgm_t& rhs);
    2. friend std::ostream& operator<<(std::ostream& s, pgm_t& rhs);
    Include overloaded operators for pointers to the classes as well.
  3. Private data members should at least include:
  4. The main.cpp code driver tests the modules by:
    1. calling pgm_t org("mandrill.pgm"); constructor
    2. calling pgm_t cpy(); constructor
    3. calling cpy = org; assignment operator

Further Suggestions

  1. The bulk of the code for this project sits in the I/O functions for reading and writing PGM 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.
  3. Use the assignment operator to make a copy instance of the PGM image read in, output both images.
  4. Here is a working main.cpp driver:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #include "pgm.h"
    
    int main()
    {
           pgm_t            org("mandrill.pgm");
           pgm_t            cpy("gray.pgm");
    
           std::ofstream    ofs;
    
      ofs.open("copy.pgm",std::ios::out|std::ios::binary);
      ofs << org;
      ofs.close();
    
      cpy = org;
    
      ofs.open(cpy.fname().c_str(),std::ios::out|std::ios::binary);
      ofs << cpy;
      ofs.close();
    
      return 0;
    }
    	

Example Program Input


(click to download mandrill.pgm)

Supplemental

  1. If you call your source code file main.cpp here is a Makefile that you can use to try to compile the project:
    CC = g++
    
    INCLUDE = -I.
    
    CFLAGS = -g
    
    LDFLAGS = -L. -L/usr/lib
    
    LDLIBS = -lc -lm
    
    .cpp.o:
    	$(CC) $(INCLUDE) $(CFLAGS) -c -o $@ $<
    
    all: main
    
    main: pgm.o main.o main.cpp
    	$(CC) -o $@ $@.o pgm.o $(LDFLAGS) $(LDLIBS)
    
    main.o: main.cpp
    pgm.o: pgm.cpp
    
    clean:
    	rm -f *.o
    	rm -rf main
    	
    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