Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When we mark your program, the first thing we will do is run a function called fixCodes(). This function will call functions for all of

When we mark your program, the first thing we will do is run a function called fixCodes(). This function will call functions for all of the other tasks in turn, and will save all of the resulting images. You will find some initial code for fixCodes() listed later in this assignment. Each time you finish writing the function for a task, add suitable lines to fixCodes(). When you hand in your assignment, all of the functions that work will be called by fixCodes(), which will save all of the images that your program produces. Note that the function takes a single argument, the Inft1004 Introduction to Programming Assignment 4 number of pixels per module in the resulting QR code; fixCodes(3) will display quite small QR codes, while fixCodes(25) will display much bigger ones. Your assignment should eventually do tasks 1-10, below, so if you complete all of the tasks, fixCodes() will save ten images. Remember, the cells in these images must be black or white, no matter what colour they are in the vandalised images. Every new image should be expanded and saved using the file naming scheme shown in the initial code: your name and the task number. Task 11 is an extra challenge for those who want one, but is not worth any marks. Task 1: expand images: expand(smallPic, moduleSize) - 5 marks While it might be easier to manipulate minimal images, bigger ones are easier to look at, and generally easier to scan. The function expand(smallPic, moduleSize) will take a picture representing a minimal QR code and will return a picture that is an expanded version of the same code, with moduleSize giving the module size in pixels, and with a quiet zone of twice the module size. So, for example, if smallPic is the minimal QR image shown on the previous page, expand(smallPic, 7) will produce a larger image, like this one, of size 203x203 pixels. When your function is working, call it from fixCodes(), as in the code near the end of this assignment. Task 2: invert minimal images: invert(smallPic) - 5 marks When a QR code has been inverted - all black(ish) cells made white(ish) and all white(ish) cells made black(ish) - a scanner will no longer recognise it as a QR code. This has been done to image2.png. Your function invert(smallPic) will invert it again, restoring its original form. As you can see, fixCodes()then expands it before saving it. Task 3: add positional squares to an image: addSquares(smallPic) - 7 marks One easy way to confuse a QR scanner is to replace the three positioning squares of a code with random cells. This has been done to image3.png, which is in minimal form. Your function addSquares(smallPic) will add the three positioning squares and the white cells separating them from the active cells. Then fixCodes()will expand the resulting image and save it. Task 4: invert just the active cells: invertActive(smallPic) - 7 marks When inverting QR codes, the vandal noticed that they no longer look like QR codes, because the positioning squares, the most obvious features, are destroyed. So she decided to invert just the active cells, leaving the positioning squares and their margins untouched. How would you undo this? It could be quite fiddly to access just the active cells. Maybe there's a way that works by combining functions you've already written. Try it on image4.png. Task 5: merge three images: merge(smallPic1, smallPic2, smallPic3) - 7 marks Another idea that occurred to the vandal was to split a QR code into three images, none of which is a valid code, but which can be combined to produce a valid code. Here's what you need to do to merge image5a.png, image5b.png, and image5c.png (which we'll just call A, B, and C now). For every cell of the combined image, if the corresponding cells of A and B are both the same colour, that's the colour to use in the combined image; but if the corresponding cells of A and B are not the same colour, use the colour of the corresponding cell in C. So, for example, if cell(3, 15) of A is Inft1004 Introduction to Programming Assignment 5 white and cell(3, 15) of B is white, cell(3, 15) of the combined image should be white; but if cell(3, 15) of A is white and cell(3, 15) of B is black, cell(3, 15) of the combined image should be the same colour as cell(3, 15) of C. Remember, don't assume that the 'black' cells are pure black and the 'white' cells are pure white. Task 6: produce minimal images: reduce(qrPic) - 10 marks So far we've only worked with minimised images, where each pixel corresponds to a single black or white cell of the QR code. The images provided from here on are 'normal' sized QR codes, with multiple pixels per cell, so your program will need to take a normal QR code and reduce it to minimal form. Before doing that, your program needs to know how wide the quiet zone is (so as to remove it) and how wide a single module is (so as to reduce each cell to a single pixel). If every image had a top left positioning square, these widths would be quite easy to find. Unfortunately, the vandal has removed the positioning squares in some of the images, so we have to be a little more resourceful. In the picture on the right, three pixels have been made grey so that you can count the rows of pixels in the quiet zone: you should see that there are 6 (represented by three grey and three white pixels). One way to find the size of the quiet zone is to search the outside layer of the image (the outermost line of pixels) for any pixel that isn't the same colour as the one in the top left. If all the pixels in that layer are the same colour, search the next layer, one pixel in from the edge. Keep searching in this way until you first find a pixel that is a clearly different in colour from the top left one. That pixel is not part of the quiet zone, so the quiet zone must be all the pixels outside that one. In the example here (minus the grey pixels), you start to encounter differently coloured pixels in the seventh layer from the edge, so the quiet zone must be six pixels wide. When you've found the size of the quiet zone, a little subtraction and division should give you the cell size. Once you know this, you should be able to work out (after a lot of thought!) how to produce the minimal version of this or any other QR image - so long as all of its cells are exactly the same number of pixels wide and high, which they certainly should be. Remember, when you need to repeat some process, and you don't know how many times it will be repeated, a while loop will be very useful. The function reduce(qrPic) will take a picture representing a QR code and will return a picture representing the corresponding minimal image. While developing this function you can test it on any of the bigger QR code images, but for this task fixCodes2() should run it on image6.png - then expand it again and save it. Task 7: shift cells along rows: rowShift(smallPic, shiftSize) - 6 marks In lecture 9 we briefly describe how to shift array elements, using the modulo operator (%) to work out the new position of each element, wrapping them around to the beginning if the shift would push them off the end. Our QR code vandal has done this with image7.png: in every row the elements have been shifted 12 positions to the right. Because the elements wrap around, you don't need to write something to shift them backwards: you just write the same sort of code that the vandal used, and shift the elements by 13. This will make a total shift of 25, which will put all the cells back where they started. Then fixCodes() will expand the resulting image and save it. Task 8: progressively shift cells along rows: progressiveRowShift(smallPic) - 6 marks Was that too easy? The same principle can be applied to make a much bigger mess of the QR code. Instead of shifting each row by the same amount, each row has been shifted by its row number. So, Inft1004 Introduction to Programming Assignment 6 for example, row 0 hasn't shifted at all, but row 10 has shifted by 10 positions. The vandal has performed these shifts leftward to produce image8.png, so to restore the QR code you will need to shift each row to the right by its row number. It should then be expanded and saved. Task 9: reflect rows: rowReflect(smallPic) - 6 marks Another thing covered in lecture 9 is reflecting an array about its middle element. The QR code vandal has done this to every row of image9.png, so you will need to do the same to restore the code, and then to expand and save it. Note that what we call reflecting is different from what Guzdial & Ericson call mirroring, which reflects just half of the image to overwrite the other half. Task 10: reflect alternate columns: alternateColumnReflect(smallPic) - 6 marks It's easy enough to do the same thing with columns as with rows, but the vandal has added a new twist. Instead of reflecting every column about its centre, she has reflected alternate columns, starting with column 0. So columns 0, 2, 4, etc are reflected, but columns 1, 3, 5, etc are just as they were in the original image. See if you can restore image10.png to a working QR code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

What is an aquifer?

Answered: 1 week ago