Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the picture produced by PR01, create a dithered monochrome image of it. Pr

ID: 3793929 • Letter: U

Question

Using the picture produced by PR01, create a dithered monochrome image of it.
Print the report (and capture in your ReadMe.txt file) -- format details provided separately.

#include <stdio.h>
#include <stdlib.h>

#include "BMP.h"
#include "MyBMP.h"

int main(void)
{
   // Open the original image and get dimensions
   BMP *bmp = BMP_open("Problem_1.bmp");
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   // Create the output image to be the same width and twice as high
   BMP *composite = BMP_create(width, height);
   BMP_setFileName(composite, "Problem_3.bmp");
  
   // Build composite image
   MyBMP_dither(bmp); - ----------- this is the function that is ment to build the pic
   MyBMP_paste(composite, bmp, 0, 0);
  
   MyBMP_report(bmp, "Problem_3.bmp");

   // Save the output image              
   BMP_save(composite);
  
   // Perform housekeeping tasks
   BMP_del(composite);
   BMP_del(bmp);
  
   return EXIT_SUCCESS;
}

MyBMP * MyBMP_open(char *filename)
{
   return BMP_open(filename);
}

void MyBMP_getFilename(MyBMP *p, char *filename)
{
  
}

// Prints a report to the screen with basic information about the image
void MyBMP_report(MyBMP *bmp, char *filename)
{
   printf("=============================================== ");
   printf("MyBMP REPORT ");
   printf("=============================================== ");
   printf("Address:............... %p ", (void *) bmp);
   printf("Filename:.............. %s ", filename);
   printf("----------------------------------------------- ");
   printf("Image Width:........... %6i ", BMP_getWidth(bmp));
   printf("Image Height:.......... %6i ", BMP_getHeight(bmp));
   printf("----------------------------------------------- ");
   printf("Avg Red Intensity:..... %6.2f ", MyBMP_avgR(bmp));
   printf("Avg Green Intensity:... %6.2f ", MyBMP_avgG(bmp));
   printf("Avg Blue Intensity:.... %6.2f ", MyBMP_avgB(bmp));
   printf("----------------------------------------------- ");
   printf("Avg Gray Intensity:.... %6.2f ", MyBMP_avgGray(bmp));
   printf("=============================================== ");
}
// Paste the color
void MyBMP_swapPixel(BMP *bmp, int row, int col, int newR, int newG, int newB)
{
   BMP_setRed(bmp, row, col, newR);
   BMP_setGreen(bmp, row, col, newG);
   BMP_setBlue(bmp, row, col, newB);
}

// Change the color if needed
void MyBMP_swapColors(BMP *bmp, int oldR, int oldG, int oldB, int newR, int newG, int newB)
{
   // Get demisions
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   // Go through each point
   for (int row = 0; row < height; row++)
       for (int col = 0; col < width; col++)
       {
           int red = BMP_getRed(bmp, row, col);
           int green = BMP_getGreen(bmp, row, col);
           int blue = BMP_getBlue(bmp, row, col);
          
           // Change color or keep it
           if((red + green + blue) == (oldR + oldG + oldB))
           {
               MyBMP_swapPixel(bmp, row, col, newR, newG, newB);
           }
           else
           {
               MyBMP_swapPixel(bmp, row, col, red, green, blue);
           }
       }
}

void MyBMP_drawPixel(BMP *bmp, int x, int y, int r, int g, int b)
{
   BMP_setRed(bmp, y, x, r);
   BMP_setGreen(bmp, y, x, g);
   BMP_setBlue(bmp, y, x, b);
}

void MyBMP_drawLine(BMP *bmp, int x1, int y1, int x2, int y2, int r, int g, int b)
{
   int dy = y2 - y1;
   int dx = x2 - x1;
  
   // Horizontal line
   if (0 == dy)
   {
       if (dx < 0)
       {
           MyBMP_drawLine(bmp, x2, y2, x1, y1, r, g, b);
           return;
       }
       for (int x = x1; x <= x2; x++)
           MyBMP_drawPixel(bmp, x, y1, r, g, b);
       return;
   }
  
   // Vertical Line
   if (0 == dx)
   {
       if (dy < 0)
       {
           MyBMP_drawLine(bmp, x2, y2, x1, y1, r, g, b);
           return;
       }
       for (int y = y1; y <= y2; y++)
           MyBMP_drawPixel(bmp, x1, y, r, g, b);
       return;
   }

   // Make first coordinate leftmost
   if (dx < 0)
   {
       MyBMP_drawLine(bmp, x2, y2, x1, y1, r, g, b);
       return;
   }
  
   // Draw lines in first octant
   if ( (dy > 0) && (dy <= dx) )
   {
       int a = 0;
       int b = 0;
       int bdx_ady = 0;
      
       while ( (a < dx) && (b < dy) )
       {
           if(bdx_ady > 0)
           {
               a++;
               bdx_ady -= dy;
           }
           else
           {
               b++;
               bdx_ady += dx;
           }

           MyBMP_drawPixel(bmp, x1 + a, y1 + b, r, g, b);
       }
   }
}

void MyBMP_drawBox(BMP *bmp, int x1, int y1, int x2, int y2, int r, int g, int b, int filled)
{
   MyBMP_drawLine(bmp, x1, y2, x2, y2, r, g, b);
   MyBMP_drawLine(bmp, x1, y1, x2, y1, r, g, b);
   MyBMP_drawLine(bmp, x1, y1, x1, y2, r, g, b);
   MyBMP_drawLine(bmp, x2, y1, x2, y2, r, g, b);
  
   int dy = y2 - y1;
  
   if (filled == 1)
   {
       for(int y = 0; y < dy; y ++)
       {
           MyBMP_drawLine(bmp, x1, y1 + y, x2, y1 + y, r, g, b);
       }
   }
}

void MyBMP_drawCircle(BMP *bmp, int x1, int y1, int radius, int r, int g, int b, int filled)
{
   int x2 = 0;
   int y2 = 0;
   int count = 0;
   double x = 1;
   double y = 1;
  
   while( x != 0)
   {
      
       x = sqrt((radius*radius) - (y2*y2));
       MyBMP_drawPixel(bmp, x1 + x, y1 - y2, r, g, b);
       MyBMP_drawPixel(bmp, x1 - x, y1 - y2, r, g, b);
       MyBMP_drawPixel(bmp, x1 - x, y1 + y2, r, g, b);
       MyBMP_drawPixel(bmp, x1 + x, y1 + y2, r, g, b);
      
       y = sqrt((radius*radius) - (x2*x2));
       MyBMP_drawPixel(bmp, x1 - x2, y1 + y, r, g, b);
       MyBMP_drawPixel(bmp, x1 - x2, y1 - y, r, g, b);
       MyBMP_drawPixel(bmp, x1 + x2, y1 + y, r, g, b);
       MyBMP_drawPixel(bmp, x1 + x2, y1 - y, r, g, b);
      
       if (filled == 1)
       {
           MyBMP_drawPixel(bmp, x1, y1, r, g, b);
          
           for(int dy = 0; dy < x; dy ++)
           {
               // x displacment
               MyBMP_drawPixel(bmp, x1 + x - dy, y1 - y2, r, g, b);
               MyBMP_drawPixel(bmp, x1 - x + dy, y1 - y2, r, g, b);
               MyBMP_drawPixel(bmp, x1 - x + dy, y1 + y2, r, g, b);
               MyBMP_drawPixel(bmp, x1 + x - dy, y1 + y2, r, g, b);
          
               // y displacment
               MyBMP_drawPixel(bmp, x1 - x2, y1 + y - dy, r, g, b);
               MyBMP_drawPixel(bmp, x1 - x2, y1 - y + dy, r, g, b);
               MyBMP_drawPixel(bmp, x1 + x2, y1 + y - dy, r, g, b);
               MyBMP_drawPixel(bmp, x1 + x2, y1 - y + dy, r, g, b);
           }
       }
      
       x2 += 1;
       y2 += 1;
   }
  
}

