Effective Programming in C and UNIX Lab 6 Image Manipulation with BMP Images Due Date: Sunday April 3rd, 2011 by 11:59pm

Similar documents
ECE 3331, Dr. Hebert, Summer-3, 2016 HW 11 Hardcopy HW due Tues 07/19 Program due Sunday 07/17. Problem 1. Section 10.6, Exercise 3.

15-122: Principles of Imperative Computation, Spring 2016

CpSc 101, Fall 2015 Lab7: Image File Creation

Lecture 17 Bit Operations

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Lecture 13 Bit Operations

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Chapter 3. Texture mapping. Learning Goals: Assignment Lab 3: Implement a single program, which fulfills the requirements:

CS 315 Data Structures Fall Figure 1

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Week 5: Files and Streams

ICS(II),Fall 2017 Performance Lab: Code Optimization Assigned: Nov 20 Due: Dec 3, 11:59PM

CS 135, Fall 2010 Project 4: Code Optimization Assigned: November 30th, 2010 Due: December 12,, 2010, 12noon

Data Structures Unit 02

CS201: Lab #2 Code Optimization

Programming Assignment Multi-Threading and Debugging 2

CpSc 1111 Lab 5 Formatting and Flow Control

Spring CS Homework 3 p. 1. CS Homework 3

CSE2003: System Programming (Spring 2009) Programming Assignment #3: Drawing grid lines in an image. Due: Fri May 15, 11:59PM

ECEn 424 The Performance Lab: Code Optimization

CS 429H, Spring 2014 Lab Assignment 9: Code Optimization Assigned: April 25 Due: May 2, 11:59PM

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

ORB Education Quality Teaching Resources

CMSC 311, Fall 2009 Lab Assignment L4: Code Optimization Assigned: Wednesday, October 14 Due: Wednesday, October 28, 11:59PM

OptimiData. JPEG2000 Software Development Kit for C/C++ Reference Manual. Version 1.6. from

Programming refresher and intro to C programming

Creating Icons for Leopard Buttons

CS 051 Homework Laboratory #2

