Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Specification A digital image is typically stored in computer by means of its pixel values, as well as some formatting information. Each pixel value consists

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Specification A digital image is typically stored in computer by means of its pixel values, as well as some formatting information. Each pixel value consists of 3 values of 0 ~ 255, representing red (R), green (G) and blue (B). As mentioned in class, Java's Bufferedlmage class has a method int get RGB (int x, int y), which allows you to retrieve the RGB value of an image at pixel position (x,y). How could the method return 3 values at a time? As mentioned in class, the 'trick' is to return an integer (32 bits) that packs the 3 values into it. Since each value is 0-255 thus 8 bits are enough to represent it, a 32 bits integer has sufficient bits. They are packed in such a way that, counting from the right most bit, B values occupies the first 8 bits (bit 0~7), G occupies the next 8 bits, and R occupies the next 8 bits. This is shown below. (The left-most 8 bits is packed with some other information about the image, called Alpha, which we are not interested here.) 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Suppose a pixel has R value 2 (which is binary 00000010), G value 7 (which is binary 00000111) and B value 8 (which is binary 00001000), and Alpha has value 100, then the integer is packed as 0 0 1 0 1 0 1 0 0 O O O O O 1 0 0 0 1 1 1 0 0 0 0 1 000 100 for Alpha 2 7 8 Implementation In this exercise, you are going to use bitwise operations to pack input R, G and B values into an integer, and then use bitwise operations again to unpack the packed integer to retrieve the R, G and B values. Download and complete lab3RGB.c. This C program keeps on reading input from the stdin. Each input contains 3 integers representing the R, G and B value respectively, and then outputs the 3 values with their binary representations (implemented for you). The binary representations are generated by calling function void printBinary(int val), which is defined for you in another program binaryFunction.c. Next is the packing part that you should implement. This packs the 3 input values, as well as Alpha value which is assumed to be 100, into integer variable rgb_pack. Then the value of rgb_pack and its binary representation is displayed (implemented for you). Next you should unpack the R. G and B value from the packed integer rgb_pack. Note that you should not just use the original R,G and B value, as this is considered a "hoax. After that, the unpacked RG and B value and their Binary, Octal and Hex representations are displayed (implemented for you). The program terminates when you enter a negative number for either R, G or B value. Hint: Packing might be a little easier than unpacking. Considering shifting R.G.B values to the proper positions and then somehow merge them into one integer (using bitwise operators). For unpacking, you can either do shifting + masking, or, masking + shifting, or, shifting only. Shifting + masking means you first shift the useful bits to the proper positions, and then turn off (set to 0) the unwanted bits while keeping the values of the useful bits. What you want to end up with, for example for R value, is a binary representation of the following, which has decimal value 2. 0000000000000000000000000000000000010 Masking + shifting means you first use & to turn off some unrelated bits and keep the values of the good bits, and then do a shifting to move the useful bits to the proper position. When doing shifting, the rule of thumb is to avoid right shifting on signed integers. Explore different approaches for unpacking Finally, it is interesting to observe that function printBinary() itself uses bitwise operations to generate artificial 'O' or '1', printing the binary representation of the int. Since printBinary() is defined in another file, how to use a function that is defined in another file? Don't do include, instead, declare the function printBinary in the main program lab3RGB.c (how?), and then compile the files together, as shown below. (We will talk more about multiple files next week). Sample Inputs/Outputs: red 338 gec lab3RGB.C binaryFunction.c pay attention to how the red 339 a.out program is compiled enter R value (0-255): 1 enter G value (0-255): 3 enter B value (0-255): 5 A: 100 binary: 00000000 00000000 00000000 01100100 binary: 00000000 000000000000000000000001 G: 3 binary: 00000000 00000000 00000000 00000011 binary: 00000000 00000000 00000000 00000101 Packed: binary: 01100100 00000001 00000011 00000101 (1677787909) R: 1 B: 5 Unpacking R: binary: 00000000 0000000000000000 00000001 G: binary: 00000000 00000000 00000000 00000011 B: binary: 00000000 00000000 00000000 00000101 (1,01,0x1) (3,03,0x3) (5,05,0x5) enter R value (0-255): 22 enter G value (0-255): 33 enter B value (0-255): 44 A: 100 binary: 00000000 00000000 00000000 01100100 R: 22 binary: 00000000 00000000 00000000 00010110 G: 33 binary: 00000000 0000000000000000 00100001 B: 44 binary: 0000000000000000 00000000 00101100 Packed: binary: 01100100 00010110 00100001 00101100 (1679171884) Unpacking R: binary: 00000000 00000000 00000000 00010110 G: binary: 00000000 00000000 00000000 00100001 B: binary: 00000000 00000000 00000000 00101100 (22, 026, 0X16) (33, 041, 0x21) (44, 054, 0X2C) enter R value (0-255): 123 enter G value (0-255): 224 enter B value (0-255): 131 A: 100 binary: 00000000 00000000 00000000 01100100 R: 123 binary: 00000000 00000000 00000000 01111011 G: 224 binary: 00000000 00000000 00000000 11100000 B: 131 binary: 00000000 00000000 00000000 10000011 Packed: binary: 01100100 01111011 11100000 10000011 (1685840003) Unpacking R: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) G: binary: 00000000 00000000 00000000 11100000 (224, 0340, OXEO) B: binary: 00000000 00000000 00000000 10000011 (131, 0203, 0X83) enter R value (0-255): 254 enter G value (0-255): 123 enter B value (0-255): 19 A: 100 binary: 0000000000000000 00000000 01100100 R: 254 binary: 00000000 00000000 00000000 11111110 G: 123 binary: 00000000 00000000 00000000 01111011 B: 19 binary: 00000000 00000000 00000000 00010011 Packed: binary: 01100100 11111110 01111011 00010011 (1694399251) Unpacking R: binary: 00000000 00000000 00000000 11111110 (254, 0376, OXFE) G: binary: 00000000 00000000 00000000 01111011 (123, 0173, OX7B) B: binary: 00000000 00000000 0000000000010011 (19, 023, 0X13) enter R value (0-255): -3 enter G value (0-255): 3 enter B value (0-255): 56 red 340 % #include #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack; int r_unpack, g_unpack, b_unpack; int alpha = AlphaValue; printf("enter R value (0-255): "); scanf("%d",&r); printf("enter G value (0-255): "); scanf("%d",&g); printf("enter B value (0-255): "); scanf("%d",&b); while(! (r

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

More Books

Students also viewed these Databases questions

Question

=+employee to take on the international assignment?

Answered: 1 week ago