Question

7. Problem D2 Bitwise operation 7.1 Specification A digital image is typically stored in computer by means of its pixel value

ackedi binay: 011010 011113 100 1000 Unpacking R: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) G: binary: 00

lab3RGB.c file:

#include <stdio.h>

#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<0 || g<0 || b <0) )
  {             
     printf("A: %d\t", alpha);  printBinary(alpha); printf("\n");
     printf("R: %d\t", r);  printBinary(r); printf("\n");
     printf("G: %d\t", g);  printBinary(g); printf("\n"); 
     printf("B: %d\t", b);  printBinary(b); printf("\n"); 

    /* do the packing */






     //printf("\nPacked: value %d\t", rgb_pack); printBinary(rgb_pack);printf("\n");
     printf("\nPacked:\t"); printBinary(rgb_pack);printf(" (%d)\n", rgb_pack);
     printf("\nUnpacking  ......\n");

    /* do unpacking */






     printf("R: "); printBinary(r_unpack); printf(" (%d, %#o, %#X)\n", r_unpack, r_unpack, r_unpack);
     printf("G: "); printBinary(g_unpack); printf(" (%d, %#o, %#X)\n", g_unpack, g_unpack, g_unpack);
     printf("B: "); printBinary(b_unpack); printf(" (%d, %#o, %#X)\n", b_unpack, b_unpack, b_unpack);
     printf("------------------------------------\n"); 
         
     /* read again */
     printf("\nenter R value: ");
     scanf("%d",&r);
     printf("enter G value: ");
     scanf("%d",&g);
     printf("enter B value: ");
     scanf("%d",&b);

  }

}
7. Problem D2 Bitwise operation 7.1 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 BufferedImage class has a method int getRGB (int x, İnt 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 0255 thus 8 bit is enough to represent it, an 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 07), 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 987 6 5 4 3 210 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 100 for Alpha 7.2 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. The binary fepresentations are generated by calling function void printBinary (int val), which is defined for you in another program binaryFunction.c. (How do you use a function that is defined in another file? Don't include) Next is the pack 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. After that, the unpacked R,G 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
ackedi binay: 011010 011113 100 1000 Unpacking R: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) G: binary: 00000000 00000000 00000000 11100000 (224, 0340, 0XEO) B: binary: 00000000 00000000 00000000 10000011 (131, 0203, 0x83) enter R value: 254 enter G value: 123 enter B value: 19 A: 100 binary: 00000000000000000000000001100100 R: 254 binary: 00000000 00000000 00000000 11111110 G: 123 binary: 00000000 00000000 00000000 01111011 B: 19 binary: 00000000 00000000 00000000 00010011 Unpacking R: binary: 00000000 00000000 00000000 11111110 (254, 0376, 0XFE) G: binary: 00000000 00000000 00000000 01111011 (123, 0173, 0X7B) B: binary: 00000000 00000000 00000000 00010011 19, 023, 0X13) enter R value: -3 enter G value:3 enter B value: 56 red 340 % Assume all the inputs are valid.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I have pasted the code and screenshot of successful run below.
  • In case of any doubts just comment down.

-------------------------Screenshot-----------------------

ksmukta:10.c->gcc a.c &&./a.out enter R value (0-255): 254 enter G value (0-255): 123 enter B value (0-255): 19 A: 100 000000
-------------------------Code-----------------------

#include <stdio.h>
#define AlphaValue 100

