Thread: Reading 1D data into 2D array

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Reading 1D data into 2D array

    For my video processing program, I'm attempting to merge a series of video frames to get a sensation of a higher frame rate but be played back at a higher speed. Merging 5 frames together (5 BMP files) for example, has 5 frames merged together, appears to play at 150 fps but plays back at 30 fps giving the sense of 5x time. What my question is involves reading and writing multidimensional arrays, 2D arrays in my case. How would I do so in my read/write functions and set it up otherwise?

    Code:
    ... // includes, defines and globals
    unsigned char FrameData[1036800][5]; // 5 frames' worth of 720x480x24 images
    
    ... // other globals and function to get file path and names
    
    fread(&FrameData[0], 1, 1036800, FileHandle); // read 1,036,800 bytes of dataj // ???
    
    ... // Brighten, merge, then crop - these functions (except the newest merge) all work as expected but need adjusting
    
    fwrite(&OutputFrameData, 1, CroppedImageDataSize, FileHandle); // write related single frame
    
    ... // main function
    The two questions/concerns are:

    1. For the variable declaration, what should the order of the elements be? In other words, should the big number be first or second?
    2. Since I want to read the data into the large part of the array, how would I do so?

    The reason is for a loop to process the input frames making it easier to do (otherwise, it'd just be a big mess with many very similar instructions.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1. For the variable declaration, what should the order of the elements be?
    Consider
    char lines[LINES_PER_FILE][CHARS_PER_LINE];
    as a template for your ideas.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    So the smaller number should be first in my case or:

    Code:
    unsigned char FrameData[FrameCount][BytesPerFrame];
    Then, what about the second question/concern where I read into the array? How would I read into the large dimension of it rather than the smaller (since that would cause array overflow). Each original BMP file is always the same size - 720x480 at 24-bit color meaning 1,036,800 bytes are needed (just shy of 1 MB).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I guess
    for ( frameNum = 0 ; frameNum < 5 ; frameNum++ ) fread( FrameData[frameNum], 1, 1036800, FileHandle);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I got it working now and fixed unrelated bugs. It goes like this:

    Code:
    unsigned char Data[NumberOfFrames][FrameData];
    
    ...
    
    fread(Data[FrameNumber], 1, Size, FileHandle);
    The results I got were a bit unexpected at first, but I've got everything working now and I'll be posting this video on YouTube when I finish processing it. Thanks.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. switch from 2d array to 1d array
    By sass208 in forum C Programming
    Replies: 12
    Last Post: 12-11-2006, 09:34 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Putting Data into a 2D array
    By Zalbik in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:55 PM