Your objective in this homework is to learn how to organize and process data using arrays. You will do this in the context of writing a program that generates plaid patterns.
Arrays have two features that are indispensible when building software: they help us manage large sequences of data by collecting the data into a single container, and they let us reach into the data with integer indices—which means we can write all sorts of algorithms that look up or access data indirectly.
This assignment is more involved and less easy to test in small chunks than your previous assignments. Plan accordingly.
Before we dig into the specification, let’s talk about plaid and the image formats you will be using.
Plaid patterns are generated by weaving together yarns of different colors in algorithmic patterns. One can use a rectangular frame or loom to generate these patterns. First, yarn is stretched from the top of the loom to the bottom in a series of columns or warps. Then horizontal strands of yarn are weaved through the warps. Because these strands are the active agents of the weaving, they are called wefts.
The weaver has choices about how to string the horizontal wefts through the vertical warps. One could go over one warp, under the next, over the next, under the next, and so on. This pattern is called a 1/1 twill. The weaver goes over two warps, under one warp, over two warps, under one warp, and so on, to produce a 2/1 twill pattern. A 3/2 twill pattern goes over 3 warps, under 2, over 3, under 2, and so on.
The yarns themselves are dyed in repeating patterns. For example, a yarn might be dyed cornflower blue for 2 inches and pumpkin for 1 inch, with this pattern repeated along the whole length of the yarn.
The combination of the geometric alternation of the twill and the alternation of colors of the warps and wefts produces the comforting plaid patterns that we find in our flannel shirts, tablecloths, and other fabrics.
In this homework, we describe plaid patterns in a plain text file that has a form like the following:
hmirror true vmirror false twill 3 1 palette salmon 240 142 125 marzipan 240 200 125 endpalette yarn salmon 2 marzipan 4 endyarn
There are five possible commands:
yarn. They may appear in any order. There may be multiple
yarn sections—but if you follow the pseudocode suggested later, you don’t need to do anything special to support this. We will consider how these commands are interpreted in the following sections.
In the example plaid specification above, we see that the yarn alternates between 2 units of salmon and 4 units of marzipan. The RGB values of these colors are defined in the
palette section. The base yarn pattern looks like this:
We use this base pattern to form the colorings of the warps and wefts.
vmirror property is false, so we use the base yarn pattern directly to form the vertical warp pattern:
When strung on the loom, the warp pattern will repeat to span the loom’s height:
If we were to string 12 of these warps on a loom, we’d see the following color pattern:
Of course, this is not the final fabric. We still need to string the weft through.
The weft is different because
hmirror is true. Instead of repeating the base yarn pattern directly, we mirror it first. The base is 2 salmon and 4 marzipan, and therefore the mirrored pattern is 2 salmon, 4 marzipan, 4 marzipan, and 2 salmon:
When repeated along the width of the loom, we see this for the wefts:
If we laid out a number of wefts as we did with the warps, we might suppose we’d see a pattern like this:
To add some visual variety, however, weavers shift the pattern one step after each row, like so:
We now know what a fabric of all warps would look like, and we know what a fabric of all wefts would look like. But a weave is a selective combination of the warps and the wefts. The twill pattern tells us which of the two will be visible. If the twill is 2/1, we see weft-weft-warp, weft-weft-warp, and so on. If the twill is 3/1, we see weft-weft-weft-warp, weft-weft-weft-warp, and so on.
Suppose we weave the plaid patterns in an image instead of a loom, stringing the warps along its columns and the wefts along its rows. At each pixel, we must decide whether we use the color from the warp or the weft. Considering all the information above regarding the twill, warps, wefts, and shifting, we formulate this algorithm:
period = over count + under count for each row r twill index = 0 for row 0, 1 for row 1, etc. for each column c if twill index says we're in weft zone choose color from weft according to c else choose color from warp according to r set pixel to chosen color advance twill index by 1
Though not shown in this pseudocode, the twill index must always wrap back around to 0 when it reaches the period or beyond.
To visualize the color data that we generate by our weaving algorithm, we have to write it out in a format that image editors can read. JPEG and PNG are popular formats, but they use mathematics beyond the scope of this course to shrink down the file size. We will instead use a very readable format called Portable Pixmap (PPM).
Humans can open a PPM file in a text editor and understand it with some labor. Consider this PPM, for example:
P3 2 4 255 255 0 0 0 255 0 0 0 255 255 255 0 255 0 255 0 255 255 255 255 255 0 0 0
The first line is
P3. Many file formats specify a magic number, a special sequence of bytes that tells applications what kind of file it is.
P3 is the magic number for the PPM format. The second line (
2 4) is the image’s width and height. The third line (
255) is the maximum intensity found in any color. A color whose red, green, and blue components are this number will display as white. For our purposes, assume this number is always 255. The next lines are the individual pixel triplets, row by row. The first pixel in this example is red, and the second is green. Then we have blue and yellow. Then magenta and cyan. And finally white and black. The first pixel in the file is shown at the top-left of the image.
Paste the text into a file and view it with an image editor like The GIMP. Not all editors can read PPM. You should see an image that looks like this:
Tweak color values in the text file and see what happens. Make sure you understand the file structure before moving on.
Complete the classes described below. Place all classes in package
hw5. Make all methods
Main with a
main method, which you are encouraged to use to test your code. Nothing in particular is required of it, but it must exist.
ImageUtilities with the following methods:
writePPMthat exports a 2D
Colorarray to an image file in the Portable Pixmap (PPM) format. It accepts two parameters in the following order:
Fileto which to write
Colorarray of pixels
pixelsToImagethat converts a 2D
Colorarray to a
BufferedImage. It accepts a 2D
Colorarray of pixels as its sole parameter. Assume the array is organized in row major fashion, has at least one row of pixels, and has rows that are of identical length. It returns a
TYPE_INT_RGB. This method is useful for testing. You could, for example, call this method in
Main, and use
ImageIO.writeto save the image in a more standard format than PPM.
PlaidUtilities with the following methods:
extendSequencethat appends a color to a list a certain number of times. It accepts three parameters in the following order:
extendSequence(list, Color.BLUE, 2)changes the list so that it has elements [
mirrorSequencethat appends a reversed version of a list to itself. It accepts as its sole parameter a list of colors of type
mirrorSequence(list)changes the list so that it has elements [
parsePalettethat parses the palette section of a plaid specification. It accepts three parameters in the following order:
Scannerthat’s poised to read the first color entry
iis the name given to color
i. This method reads colors until it encounters
endpalette. Names are appended to the names list, and colors are appended to the colors list—maintaining their association. For example, suppose the text about to be read is this:
cornflower 100 149 237 teal 0 128 128 endpaletteSuppose that
namescurrently holds [
Color.BLACK]. After a call to
parsePalette(in, names, colors),
nameswill hold [
colorswill hold [
new Color(100, 149, 237),
new Color(0, 128, 128)].
Spreading data that is linked together, like colors and their names, across multiple arrays is not the best design. (These linked arrays are sometimes called parallel arrays.) We will see a better way of keeping linked data together when we start creating our own objects.
parseYarnthat parses the yarn section of a plaid specification. It accepts three parameters in the following order:
Scannerthat’s poised to read the first color entry
iis associated with length
i. This method reads yarn colors until it encounters
endyarn. Names are appended to the names list, and lengths are appended to the lengths list—maintaining their association. For example, suppose the text about to be read is this:
cornflower 5 teal 3 endpaletteSuppose that
namescurrently holds [
lengthsholds . After a call to
parseYarn(in, names, lengths),
nameswill hold [
lengthswill hold [2, 5, 3].
colorByNamethat looks up a named color from a directory. It accepts three parameters in the following order:
iis the readable name used to identify color
i. This method determines the
Colorassociated with the given name by scanning the names list and returning the associated color. If the name cannot be found in the directory, it returns
generateSequencethat expands a list of colors names and their run-lengths into a single flat list of colors. It accepts five parameters in the following order:
booleanthat is true if the sequence should be mirrored
Colorthat is produced by traversing the yarn colors, appending each named color to the returned list. The number of times each color is appended is determined by the color’s run-length. For example, suppose we’ve got the following variables:
paletteNameswith elements [
paletteColorswith elements [
yarnNameswith elements [
yarnRunLengthswith elements [2, 1]
generateSequence(paletteNames, paletteColors, yarnNames, yarnRunLengths, false)→ [
Color.LIGHT_GRAY]. If the sequence is mirrored, then we have [
weavethat produces a plaid pattern of a certain resolution. It accepts six parameters in the following order:
Color, which is filled according to weaving algorithm described above.
generatePlaidthat reads in a plaid specification and returns the plaid image as a 2D
Colorarray. It accepts three parameters in the following order:
Filecontaining the plaid specification
set up needed variables # do parsing while there's a command to be read read command if command is this respond to this else if command is that respond to that ... use variables to generate arrayThis method consumes a number of lines of code because of the loop, but most of its meat is accomplished by calling methods defined above.
For an extra credit participation point, identify some plaid in your life and recreate it using your program. Share a photo of the original plaid, your plaid specification, and a resulting PNG image on Piazza under folder
ec5 by the due date. The submission garnering the most votes will be honored in some way.
To check your work and submit it for grading:
hw5 SpecCheckerfrom the run configurations dropdown in IntelliJ IDEA and clicking the run button.
A passing SpecChecker does not guarantee you credit. Your grade is conditioned on a few things: