T
T
tj572018-10-07 23:53:13
C++ / C#
tj57, 2018-10-07 23:53:13

How to properly parse a .pgm file in C++?

There is a task - to create an empty file with a .pgm image (just a white background) and draw a black line with arbitrary coordinates. It must be a P5 file containing bits, not just numbers. The task was implemented using the P2 format. How to handle file with P5 extension? And is it possible to use streams in the same way? For some reason, the teacher strongly suggests using fprintf. Here is the code:

#include "stdafx.h"

#include <iostream> 

#include <iomanip>

#include <fstream> 

#include <sstream>

using namespace std;

void drawLine(int** array, int x1, int y1, int x2, int y2) {
  const int deltaX = abs(x2 - x1);
  const int deltaY = abs(y2 - y1);
  const int signX = x1 < x2 ? 1 : -1;
  const int signY = y1 < y2 ? 1 : -1;
  //
  int error = deltaX - deltaY;
  //
  array[x2][y2] = 0;
  
  while (x1 != x2 || y1 != y2)
  {
    array[x1][y1] = 0;
    const int error2 = error * 2;
    //
    if (error2 > -deltaY)
    {
      error -= deltaY;
      x1 += signX;
    }
    if (error2 < deltaX)
    {
      error += deltaX;
      y1 += signY;
    }
  }

}


int main(void)
{
  ofstream fout;
  
  fout.open("E:\\line.pgm");

  fout.write("", 0);

  fout << "P2" << "\n" ;

  int numcols, numrows, x1, x2, y1, y2;

  cout << "Enter rows and columns number: ";

  cin >> numrows >> numcols;

  cout << "Enter coords x1 and y1: ";

  cin >> x1 >> y1;

  cout << "Enter coords x2 and y2: ";

  cin >> x2 >> y2;

  fout << numrows << " " << numcols << "\n";

  fout << 255 << "\n";

  int** array = new int*[numrows];

  for (int count = 0; count < numrows; count++)

    array[count] = new int[numcols];

  for (int i = 0; i < numrows; ++i) {

    for (int j = 0; j < numcols; ++j) {
       array[i][j] = 255;
    }

  }

  drawLine(array, x1, y1, x2, y2);

  for (int i = 0; i < numrows; ++i) {

    for (int j = 0; j < numcols; ++j) {
      fout << array[i][j] << " ";
    }

  }

  fout.close();

}

This is the output file as a result of work:

P2
100 100
255
255 255 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 25555 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

and so on.
P5 looks like this:

P5
# created by 'xv marcie_bw.tif'
#
#
640 480
255
'†tgbaaaaaaaccbbbaabbdeimqruz~…Њ”њЎ¦ЁЁ¤ ћќќ ¦“Їіґµґіі°Ї®“¦џ—Ћ~qgba^]]_bgmqx{}~Ѓѓ€' Є“Јљ•љџЎќќ—'€|њssy†— “ЋЃnfjmlpruursx‰”ћ¤Ё©«Є©¤џ•Џ‹‹‰…ЃЃЃѓ††‚ЃЂ~|vroigfgggeegimy‡Џ•ќЎў¤ўЈ¤¦¦¤Ўqegioqz™'‰‚{wwwwxxyyyqwqs{wwwwxxyyyqwqs{ “ћ©±ё”»ѕАїїѕјAZHYNONNLZHAѕSS»№·іЇЄўђ€‚ЃЂЂЃѓ…ѓѓ††…ѓ††ѓ‚}}~~~~~}|zz|ѓ'ќ§¬«Є©ЁЈ њ–'ЏЊђ– ™™™љ”ЏЉwtu{‚‰Ќ'–љќќћќћџЎЈ“¶AIMSFFUTTUTRONOPNONLYZBSј№¶·є№¶¶i®ЇЇЇi±°

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey Skobkin, 2015-09-03
@skobkin

Look towards Moodle. It's made for learning, but I'm not sure if it covers all your needs.

E
ETSEED, 2019-09-09
@ETSEED

SEED( Source of education empowerment and development) is introduced an innovative unique software for an educational institutions to manage their institutions and it vanish the gap of communication between management and staff, staff to student/parents, "SEED "provides real time information through the parent portal.

E
e-school ERP Software, 2020-02-19
@eschoolerp

If you are looking for the best School ERP Software than I will Suggest you.
Web-School
E-school ERP
School ERP
Fedena

S
shubhamjain21094, 2020-06-09
@shubhamjain21094

If you are looking for the best School Management Software then I will Suggest you.
Schoollog School Management Software

S
smithwood, 2021-07-28
@smithwood

Share great information about your blog , Blog really helpful for us . We read your blog , share the most useful information in blog . Thanks for sharing your blog here. With the aid of 60+ excellent module, School Management Software covers every aspect that you need to effectively manage all of the operations of the School.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question