BMP file format. Contents. Pixel storage. The BMP file format, sometimes called bitmap. or DIB file format (for device-independent

Assignment 6: Ray Tracing

Lab Assignment L5: Code Optimization Assigned: June 3 Due: June 15, 11:59PM

Graduate-Credit Programming Project

BMP file format - Wikipedia

TODO. parse float input update outfile s header info resize horizontally remember padding! resize vertically

ECE454, Fall 2014 Homework2: Memory Performance Assigned: Sept 25th, Due: Oct 9th, 11:59PM

10 Connector Designer

Lab 4. Out: Friday, February 25th, 2005

CS 429H, Spring 2011 Code Optimization Assigned: Fri Apr 15, Due: Friday May 6, 11:59PM

An Introduction to Video Compression in C/C++ Fore June

Binary representation of integer numbers Operations on bits

CS 105, Fall 2003 Lab Assignment L5: Code Optimization See Calendar for Dates

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

PVR File Format. Specification

CS 580 FINAL EXAM. Fall April 29, 2014

TODO. open file update outfile s header info read infile s scanline, pixel by pixel resize horizontally remember padding!

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

LogiCORE IP Image Noise Reduction v2.0 Bit Accurate C Model

CS 213, Fall 2001 Lab Assignment L4: Code Optimization Assigned: October 11 Due: October 25, 11:59PM

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4620/8626 Computer Graphics Spring 2014 Homework Set 1 Suggested Answers

Lab 09 - Virtual Memory

CISC 360, Fall 2008 Lab Assignment L4: Code Optimization Assigned: Nov 7 Due: Dec 2, 11:59PM

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS 105 Lab 5: Code Optimization See Calendar for Dates

UW CSE 351, Winter 2013 Final Exam

rawtektv, Version 1.0: A File Format For Storing Video Sequences

BIL220, Spring 2012 Data Lab: Manipulating Bits Assigned: Feb. 23, Due: Wed., Mar. 8, 23:59PM

Programming Assignment 4 (PA4) - myxd Milestone Due: Final Due:

Lab Assignment 4: Code Optimization

Introduction to Computer Science (I1100) Data Storage

CS 253. January 14, 2017

Advanced Lighting Techniques Due: Monday November 2 at 10pm

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Example 1: Color-to-Grayscale Image Processing

Colour and Number Representation. From Hex to Binary and Back. Colour and Number Representation. Colour and Number Representation

CS103L PA3 It's So Belurry

CS 134 Programming Exercise 3:

Pre-Lab due: at 3 p.m. Post-Lab due: at 5 p.m. Name:... Forename:... Initials:...

CIS 121 Data Structures and Algorithms with Java Spring 2018

Project 1. 1 Introduction. October 4, Spec version: 0.1 Due Date: Friday, November 1st, General Instructions

Parallel Image Processing

Computer Labs: Lab 5 & Sprites

CGS 3460 Summer 07 Midterm Exam

C++ Structures Programming Workshop 2 (CSCI 1061U)

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

Programming Assignment 8 ( 100 Points )

Blocky: A Game of Falling Blocks

CMPE 655 Fall 2016 Assignment 2: Parallel Implementation of a Ray Tracer

CS Homework 11 p. 1. CS Homework 11

Mini-Project 1: Steganography. Lecture Barbara Jobstmann

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter Lab 8 - Structures

ENVI Tutorial: Introduction to ENVI

CSE 333 Midterm Exam 7/29/13

The following are the requirements for the password server client program named pass.cpp:

Data Representation 1

Project #1 Seamcarve

Project #1 Seam Carving

CSE2003: System Programming Final Exam. (Spring 2009)

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

Lab 1 - Reliable Data Transport Protocol

Programming Using C Homework 4

ECE 375: Computer Organization and Assembly Language Programming

Lab 5 - Linked Lists Git Tag: Lab5Submission

SBIG ASTRONOMICAL INSTRUMENTS

(T) x. Casts. A cast converts the value held in variable x to type T

COP Programming Assignment #7

Lab 4 - Linked Lists

CSCI 2212: Intermediate Programming / C Chapter 15

Transcription:

15-123 Effective Programming in C and UNIX Lab 6 Image Manipulation with BMP Images Due Date: Sunday April 3rd, 2011 by 11:59pm The Assignment Summary: In this assignment we are planning to manipulate BMP images using some widely used image manipulation techniques. In particular, we will be using techniques such as quantization, rotation and blurring of a given image using image bit manipulation techniques. First we need to understand the bitmap file format. The BMP file format: BMP file format is one of the most popular uncompressed file formats for images. BMP file format developed by Microsoft stores an n by m image as a file of size 3 n m + 54+ some extra bytes bytes. The 54 bytes is allocated for header information and each pixel takes 3 bytes for Red, Green and Blue (RGB) colors and if the image width is not a multiple of 4, image may also have some padding bytes (why?). Each pixel in BMP image uses 3 bytes (RGB) or 24-bits and therefore 2 24 possible colors can be represented in a BMP image. Each R, G, or B byte can store unsigned numbers in the range of 0-255. Hence the color red is (255, 0, 0) and color green is (0, 255, 0). The color (128, 0, 0) makes dark red, and (255, 0, 255) makes purple since purple is red + blue. Typical view of a bit map image as follows where last image row is represented first. Although we think of an image as a rectangle, the actual bytes are stored in a 1D array as follows Header 54 bytes R G B R G B some extra bytes may be added at end of each row R G B The left most RGB is the lower right pixel of the image and last RGB is the upper left pixel of the image. As we read the image, we will first store the header information (54 bytes) in a buffer and then read the image bytes (3 for each pixel) into an array.

Part I In this part of the assignment you are simply trying to read, store and write a BMP image file. The attributes of the BMP file can be stored in the following struct. typedef struct { char* imagebytes; /* an array of bytes. 3 bytes (RGB) for each pixel */ char header[54]; /* header information buffer */ int height, width, size, padding; /* other image attributes */ } bmp; You are to complete the following functions. This part gives a good exercise on reading and writing an image. You will have to make sure, you write the header back, all image bytes (RGB) and padding bytes (when applicable) to the output file. You should be able to open the output file using any image viewer. //Read the image into mybmp. You must allocate just enough memory to hold the image bytes. This can be done by first reading the header and figuring out what the size of the image is. Note that we pass a FILE* since file was already opened in calling program. void readimage(file* fp, bmp* mybmp); // writes the image as is to outfile. The name of the file is passed to the function void writeimage(char* outfile, bmp* mybmp); You also need to complete the main program so that when the command $./a.out file1 file2 is given, it will simply read BMP file1 and write to BMP file2. When you open file2 using an image viewer, you should see the same image as in file 1. This will allow us to make sure; we can read and write BMP images w/o losing any data. Part II Quantization of an Image In this problem, you will implement a function that achieves a quantization on an image. Quantization reduces the total number of colors used in an image. You can see an example in Figure 1.

Given an ordinary image of size w x h and a quantization level q between 0 and 7, inclusive, for each pixel in the image, take each color component (red, green and blue) and clear the lowest q bits. For example, suppose the color components for a pixel are given by the bytes RED GREEN BLUE 01101011 10111110 11010111 If the quantization level is 5, then the resulting pixel should have the following color components (note how the lower 5 bits are all cleared to 0): RED GREEN BLUE 01100000 10100000 11000000 Note that an image processed with a quantization level of 0 should not change. In this part you will complete the function int quantize(char* infile, bmp* mybmp, char* outfile, int qlevel); This function will take a valid BMP image, given by infile, read the file into mybmp using readimage developed in Part I, apply the quantization level and write a valid BMP image back to file given by outfile. You also need to complete the main program so that when the command (for example) $./a.out q 7 file1 file2 (The level can be anywhere from 0 to 7) is given, it will simply read BMP file1, apply quantization level 7 and write the BMP to file2. When you open file2 using an image viewer, you should see the quantized image.

Part III Blurring an Image In this problem, you will write a function to blur an image. This is done by using a mask. A mask is an n by n array of non-negative integers representing weights. (Note: although weights can be 0, the weight in the center position of the mask cannot be zero.) For our purposes, n must be odd. The origin of the mask is its center position. For each pixel in the input image, think of the mask as being placed on top of the image so its origin is on the pixel we wish to alter. The original intensity value of each pixel under the mask is multiplied by the corresponding value in the mask that covers it. These products are added together and then we divide by the total of the weights in the mask to get the new intensity of the mask. Always use the original values for each pixel for each mask calculation, not the new values you compute as you process the image. For example, refer to Figure 3, which shows a 3 by 3 mask and an image that we want to blur. Suppose we want to compute the new intensity value for pixel e. Imagine overlaying the mask so its center position is on e. We would compute the new intensity for the pixel e as: (a + 3b + c + 3d + 5e + 3f + g + 3h + i) / 21 This calculation is done three times for each pixel, once for its red channel, once for its green channel, and once for its blue channel. Note that sometimes when you center the mask over a pixel you want to blur, the mask will hang over the edge of the image. In this case, compute the weighted sum of only those pixels the mask covers. Remember that you must divide by the sum of only those weights that you use from the mask. For the example shown in Figure 4, the new intensity for the pixel e is given by: (3b + c + 5e + 3f + 3h + i) / 16

Figure 5 shows a sample image blurred using the following masks: In this part you will complete the function int blurring(char* imagein, char* imageout, int masklevel); Where mask level is either 1 or 3 or 5. If it is 1, no changes to the image are made, if it is 3, then the 3 by 3 mask is applied and if it is 5, then 5 by 5 mask is applied.

You also need to complete the main program so that when the command $./a.out b <1/3/5> file1 file2 is given, it will simply read BMP file1 and apply mask 1 or 3 or 5 and write to BMP file2. When you open file2 using an image viewer, you should see images as shown in Figure 5. Part IV Rotation Effect In this problem, you will create a rotation effect on an image. Your task here is to implement a function that takes as input an image of size wx h and creates a Rotation image of size 2w x 2h that contains the same image repeated four times, the top right image containing the original image, the top left containing the original image rotated 90 degrees counterclockwise, the bottom left containing the original image rotated 180 degrees, and the bottom right containing the original image rotated 90 degrees clockwise. Note that the original image must have the same width and height (square image) in order to do the rotation effect. A sample image is shown in Figure 2. In this part you will complete the function int rotation(char* infile, bmp*mybmp, char* outfile); This function will take a valid BMP image, given by infile, create the rotation as described and shown above and write a valid BMP image back to file outfile. You also need to complete the main program so that when the command $./a.out r file1 file2

is given, it will simply read BMP file1 and rotate as defined above and write to BMP file2. When you open file2 using an image viewer, you should see images as shown in Figure 2. Suggested Data Structure: We will read the image file and maintain image attributes in a struct as follows typedef struct { char** rows; /* an array of rows */ char header[54]; /* header information buffer */ int height, width, size, padding; /* other image attributes */ } BITMAP; The BITMAP struct will help us perform the necessary operations on the image. First, you need to allocate memory for rows (an array that holds rows of the image) The structure of the 54 byte header of a BMP file is as follows. The first 14 bytes are allocated for typedef struct { unsigned short int type; /* BMP type identifier */ unsigned int size; /* size of the file in bytes*/ unsigned short int reserved1, reserved2; unsigned int offset; /* starting address of the byte */ } HEADER; The next 40 bytes are reserved for a structure as follows. typedef struct { unsigned int size; /* Header size in bytes */ int width,height; /* Width and height in pixels */ unsigned short int planes; /* Number of color planes */ unsigned short int bits; /* Bits per pixel */ unsigned int compression; /* Compression type */ unsigned int imagesize; /* Image size in bytes */ int xresolution,yresolution; /* Pixels per meter */ unsigned int ncolors; /* Number of colors */ unsigned int importantcolors; /* Important colors */ } INFOHEADER; Part V (Optional) In this part of the assignment you are to implement a known algorithm that allows us to hide a message inside an image. Steganography is the art and science of writing hidden messages in such a way that no-one, apart from the sender and intended recipient, suspects the existence of the message, a form of security through obscurity[source: Wikipedia] In this part we will only test the following two images.

//Apply the following algorithm to an image. Remove all but last two bits of each color component. Make the resulting image 85 times brighter void coolalgorithm(bitmap* mybmp); This algorithm will take this image and convert to this image when you apply the above algorithm. That is, remove all but last 2 bits of each color component. Then multiply (or bit shift) the color component by 85. [source: Wikipedia] You also need to complete the main program to handle -r flags $./lab6 -c file1 file2 // tree is converted to a cat really cool!! Style Points Proper use of functions (passing/returning arguments etc), commenting, style, handin, compile, indenting, and efficiency of the solution are worth an additional 10 points. Also 3 points will be allocated for selflog.txt file. Extra Credit: There is some opportunity to get extra credit in this assignment. TA s will consider giving up to +5 points for really cool algorithms. You should write why it is a really cool algorithm in a readme.txt file in the handin folder. For more advanced users, you can also think of how to implement an edge detection algorithm. Edges can be detected, in theory, by observing changes in color intensity, variations in scene illumination, change in material properties etc. Your algorithm can generalize to images say, passport images of people, so you can mark the outline. If you are completing this part, give a link to an image folder you tested with so TA s can try things out. Mark the outline with red pixels. Commenting your code: Please comment your code to show what you are doing. The least amount of comments is a short description at the beginning of each function that explains what it does, what parameters it takes, and what the expected output or return value is. Downloading your code: You can download some starter code (although you have to complete most of it) and sample images from /afs/andrew.cmu.edu/course/15/123/downloads/lab6 Submitting your code: submit main.c to /afs/andrew/course/15/123/handin/lab6/yourid. DO NOT submit any image files you are using to test the program.