void MyBMP_drawBorder(BMP *bmp, int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
{
   for(int y = 0; y < thickness; y ++)
       {
           // bottom
           MyBMP_drawLine(bmp, x1, y1 + y, x2, y1 + y, r, g, b);
          
          
           MyBMP_drawLine(bmp, 319 - y, 0, 319 - y, 239, r, g, b);
          
           MyBMP_drawLine(bmp, 0, 239 - y, 319, 239 - y, r, g, b);
          
           MyBMP_drawLine(bmp, 0 + y, 0, 0 + y, 239, r, g, b);
       }
}

void MyBMP_paste(BMP *target, BMP *source, int rowOffset, int colOffset)
{
   int width = BMP_getWidth(source);
   int height = BMP_getHeight(source);

   // Copy the original image into the bottom half of the output image
   for (int row = 0; row < height; row++)
       for (int col = 0; col < width; col++)
       {
           // Copy each color from original to copy
           BMP_setRed(target, row + rowOffset, col + colOffset, BMP_getRed(source, row, col));
           BMP_setGreen(target, row + rowOffset, col + colOffset, BMP_getGreen(source, row, col));
           BMP_setBlue(target, row + rowOffset, col + colOffset, BMP_getBlue(source, row, col));
       }

}

int myBMP_getGray(BMP *bmp, int row, int col)
{
   int gray = 0;
  
   gray += BMP_getRed(bmp, row, col);
   gray += BMP_getGreen(bmp, row, col);
   gray += BMP_getBlue(bmp, row, col);
   gray /= 3;
  
   return gray;
}

void myBMP_setGray(BMP *bmp, int row, int col, int gray)
{
   BMP_setRed(bmp, row, col, gray);
   BMP_setGreen(bmp, row, col, gray);
   BMP_setBlue(bmp, row, col, gray);
}

void MyBMP_convertToGrayscale(BMP *bmp)
{
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);

   // Convert pixel data from color to grayscale
   for (int row = 0; row < height; row++)
       for (int col = 0; col < width; col++)
           myBMP_setGray(bmp, row, col, myBMP_getGray(bmp, row, col));
}

void MyBMP_dither(MyBMP *bmp);
{
------ not sure where to start
}

double MyBMP_avgR(MyBMP *bmp)
{
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   double color = 0;
   int count = 0;

   // Convert pixel data from color to grayscale
   for (int row = 0; row < height; row++)
   {   for (int col = 0; col < width; col++)
       {
           color += BMP_getRed(bmp, row, col);
           count += 1;
       }
   }
  
   color /= count;
  
   return color;
}

double MyBMP_avgG(MyBMP *bmp)
{
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   double color = 0;
   int count = 0;

   // Convert pixel data from color to grayscale
   for (int row = 0; row < height; row++)
   {   for (int col = 0; col < width; col++)
       {
           color += BMP_getGreen(bmp, row, col);
           count += 1;
       }
   }
  
   color /= count;
  
   return color;
}

double MyBMP_avgB(MyBMP *bmp)
{
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   double color = 0;
   int count = 0;

   // Convert pixel data from color to grayscale
   for (int row = 0; row < height; row++)
   {   for (int col = 0; col < width; col++)
       {
           color += BMP_getBlue(bmp, row, col);
           count += 1;
       }
   }
  
   color /= count;
  
   return color;
}

double MyBMP_avgGray(MyBMP *bmp)
{
   int width = BMP_getWidth(bmp);
   int height = BMP_getHeight(bmp);
  
   double color = 0;
   int count = 0;

   // Convert pixel data from color to grayscale
   for (int row = 0; row < height; row++)
   {   for (int col = 0; col < width; col++)
       {
           color = myBMP_getGray(bmp, row, col);
           count += 1;
       }
   }
  
   color /= count;
  
   return color;
}

Explanation / Answer

void mybmp dither(mybmp *bmp)

{

int a,b;

BMP obj;

unsigned char* Datas;

int in=0;

unsigned char c=0;

File *fp;

fp=fopen(bmp,"rb");

if(!fp){

printf("error : unable to open file");

exit(0);

}

fread(&obj,sizeof(obj),1,fp);

if(onj.BitsPerPixel!=4)

{

fclose(fp);

printf("error : file format not suppoort");

exit(0);

}

fseek(fp,objoffset,seek_set);

for(b=obj.Height;b>0;b--)

{fread(Datas,sizeof(unsigned char),obj.width/2,fp);

c=0;in=0;

for(a=0;a<obj.Width;a+=2)

{

c=(Datas[in]|0)>>4;

putpixel(a,b,c);

c=(Datas[in];

putpixel(a+1,b+1,c);

in++;

}

}

free(datas);

fclose(fp)

return 1;

}

}