#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include "proto.h"

void SavePNMImage(Image *, char *);
Image *SwapImage(Image *);
Image *ReadPNMImage(char *);
Image *CreateNewImage(Image *, char *comment);
int TestReadImage(char *, char *);

int main(int argc, char **argv)
{
TestReadImage(argv[1],argv[2]);

return(0);
}

int TestReadImage(char *filename, char *outfilename)
{
Image *image;
Image *outimage;

image=ReadPNMImage(filename);
outimage=SwapImage(image);
SavePNMImage(outimage, outfilename);

return(0);
}

Image *SwapImage(Image *image)
{
unsigned char *tempin, *tempout;
int i, size;
Image *outimage;

outimage=CreateNewImage(image,"#testing Swap");
tempin=image->data;
tempout=outimage->data;

if(image->Type==GRAY) size = image->Width * image->Height;
else if(image->Type==COLOR) size = image->Width * image->Height * 3;

for(i=0;i<size;i++)
{
*tempout=*tempin;
tempin++;
tempout++;
}
return(outimage);
}

/*******************************************************************************/
//Read PPM image and return an image pointer   
**************************************************************************/
Image *ReadPNMImage(char *filename)
{
char ch;
int maxval, Width, Height;
int size, num,j;
FILE *fp;
Image *image;
int num_comment_lines=0;


image=(Image *)malloc(sizeof(Image));

if((fp=fopen(filename,"r"))==NULL){
printf("Cannot open %s\n", filename);
exit(0);
}

printf("Loading %s ...",filename);

if (fscanf(fp, "P%c\n", &ch) != 1) {
printf("File is not in ppm/pgm raw format; cannot read\n");
exit(0);
}
if( ch != '6' && ch !='5') {
printf("File is not in ppm/pgm raw format; cannot read\n");
exit(0);
}

if(ch == '5')image->Type=GRAY; // Gray (pgm)
else if(ch=='6')image->Type=COLOR; //Color (ppm)
/* skip comments */
ch = getc(fp);
j=0;
while (ch == '#')
{
image->comments[num_comment_lines][j]=ch;
j++;
do {
ch = getc(fp);
image->comments[num_comment_lines][j]=ch;
j++;
} while (ch != '\n'); /* read to the end of the line */
image->comments[num_comment_lines][j-1]='\0';
j=0;
num_comment_lines++;
ch = getc(fp); /* thanks, Elliot */
}

if (!isdigit((int)ch)){
printf("Cannot read header information from ppm file");
exit(0);
}

ungetc(ch, fp); /* put that digit back */

/* read the width, height, and maximum value for a pixel */
fscanf(fp, "%d%d%d\n", &Width, &Height, &maxval);

/*
if (maxval != 255){
printf("image is not true-color (24 bit); read failed");
exit(0);
}
*/

if(image->Type==GRAY)
size = Width * Height;
else if(image->Type==COLOR)
size = Width * Height *3;
image->data = (unsigned char *) malloc(size);
image->Width = Width;
image->Height = Height;
image->num_comment_lines= num_comment_lines;

if (!image->data){
printf("cannot allocate memory for new image");
exit(0);
}

num = fread((void *) image->data, 1, (size_t) size, fp);
//printf("Complete reading of %d bytes \n", num);
if (num != size){
printf("cannot read image data from file");
exit(0);
}

//for(j=0;j<image->num_comment_lines;j++){
// printf("%s\n",image->comments[j]);
// }

fclose(fp);

/*----- Debug ------*/

if(image->Type==GRAY)printf("..Image Type PGM\n");
else printf("..Image Type PPM Color\n");
/*
printf("Width %d\n", Width);
printf("Height %d\n",Height);
printf("Size of image %d bytes\n",size);
printf("maxvalue %d\n", maxval);
*/
return(image);
}

void SavePNMImage(Image *temp_image, char *filename)
{
int num,j;
int size ;
FILE *fp;
//char comment[100];


printf("Saving Image %s\n", filename);
fp=fopen(filename, "w");
if (!fp){
printf("cannot open file for writing");
exit(0);
}

//strcpy(comment,"#Created by Dr Mohamed N. Ahmed");

if(temp_image->Type==GRAY){ // Gray (pgm)
fprintf(fp,"P5\n");
size = temp_image->Width * temp_image->Height;
}
else if(temp_image->Type==COLOR){ // Color (ppm)
fprintf(fp,"P6\n");
size = temp_image->Width * temp_image->Height*3;
}

for(j=0;j<temp_image->num_comment_lines;j++)
fprintf(fp,"%s\n",temp_image->comments[j]);

fprintf(fp, "%d %d\n%d\n", temp_image->Width, temp_image->Height, 255);

num = fwrite((void *) temp_image->data, 1, (size_t) size, fp);

if (num != size){
printf("cannot write image data to file");
exit(0);
}

fclose(fp);
}

/*************************************************************************/
/*Create a New Image with same dimensions as input image */
/*************************************************************************/

Image *CreateNewImage(Image * image, char *comment)
{
Image *outimage;
int size,j;

outimage=(Image *)malloc(sizeof(Image));

outimage->Type =image->Type;
if(outimage->Type==GRAY) size = image->Width * image->Height;
else if(outimage->Type==COLOR) size = image->Width * image->Height * 3;

outimage->Width =image->Width;
outimage->Height=image->Height;
outimage->num_comment_lines=image->num_comment_lines;

/*--------------------------------------------------------*/
/* Copy Comments for Original Image */
for(j=0;j<outimage->num_comment_lines;j++)
strcpy(outimage->comments[j],image->comments[j]);

/*----------- Add New Comment ---------------------------*/
strcpy(outimage->comments[outimage->num_comment_lines],comment);
outimage->num_comment_lines++;


outimage->data = (unsigned char *) malloc(size);
if (!outimage->data){
printf("cannot allocate memory for new image");
exit(0);
}
return(outimage);
}

/////////////////////

//proto.h


#define GRAY 1
#define COLOR 2


typedef struct Image
{
int Width;
int Height;
int Type; // Gray=1, Color=2;
unsigned char *data;
char comments[15][100];
int num_comment_lines;
}Image;

 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}