MP2
Inside a PNG
Due dates may be found on the schedule.

Your second C program explores the implementation and use of a widely-used technical specification. You will implement a small part of the PNG technical specification and use your implementation to hide a GIF within a PNG file.

340.png natalia.png luther.png
All of these images look the same, but two have a hidden GIF inside of them!

1 Initial Files

In your CS 340 directory, merge the initial starting files with the following commands:

git fetch release
git merge release/mp2 --allow-unrelated-histories  -m "Merging initial files"

2 Part 1: Implementing the PNG File Format

The World Wide Web Consortium (W3C) is an international community where [many] work together to develop Web standards1 Quoted from https://www.w3.org/about/. One of the web standards that the W3C published and maintains is the Portable Network Graphics (PNG) Specification (Second Edition). This specification fully and completely defines how PNG image files work.

To complete Part 1 of this MP, you must complete a small PNG library that reads the PNG signature and chunks found in PNG files:

The design on the PNG library is a file-stream based library where you need only to return the next chunk in the file. Therefore, it is highly recommended you only read the data necessary to complete the function and read the next part of the file the next time a library function is called.

You will need to open a file for reading or writing in C. The following C library calls may be helpful:

2.1 Running Your Program

3 Part 2: Understanding the Provided Code

In this MP, we have provided two programs that are already complete:

  1. A complete program that analyzes a PNG file, png-analyze.c.
  2. A complete program that reads and rewrites the PNG file using the provided PNG library, png-rewrite.c.

The png-analyze.c and png-rewrite.c files use your library from Part 1. If your library is not complete, these programs will not run correctly! Here are several example runs of the programs that you should run to explore what the provided code does for you:

4 Part 3: Extract the Hidden uiuc Chunk

Complete png-extractGIF.c to extract the uiuc chunk from the PNG file specified in the command line (argv[1]) and save the data from the uiuc chunk into the GIF file specified in the command line (argv[2]). You will almost certainly find the provided png-analyze.c as a great starting point for your code.

The hidden GIFs in the files within the sample folder are valid GIF and should be able to be opened by any program that can open images if extracted correctly.

4.1 Testing Your Code

Once you have finished png-extractGIF.c, make sure to test it before moving on to Part 3! To do so:

Finally, you can also run the test suite we have provided:

5 Part 4: Hide Your Own GIF

Complete png-hideGIF.c to hide a GIF file within a uiuc chunk in a provided PNG file. The first command line argument (argv[1]) is the normal PNG file that needs to be modified to hide an GIF image and the second argument (argv[2]) is the GIF to be hidden.

Your png-hideGIF.c file will be very similar to the provided png-rewrite.c program except that you need to add an additional PNGChunk somewhere between IHDR and IEND (it cannot be before the IHDR chunk or after the IEND chunk, but it otherwise can be anywhere in between the two chunks).

5.1 Testing Your Code

Once complete, make sure to test your program:

As a final check, run ./test to ensure you pass all test cases.

5.2 Memory Correctness

For full credit, your MP must run valgrind clean.

docker run --rm -it -v "`pwd`":/mp2 cs340 "make clean"
docker run --rm -it -v "`pwd`":/mp2 cs340 "make test"
docker run --rm -it -v "`pwd`":/mp2 cs340 "valgrind --leak-check=full --show-leak-kinds=all ./test"

6 Modifiable Files

In your solution, you must only modify the following files. Modifications of other files may break things:

7 Submission and Grading

This MP also has an extra credit addition, MP2++, with the same due date as the main MP

7.1 Submission

Once you have locally passed all the tests, you will need to submit and grade your code. First commit using the standard git commands:

git add -A
git commit -m "MP submission"
git push

7.2 Grading

The initial grading is done via a manual GitHub Action. You MUST complete this step before the deadline to earn any points for the MP:

7.3 Points

2/3 of the points are for passing the automated tests; 1/3 are for valgrind finding no errors.