Write several functions in c /** * This function is responsible for reading an i
ID: 3561223 • Letter: W
Question
Write several functions in c
/**
* This function is responsible for reading an image file and placing the pixel data
* from the file into the array image_data
* The function automatically determines whether the image data is gray scale or
* rgb and sizes image_data accordingly
*
* @param char* file_name - The name of the file to be opened
* @param char** image_data - A pointer to an array of pixel data
* @param int* width - The width of the read image in pixels
* @param int* height - The height of the read image in pixels
*
* @return int - The size of the allocated image_data array, otherwise, -1
*/
int read_image(char* file_name, char** image_data, int* width, int* height);
/**
* This function is responsible for writing image data to a file
* The function automatically determines whether the image data is gray scale or rgb
*
* @param char* file_name - The name of the file to be created
* @param int width - The width of the image in pixels
* @param int height - The height of the image in pixels
* @param char* image_data - The image data array to be written to file
* @param int data_length - The length of the image data array
*
* @return int - the numeric value 0 on success,
* -1 if the image file could not be opened for writing
*/
int write_image(char* file_name, int width, int height, char* image_data, int data_length);
/**
* This function is responsible for embedding a message into an array of
* image data and placing the resulting array into encoded
*
* @param char* image_data - The image data array which will be used
* to embed the message
6* @param int data_length - The length of the image data array
* @param char* message - The message to be encoded
* @param char** encoded - A new array containing the image data
* embedded with message
*
* @return int - the numeric value 0 on success,
* -1 if the message is too large for the image data array
*/
int embed(char* image_data, int data_length, char* message, char** encoded);
/**
* This function is responsible for extracting the potential message
* from the supplied image data
*
* @param char* image_data - The image data array from which we extract the message
* @param int data_length - The length of the image data array
* @param char** message - The extracted message array
*
* @return int - the numeric value 0 on success
*/
int extract(char* image_data, int data_length, char** message);
/**
* This function is responsible for rotating each alphabetic character in the message
* k times to hide the meaning of the message
*
* @param char* message - The message which the rotation will be applied to
* (null terminated string)
*
* @param int k - The number of rotations to be applied
*
* @return int - A count of the number of characters modified
*/
int rot_k(char* message, int k);
/**
* This function attempts to look for a rotated message by brute-force
* until a known word is found or the word could not be detected
*
* @param char* encoded - The potentially rotated message
* @param char* known_word - The word that will be searched for while rotating
* the encoded message
* @param char** message - A new array containing the detected message
*
* @return int - the k value needed to remove the rotation on success,
* -1 if no message was detected
*/
int crack(const char* encoded, char* known_word, char** message);
Explanation / Answer
01 #include <stdio.h>
02 #include <string.h>
03
04 typedef struct {
05 unsigned int width;
06 unsigned int height;
07 unsigned int planes;
08 unsigned short bitcount;
09 unsigned int size;
10 } BITMAPINFOHEADER;
11
12 typedef struct {
13 unsigned char blue;
14 unsigned char green;
15 unsigned char red;
16 } PIXEL;
17
18
19 int main()
20 {
21 FILE *image,*test;
22 char fpath[1000],tpath[1000];
23 BITMAPINFOHEADER bih;
24 int i=0,b[8],g[8],r[8];
25
26
27 printf("Enter BMP file path:");
28 scanf("%s",fpath);
29
30 image=fopen(fpath,"rb+");
31
32 while(image==NULL)
33 {
34 printf("Error! Enter path again:");
35 scanf("%s",fpath);
36 image=fopen(fpath,"rb+");
37 }
38
39 fseek(image,2,SEEK_SET);
40 fread(&bih.size,4,1,image);
41 printf("Size=%d ",bih.size);
42 fseek(image,18,SEEK_SET);
43 fread(&bih.width,4,1,image);
44 fseek(image,22,SEEK_SET);
45 fread(&bih.height,4,1,image);
46 printf("Width=%d Height=%d ",bih.width,bih.height);
47 fseek(image,26,SEEK_SET);
48 fread(&bih.planes,2,1,image);
49 printf("Number of planes:%d ",bih.planes);
50 fseek(image,28,SEEK_SET);
51 fread(&bih.bitcount,2,1,image);
52 printf("Bit Count:%d ",bih.bitcount);
53
54 PIXEL pic[bih.width*bih.height*2],p;
55
56 printf("Enter path of test file:");
57 scanf("%s",tpath);
58
59 test=fopen(tpath,"wb+");
60
61 fseek(image,54,SEEK_SET);
62
63 /*for(i=0;i<bih.width*bih.height;i++)*/while(!feof(image))
64 {
65 //fread(&p,sizeof(p),1,image);
66 fread(&p.blue,sizeof(p.blue),1,image);
67 fread(&p.green,sizeof(p.green),1,image);
68 fread(&p.red,sizeof(p.red),1,image);
69 pic[i]=p;
70 printf(" %d= %d %d %d ",i+54,pic[i].blue,pic[i].green,pic[i].red);
71 fprintf(test, "%d= %d %d %d ",i+54,pic[i].blue,pic[i].green,pic[i].red);
72 i++;
73 }
74 fclose(image);
75 fclose(test);
76
77 printf(" ");
78
79 return 0;
80 }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.