Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have also attached the code that that should be improved upon. thank you. to be done in c UPDATED: EXISTING CODE IN TXT #include

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

i have also attached the code that that should be improved upon. thank you. to be done in c

UPDATED: EXISTING CODE IN TXT

#include

#include

typedef unsigned char byte;

typedef unsigned short WORD;

typedef unsigned int DWORD;

typedef unsigned int LONG;

struct tagBITMAPFILEHEADER

{

WORD bfType; // specifies the file type

DWORD bfSize; // specifies the size in bytes of the bitmap file

WORD bfReserved1; // reserved; must be 0

WORD bfReserved2; // reserved; must be 0

DWORD bfOffBits; // specifies the offset in bytes from the bitmapfileheader to the bitmap bits

};

struct tagBITMAPINFOHEADER

{

DWORD biSize; // specifies the number of bytes required by the struct

LONG biWidth; // specifies width in pixels

LONG biHeight; // specifies height in pixels

WORD biPlanes; // specifies the number of color planes, must be 1

WORD biBitCount; // specifies the number of bit per pixel

DWORD biCompression; // specifies the type of compression

DWORD biSizeImage; // size of image in bytes

LONG biXPelsPerMeter; // number of pixels per meter in x axis

LONG biYPelsPerMeter; // number of pixels per meter in y axis

DWORD biClrUsed; // number of colors used by the bitmap

DWORD biClrImportant; // number of colors that are important

};

int main(int argc, char *argv[])

