Docsity

Lab Exercise for ECS 102: Processing a Picture File - Prof. Marjory Baruch, Lab Reports of Computer Science

Toggle sidebar
bg1
ECS 102 Lab 19 Spring 2009
Name ________________________ Section 1(3:45) 2(12:45)
Anywhere that I ask you to observe what happens, write down your observations in the space provided.
Working with pictures, 2 dimensional array.
I. Open the file picture1.txt. Notice that it has two numbers on the first line, and a whole lot of
numbers underneath.
A) Without driving yourself nuts, make some observations about the numbers, EXCLUDING THOSE
FIRST TWO NUMBERS.
1. Their data type (int, char, double) circle one
2. Their approximate range:
the smallest number the biggest number
B) Look at the collection of numbers. Can you tell what the picture is? (Ha ha. Notice I don't leave
room for an answer. )
II.
A) Copy and paste this program into Visual Studio .Net.
/* pictureprocessor.c */
#include<stdio.h>
int main(void)
{
int numrows, numcols; /*the number of rows and columns in the
picture, measured in pixels:
"picture elements" */
int r, /*row index*/
c; /*column index*/
int raw[350][250]; /* 2-D array of raw picture data */
char pic[350][250]; /* 2-D array of Character picture */
/*In a 2-D array, the first number is the number of rows,
and the second number is the number of columns.
*/
FILE* infile; /* the source file */
FILE* outfile; /* where we will put the processed picture */
infile=fopen("picture1.txt","r");
if (infile == 0)
{
printf("Input file not found.");
return 0;
}
outfile=fopen("picture1out.txt","w");
pf3
pf4

Partial preview of the text

Download Lab Exercise for ECS 102: Processing a Picture File - Prof. Marjory Baruch and more Lab Reports Computer Science in PDF only on Docsity!

ECS 102 Lab 19 Spring 2009 Name ________________________ Section 1(3:45) 2(12:45) Anywhere that I ask you to observe what happens, write down your observations in the space provided. Working with pictures, 2 dimensional array. I. Open the file picture1.txt. Notice that it has two numbers on the first line, and a whole lot of numbers underneath. A) Without driving yourself nuts, make some observations about the numbers, EXCLUDING THOSE FIRST TWO NUMBERS.

  1. Their data type (int, char, double) circle one
  2. Their approximate range: the smallest number the biggest number B) Look at the collection of numbers. Can you tell what the picture is? (Ha ha. Notice I don't leave room for an answer. ) II. A) Copy and paste this program into Visual Studio .Net. /* pictureprocessor.c / #include<stdio.h> int main(void) { int numrows, numcols; /the number of rows and columns in the picture, measured in pixels: "picture elements" / int r, /row index/ c; /column index/ int raw[350][250]; / 2-D array of raw picture data / char pic[350][250]; / 2-D array of Character picture / /In a 2-D array, the first number is the number of rows, and the second number is the number of columns. / FILE infile; /* the source file / FILE outfile; /* where we will put the processed picture */ infile=fopen("picture1.txt","r"); if (infile == 0) { printf("Input file not found."); return 0; } outfile=fopen("picture1out.txt","w");

fscanf(infile,"%d%d", &numcols, &numrows); printf("number of rows %d\n number of columns %d", numrows,numcols); /*To read in the picture data, we will read across the first row, one column (number) at a time, then across the second row, and so on, for the right number of rows. / for (r=0; r<numrows; r++) { for (c=0; c<numcols; c++) fscanf(infile,"%d", &raw[r][c]); } / The picture is in gray scale, with 0 representing white and 255 representing black. We will convert it to black and white, first evenly, that is, anything below or equal to 255/2 will be white and anything above will be black. The character ' ' (a space) will be white, and the character 'M' will be black. / / Here we are NOT printing the picture to a file, just setting up the array with the picture. / for (r=0; r<numrows; r++) { for (c=0; c<numcols; c++) { if (raw[r][c]>255/2) pic[r][c]='M'; else pic[r][c]=' '; } / end for c / } / end for r / / Print the picture, one row at a time, going column to column, printing to file characters, not separated by spaces, but with a newline at the end of each row. */ for (r=0; r<numrows; r++) { for (c=0; c<numcols; c++) fprintf(outfile,"%c", pic[r][c]); fprintf(outfile,"\n");

What does a low number represent? a high number? B) Store a copy of the file picture1.txt in the project folder so the program can find it. C) Run the program. On the console window you can see how many rows and columns there are. Number of rows: Number of columns: Open the output file, picture1out.txt. Change the font to a fixed width font, and set the size to 3 or 4 (or smaller). Open the window wide so you can see the right hand edge (it should be pretty smooth.) If you look in the middle, you may be able to see the picture. It is a little elongated because characters, unlike pixels, are taller than they are wide (including the space between lines). D) Since fixed width characters are about twice as tall as they are wide, modify the print section of the program so that each character is printed twice. III. Your turn to make the picture better, using photo processing. Some things to try  a different cutoff for black and white: maybe everything below 230 counts as white instead of everything below 128. Experiment with different cutoffs.  "2 bit" gray scale, ie, 4 colors, maybe using the space, the comma, the o, and the M. Or you pick the characters for different shades of "gray" (black to white ratio). Experiment with different cutoffs. What would the cutoffs be if they were evenly spaced? What cutoff did you like best for the output? What characters did you use?  more than 4 shades, with cutoffs of your choice. Print out your code for one nice modification, and print the picture. Make the font (of the picture) small enough so it almost fits on one page, then print one page only.


picture source: http://www.cs.bath.ac.uk/~pjw/Q3D/newroomflower/panda.jpg