void printBinary(int a){
char binary[33];
for(int i=31;i>=0;i--){
binary[i] = '0'+(a%2);
a/=2;
}
binary[32] = '\0';
for(int i=0;i<32;i++){
printf("%c",binary[i]);
if(i>0 && ((i+1)%8==0) && i!= 31) printf(" ");
}
}

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<0 || g<0 || b <0) )
{   
printf("A: %d\t", alpha); printBinary(alpha); printf("\n");
printf("R: %d\t", r); printBinary(r); printf("\n");
printf("G: %d\t", g); printBinary(g); printf("\n");
printf("B: %d\t", b); printBinary(b); printf("\n");

/* do the packing */
rgb_pack = 0;
rgb_pack = rgb_pack | (alpha<<24);
rgb_pack = rgb_pack | (r<<16);
rgb_pack = rgb_pack | (g<<8);
rgb_pack = rgb_pack | (b<<0);

//printf("\nPacked: value %d\t", rgb_pack); printBinary(rgb_pack);printf("\n");
printf("\nPacked:\t"); printBinary(rgb_pack);printf(" (%d)\n", rgb_pack);
printf("\nUnpacking ......\n");

/* do unpacking */

int rmask = ((1<<8)-1)<<16;
r_unpack = (rgb_pack & rmask)>>16;
int gmask = ((1<<8)-1)<<8;
g_unpack = (rgb_pack & gmask)>>8;
int bmask = ((1<<8)-1)<<0;
b_unpack = (rgb_pack & bmask)>>0;


printf("R: "); printBinary(r_unpack); printf(" (%d, %#o, %#X)\n", r_unpack, r_unpack, r_unpack);
printf("G: "); printBinary(g_unpack); printf(" (%d, %#o, %#X)\n", g_unpack, g_unpack, g_unpack);
printf("B: "); printBinary(b_unpack); printf(" (%d, %#o, %#X)\n", b_unpack, b_unpack, b_unpack);
printf("------------------------------------\n");

/* read again */
printf("\nenter R value: ");
scanf("%d",&r);
printf("enter G value: ");
scanf("%d",&g);
printf("enter B value: ");
scanf("%d",&b);

}

}

Add a comment
Know the answer?
Add Answer to:
lab3RGB.c file: #include <stdio.h> #define AlphaValue 100 int main() { int r, g,b; unsigned int rgb_pack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int...

    Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void...

    #include<stdio.h> int main() { int i; printf("Type an integer value: "); scanf("%d",&i); evaluate(i); return(0); } void evaluate(int x) { if(x > 10) printf("Value entered by you is greater than 10"); else if(x < 10) printf("Value entered by you is less than 10"); else printf("Value entered by you is equal 10"); } _______________________________________ report for this in hr ?

  • How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary;...

    How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary; int reminder, weight = 1; binary = 0.0; //Input decimal number from user printf("Enter any decimal number: "); scanf("%lld", &decimal); // Since we do not want change the value of decimal in the code // let us use tempDecimal to store the value of decimal tempDecimal = decimal; printf("\n\n"); // Let us use while loop first to implement printf("while loop part:\n"); /**************START FROM HERE...

  • #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] +...

    #include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...

  • 10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to...

    10.11 (Left Shifting Integers ) Left shifting an unsigned int by 1 bit is equivalent to multiplying the value by 2. Write function power2 that takes two integer arguments number and pow and calculates number * 2pow. You should declare pow as an unsigned integer , but remember that the result may be a negative value . Use the shift operator to calculate the result. Print the values as integers and as bits . Assume that an integer is of...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • 5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns...

    5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns igned int a, unsigned int b): // function prototype 5 int main(void) printf("%s". "Enter two positive integers: unsigned int x: I/ first integer unsigned int y: // second integer scanf("Su%u". &x, &y); "); 12 13 14 15 II Parameter b must be a positive integer 16 to prevent infinite recursion 7 unsigned int mystery Cuns igned int a, unsigned int b) 18 printf("The result...

  • In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...

    In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holder, int flag-position); int check-flag (unsigned int flag-holder , int flag-position); void display -32_flags (unsigned int flag-holder); int main(int argc, char* argv (1) unsigned int flag -holder = 0; set-flag (& flag-holder, 3); set-flag (& flag-holder, 16); set-flag (& flag-holder, 31); display-32-flags (flag-holder); unset-flag(& flag-holder , 31); unset-flag (& flag-holder, 3); set-flag (& flag-holder , 9); display-32-flags (flag-holder ); return 0; Write...

  • Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf(&#3...

    Code that needs to be modified: #include #include #include int main() { int i,N,x; unsigned int seed; double R; printf("\nEnter number of iterations and seed"); printf("\n"); scanf("%i %u", &N,&seed); srand(seed);    for(i=0;i { R=(double)rand()/RAND_MAX; if (R<0.5) x=x+1; else x=x-1; } printf("Final location is "); printf("%d",x); printf("\n"); } Question: Write a code that generates N pairs of random numbers. Call the first member of each pair x and the second member y. Count how many of the N pairs obey x^2+y^2<1. Call...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT