-1

I am trying to find a way to pass definitions onto a header file for using it when formatting an array. My code is something like this:

#define CONSTANT 10
#include "myHeaderFile.h"    
int main(headerFunction(array)){}

Header file:

int headerFunction(array[][CONSTANT]) { // Multidimensional array
}

It gives me an error that CONSTANT hasn't been set. To be more exact: error: ‘CONSTANT’ undeclared here (not in a function)

All the code:

#include <stdio.h>
#define W 946
#define H 528
#include "myFunctions.h"


// Video resolution




// Allocate a buffer to store one frame
unsigned char frame[H][W][3] = {0};




int main(void)
{
    int x, y, count;
     
    // Open an input pipe from ffmpeg and an output pipe to a second instance of ffmpeg
    FILE *pipein = popen("ffmpeg -i 1.mp4 -f image2pipe -c:v rawvideo -pix_fmt rgb24 - ", "r"); // ffmpeg -i 1.mp4 -f image2pipe -vcodec rawvideo -pix_fmt rgb24 -       
    FILE *pipeout = popen("ffmpeg -y -f rawvideo -vcodec rawvideo -pix_fmt  rgb24 -s 946x528  -i - -f mp4 -q:v 5 -an -vcodec mpeg4 output.mp4", "w");
     
    // Process video frames
    while(1)
    {
        // Read a frame from the input pipe into the buffer
        count = fread(frame, 1, H*W*3, pipein);
          
        // If we didn't get a frame of video, we're probably at the end
        if (count != H*W*3) break;
         
        // Process this frame
        flip(frame,x,y,H,W);

        // Write this frame to the output pipe
        fwrite(frame, 1, H*W*3, pipeout);
    }
     
    // Flush and close input and output pipes
    fflush(pipein);
    pclose(pipein);
    fflush(pipeout);
    pclose(pipeout);
}

Headers File:

int invertRGB(unsigned char frame [H][W][3],int x, int y,int H, int W){
      for (y=0 ; y<H ; ++y) for (x=0 ; x<W ; ++x)
      {
          frame[y][x][0] = 255 - frame[y][x][0]; 
          frame[y][x][1] = 255 - frame[y][x][1]; 
          frame[y][x][2] = 255 - frame[y][x][2];            
      }
      return frame;
}

int flip(unsigned char frame [H][W][3],int x, int y, int H, int W){
      unsigned char frameTemp[H][W][3];
      for (y=0 ; y<H ; ++y) for (x=0 ; x<W ; ++x)
      {   
          frameTemp[(H-1)-y][(W-1)-x][0] = frame[y][x][0];
          frameTemp[(H-1)-y][(W-1)-x][1] = frame[y][x][1]; 
          frameTemp[(H-1)-y][(W-1)-x][2] = frame[y][x][2];
      }
      for (y=0 ; y<H ; ++y) for (x=0 ; x<W ; ++x)
      {
          frame[y][x][0] = frameTemp[y][x][0];
          frame[y][x][1] = frameTemp[y][x][1]; 
          frame[y][x][2] = frameTemp[y][x][2]; 
      }
      return frame;
}

This is my attempt at trying to create a video editor :)

DAVID
  • 92
  • 1
  • 8
  • 1
    Header files don't read anything. The compiler reads the header file as if it were included in the source. #defines only affect what comes after them. – William Pursell Aug 17 '20 at 17:16
  • You might want to [buy a good book about the C programming language](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – user3386109 Aug 17 '20 at 17:26
  • Already have ... KN Kings C Programming ... A modern approach – DAVID Aug 17 '20 at 17:27
  • 1
    Header file shouldn't include definitions of functions. Header files is for declareing things. – MikeCAT Aug 17 '20 at 17:33
  • @MikeCAT What do you mean? – DAVID Aug 17 '20 at 17:37
  • 1
    Generally you should write only definitions of functions like `int invertRGB(unsigned char frame [H][W][3],int x, int y,int H, int W);` and definitions of functions should be in another source file (`.c` file). This is because function definitions in header file will lead to multiple definition errors when the header file is used from multiple source files to be compiled into one executable file. – MikeCAT Aug 17 '20 at 17:39
  • In this case, separating functions into another source file will make it difficult to use the definition of macros `H` and `W`, so it seems it is good to copy-and-pasting the contents of the header to the main source file and removing header inclusion. – MikeCAT Aug 17 '20 at 17:41
  • @MikeCAT Problem is I'm going to be adding more functions and it is going to get messy ... – DAVID Aug 17 '20 at 17:42
  • @MikeCAT Also how would I pass the macros constant over to the function when defining the array's size? – DAVID Aug 17 '20 at 18:08
  • 1
    `int main(headerFunction(array)){}` - that is definitely not a valid definition for `main`. Is that what's in your actual code? – John Bode Aug 17 '20 at 18:22
  • If you're going to pass height and width values to your functions as arguments, then you do not need to convey the same information via macros, too, and *vice versa*. If you are going to use arguments then be aware that you would be leveraging C's *optional* variable-length array feature, which is not universally implemented. In that case, the parameters for the variable-length dimensions need to appear earlier in the parameter list than the variable-length arrays themselves. – John Bollinger Aug 17 '20 at 20:06

1 Answers1

2

It is because CONSTANT is not defined at the point that the header file is read.

Try this:

#define CONSTANT 10
#include "myHeaderFile.h"
int main(headerFunction(array)){}

In "All the code", you used the same names for constants (used in unsigned char frame [H][W][3]) and arguments (used in int H, int W), so the arguments is also treated as macros and replaced with integers, making the definition invalid. In this case the size is fixed, so you won't need the arguments H and W and they should be removed. Don't forget to remove the extra arguments also from calling points.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70