CS 70

Homework 1: Coding Part: Meme Generator

In this part, you will improve upon a simple meme-generating program written in C++.

  • Duck speaking

    Hey, there are a lot of helpful hints at the bottom!

  • LHS Cow speaking

    Yes. They cover common issues students have when doing this assignment—be sure to check them out!

Starter Code: Simple Memes

The starter code, which you’ll find in the file meme.cpp, contains a program that will

  • Ask the user for the name of an image file.
  • Open that image using the computer-vision library opencv.
  • Add the text “CS70: COWS ALL THE WAY DOWN” to the image.
  • Save the edited image to a file named given-out.jpg.
  • Print out the input file name, the meme text, and the output file name.

Compiling and Running the Code

Verify that the provided code works as advertised:

  • Open the VS Code terminal in the memes directory. You can do this by right-clicking the memes directory in the file explorer and clicking “Open in Integrated Terminal”. Or, if you already have a terminal window open to Homework-01, you can type cd memes to navigate to the memes directory.

  • Compile the starter code. Remember, you must always compile, link, and run your code inside of the ssh server! Use the command

    clang++ -c -std=c++17 -gdwarf-4 -Wall -Wextra -pedantic -isystem /usr/include/opencv4 meme.cpp

    The meanings of the flags are

    • -c : Compile the code, to make a .o file.
    • -std=c++17 : Use a recent (2017) version of the C++ standard.
    • -gdwarf-4 : Provide debugging information (in a format called “DWARF”, version 4) that debugging tools can use to help you find bugs in your code.
    • -Wall -Wextra -pedantic : Warn us about things that C++ technically allows but that are more likely to be coding errors.
    • -isystem /usr/include/opencv4 : Also look for system #include files in the directory /usr/include/opencv4. (This obscure option is specific to this assignment because we're using the opencv library and Ubuntu 22.04 puts the necessary files in an odd place.)
  • Link the resulting machine code (in meme.o) with the standard library and the necessary parts of opencv to make an executable. Use the command

    clang++ -o meme meme.o -lopencv_imgcodecs -lopencv_core -lopencv_imgproc

    The meanings of the flags here are

    • -o meme : Name the output file (our executable) meme.
    • -lopencv_imgcodecs -lopencv_core -lopencv_imgproc : Also link with these libraries in addition to the standard C and C++ libraries.
  • Run the starter code (by typing ./meme) and enter cow.jpg as the input (source) image filename. If everything runs correctly then you should see a file named given-out.jpg.

    (If you have prior experience or have answered the Written Questions already, you may be aware that for a trivial one-file program like meme.cpp, it’s possible to compile and link with a single command. When working on this coding part, please practice using the separate compiling and linking commands—familiarity with using separate commands will pay off in future assignments!)

Task 1: More User Input

You will now change the program so it can add any text we specify to the meme image and use any output file name. Change the main() function so that it asks the user to provide the text to add to the image and the outputFilename for saving the edited image.

The autograder is expecting a certain input/output format, in which user I/O happens on a single line for each prompt to the user. An example of a successful user interaction with your program should look something like

Enter image file name: student-in.jpg
Enter meme text: this is some text
Enter output file name: student-out.jpg
Opening file: student-in.jpg
Adding text: tHiS Is sOmE TeXt
Writing file: student-out.jpg

Task 2: Improved Spongebob-Mocking Capitalization

The text you add to your meme is processed by a function called spongebobMocking(). Right now, the spongebobMocking() function capitalizes all of the letters in a string, using the toupper() function. Change the code to generate true Spongebob Mocking text by implementing the following:

  • Every other character should be capitalized with toupper().
  • The other characters should be lowercased with tolower().
  • The first character should always be lowercase.
  • Don’t worry about nonalphabetic characters—calls to toupper() and tolower() won’t change them at all.
  • Example: The string h3llo world! would end up as h3lLo wOrLd!

Task 3: Modifying Text Properties

Now it’s time to customize the look of your meme! Find your own JPEG image that will serve as the base of your meme. Name this file student-in.jpg and add it to your repository.

The makeMeme() function has some variables that affect how text is displayed on the image:

int font = FONT_HERSHEY_PLAIN;
float textScaling = 3.0;
Scalar textColor{255, 255, 255};
Point textPosition{100, 100};

The font variable is an int representing which one of the fonts that opencv understands. Somewhere else in the opencv library, each possible font name is associated with a unique integer value. As users of the opencv library, we shouldn’t need to know which integer is associated with each name, but we do need to know what the possible names are. In our version of opencv, they are

FONT_HERSHEY_SIMPLEX             FONT_HERSHEY_PLAIN
FONT_HERSHEY_DUPLEX              FONT_HERSHEY_COMPLEX
FONT_HERSHEY_TRIPLEX             FONT_HERSHEY_COMPLEX_SMALL
FONT_HERSHEY_SCRIPT_SIMPLEX      FONT_HERSHEY_SCRIPT_COMPLEX

The textScaling variable is a float representing how big (relative to opencv’s default) the font should be. A scaling value of 3.0 will make the text three times as big as the default.

The textColor variable stores the color to draw the text, in blue–green–red (BGR) format. The first number represents the amount of blue, from 0 to 255. The second number gives the amount of green, and the third number gives the amount of red. The color settings shown above will create white text.

The textPosition variable stores the location to start drawing the text. The first number indicates how many pixels in from the left edge to start, and the second number indicates how many pixels down from the top edge.

Modify these variables to make your own meme with whatever text style you want. Name your output file student-out.jpg and add it to your repository.

Important: if the name of your output file is not exactly student-out.jpg, the autograder will complain. Files with similar but different names (e.g., student-out.jpeg, student-out.JPG, sTuDeNt-oUt.JpG, student-out.JPEG, sTuDeNt-oUt.JpEg) will be rejected.

Helpful Hints

Choosing Images

If you want to share your meme outside of CS70, you should either use an image that you already own the rights to, or use find an image with its “usage rights” set to “Allow for noncommercial use with modification” (using Google Image or a similar tool).

  • Duck speaking

    Can I pick any image at all beyond that?

  • LHS Cow speaking

    You should also make sure that any image (or text) you submit is school-appropriate.

  • RHS Cow speaking

    Indeed! Remember that our goal is to create an inclusive, welcoming community here in CS 70. Please help us with that by making sure that the memes you submit are appropriate for all of your grutors, instructors, and classmates to enjoy.

Submitting inappropriate or offensive material is not acceptable in any CS 70 assignment, and may impact your assignment grade.

Don't Forget to Recompile

Remember that every time you change meme.cpp, you need to recompile and relink it before you can run the program to test the results. (If you “fix” a bug in the code and the program doesn’t behave differently, you probably forgot one or both of these steps.)

Be Attentive to Warnings About Your Code

We recommend that you fix compiler warnings immediately, rather than waiting until just before you submit your code. Even if many warnings are about issues that don’t cause trouble in practice, warnings often mean there are serious errors in your code!

Keep an Eye on Your Code's Syntax

The cpplint program checks your code to ensure that it adheres to Google's C++ style guide, which we're using in CS 70 so your code is readable and consistently formatted from submission to submission.

You'll find that cpplint is pretty picky about how you write your code, so you should run it periodically and correct any formatting errors it calls out. Keeping on top of formatting issues will help you internalize the expected code style, and will also save you from the hassle of trying to fix dozens (or for larger programs, hundreds) of small annoying errors.

Run cpplint from the command line as

cpplint myfile.cpp

(where myfile.cpp is, obviously, replaced with the name of the file you want to style-check).

Write Comments as You Go

Writing comments as your write your code has two major benefits. First, the comments you write will help you be sure that your code is doing what you think it should be doing. Second, comments help anyone else who needs to read your code (like grutors or professors) understand what you think your code should be doing. Comments are especially useful when what you're trying to do is complicated and requires some abstract thinking to understand.

Just like dealing with compiler warnings and code style, writing comments while you're working will be much easier than trying to write them afterwards, when you might not remember exactly why you did something in a particular way.

To Complete This Part of the Assignment…

(When logged in, completion status appears here.)