home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
lab30
More image manipulation
As a class, we'll choose one of the following individual-pixel operations:
- convert to black-and-white
- invert (make a color-negative)
- darken
- swap green & blue
- set redness to a random value
- make sure the redness is no more than 100 (using max)
- anything you want to do?
Your task will be to write a function which performs the operation on
an entire Picture-object:
-
Write a function
changePixelAt : Picture, int, int → None
which takes in a Picture and an x,y coordinate,
and changes just the one pixel at that location,
according to the operation we chose above.
- write a function
changeRow : Picture, int → None
which takes in a Picture and a y-coordinate,
and changes the picture's entire row at that y.
Note:
To get checked off, this function should have a loop,
and the only statement in the body of the loops is
a call to changePixelAt.
Note:I suggest that as you develope your program, you have this
function also call repaint, so you can see its effect easily.
However, call repaint from outside your loop —
only once, not once-per-column.
- write a function
changeAllRows : Picture → None
which takes in a Picture and
and changes the entire picture.
Note:
To get checked off, this function should have a loop,
and the only statement in the body of the loops is
a call to changeRowAt.
Self-challenges
- make a function
changeRowRange which is like changeRow ,
but takes extra arguments — a starting-column and a width —
and changes just a selected region of a row.
- make a function
changeRectangle which is like changeAllRows,
but takes four extra arguments — a starting-column & starting-row,
as well as a width & height —
and changes just the selected region.
-
Once those are working, go back and modify your changeRow
and changeAllRows so that
they work exactly as before,
but they are each a single line — a call to
the more general functions you just wrote.
-
To show how your new functions are more useful than changing the entire picture,
write a function which changes a random 60×30 rectangle1.
See randrange, which you can use after you include the line
include random near the top of your file.
1
Note that the west edge of the rectangle will be between
0 and the width-minus-60, not between 0 and the entire width.
Also, note that this function will only work on Pictures that are at least 60×30 big!
(Can you of ways to modify this function to do something reasonable for any-size Picture?)
↩
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs