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

File IO functions in C I have a questions like so: The read image and write imag

ID: 3561368 • Letter: F

Question

File IO functions in C

I have a questions like so:

The read image and write image functions specied in Lad1.h should be implemented so

that they can read in either a grayscale or colour image in binary format.The read image function should remove the header and create an array storing the pixel data only. Code that calls read image should be able to access this newly created array. The write image function should reinstate the header so that a standard viewer can be used to view the output image.

Below are the prototypes of the functions that can be found in the header le named

lab1.h.

The tags @param

and

@return

in the comments are there to document the method's parameters

and return value. Good code documentation is discussed in Section 9.

The only input parameter needed to read an image le is the le name. The output parameters

are the one dimensional array for the pixel data, the image width and the image height.

/**

* 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);

To write an image to a le, we need to specify a le name, the width and height of the image,

and the image data array.

/**

* 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);

what i have completed so far on the read function is this

    int read_image(char* file_name, char** image_data, int* width, int* height) {
            FILE* image = fopen(file_name, "rb");
            if (image == NULL){
           return -1;
           }else{
               int max;
               int type;
                   fscanf(image, "P%d %d %d %d ", &type, width, height, &max); // what a pain to get working
                   *image_data = (char*)calloc((*width)*(*height), sizeof(char));
              
                   int i =0;
                   int j =0;
                  

                  
                   for(i =0; i<*height; i++){
                       for(j=0;j<*width;j++){
                           fscanf(image,"%d",&image_data[(i*10)+j]);
                           //printf("%c ",image_data[(i*10)+j]);
                      
                      
                      
                       }
                       //printf(" ");                      
                   }
                  


               //printf("%d", fread(image_data, sizeof(int), 1, image));
                  
                                  
                  
                   fclose(image);
                  
                   //if(type != "P5" && type != "P6"){
                   //   printf("wrong file type");
                   //}
              
               //printf("%d", *width);
           }
    }

but i just cannot get it to read correctly if you could please provide a read and write fuction that will be great and point out were i am messing up.

Explanation / Answer

#include <Lad1.h>
#include <iostream>
using namespace std;
using namespace Lad;
int main(int argc,char **argv)
{
InitializeLad(*argv);

// Construct the image object. Seperating image construction from the
// the read operation ensures that a failure to read the image file
// doesn't render the image object useless.
Image image;
try {
    // Read a file into image object
    image.read( "girl.gif" );

    // Crop the image to specified size (width, height, xOffset, yOffset)
    image.crop( Geometry(100,100, 100, 100) );

    // Write the image to a file
    image.write( "x.gif" );
}
catch( Exception &error_ )
    {
      cout << "Caught exception: " << error_.what() << endl;
      return 1;
    }
return 0;
}

The following is the source to a program which illustrates the use of Lad1's efficient reference-counted assignment and
copy-constructor operations which minimize use of memory and eliminate unncessary copy operations .

#include <Lad1.h>
#include <iostream>
using namespace std;
using namespace Lad;
int main(int argc,char **argv)
{
InitializeLad(*argv);

Image master("horse.jpg");
Image second = master;
second.resize("640x480");
Image third = master;
third.resize("800x600");
second.write("horse640x480.jpg");
third.write("horse800x600.jpg");
return 0;
}

The following is the source for another simple program can read in either a grayscale or colour image in binary format
#include <lad1.h>
using namespace std;
using namespace lad;
int main(int argc,char **argv)
{
Initializelad(*argv);
Image image( "100x100", "white" );
image.pixelColor( 49, 49, "red" );
image.write( "red_pixel.png" );
image.quantizeColorSpace( GRAYColorspace );
image.quantizeColors( 256 );
image.quantize( );
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote