Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hey, can I get some help, have no clue how this works. #74

Closed
bruhhhhhhhhhh7 opened this issue Nov 23, 2020 · 5 comments
Closed

Hey, can I get some help, have no clue how this works. #74

bruhhhhhhhhhh7 opened this issue Nov 23, 2020 · 5 comments
Labels

Comments

@bruhhhhhhhhhh7
Copy link

bruhhhhhhhhhh7 commented Nov 23, 2020

static void save_gray_frame(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
{
    FILE *f;
    int i;
    f = fopen(filename,"w");
    // writing the minimal required header for a pgm file format
    // portable graymap format -> https://en.wikipedia.org/wiki/Netpbm_format#PGM_example
    fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);

    // writing line by line
    for (i = 0; i < ysize; i++)
        fwrite(buf + i * xsize, 1, xsize, f);
    fclose(f);
}

fwrite(buf + i * xsize, 1, xsize, f);
buf + i * xsize
How do you get the data stream to do that? Like, what is it doing? Where do you get that expression from?

@leandromoreira
Copy link
Owner

leandromoreira commented Nov 24, 2020

// for each frame we take the Y plane (think of a huge "2d" array containing all the pixels)
// and the linesize (wrap - size in bytes of each picture line) if we use a byte per pixel then it's equals to width
// , width, height, and the file name
save_gray_frame(pFrame->data[0], pFrame->linesize[0], pFrame->width, pFrame->height, frame_filename);

// we must write the minimal header (providing resolution and magical string)
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);

// and finally we just need to write to the file
// since we write line by line then what we do is just to use the ysize iteration 
// (one increment for i, up to the frame height((yssize))

// FOR THAT matter, we use pointer arithmetics and
    for (i = 0; i < ysize; i++)
        // fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
        // a pointer (updated/calculated each line) of size 1 but
        // writes xsize items of data on file f
        fwrite(buf + i * wrap, 1, xsize, f);

Anyway, to sum up, buf + i * wrap it's just a way to move for the next line in number of bytes. Let's do a simple simulation here:

height=720
width=1280

for i = 0; i < height; i++ 
 // for 0 we just point to the start of the buffer
buf(0x00000) + 0 *  1280 = 0 
storing 1280 bytes
// for 1 we just move the pointer to the next line
buf(0x00000) + 1 *  1280 = 1280
.... 

links

@leandromoreira
Copy link
Owner

if you still have doubts, feel comfortable to ask.

@bruhhhhhhhhhh7
Copy link
Author

bruhhhhhhhhhh7 commented Nov 25, 2020

Does the data stream itself get multiplied? I know that it doesn't, but why does that work? Thanks btw, I appreciate this a lot. I'm new to C++, this is the first library that I'm learning haha.

@leandromoreira
Copy link
Owner

Does the data stream itself get multiplied?

No, think of buf as an address to the memory where the data stream starts, then if you do buf + 1 what is effectively happening is you're now pointing to the start address + 1 byte, then if you transpose that for any operation you are just moving where to start reading.

# an oversimplification of a memory

memory = []

memory[0x000] = 0xFF # random information on memory
memory[0x001] = 0x2B # random information on memory
# ....
memory[0xFC1] = 0x03 # the data stream starts here
memory[0xFC2] = 0x3A
memory[0xFC3] = 0x19
memory[0xFC4] = 0xFB

buff = 0xFC1
# when we do any arithmetic it changes the address 

@bruhhhhhhhhhh7
Copy link
Author

Thanks a ton, I get it now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants