Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

you can read task from the image.i need to help to fix my code. I have two ppm files image 1 and image 2 i

you can read task from the image.i need to help to fix my code. I have two ppm files image1 and image2 i need to merge these files and image2 should be shown top right corner of the image1. i am pasting my code. In my code mergeImage.ppm created but only display image1 and nothing show image2 on the right corner of image1. you can use any image and show me the output ppm image please.i need to see output and fix my code please ,I worked on it for last 5 hours
#include
#include
#include
#include
#include
#include
#define BUFFSIZE 1024
// Function to read and write PPM files using system calls
void merge_ppm(const char *file1, const char *file2, const char *outfile){
int fd1, fd2, outfd;
ssize_t bytes_read, bytes_written;
char buf[BUFFSIZE];
// Open input files
fd1= open(file1, O_RDONLY);
if (fd1==-1){
perror("Error opening first file");
exit(1);
}
fd2= open(file2, O_RDONLY);
if (fd2==-1){
perror("Error opening second file");
exit(1);
}
// Open output file
outfd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (outfd ==-1){
perror("Error opening output file");
exit(1);
}
// Read and write the first PPM file
while ((bytes_read = read(fd1, buf, BUFFSIZE))>0){
bytes_written = write(outfd, buf, bytes_read);
if (bytes_written != bytes_read){
perror("Write error");
exit(1);
}
}
// Calculate the position to start writing the second image (top-right corner)
lseek(outfd,0, SEEK_SET); // Move to the beginning of the output file
lseek(outfd,2, SEEK_CUR); // Skip the P6 header
// Read the width and height of the first image
int width1, height1;
read(outfd, buf, BUFFSIZE);
sscanf(buf,"%d %d", &width1, &height1);
// Read the maximal value of color components
read(outfd, buf, BUFFSIZE);
// Move to the position to start writing the second image (top-right corner)
lseek(outfd, width1* height1*3, SEEK_SET);
// Read and write the second PPM file to the top-right corner of the first file
while ((bytes_read = read(fd2, buf, BUFFSIZE))>0){
bytes_written = write(outfd, buf, bytes_read);
if (bytes_written != bytes_read){
perror("Write error");
exit(1);
}
}
// Close all files
close(fd1);
close(fd2);
close(outfd);
}
// Function to check PPM file dimensions
void check_dimensions(const char *file1, const char *file2){
int fd1, fd2;
char buf[BUFFSIZE];
ssize_t bytes_read;
int width1, height1, width2, height2;
// Open and read first PPM file
fd1= open(file1, O_RDONLY);
if (fd1==-1){
perror("Error opening first file");
exit(1);
}
bytes_read = read(fd1, buf, BUFFSIZE);
close(fd1);
// Parse width and height from the first file
sscanf(buf,"P6
%d %d
%*d
", &width1, &height1);
// Open and read second PPM file
fd2= open(file2, O_RDONLY);
if (fd2==-1){
perror("Error opening second file");
exit(1);
}
bytes_read = read(fd2, buf, BUFFSIZE);
close(fd2);
// Parse width and height from the second file
sscanf(buf,"P6
%d %d
%*d
", &width2, &height2);
// Check dimensions
if (width1 width2|| height1 height2){
fprintf(stderr, "Error: Dimensions of the first image are smaller than the second image.
");
exit(1);
}
}
int main(int argc, char *argv[]){
if (argc !=4){
fprintf(stderr, "Usage: %s
", argv[0]);
exit(1);
}
// Check dimensions of the PPM files
check_dimensions(argv[1], argv[2]);
// Merge the PPM files
merge_ppm(argv[1], argv[2], argv[3]);
printf("Images merged successfully into %s
", argv[3]);
return 0;
}
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

Why aren't permanent magnets really permanent?

Answered: 1 week ago

Question

Prepare for a successful job interview.

Answered: 1 week ago

Question

Describe barriers to effective listening.

Answered: 1 week ago

Question

List the guidelines for effective listening.

Answered: 1 week ago