Taking output of pgm file format to construct frequncy table

here I am reading a PGM format I want to construct a frequency table for Huffman compression and decompression in C++ how can I extract the output of the picture to make a frequency table with it? should i convert it to text file and read it again taking in consideration i may use this source code

https://github.com/cynricfu/huffman-coding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
#include <string>
#include <fstream>
#include <sstream> 
using namespace std;

//----------------------------READ IMAGE--------------------------------
void ReadImage(char fname[], int ***fimage, int& M, int& N, int& Q)
{
    int i, j;
    char d;

    char header [100], *ptr;
    ifstream ifp;

    ifp.open(fname, ios::binary);

    if (!ifp)
    {
        cout << "Can't read image: <" << fname << '>' << endl;
        void getch();
        exit(1);
    }

    ifp.getline(header,100,'\n');
    if((header[0]!='P') || header[1]!='5')   /* 'P5' Format */
    {
        cout << "Image <" << fname << "> is not in binary PGM 'P5' format." << endl;
        void getch();
        exit(1);
    }

    ifp.getline(header,100,'\n');
    while(header[0]=='#')
        ifp.getline(header,100,'\n');

    M=strtol(header,&ptr,0);
    N=atoi(ptr);

    ifp.getline(header,100,'\n');

    Q=strtol(header,&ptr,0);

    fimage = new int [N];
    for(i=0; i<N; i++)
        (*fimage)[i] = new int[M];

    for(i=0; i<N; i++)
    {
        for(j=0; j<M; j++)
        {
            d = ifp.get();
            (*fimage)[i][j]= (int)d;
            cout <<j;
        }
        cout <<i;
        }
    }
//-----------------------------Main-------------------------------------
int main()
{
    int i, j;
    int N, M, Q;        // N=Rows   M=Cols   Q=GreyLevels
    int **fimage;       // int **I 2-D Array of Image
    char infile[40];        // name of input file
    char outfile[40];   // name of output image file

    cout << "Enter name of *.pgm INPUT image file: ? ";
    cin  >> infile;
    cout << i<<endl;
    cout <<j<<endl;

    ReadImage(infile, &fimage, M, N, Q);    // Memory created for fimage
}Put the code you need help with here.
> void ReadImage(char fname[], int ***fimage, int& M, int& N, int& Q)
It's odd that you would use references for integers, but go all 'three star' on image pointer, where a reference would have made your life easier.

> how can I extract the output of the picture to make a frequency table with it?
How would you do a frequency table for
 
char data[] = "hello";


How would that be different from
 
int data[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f};




> i may use this source code https://github.com/cynricfu/huffman-coding
Go for it - try it - see what happens.

i'm new to C++ and data structure
can you tell me in more how can i edit that so i can make it fit for a text file
so i can i read it using with the source code ?
i just want to understand the output that i will insert in the input of constructing the table
Last edited on
> i'm new to C++ and data structure
Perhaps you should start by writing your own code for once.

> void ReadImage(char fname[], int ***fimage, int& M, int& N, int& Q)
This code is basically all over the place, so you obviously just googled for it.
https://www.dreamincode.net/forums/topic/129641-write-and-read-an-image-in-c/

> i may use this source code https://github.com/cynricfu/huffman-coding
Again, more google - more copy/paste.

Finding it is easy. The problem is, you have no idea what to do when you've found it.



> can you tell me in more how can i edit that so i can make it fit for a text file
> so i can i read it using with the source code ?
What would be the point of doing that?

If all it's going to do for you is maintain the illusion in your tutor's mind for one more week that you're an able student, then there really is no point in helping at all.

Sooner or later, the question will be so unique that your google-fu will find zero results, and you'll be screwed.

Edit:
Of course it's cross-posted
https://stackoverflow.com/questions/62186289/taking-output-of-pgm-file-format-to-construct-frequncy-table


Last edited on
Topic archived. No new replies allowed.