{

//float ratio = *argv[2];

FILE *image1 = fopen("lion.bmp", "rb");

FILE *image2 = fopen("wolf.bmp", "rb");

struct tagBITMAPFILEHEADER image1FileHeader;

struct tagBITMAPINFOHEADER image1InfoHeader;

struct tagBITMAPFILEHEADER image2FileHeader;

struct tagBITMAPINFOHEADER image2InfoHeader;

if(image1 == NULL)

{

printf("Error opening image 1 ");

return 0;#include

#include

typedef unsigned char byte;

typedef unsigned short WORD;

typedef unsigned int DWORD;

typedef unsigned int LONG;

struct tagBITMAPFILEHEADER

{

WORD bfType; // specifies the file type

DWORD bfSize; // specifies the size in bytes of the bitmap file

WORD bfReserved1; // reserved; must be 0

WORD bfReserved2; // reserved; must be 0

DWORD bfOffBits; // specifies the offset in bytes from the bitmapfileheader to the bitmap bits

};

struct tagBITMAPINFOHEADER

{

DWORD biSize; // specifies the number of bytes required by the struct

LONG biWidth; // specifies width in pixels

LONG biHeight; // specifies height in pixels

WORD biPlanes; // specifies the number of color planes, must be 1

WORD biBitCount; // specifies the number of bit per pixel

DWORD biCompression; // specifies the type of compression

DWORD biSizeImage; // size of image in bytes

LONG biXPelsPerMeter; // number of pixels per meter in x axis

LONG biYPelsPerMeter; // number of pixels per meter in y axis

DWORD biClrUsed; // number of colors used by the bitmap

DWORD biClrImportant; // number of colors that are important

};

int main(int argc, char *argv[])

{

//float ratio = *argv[2];

FILE *image1 = fopen("lion.bmp", "rb");

FILE *image2 = fopen("wolf.bmp", "rb");

struct tagBITMAPFILEHEADER image1FileHeader;

struct tagBITMAPINFOHEADER image1InfoHeader;

struct tagBITMAPFILEHEADER image2FileHeader;

struct tagBITMAPINFOHEADER image2InfoHeader;

if(image1 == NULL)

{

printf("Error opening image 1 ");

return 0;

}

if(image2 == NULL)

{

printf("Error opening image 2 ");

return 0;

}

// reading in image 1 file header

fread( &(image1FileHeader.bfType), sizeof(WORD), 1, image1);

fread( &(image1FileHeader.bfSize), sizeof(DWORD), 1, image1);

fread( &(image1FileHeader.bfReserved1), sizeof(WORD), 1, image1);

fread( &(image1FileHeader.bfReserved2), sizeof(WORD), 1, image1);

fread( &(image1FileHeader.bfOffBits), sizeof(DWORD), 1, image1);

// reading in image 1 info header

fread( &(image1InfoHeader.biSize), sizeof(DWORD), 1, image1);

fread( &(image1InfoHeader.biWidth), sizeof(LONG), 1, image1);

fread( &(image1InfoHeader.biHeight), sizeof(LONG), 1, image1);

fread( &(image1InfoHeader.biPlanes), sizeof(WORD), 1, image1);

fread( &(image1InfoHeader.biBitCount), sizeof(WORD), 1, image1);

fread( &(image1InfoHeader.biCompression), sizeof(DWORD), 1, image1);

fread( &(image1InfoHeader.biSizeImage), sizeof(DWORD), 1, image1);

fread( &(image1InfoHeader.biXPelsPerMeter), sizeof(LONG), 1, image1);

fread( &(image1InfoHeader.biYPelsPerMeter), sizeof(LONG), 1, image1);

fread( &(image1InfoHeader.biClrUsed), sizeof(DWORD), 1, image1);

fread( &(image1InfoHeader.biClrImportant), sizeof(DWORD), 1, image1);

int image1Bytes = ((image1InfoHeader.biWidth) * 3) * (image1InfoHeader.biHeight);

byte* image1Color = (byte*)malloc(image1Bytes);

byte temp;

for(int i = 0; i

{

fread( &temp, sizeof(byte), 1, image1);

image1Color[i] = temp;

}

fclose(image1);

byte* resultImage = (byte*)malloc(resultBytes);

// write to a new BMP file for result image

FILE *resultImageFile = fopen("merged.bmp", "wb");

// writing image 1 file header to result file

fwrite( &(image1FileHeader.bfType), sizeof(WORD), 1, resultImageFile);

fwrite( &(image1FileHeader.bfSize), sizeof(DWORD), 1, resultImageFile);

fwrite( &(image1FileHeader.bfReserved1), sizeof(WORD), 1, resultImageFile);

fwrite( &(image1FileHeader.bfReserved2), sizeof(WORD), 1, resultImageFile);

fwrite( &(image1FileHeader.bfOffBits), sizeof(DWORD), 1, resultImageFile);

// writing image 1 info header to result file

fwrite( &(image1InfoHeader.biSize), sizeof(DWORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biWidth), sizeof(LONG), 1, resultImageFile);

fwrite( &(image1InfoHeader.biHeight), sizeof(LONG), 1, resultImageFile);

fwrite( &(image1InfoHeader.biPlanes), sizeof(WORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biBitCount), sizeof(WORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biCompression), sizeof(DWORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biSizeImage), sizeof(DWORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biXPelsPerMeter), sizeof(LONG), 1, resultImageFile);

fwrite( &(image1InfoHeader.biYPelsPerMeter), sizeof(LONG), 1, resultImageFile);

fwrite( &(image1InfoHeader.biClrUsed), sizeof(DWORD), 1, resultImageFile);

fwrite( &(image1InfoHeader.biClrImportant), sizeof(DWORD), 1, resultImageFile);

// writing image 1 to result file

byte b1;

for(int i = 0; i

{

b1 = *(image1Color + i);

fwrite( &b1, sizeof(byte), 1, resultImageFile);

}

fclose(resultImageFile);

Exercise: Lets speed up working on a file: Read ar bitmap file and change its brightness. However, do this in 2 processes to speed up the CPU worktime. Measure the time with and without forko. Howto: Similar to program 1. read a bitmap (BMP) file into the memory. Allocate your memory with mmapl) and don't forget to set the shared memory flag Fork the process with fork(). Check which process you are. Are you the parent process? Work on the upper half of all povels Child? Work on the lower half. Having an odd number of rows? Catch that problem! Measure the time with and without fork and print the result time! Program call: /yourprogram (IMAGEFILE) (BRIGHTNESS) (PARALLEL (OUTPUTRLE] [IMAGEFILE] that's your bitmap (BRIGHTNESS) a number between 0 and 1 [PARALLEL O for working without fork() and 1 with fork) OUTPUTRILE te output file For the brightness: Every color must add BRIGHTNESS 255, but make sure not to exceed 2551 How we test Very simple: We run your code with and without fork(), check the time gained and check the output image. Submission: Submit the source code file, the executable and the bitmap zipped to: fork.zip help please. i think i know how to read in the blp file but help would be much appreciated. thank you. to be done in c "sorry bmp file Cabac > 1 #include 2 #include 3 4 5 typedef unsigned char byte; 6 7 typedef unsigned short WORD; 8 typedef unsigned int DWORD; 9 typedef unsigned int LONG 10 11 struct tagBITMAPFILEHEADER 12 13 WORD bfType: // specifies the file type 14 DWORD bfsize: 1/ specifies the size in bytes of the bitmap file 15 WORD bfReservedl; // reserved; must be 16 WORD bfReserved2; // reserved; must be a 17 DWORD btoffbits: 1/ specifies the offset in bytes from the bitmapfileheader to the bitmap bits 18 19 20 struct tagBITMAPINFOHEADER 22 22 CHORD bisize; // specifies the number of bytes required by the struct 23 LONG biwidth: // specifies width in pixels 24 LONG biHeight: 1/ specifies height in pixels 25 WORD biPlanes; // specifies the number of color planes, must be 1 WORD biBitCount; // specifies the number of bit per pixel DWORD bicompression; // specifies the type of compression DWORD bisizeImage; // size of image in bytes LONG bixPelsPerheter; // number of pixels per meter in x axis 30 LONG biyPelsPerMeter: // number of pixels per meter in yaxis 31 SWORLD biclrused; // number of colors used by the bitmap 32 DWORD bichrimportant; // number of colors that are important }; 34 35 36 37 int main(int argc, char *argv[]) 38 f 39 40 //float ratio = argv[2]; 41 FILE imagel - fopen("Lion.bmp", "rb"); FILE "image2 = fopen("wolf.bnp", "rb"); struct tagBITHAPFILEHEADER inagelFileHeader 46 struct tagBITMAPENFOMEADER ImageInfohlender: 47 struct tagBITMAPFILEHEADER Image2FileHeaders 49 struct tagBITMAPINFOHEADER Image2 Info leader 1 (nagel - NULL) 48 printf("Error opening Image IM*); return; 54 56 57 53 if(isage2 - NULL) if(image2 == NULL) printf("Error opening image 2 "); return 0; } // reading in Image 1 file header fread( 6(image.FileHeader.bfType), sizeof(WORD), 1, 1nage1): fread &(imageFilelteader.bfsize), sizeof(DWORD), 1, image1): fread(&(imageFileHeader.bfReservedi), sizeof(WORD), 1, Image1): fread( &(imagelFileHeader.bfReserved2), sizeof(WORD), 1, Image1): fread( 6(imagelFileHeader.bfoffbits), sizeof(DWORD), 1, image1): 11 reading in image 1 info header fread( &(image1Info Header.bisize), sizeof(DWORD), 1. image1): fread( 6(image InfoHeader.biwidth), sizeof(LONG), 1, Imagel); fread( &(image InfoHeader.biHeight), sizeof(LONG), 1. imagel); fread( &(image InfoHeader.biplanes), sizeof(WORD). 1, Imagel): fread( 6(image1Infohleader.bibitCount), sizeof(WORD), 1. image1): fread( 6(imageRInfo leader. bicompression), sizeof(DWORD). 1. image1): fread( &(image InfoHeader.biSize Image), sizeof(DWORD), 1. image1): fread( &(image InfoHeader.bixPelsPerfeter), sizeof(LONG), 1. imagel); fread( &(image1InfoHeader.biyPelsPerMeter), sizeof(LONG), 1, image1): fread( &(imagel InfoHeader.biclrUsed), sizeof (DWORD). 1, Image1): fread( 6(image1Info leader.biclrImportant), sizeof(DWORD). 1, image1): 1/ since dont know what size byte array should be at compile time, need to dynamically allocate memory - a byte pointer 11 for non divisible by 4 width, then do a different allocation. 1/ sizeof returns the size of the variable so here its 4 bc its an integer int inageBytes = ((image1Infoleader.biwidth) . 3) . (image lInfoHeader.blHeight); byte Image color = (byte)malloc(image1Bytes); byte temps for(int i = 0; i

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

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions