Introduction to Computer Science
HW #1
Due: 2015/03/25
Homework Rules:
Hand-written homework can be handed in before lecture starts. Otherwise, you
may contact the TA in advance and then bring the hardcopy to the TA in BL421
(please send e-mail in advance).
As for the programming part, you need to upload it to CEIBA before the
deadline (2014/03/13 3am). The file you upload must be a .zip file that contains the
following files:
README.txt
HW01_b03901XXX (a folder that contains all .cpp & .h as required),
1. Do not submit executable files (.exe) or objective files (.o, .obj). Files with
names in wrong format will not be graded. You must remove any system calls,
such as system ("pause"), in your code if any.
2. In README.txt, you need to describe which compiler you used in this homework
and how to compile it (if it is in a project form).
3. In your .cpp files, we suggest you write comments as detailed as you can. If
your code does not work properly, code with comments earns you more partial
credits.
Introduction to Computer Science
HW #1
Due: 2015/03/25
Chapter 1 Review Problems (40%)
Programming Problem (60%)
We have learned lots of data storage. Dont you ever want to know how exactly
images are stored in our computer? Lets try the easiest one: bmp format. In this
problem, you are going to write a BMPImg class that can:
(1) Load a bmp file. (In simple format, no need to deal with arbitrarily cases)
(2) Do a simple color transform: transfer the R,G,B color into gray-scale.
(But still store in R,G,B channel, we will talk about it later.)
(3) Store it as another bitmap picture. You may check it by any bmp reader.
Introduction to Computer Science
HW #1
Due: 2015/03/25
How to start? (File format)
Bitmap files are composed of 2 parts: header and content (bitmap data).
The header stores a table that describes information about this picture. In this
homework, we only consider the most common case as follows:
Shift
Name
Size
Notes
0x00
Identifier (ID)
Always be BM (char)
0x02
File Size
0x06
Reserved
0x0A
Bitmap Data Offset
Unit: byte
0
int(54) in our case
0x0E
0x12
0x16
0x1A
0x1C
Bitmap Header Size
Width
Height
Planes
Bits Per Pixel
4
4
2
2
0x1E
Compression
int(40) in our case
Unit: pixel
Unit: pixel
1
24 for RGB[8,8,8] (in our case)
16 for RGB[5,5,5]
0 in our case
4
4
4
4
4
(no compression )
Unit: bytes
Keep it untouched
Keep it untouched
0 in our case
0 in our case
0x22
0x26
0x2A
0x2E
0x32
Bitmap Data Size
H-Resolution
V-Resolution
Used Colors
Important Colors
For more detail, you can refer to: http://crazycat1130.pixnet.net/blog/post/1345538
If you want to do it byte-by-byte yourself, be careful of the Little Endian problem.
Hint: You dont need to worry about it if you read/write as int/short directly.
As for the content, it depends on the Bits Per Pixel and Compression to
determine the format. This problem sticks with RGB24 and no-compression.
That means the color data would be stored like this:
Introduction to Computer Science
HW #1
Due: 2015/03/25
Total: 3 x Width x Height Bytes
1A 6F D9 6B 48 22 4C 3A 2E
1 Byte
D2 33 66
3 Bytes per pixel
Gamma Correction:
In this homework, you are asked to do the gamma correction for a colored image.
Gamma correction can be formulated as:
= 1/ , [0,1], , ,
Gamma correction is a common operation in our digital cameras. Without it,
pictures might be darker because the non-linear response caused by electronic
sensors or display. Heres an example:
victim to =2.2
(before correction)
Vo = Vi(1/2.2)
(after correction)
For more detail, you can refer to: http://en.wikipedia.org/wiki/Gamma_correction
A little bit inaccuracy is OK, we wont be picky.
Hint:
V in the formula ranges from 0.0 to 1.0. You may need to transform unsigned
char to float/double first and transform it back to 0~255 when storing it.
Before you start, you can have a look at the code we provided. Maybe it can
inspire you how to finish this problem easily. You are not requested to follow it
strictly, but you need to obey the homework correcting rules below.
Introduction to Computer Science
HW #1
Due: 2015/03/25
Important rules:
You MUST follow these coding rules:
(1) A class named as BMPImg, TA will use it for grading.
(2) There must be these member functions as interfaces:
bool/void loadPic (string/char* picPath); //Loading bmp file
bool/void gammaCorrection(double gamma); //do V=V^(1/gamma)
bool/void storePic(string/char* outPath); //Store bmp file
(3) TA will test your code in a way like this:
#include "BMPImg.h"
int main(){
BMPImg img;
img.loadPic("lantern.bmp");
img.storePic("result1.bmp");
img.gammaCorrection(2.2);
img.storePic("result2.bmp");
return 0;
}
Bonus (5%): Edge Detection
Here, we are going do something special on our images. There is an easy but useful
function to detect (enhance) edges in a picture! First, change the image from RGB
to gray scale:
= 0.299 R + 0.587 G + 0.114 B
Then do the calculation:
[, ] =
[ 1, 1] 2[ 1, ] [ 1, + 1]
+[ + 1, 1] + 2[ + 1, ] + [ + 1, + 1]
[, ] =
[ 1, 1] 2[, 1] [ + 1, 1]
+[ 1, + 1] + 2[, + 1] + [ + 1, + 1]
[, ] = 2 + 2
Its called Sobel operator. Ref: http://en.wikipedia.org/wiki/Sobel_operator
After calculating the edge value, store it into all R, G, and B channels (still RGB24
format).
Introduction to Computer Science
HW #1
Due: 2015/03/25
TA will test your code this way like:
#include "BMPImg.h"
int main(){
BMPImg img;
img.loadPic("result2.bmp");
img.sobelEdge();
img.storePic("result3.bmp");
return 0;
}
If you meet the bonus requirements, write I finished the bonus part. in the
readme file to let TA know.