This is for an intro c++ programming class using basic concepts such as loops, f
ID: 3829412 • Letter: T
Question
This is for an intro c++ programming class using basic concepts such as loops, functions, files input and output for storage to a ppm file (Irfanview), functions and Arrays (three arrays for each color rg,b). Please explain key parts to the code and please do not use advanced concepts such as classes, maps and vectors. The file input must use the data types fstream, ofstream, abd ifstream. Example of my unfinished code is shown below. Creoche a Case to trans an Image semm mage in the PPM fie Chenge the leers of the in Semo cool may otupwr chess ng wn the near)mage inExplanation / Answer
C++ CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct {
unsigned char Red-color,green-color,blue-color;
} PPMpixel;
typedef struct {
int x, y;
PPMpixel *info;
} PPMImage;
#define CREATOR "SUDIPTAARNAB"
#define RGB_COMPONENT_COLOR 255
static PPMImage *InputPPM(const char *fname)
{
char buffer[16];
PPMImage *in_img;
FILE *filePointer;
int c, Rgb_comp_col;
filePointer = fopen(fname, "rb");
if (!filePointer) {
fprintf(stderr, "Unable to open file '%s' ", fname);
exit(1);
}
if (!fgets(buffer, sizeof(buffer), filePointer)) {
perror(fname);
exit(1);
}
if (buffer[0] != 'P' || buffer[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6') ");
exit(1);
}
in_img = (PPMImage *)malloc(sizeof(PPMImage));
if (!in_img) {
fprintf(stderr, "Unable to allocate memory ");
exit(1);
}
c = getc(filePointer);
while (c == '#') {
while (getc(filePointer) != ' ') ;
c = getc(filePointer);
}
ungetc(c, filePointer);
if (fscanf(filePointer, "%d %d", &in_img->x, &in_img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s') ", fname);
exit(1);
}
if (fscanf(filePointer, "%d", &Rgb_comp_col) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s') ", fname);
exit(1);
}
if (Rgb_comp_col!= RGB_COMPONENT_COLOR) {
fprintf(stderr, "'%s' does not have 8-bits components ", fname);
exit(1);
}
while (fgetc(filePointer) != ' ') ;
in_img->info = (PPMpixel*)malloc(in_img->x * in_img->y * sizeof(PPMpixel));
if (!in_img) {
fprintf(stderr, "Unable to allocate memory ");
exit(1);
}
if (fread(in_img->info, 3 * in_img->x, in_img->y, filePointer) != in_img->y) {
fprintf(stderr, "Error loading image '%s' ", fname);
exit(1);
}
fclose(filePointer);
return in_img;
}
void writePPM(const char *fname, PPMImage *in_img)
{
FILE *filePointer;
filePointer = fopen(fname, "wb");
if (!filePointer) {
fprintf(stderr, "Unable to open file '%s' ", fname);
exit(1);
}
fprintf(filePointer, "P6 ");
fprintf(filePointer, "# Created by %s ",CREATOR);
fprintf(filePointer, "%d %d ",in_img->x,in_img->y);
fprintf(filePointer, "%d ",RGB_COMPONENT_COLOR);
fwrite(in_img->info, 3 * in_img->x, in_img->y, filePointer);
fclose(filePointer);
}
void imgDivide(const char *fname,PPMImage *in_img)
{
FILE *filePointer = fopen(fname,"rb");
FILE *fptr;
fptr = fopen ("floatArray.txt","w");
int width = 288;
int height = 352;
int i,j,m,k,l,i1,j1,l1,n1;
int *Sum_in;
float *mean,*Variable_in;
unsigned char buffer[(288*352)];
unsigned char image[288][352];
size_t n = fread( buffer, sizeof(buffer[0]), sizeof(buffer), filePointer );
fclose(filePointer);
for(i =0; i < height; i++)
{
for(j = 0; j < width;j++)
{
image[j][i] = buffer[(i*width)+j];
}
}
unsigned char dividedimage[(288*352)/(8*8)][8][8];
mean=(float *)malloc(sizeof(float)*1584);
Variable_in=(float *)malloc(sizeof(float)*1584);
Sum_in=(int *)malloc(sizeof(int)*1584);
for(i = 0; i < height/8; i++)
{
for(j = 0; j < width/8;j++)
{
for(k = i*8, l = 0; k < (i*8)+8; k++,l++)
{
for(m = j*8, n = 0; m < (j*8)+8; m++,n++)
{
dividedimage[(i*(width/8))][n][l] = image[m][k];
}
}
}
}
printf(" no of grids::%d i=%d j=%d,k=%d,m=%d n=%d",(i*(width/8)),i,j,k,m,n);
for(i1=0;i1<(i*(width/8));i1++)
{
Sum_in[i1]=0;
printf(" printing info of %dth grid:: ",i1+1);
for(n1=0;n1<n;n1++)
{
for(l1=0;l1<n;l1++)
{
Sum_in[i1]=Sum_in[i1]+(int)dividedimage[i1][j1][l1];
}
printf(" ");
}
mean[i1]=Sum_in[i1]/64;
}
for(i1=0;i1<(i*(width/8));i1++)
{
Variable_in[i1]=0;
printf(" printing info of %dth grid:: ",i1+1);
for(n1=0;n1<n;n1++)
{
for(l1=0;l1<n;l1++)
{
printf("%5d",dividedimage[i1][j1][l1]);
Variable_in[i1]=Variable_in[i1]+pow(((int)dividedimage[i1][j1][l1]-mean[i1]),2);
}
printf(" ");
}
Variable_in[i1]=Variable_in[i1]/64;
printf(" Variable_iniance of grid %d is ::%f ",i1+1,Variable_in[i1]);
}
for (i = 0; i < 1584; i++) {
printf(" %f",Variable_in[i]);
fprintf (fptr, "%5f ", Variable_in[i]);
}
Haar_1Dimension(Variable_in,1584);
}
void Haar_1Dimension(float *vec, int n)
{
int i=0;
int w=n;
FILE *fptr;
fptr = fopen ("1dhaarwavelet.txt","w");
float *vecp ;
vecp=(float *)malloc(sizeof(float)*n);
for(i=0;i<n;i++)
vecp[i] = 0;
while(w>1)
{
w/=2;
for(i=0;i<w;i++)
{
vecp[i] = (vec[2*i] + vec[2*i+1])/sqrt(2.0);
vecp[i+w] = (vec[2*i] - vec[2*i+1])/sqrt(2.0);
}
for(i=0;i<(w*2);i++)
vec[i] = vecp[i];
}
printf(" the 1d haarwavelet transform is::");
for (i = 0; i < n; i++) {
printf(" %f",vec[i]);
fprintf (fptr, "%5f ", vec[i]);
}
}
void haar1D(float *vec, int n, int w)
{
int i=0;
float *vecp = (float *)malloc(sizeof(float)*n);
for(i=0;i<n;i++)
vecp[i] = 0;
w/=2;
for(i=0;i<w;i++)
{
vecp[i] = (vec[2*i] + vec[2*i+1])/sqrt(2.0);
vecp[i+w] = (vec[2*i] - vec[2*i+1])/sqrt(2.0);
}
for(i=0;i<(w*2);i++)
vec[i] = vecp[i];
}
void Haar_2Dimension(float **matrix, int Rows, int columns)
{
float *tmp_row = (float *)malloc(sizeof(float)*columns);
float *tmp_col = (float *)malloc(sizeof(float)*Rows);
int i=0,j=0;
int w = columns, h=Rows;
while(w>1 || h>1)
{
if(w>1)
{
for(i=0;i<h;i++)
{
for(j=0;j<columns;j++)
tmp_row[j] = matrix[i][j];
haar1D(tmp_row,columns,w);
for(j=0;j<columns;j++)
matrix[i][j] = tmp_row[j];
}
}
if(h>1)
{
for(i=0;i<w;i++)
{
for(j=0;j<Rows;j++)
tmp_col[j] = matrix[j][i];
haar1D(tmp_col, Rows, h);
for(j=0;j<Rows;j++)
matrix[j][i] = tmp_col[j];
}
}
if(w>1)
w/=2;
if(h>1)
h/=2;
}
}
int main(){
char filein[100],fileout[100];
PPMImage *image1,*image2;
int m,i,j;
printf(" Enter the input file name::");
gets(filein);
image1 = InputPPM(filein);
imgDivide(filein,image1);
float **mat = (float **)malloc(sizeof(float*)*4);
for(m=0;m<4;m++)
mat[m] = (float *)malloc(sizeof(float)*4);
mat[0][0] = 5; mat[0][1] = 6; mat[0][2] = 1; mat[0][3] = 2;
mat[1][0] = 4; mat[1][1] = 2; mat[1][2] = 5; mat[1][3] = 5;
mat[2][0] = 3; mat[2][1] = 1; mat[2][2] = 7; mat[2][3] = 1;
mat[3][0] = 6; mat[3][1] = 3; mat[3][2] = 5; mat[3][3] = 1;
Haar_2Dimension(mat,4,4);
printf(" after 2d haarwavelet:: ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(" %f ",mat[i][j]);
}
printf(" ");
}
printf("Press any key...");
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.