Assignment 8 — A picture is bytes
One image library and three programs on top of it, passing pictures to each other through a pipe: a drawing written out as a binary picture, a picture read back in and turned gray, and one cut to half its size.
Due Oct 28, 2026
In Class 17, a picture was one array of bytes, three per pixel, and set_pixel found a pixel by the formula 3 * (y * w + x). This assignment makes that code a library, image.c, and writes three programs on top of it that pass pictures to each other the way the shell has passed text all term:
./draw | ./gray | ./half > out.ppmThe reading for the week is [BGC] 9.1 — The FILE* Data Type and [BGC] 9.6 — Binary File I/O; Tuesday’s class uses them for steps 2 and 3, and Quiz 7 is on them.
You will hand in seven files: image.h, image.c, Makefile, draw.c, gray.c, half.c and notes.txt — and mandel.c, if you do the optional step 6.
Every program builds with make and no warnings, with -fsanitize=address in the Makefile, and runs with nothing reported. Every function you write gets the design recipe’s one-line comment above it, and a function that hands back new memory says so: the caller frees it. Every program frees every image it asked for. Still no [ or ] in any function but main, and no *(p + i) with a counter standing in for them: a walk moves its pointer, p++, or p += 3 a pixel at a time, or put_pixel. A pixel reached by its column and row, rather than walked to, goes through set_pixel’s formula.
image.h,image.cand aMakefile— the library. Movenew_image,set_pixel,put_pixelandwrite_p3out ofdraw.cintoimage.c, with their prototypes and comments inimage.h, so that every program here can use them.draw.ckeeps its drawings and itsmain, and includes"image.h". Makemain’s picture 321 by 201 — odd on purpose, for step 4. TheMakefile, in the shape of Class 9’s, builds each program from its own.oandimage.o; copy it as it is, tabs included:CFLAGS = -Wall -g -fsanitize=address PROGRAMS = draw gray half all: $(PROGRAMS) draw: draw.o image.o gcc $(CFLAGS) -o draw draw.o image.o gray: gray.o image.o gcc $(CFLAGS) -o gray gray.o image.o half: half.o image.o gcc $(CFLAGS) -o half half.o image.o %.o: %.c image.h gcc $(CFLAGS) -c $< clean: rm -f $(PROGRAMS) *.oUntil steps 3 and 4 exist,
make drawbuilds the one program there is. Done whenmake drawbuilds it with no warnings,./draw > s.ppmopens in Loupe as it did in class, andmake drawagain says there is nothing to do.write_ppm— the binary form, P6. A P3 file writes each byte as up to three digits and a space. The binary form, P6, writes the bytes as they are. Replacewrite_p3withvoid write_ppm(unsigned char * px, int w, int h): the header is the same three lines withP6in place ofP3, printed withprintf, and then one call writes every byte at once:[BGC] 9.6 — Binary File I/O is where
fwritecomes from:3 * w * hitems of 1 byte each, frompx, to the output.stdoutis theFILE *thatprintfhas been writing to all along, from 9.1. Done when./draw > s.ppmopens in Loupe as before, andls -lshowss.ppmabout a third of the size it was.read_ppmandgray.c— a picture in, and turned gray. Writeunsigned char * read_ppm(int * w, int * h)inimage.c, which reads a P6 picture from the input into a new image and hands it back; the width and the height come out throughwandh. Its comment ends the caller frees it. The header is onescanf:scanfreturns how many numbers it read, and 3 means the header was there;wandhare already addresses. Exactly one whitespace byte separates the255from the first pixel, andgetchar()takes it. Thenfread(px, 1, 3 * *w * *h, stdin)reads every byte at once, from 9.6.Then
gray.c:void gray(unsigned char * px, int w, int h)makes every pixel gray by giving its three bytes their average, andmainreads a picture, grays it, writes it and frees it. Done when./draw | ./gray > g.ppmopens in Loupe as three gray stripes — and the red stripe and the blue one come out the same gray, which step 5 asks about.half.c— a smaller copy. Writeunsigned char * half(unsigned char * px, int w, int h), which hands back a new imagew / 2wide andh / 2high, keeping every other pixel: columnx, rowyof the new picture is column2 * x, row2 * yof the old. Its comment ends the caller frees it.mainreads a picture, halves it, writes the small one, and frees both:half’s result is new memory, and the picture read in is still there.Done when
./draw | ./gray | ./half > out.ppmopens in Loupe as a 160 by 100 gray picture, and the sanitizer reports nothing — no leak included.notes.txt— three short answers.- In a picture 10 pixels wide, which byte, counting from 0, holds the green of the pixel in column 2, row 3? Show the arithmetic.
- In step 3, the red stripe and the blue stripe came out the same gray. Why, with the numbers?
s.ppmfrom step 2 is about a third of the size the P3 file was, for the same picture. Where did the other two thirds go?
mandel.c— optional: the Mandelbrot set. This step is optional and not needed for full marks. A pointcof the plane is in the set when the sequencez = z * z + c, starting fromz = 0, never gets further than 2 from the origin. The picture is one pixel per point: black for the points in the set, and a gray for the others that says how fast they escaped. The design, in order:- A point is two
doubles,crandci, and so isz:zrandzi. One step of the sequence iszr' = zr * zr - zi * zi + crandzi' = 2 * zr * zi + ci; compute the newziwith the oldzr, so keep the newzrin a variable of its own first. zis further than 2 from the origin whenzr * zr + zi * zi > 4: no square root needed.- Write
int steps(double cr, double ci), which counts the steps until that happens, and returnsMAX, a#defineof100, if it has not happened by then. Two asserts inmain, worked out below:steps(1.0, 0.0) == 3andsteps(-0.5, 0.0) == MAX. - Write
void mandelbrot(unsigned char * px, int w, int h). Columnxstands forcr = -2.0 + 3.0 * x / wand rowyforci = -1.2 + 2.4 * y / h— the3.0 *first, so that the division is ondoubles. A pixel gets255 * n / MAXin all three bytes, wherenis itssteps, and 0 whennisMAX. - Two lines in the
Makefile, in the shape ofdraw’s two, andmandeladded toPROGRAMS.maindraws 640 by 512.
The worked example for the asserts: for
c = 1 + 0i,zgoes0, then1, then2, then5, and5 * 5is past 4, sostepsis 3. Forc = -0.5 + 0i,zgoes0,-0.5,-0.25,-0.4375and settles there, never escaping:MAX.Done when
./mandel > m.ppmopens in Loupe as the set, black, with gray around it, and the two asserts pass. Notice, for later in the term, that the rows through the middle of the picture take longer to compute than the rows at the top and the bottom.- A point is two